利用Sentinel实现Redis主从切换

Posted ahaii

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用Sentinel实现Redis主从切换相关的知识,希望对你有一定的参考价值。

利用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。

 

以上是关于利用Sentinel实现Redis主从切换的主要内容,如果未能解决你的问题,请参考以下文章

redis配置读写分离以及利用哨兵sentinel进行自动主从切换

Spring整合redis,通过sentinel进行主从切换

Redis技术探索「高可用架构模式」哨兵(sentinel)模式实现主从故障互切换模式详解

Redis主从sentinel故障自动切换

动手实践Redis主从复制Sentinel主从切换Cluster分片

20190327 Redis主从同步三台服务器部署,基于sentinel实现redis主从切换