[摘要]之前的文章中介绍了redis+keepalived来实现HA环境的搭建,redis2.8版本以上加入了sentinel的模块,可以实现高可用,下面对sentinel+redis下HA集群的搭建进行了一下测试,首先配置redis的master+slave,其中我使用的redis版本为2.8.7。
基本环境状况:
redis-master: 192.168.100.117
redis-slave: 192.168.100.118
setinel: 192.168.100.119
sentinel配置
$ cat sentinel.conf port 26379 sentinel monitor def_master 192.168.100.117 6379 1 sentinel auth-pass def_master 123456 sentinel down-after-milliseconds def_master 5000 sentinel can-failover def_master yes sentinel parallel-syncs def_master 1 sentinel failover-timeout def_master 900000
(1)启动主从redis
(2)查看主从状态
# /usr/local/redis-2.8/bin/redis-cli -h 192.168.100.118 -p 6379 -a 123456 info Replication # Replication role:slave master_host:192.168.100.117 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:3066 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
(3)启动sentinel,具体参数定义可以参考redis官方文档
/usr/local/redis-2.8/bin/redis-server /usr/local/redis-2.8/conf/sentinel.conf --sentinel _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.8.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 4055 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [4055] 17 Mar 12:33:46.397 # Sentinel runid is 954943cc4c46afa159e063953c056f2e9068ecce [4055] 17 Mar 12:33:46.397 # +monitor master def_master 192.168.100.117 6379 quorum 1 [4055] 17 Mar 12:33:47.400 * +slave slave 192.168.100.118:6379 192.168.100.118 6379 @ def_master 192.168.100.117 6379
(4)停止Master redis,及Sentinel日志如下:
# /usr/local/redis-2.8/bin/redis-cli -h 192.168.100.117 -p 6379 -a 123456 shutdown [4055] 17 Mar 12:35:11.755 # +sdown master def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:11.755 # +odown master def_master 192.168.100.117 6379 #quorum 1/1 [4055] 17 Mar 12:35:11.755 # +new-epoch 1 [4055] 17 Mar 12:35:11.755 # +try-failover master def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:11.755 # +vote-for-leader 954943cc4c46afa159e063953c056f2e9068ecce 1 [4055] 17 Mar 12:35:11.755 # +elected-leader master def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:11.755 # +failover-state-select-slave master def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:11.839 # +selected-slave slave 192.168.100.118:6379 192.168.100.118 6379 @ def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:11.839 * +failover-state-send-slaveof-noone slave 192.168.100.118:6379 192.168.100.118 6379 @ def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:11.916 * +failover-state-wait-promotion slave 192.168.100.118.:6379 192.168.100.118 6379 @ def_master 192.168.100.117 6379
(5)查看redis切换状态(原slave)
# /usr/local/redis-2.8/bin/redis-cli -h 192.168.100.118 -p 6379 -a 123456 info Replication # Replication role:master connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
(6)再次启动Redis master
/usr/local/redis-2.8/bin/redis-server /usr/local/redis-2.8/conf/redis.conf &
(7)查看Sentinel日志
[4055] 17 Mar 12:35:12.817 # +promoted-slave slave 192.168.100.118:6379 192.168.100.118 6379 @ def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:12.818 # +failover-state-reconf-slaves master def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:12.835 # +failover-end master def_master 192.168.100.117 6379 [4055] 17 Mar 12:35:12.835 # +switch-master def_master 192.168.100.117 6379 192.168.1.118 6379 [4055] 17 Mar 12:35:12.835 * +slave slave 192.168.100.117:6379 192.168.100.117 6379 @ def_master 192.168.100.118 6379 [4055] 17 Mar 12:35:17.850 # +sdown slave 192.168.100.117:6379 192.168.100.117 6379 @ def_master 192.168.100.118 6379 [4055] 17 Mar 12:36:36.204 * +convert-to-slave slave 192.168.100.117:6379 192.168.100.117 6379 @ def_master 192.168.100.118 6379
(8)再次查看redis主从状态
# /usr/local/redis-2.8/bin/redis-cli -h 192.168.100.117 -p 6379 -a 123456 info Replication # Replication role:slave master_host:192.168.100.118 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:166620 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # /usr/local/redis-2.8/bin/redis-cli -h 192.168.100.118 -p 6379 -a 123456 info Replication # Replication role:master connected_slaves:1 slave0:ip=192.168.100.117,port=6379,state=online,offset=167778,lag=1 master_repl_offset:167921 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:167920
可以看到主从已经恢复,不过Master和Slave做了对调,这样sentinel+redis下HA集群的搭建测试完成,不过本文只是使用了一个sentinel节点,特此说明。