哨兵机制
在前面讲的master/slave模式,在一个典型的一主多从的系统中,slave在整个体系中起到了数据冗余备份和读写分离的作用。
当master遇到异常终端后,需要从slave中选举一个新的master继续对外提供服务,这种机制在前面提到过N次,比如在zk中通过leader选举、kafka中可以基于zk的节点实现master选举。
所以在redis中也需要一种机制去实现master的决策,redis并没有提供自动master选举功能,而是需要借助一个哨兵来进行监控
什么是哨兵
顾名思义,哨兵的作用就是监控Redis系统的运行状况,它的功能包括两个
\1. 监控master和slave是否正常运行
\2. master出现故障时自动将slave数据库升级为master
哨兵是一个独立的进程,使用哨兵后的架构图
为了解决master选举问题,又引出了一个单点问题,也就是哨兵的可用性如何解决,在一个一主多从的Redis系统中,可以使用多个哨兵进行监控任务以保证系统足够稳定。
此时哨兵不仅会监控master和slave,同时还会互相监控;这种方式称为哨兵集群,哨兵集群需要解决故障发现、和master决策的协商机制问题
sentinel之间的相互感知
sentinel节点之间会因为共同监视同一个master从而产生了关联,一个新加入的sentinel节点需要和其他监视相同master节点的sentinel相互感知,首先
\1. 需要相互感知的sentinel都向他们共同监视的master节点订阅channel:sentinel:hello
\2. 新加入的sentinel节点向这个channel发布一条消息,包含自己本身的信息,这样订阅了这个channel的sentinel就可以发现这个新的sentinel
\3. 新加入得sentinel和其他sentinel节点建立长连接
master的故障发现
sentinel节点会定期向master节点发送心跳包来判断存活状态,一旦master节点没有正确响应,sentinel会把master设置为“主观不可用状态”,然后它会把“主观不可用”发送给其他所有的sentinel节点去确认,
当确认的sentinel节点数大于>quorum时,则会认为master是“客观不可用”,接着就开始进入选举新的master流程;但是这里又会遇到一个问题,就是sentinel中,
本身是一个集群,如果多个节点同时发现master节点达到客观不可用状态,那谁来决策选择哪个节点作为maste呢?这个时候就需要从sentinel集群中选择一个leader来做决策。
而这里用到了一致性算法Raft算法、它和Paxos算法类似,都是分布式一致性算法。但是它比Paxos算法要更容易理解;Raft和Paxos算法一样,也是基于投票算法,只要保证过半数节点通过提议即可;
注意:sentinel需要访问所有的主从redis节点,所以确保所有的redis.config中bind变成0.0.0.0,即允许其他机器访问
动画演示地址:http://thesecretlivesofdata.com/raft/
配置实现
通过在这个配置的基础上增加哨兵机制。(在redis目录下有当前集成的sentinel配置)
在其中任意一台服务器上创建一个sentinel.conf文件,文件内容
sentinel monitor name ip port quorum
其中name表示要监控的master的名字,这个名字是自己定义。 ip和port表示master的ip和端口号。 最后一个1表示最低通过票数,也就是说至少需要几个哨兵节点统一才可以,后面会具体讲解
sentinel monitor mymaster 192.168.25.129 6379 2
sentinel down-after-milliseconds mymaster 5000 --表示如果5s内mymaster没响应,就认为SDOWN
sentinel failover-timeout mymaster 15000 --表示如果15秒后,mysater仍没活过来,则启动failover,从剩下的slave中选一个升级为master
分别修改server1、server2、server3的sentinel.conf,然后分别启动哨兵
两种方式启动哨兵
[root@localhost redis]# ./bin/redis-sentinel sentinel.conf
redis-server /path/to/sentinel.conf --sentinel
哨兵监控一个系统时,只需要配置监控master即可,哨兵会自动发现所有slave;
这时候,我们把master关闭,等待指定时间后(默认是30秒),会自动进行切换,会输出如下消息
img
+sdown表示哨兵主管认为master已经停止服务了,+odown表示哨兵客观认为master停止服务了。关于主观和客观,后面会给大家讲解。接着哨兵开始进行故障恢复,挑选一个slave升级为master
+try-failover表示哨兵开始进行故障恢复
+failover-end 表示哨兵完成故障恢复
+slave表示列出新的master和slave服务器,我们仍然可以看到已经停掉的master,哨兵并没有清除已停止的服务的实例,这是因为已经停止的服务器有可能会在某个时间进行恢复,恢复以后会以slave角色加入到整个集群中
连接sentinel哨兵,查看哨兵信息
info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=192.168.25.129:6379,slaves=2,sentinels=4
查看当前redis主从服务
[root@localhost redis]# ./bin/redis-cli 127.0.0.1:6379> info reapplication 127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.25.131,port=6379,state=online,offset=11719,lag=1 slave1:ip=192.168.25.130,port=6379,state=online,offset=11719,lag=1 master_repl_offset:11862 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:11861
关闭主节点,sentinel会投票选举新主节点,一开始选举192.128.25.131为主节点,但是失败了。最后选举了192.128.25.130为主节点
9632:X 04 May 06:44:06.942 # +sdown master mymaster 192.168.25.129 6379 9632:X 04 May 06:44:06.997 # +odown master mymaster 192.168.25.129 6379 #quorum 3/2 9632:X 04 May 06:44:06.997 # +new-epoch 1 9632:X 04 May 06:44:06.997 # +try-failover master mymaster 192.168.25.129 6379 9632:X 04 May 06:44:06.999 # +vote-for-leader c57b1466c79d692dc33e01e27ba504a4895d872e 1 9632:X 04 May 06:44:06.999 # c57b1466c79d692dc33e01e27ba504a4895d872e voted for c57b1466c79d692dc33e01e27ba504a4895d872e 1 9632:X 04 May 06:44:06.999 # 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 voted for c57b1466c79d692dc33e01e27ba504a4895d872e 1 9632:X 04 May 06:44:07.063 # +elected-leader master mymaster 192.168.25.129 6379 9632:X 04 May 06:44:07.063 # +failover-state-select-slave master mymaster 192.168.25.129 6379 9632:X 04 May 06:44:07.122 # +selected-slave slave 192.168.25.130:6379 192.168.25.130 6379 @ mymaster 192.168.25.129 6379 9632:X 04 May 06:44:07.122 * +failover-state-send-slaveof-noone slave 192.168.25.130:6379 192.168.25.130 6379 @ mymaster 192.168.25.129 6379 9632:X 04 May 06:44:07.180 * +failover-state-wait-promotion slave 192.168.25.130:6379 192.168.25.130 6379 @ mymaster 192.168.25.129 6379 9632:X 04 May 06:44:07.395 # +new-epoch 2 9632:X 04 May 06:44:07.395 # +config-update-from sentinel 7592b0773d9f362a6f0f9eecb046e63b84825d2d 192.168.25.129 26379 @ mymaster 192.168.25.129 6379 9632:X 04 May 06:44:07.395 # +switch-master mymaster 192.168.25.129 6379 192.168.25.131 6379 9632:X 04 May 06:44:07.396 * +slave slave 192.168.25.130:6379 192.168.25.130 6379 @ mymaster 192.168.25.131 6379 9632:X 04 May 06:44:07.396 * +slave slave 192.168.25.129:6379 192.168.25.129 6379 @ mymaster 192.168.25.131 6379 9632:X 04 May 06:44:08.387 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:09.414 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:10.523 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:11.464 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:11.902 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:13.498 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:13.957 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:15.501 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:16.020 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:17.508 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:18.031 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:19.571 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:20.123 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:21.602 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:22.199 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:23.624 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:24.261 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:25.647 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:26.310 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:27.663 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:28.354 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:29.764 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:30.443 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:31.825 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:32.532 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:33.855 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:34.581 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:35.930 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:36.641 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:37.451 # +sdown slave 192.168.25.129:6379 192.168.25.129 6379 @ mymaster 192.168.25.131 6379 9632:X 04 May 06:44:38.029 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:38.736 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:40.048 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:40.821 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:42.133 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:42.824 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:44.222 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:44.885 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:46.267 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:47.020 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:48.434 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:49.074 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:50.517 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:51.146 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:52.632 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:53.273 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:54.679 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:55.303 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:56.740 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:57.399 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:58.871 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:44:59.427 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:00.950 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:01.536 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:02.984 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:03.623 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:05.043 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:05.660 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:07.143 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:07.710 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:08.021 # +sdown master mymaster 192.168.25.131 6379 9632:X 04 May 06:45:08.123 # +odown master mymaster 192.168.25.131 6379 #quorum 2/2 9632:X 04 May 06:45:08.123 # +new-epoch 3 9632:X 04 May 06:45:08.123 # +try-failover master mymaster 192.168.25.131 6379 9632:X 04 May 06:45:08.125 # +vote-for-leader c57b1466c79d692dc33e01e27ba504a4895d872e 3 9632:X 04 May 06:45:08.125 # c57b1466c79d692dc33e01e27ba504a4895d872e voted for c57b1466c79d692dc33e01e27ba504a4895d872e 3 9632:X 04 May 06:45:09.162 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:09.713 * +sentinel-address-switch master mymaster 192.168.25.131 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:10.446 # +new-epoch 4 9632:X 04 May 06:45:10.446 # +config-update-from sentinel 7592b0773d9f362a6f0f9eecb046e63b84825d2d 192.168.25.129 26379 @ mymaster 192.168.25.131 6379 9632:X 04 May 06:45:10.446 # +switch-master mymaster 192.168.25.131 6379 192.168.25.130 6379 9632:X 04 May 06:45:10.446 * +slave slave 192.168.25.129:6379 192.168.25.129 6379 @ mymaster 192.168.25.130 6379 9632:X 04 May 06:45:10.446 * +slave slave 192.168.25.131:6379 192.168.25.131 6379 @ mymaster 192.168.25.130 6379 9632:X 04 May 06:45:12.507 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:12.632 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:14.512 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:14.758 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:16.573 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:16.874 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:18.634 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:19.005 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:20.639 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:21.041 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:22.700 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:23.048 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:24.746 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:25.113 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:26.844 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:27.187 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:28.901 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:29.252 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:30.918 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:31.289 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:32.934 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:33.332 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:34.949 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:35.386 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:37.002 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:37.401 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:39.039 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:39.415 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:40.465 # +sdown slave 192.168.25.129:6379 192.168.25.129 6379 @ mymaster 192.168.25.130 6379 9632:X 04 May 06:45:41.113 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 127.0.0.1 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 9632:X 04 May 06:45:41.453 * +sentinel-address-switch master mymaster 192.168.25.130 6379 ip 192.168.25.131 port 26379 for 9a13868360164c0c7ebbf1f598bd8dcd81f078c7
再次查看当前redis节点信息
主节点
127.0.0.1:6379> info replication # Replication role:master 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 127.0.0.1:6379> [root@localhost redis]# vim sentinel.conf
从节点
[root@localhost redis]# ./bin/redis-cli 127.0.0.1:6379> info replication # Replication role:slave master_host:192.168.25.130 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:113842 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:2 repl_backlog_histlen:2606 127.0.0.1:6379>
sentinel从新选举redis主节点前后,sentinel.config的变化
初始时配置 192.168.25.129为主节点
port 26379 dir /tmp sentinel monitor mymaster 192.168.25.129 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000
129挂了,129、130、131上的sentinel从新选举后的sentinel.config配置:
从中可以看到,redis主节点地址,sentinel主从信息,选举epoch信息等
port 26379 dir "/tmp" sentinel myid 9a13868360164c0c7ebbf1f598bd8dcd81f078c7 sentinel monitor mymaster 192.168.25.130 6379 2 sentinel config-epoch mymaster 4 sentinel leader-epoch mymaster 0 maxmemory 3gb sentinel known-slave mymaster 192.168.25.131 6379 sentinel known-slave mymaster 127.0.0.1 6379 sentinel known-sentinel mymaster 192.168.25.129 26379 7592b0773d9f362a6f0f9eecb046e63b84825d2d sentinel known-sentinel mymaster 192.168.25.130 26379 c57b1466c79d692dc33e01e27ba504a4895d872e sentinel current-epoch 4