续上一篇 [url] http://zyx870805.iteye.com/blog/2306729[/url]
redis sentinel(哨兵)
作用:监控redis主(master)和从(slave),master挂了,提升slave为master, 老的master正常后,自动变成新master的slave。按照官网的说法,每个master配置三个sentinel,quorum设置为2,这样有两个sentinel同时连接不上master,就认为master挂了,其中一个sentinel发起一个failover,slave变成master。
配置步骤:
上篇文章中,master节点有三个,每个节点有一个slave,这个是3.X版本的redis至少要求三个master分担所有的slots。
1、配置九个sentinel.conf文件,内容如下:
port 5000
sentinel myid db9d89df7f9534c42f75608f5de8dfc876b4e373
sentinel monitor mymaster1 127.0.0.1 7003 2
sentinel down-after-milliseconds mymaster1 5000
sentinel failover-timeout mymaster1 60000
说明:
redis instance 7000 7001 7002 分别是三个master节点
5000、5001和5002 给节点7000当哨兵
5003、5004和5005 给节点7001当哨兵
5006、5007和5008 给节点7002当哨兵
其他配置文件要修改端口和master名称 mymaster1 为 mymaster2 或者 mymaster3
2、启动
nohup ../src/redis-sentinel ./sentinel_5000.conf &
nohup ../src/redis-sentinel ./sentinel_5001.conf &
nohup ../src/redis-sentinel ./sentinel_5002.conf &
nohup ../src/redis-sentinel ./sentinel_5003.conf &
nohup ../src/redis-sentinel ./sentinel_5004.conf &
nohup ../src/redis-sentinel ./sentinel_5005.conf &
nohup ../src/redis-sentinel ./sentinel_5006.conf &
nohup ../src/redis-sentinel ./sentinel_5007.conf &
nohup ../src/redis-sentinel ./sentinel_5008.conf &
3、测试
找到7000的master节点并杀死
ps -ef | grep redis | grep 7000 | awk '{print $2}' | xargs kill -9
查看master节点是否切换了,如下:
../src/redis-cli -p 5000
sentinel get-master-addr-by-name mymaster1
[img][/img]
发现切换了
重新启动7000节点
查看集群节点
../src/redis-cli -p 7000
cluster nodes
[color=black]127.0.0.1:7000> cluster nodes
4ba7058fe396283c38c20584f51b3e2bd78c2353 127.0.0.1:7004 slave e6ba30bed33f0299f57f39a418a0ad8ec7e4bc39 0 1467187532142 5 connected
14ff7660d2ac0b59b32df0f74fbd660ab21aec67 127.0.0.1:7002 master - 0 1467187533105 3 connected 10923-16383
fa73a8bdcb73e0f58bbd18674239c5704de48ef5 127.0.0.1:7000 myself,slave 237cef66b68d96bbc0ece3aaa6c8cd747ac048a3 0 0 1 connected
e6ba30bed33f0299f57f39a418a0ad8ec7e4bc39 127.0.0.1:7001 master - 0 1467187533207 2 connected 5461-10922
867c4cf7156fa7fb84086da8dc572f33713f0a00 127.0.0.1:7005 slave 14ff7660d2ac0b59b32df0f74fbd660ab21aec67 0 1467187533104 6 connected
237cef66b68d96bbc0ece3aaa6c8cd747ac048a3 127.0.0.1:7003 master - 0 1467187534285 7 connected 0-5460
127.0.0.1:7000> [/color]
发现7000节点变成了7003节点的slave,说明master挂了,slave可以提升为master,打完收工。