利用Sentinel(哨兵)实现Redis集群的故障自主切换
首先部署redis主从集群,主要安装过程:
cd redis
make
make install
主要看配置文件:
master:
bind 0.0.0.0 port 6801 logfile "./redis.log" requirepass 12345678 masterauth 12345678 #主从密码,主变为从时,以从的身份去同步主节点
slave:
bind 0.0.0.0 port 6802 logfile "./redis.log" requirepass 12345678 masterauth 12345678 slaveof 172.17.0.2 6801
sentinel:
bind 0.0.0.0 port 26379
dir ./
logfile "./sentinel.log"
... sentinel monitor master1 172.17.0.2 6801 1 sentinel down-after-milliseconds master1 10000 sentinel failover-timeout master1 900000 sentinel config-epoch master1 4317 sentinel leader-epoch master1 4317 sentinel auth-pass master1 12345678
启动redis+sentinel集群,查看节点信息如下:
master:
slave:
可以看出主从已经生效,现在把master(172.17.0.2 6801)关掉,查看sentinel日志:
1:X 25 May 18:59:39.096 # +sdown master master1 172.17.0.2 6801 1:X 25 May 18:59:39.096 # +odown master master1 172.17.0.2 6801 #quorum 1/1 1:X 25 May 18:59:39.096 # +new-epoch 4320 1:X 25 May 18:59:39.096 # +try-failover master master1 172.17.0.2 6801 1:X 25 May 18:59:39.145 # +vote-for-leader 5a30725736229497d4f5f0ef3502462dd683f02e 4320 1:X 25 May 18:59:39.145 # +elected-leader master master1 172.17.0.2 6801 1:X 25 May 18:59:39.145 # +failover-state-select-slave master master1 172.17.0.2 6801 1:X 25 May 18:59:39.217 # +selected-slave slave 172.17.0.3:6802 172.17.0.3 6802 @ master1 172.17.0.2 6801 1:X 25 May 18:59:39.217 * +failover-state-send-slaveof-noone slave 172.17.0.3:6802 172.17.0.3 6802 @ master1 172.17.0.2 6801 1:X 25 May 18:59:39.307 * +failover-state-wait-promotion slave 172.17.0.3:6802 172.17.0.3 6802 @ master1 172.17.0.2 6801 1:X 25 May 18:59:39.312 # +promoted-slave slave 172.17.0.3:6802 172.17.0.3 6802 @ master1 172.17.0.2 6801 1:X 25 May 18:59:39.312 # +failover-state-reconf-slaves master master1 172.17.0.2 6801 1:X 25 May 18:59:39.359 # +failover-end master master1 172.17.0.2 6801 1:X 25 May 18:59:39.359 # +switch-master master1 172.17.0.2 6801 172.17.0.3 6802 1:X 25 May 18:59:39.359 * +slave slave 172.17.0.2:6801 172.17.0.2 6801 @ master1 172.17.0.3 6802 1:X 25 May 18:59:49.419 # +sdown slave 172.17.0.2:6801 172.17.0.2 6801 @ master1 172.17.0.3 6802
可以看到,从节点172.17.0.3 6802 已经成为master:
现在启动172.17.0.2:6801,sentinel日志如下:
1:X 25 May 19:03:14.929 # -sdown slave 172.17.0.2:6801 172.17.0.2 6801 @ master1 172.17.0.3 6802 1:X 25 May 19:03:24.903 * +convert-to-slave slave 172.17.0.2:6801 172.17.0.2 6801 @ master1 172.17.0.3 6802
连接到redis后查看集群信息如下:
可以看到,之前的master(172.17.0.2 6801) 已经成为了slave。