码迷,mamicode.com
首页 > 其他好文 > 详细

利用Sentinel实现Redis主从切换

时间:2017-05-25 19:48:03      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:div   连接   cond   故障   eof   try   pre   查看   convert   

利用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主从切换

标签:div   连接   cond   故障   eof   try   pre   查看   convert   

原文地址:http://www.cnblogs.com/ahaii/p/6905496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!