Redis哨兵(Sentinel)

1. 哨兵的作用

(1)监控主库状态
(2)自动选主,切换(6381 slaveof no one)
(3)2号从库(6382)指向新主库(6381)
(4)应用透明(切换过程用户无法感知,不影响体验)
(5)自动处理故障节点(自愈)

2. 哨兵搭建过程

mkdir /data/26380  #26380为哨兵的端口,生产环境中为一个全新的节点
cd /data/26380
cat > sentinel.conf << EOF
port 26380
dir "/data/26380"
sentinel monitor mymaster 127.0.0.1 6380 1   #mymaster为自定义的集群名称,后面的IP和端口为主库的信息。后面的1,表示的是如果有一台哨兵监控到主库宕机,则视为真的宕机。
如果哨兵大于1台,数量根据实际情况而定。
如有3台哨兵,上述最后的1改为2,(因为判断主库是否宕机,在多台哨兵之间是投票制的,n/2+1)

sentinel down-after-milliseconds mymaster 5000  #主库发生宕机后,会有5秒的启动机会,超过5秒,就进行切换,此处5000的单位为毫秒。

sentinel auth-pass mymaster 123  #集群主库的密码
EOF

启动:
redis-sentinel /data/26380/sentinel.conf &>/tmp/sentinel.log &

切换演示

关闭主库
[root@redis-01 /data/26380]# redis-cli -p 6380 -a 123 shutdown

查看从库信息
[root@redis-01 /data/26380]# redis-cli -p 6381 -a 123 info replication
# Replication
role:slave   #当前还是从库
master_host:127.0.0.1
master_port:6382   #主库发生了改变
……省略部分内容

[root@redis-01 /data/26380]# redis-cli -p 6382 -a 123 info replication
# Replication
role:master   #6382变成了主
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=3286,lag=1  #因为6380宕机了,所以这里只有一台slave
master_repl_offset:3419
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:3418

把宕机的6380重新加入集群
[root@redis-01 /data/26380]# redis-server /data/6380/redis.conf 
[root@redis-01 /data/26380]# redis-cli -p 6382 -a 123 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=7649,lag=1
slave1:ip=127.0.0.1,port=6380,state=online,offset=7649,lag=0
master_repl_offset:7782
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:7781
[root@redis-01 /data/26380]# redis-cli -p 6380 -a 123 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6382
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:8328
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

3. Sentinel 总结

3.1 Sentinel的作用

A、Master 状态监测

B、如果Master 异常,则会进行Master-slave 转换,将其中一个Slave作为Master,将之前的Master作为Slave 

C、Master-Slave切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换 

3.2 Sentinel的工作方式

1):每个Sentinel以每秒钟一次的频率向它所知的Master,Slave以及其他 Sentinel 实例发送一个 PING 命令 
2):如果一个实例(instance)距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被 Sentinel 标记为主观下线。 
3):如果一个Master被标记为主观下线,则正在监视这个Master的所有 Sentinel 要以每秒一次的频率确认Master的确进入了主观下线状态。 
4):当有足够数量的 Sentinel(大于等于配置文件指定的值)在指定的时间范围内确认Master的确进入了主观下线状态, 则Master会被标记为客观下线 
5):在一般情况下, 每个 Sentinel 会以每 10 秒一次的频率向它已知的所有Master,Slave发送 INFO 命令 
6):当Master被 Sentinel 标记为客观下线时,Sentinel 向下线的 Master 的所有 Slave 发送 INFO 命令的频率会从 10 秒一次改为每秒一次 
7):若没有足够数量的 Sentinel 同意 Master 已经下线, Master 的客观下线状态就会被移除。 
若 Master 重新向 Sentinel 的 PING 命令返回有效回复, Master 的主观下线状态就会被移除。
posted @ 2020-06-17 19:09  三花  阅读(185)  评论(0编辑  收藏  举报