哨兵机制

在前面讲的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

  

  

redis(5)--redis集群之哨兵机制的更多相关文章

  1. (六) Docker 部署 Redis 高可用集群 (sentinel 哨兵模式)

    参考并感谢 官方文档 https://hub.docker.com/_/redis GitHub https://github.com/antirez/redis happyJared https:/ ...

  2. redis主从、集群、哨兵

    redis的主从.集群.哨兵 参考: https://blog.csdn.net/robertohuang/article/details/70741575 https://blog.csdn.net ...

  3. Redis高可用集群方案——哨兵

    本篇文章版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文系列地址http://www.cnblogs.com/tdws/tag/NoSql/ 本人之前有篇文章,讲到了redis主从复制,读写分 ...

  4. 5-11 Redis缓存 | 持久化 | 集群_哨兵_主从复制_读写分离

    Redis 强化 缓存淘汰策略 Redis服务器繁忙时,有大量信息要保存 如果Redis服务器内存全满,再要往Redis中保存新的数据,就需要淘汰老数据,才能保存新数据 noeviction:返回错误 ...

  5. redis集群之哨兵模式【原】

    redis集群之哨兵(sentinel)模式 哨兵模式理想状态 需要>=3个redis服务,>=3个redis哨兵,每个redis服务搭配一个哨兵. 本例以3个redis服务为例: 一开始 ...

  6. Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】

    No cross,no crown . 不经历风雨,怎么见彩虹. Redis哨兵模式,用现在流行的话可以说就是一个"哨兵机器人",给"哨兵机器人"进行相应的配置 ...

  7. redis 安装 集群 主从 哨兵 docker

    安装redis 官方文档 docker run -d --net host -v /opt/myconfig/redis/redis.conf:/usr/local/etc/redis/redis.c ...

  8. sentinel监控redis高可用集群(一)

    一.首先配置redis的主从同步集群. 1.主库的配置文件不用修改,从库的配置文件只需增加一行,说明主库的IP端口.如果需要验证的,也要加多一行,认证密码. slaveof 192.168.20.26 ...

  9. Redis+Twemproxy+HAProxy集群(转) 干货

    原文地址:Redis+Twemproxy+HAProxy集群  干货 Redis主从模式 Redis数据库与传统数据库属于并行关系,也就是说传统的关系型数据库保存的是结构化数据,而Redis保存的是一 ...

随机推荐

  1. hdu 1162 Eddy's picture (prim)

    Eddy's pictureTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. nyoj 524-A-B Problem (java stripTrailingZeros, toPlainString)

    524-A-B Problem 内存限制:64MB 时间限制:1000ms 特判: No 通过数:2 提交数:4 难度:3 题目描述: A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下 ...

  3. libdispatch.dylib中dispatch_group的实现

    semaphore和group都是libdispatch提供的基于信号量的同步机制,dispatch_group继承自dispatch_semaphore,使用libdispatch层的信号量算法.d ...

  4. PHP-会话控制Cookie和Session

    会话控制:就是为了我们在访问页面和页面之间的跳转是,能够识别到你的登录状态,已经你的登录时长等 在php的会话控制当中,涉及到两个概念Cookie和Session Cookie 会话控制 原理:在登录 ...

  5. zip的压缩和解压命令

    以下命令均在/home目录下操作cd /home #进入/home目录 1.把/home目录下面的data目录压缩为data.zip zip -r data.zip data #压缩data目录   ...

  6. web 开发常用字符串表达式匹配

    记录一下 web 开发中常用的一些字符串模式,这是我遇到的一些,后面如果还有的话,欢迎大神提出,会继续更新. 正则表达式 这个主要用在前端的验证,nginx 路径匹配,shell 脚本文本处理,后端感 ...

  7. [Odoo12基础教程]之第一篇-创建Todo应用

    声明: 本教程基于 Ruter 老师的 [Odoo基础教程系列] ,Ruter老师教程的链接地址为:Odoo基础教程系列   . 至于为什么已经有了Ruter老师的教程,还要自己再搬移一份呢?是基于一 ...

  8. 【2018寒假集训 Day2】【动态规划】钱币兑换(exchange)(自己翻译的2333)

    钱币兑换(exchange) 问题描述: Dave偶然获得了未来几天的美元(dollars)与马克(marks)之间的兑换率.例如Dave开始有100marks,请编写个程序帮助Dave找出最好的买卖 ...

  9. 概率分布的python实现

    接上篇概率分布,这篇文章讲概率分布在python的实现. 文中的公式使用LaTex语法,即在\begin{equation}至\end{equation}的内容可以在https://www.codec ...

  10. 使用python2连接操作db2

    在python2.6下连接db2,步骤: 1.安装python2.6. (注:目前db2的驱动还不支持2.7) 2.安装setuptools,下载地址http://pypi.python.org/py ...