1. 程式人生 > >利用Sentinel實現Redis主從切換

利用Sentinel實現Redis主從切換

edi nbsp ilo bind redis poc 自主 日誌 sent

利用Sentinel(哨兵)實現Redis集群的故障自主切換

首先部署redis主從集群,這裏忽略過程,主要看配置文件:

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

...
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。

利用Sentinel實現Redis主從切換