Redis 高可用:Redis Sentinel 主从复制故障转移
Redis Sentinel 为 Redis 提供了高可用,可对复制集群中进行监控、通知、故障转移。
刚好有合适的两台服务器 Centos 6.4 ,已安装 Redis 3.2 和 Redis主从复制。
服务器名称:Centos222 , ip :192.168.1.222 ,主从角色:master
服务器名称:Centos224 , ip :192.168.1.224 ,主从角色:slave
在复制的基础上,现在配置 Redis Sentinel ,基本结构如下。
# 222 配置 redis 启动参数
- [[email protected] ~]# vi /etc/redis/6379.conf
- bind 127.0.0.1 192.168.1.222 #redis服务器地址
- port 26379 #sentinel端口号
- daemonize yes #后台启动
- protected-mode no #关闭保护模式,禁止远程直接访问
- requirepass 654321 #本地访问需要密码
- masterauth 654321 #备端同步访问需要密码
- slave-read-only yes #备端只读
# 224 配置redis 启动参数
- [[email protected] ~]# vi /etc/redis/6379.conf
- bind 127.0.0.1 192.168.1.224 #redis服务器地址
- port 26379 #sentinel端口号
- daemonize yes #后台启动
- slaveof 192.168.1.222 6379 #设置为192.168.1.222 6379的从库
- protected-mode no #关闭保护模式,禁止远程直接访问
- requirepass 654321 #本地访问需要密码
- masterauth 654321 #备端同步访问需要密码
- slave-read-only yes #备端只读
# 配置 Redis Sentinel ,只配置主实例的监控即可。Sentinel 可以通过 master 实例获取从实例的信息。
# 基本参数说明:
- port 26379 #Sentinel 服务端口
- sentinel monitor myredis 192.168.1.xxx 6379 1 #监控 人员组名/ip/端口/失效判定数(多少从端判断不同则认为master断开)
- sentinel down-after-milliseconds myredis 10000 #ping 超过 10000 毫秒则认为宕机
- sentinel failover-timeout myredis 900000 #主从切换超过 30000 毫秒则认为失败
- sentinel can-failover myredis yes #master断开后是否允许判断进行故障转移
- sentinel parallel-syncs myredis 1 #从端继续同步新master的数量
# 222 redis sentinel 配置
- [[email protected] ~]# vi /etc/redis/sentinel.conf
- #ip 192.168.1.222
- port 26379
- bind 0.0.0.0
- daemonize yes
- logfile "/var/log/sentinel_log.log"
- #myredis222
- sentinel monitor myredis222 192.168.1.222 6379 1
- sentinel down-after-milliseconds myredis222 10000
- sentinel failover-timeout myredis222 900000
- sentinel parallel-syncs myredis222 1
- sentinel auth-pass myredis222 654321
- #myredis224
- sentinel monitor myredis224 192.168.1.224 6379 1
- sentinel down-after-milliseconds myredis224 10000
- sentinel failover-timeout myredis224 900000
- sentinel parallel-syncs myredis224 1
- sentinel auth-pass myredis224 654321
# 224 redis sentinel 配置
- [[email protected] ~]# vi /etc/redis/sentinel.conf
- #ip 192.168.1.224
- port 26379
- bind 0.0.0.0
- daemonize yes
- logfile "/var/log/sentinel_log.log"
- #myredis222
- sentinel monitor myredis222 192.168.1.222 6379 1
- sentinel down-after-milliseconds myredis222 10000
- sentinel failover-timeout myredis222 900000
- sentinel parallel-syncs myredis222 1
- sentinel auth-pass myredis222 654321
- #myredis224
- sentinel monitor myredis224 192.168.1.224 6379 1
- sentinel down-after-milliseconds myredis224 10000
- sentinel failover-timeout myredis224 900000
- sentinel parallel-syncs myredis224 1
- sentinel auth-pass myredis224 654321
# 服务器 222 和 224 防火墙添加入站规则
- iptables -I INPUT -p tcp --dport 26379 -j ACCEPT
# 启动 sentinel 服务两种方法:
- redis-sentinel /etc/redis/sentinel.conf
- redis-server /etc/redis/sentinel.conf --sentinel
# 222 启动 sentinel 服务
- [[email protected] ~]# redis-sentinel /etc/redis/sentinel.conf
- [[email protected] ~]# cat /var/log/sentinel_log.log
- 24630:X 26 Nov 18:00:50.496 # Sentinel ID is b0c01ccf191b07bc7f8f42c395b1f45ec3849258
- 24630:X 26 Nov 18:00:50.496 # +monitor master myredis224 192.168.1.224 6379 quorum 1
- 24630:X 26 Nov 18:00:50.496 # +monitor master myredis222 192.168.1.222 6379 quorum 1
- 24630:X 26 Nov 18:00:50.497 * +slave slave 192.168.1.224:6379 192.168.1.224 6379 @ myredis222 192.168.1.222 6379
# 224 启动 sentinel 服务
- [[email protected] ~]# redis-sentinel /etc/redis/sentinel.conf
- [[email protected] ~]# cat /var/log/sentinel_log.log
- 29098:X 26 Nov 07:00:33.795 # Sentinel ID is 2303678ad7c6df83ce9c938ed9341fc33170f843
- 29098:X 26 Nov 07:00:33.795 # +monitor master myredis222 192.168.1.222 6379 quorum 1
- 29098:X 26 Nov 07:00:33.795 # +monitor master myredis224 192.168.1.224 6379 quorum 1
- 29098:X 26 Nov 07:00:33.797 * +slave slave 192.168.1.224:6379 192.168.1.224 6379 @ myredis222 192.168.1.222 6379
- 29098:X 26 Nov 07:00:34.560 * +sentinel sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis222 192.168.1.222 6379
- 29098:X 26 Nov 07:00:34.706 * +sentinel sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis224 192.168.1.224 6379
# 出现类似上面的信息,说明成功了。
# 连接 Sentinel ,查看信息(以224客户端连接为例)
- [[email protected] ~]# redis-cli -p 26379
- 127.0.0.1:26379> info Sentinel
- # Sentinel
- sentinel_masters:2
- sentinel_tilt:0
- sentinel_running_scripts:0
- sentinel_scripts_queue_length:0
- sentinel_simulate_failure_flags:0
- master0:name=myredis222,status=ok,address=192.168.1.222:6379,slaves=1,sentinels=2
- master1:name=myredis224,status=odown,address=192.168.1.224:6379,slaves=0,sentinels=2
- 127.0.0.1:26379>
# 可以看到 Sentinel 两个有2个master,其中 192.168.1.22 有1个slave。此时分别连接到 222 和 224 上查看复制信息。
# 在222 服务器连接redis:redis 角色为 master
- [[email protected] ~]# redis-cli
- 127.0.0.1:6379> auth 654321
- OK
- 127.0.0.1:6379> info Replication
- # Replication
- role:master
- connected_slaves:1
- slave0:ip=192.168.1.224,port=6379,state=online,offset=2088480,lag=1
- master_repl_offset:2088623
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:1040048
- repl_backlog_histlen:1048576
- 127.0.0.1:6379>
# 在224 服务器连接redis:redis 角色为 slave
- [[email protected] ~]# redis-cli
- 127.0.0.1:6379> auth 654321
- OK
- 127.0.0.1:6379> info Replication
- # Replication
- role:slave
- master_host:192.168.1.222
- master_port:6379
- master_link_status:up
- master_last_io_seconds_ago:1
- master_sync_in_progress:0
- slave_repl_offset:2087322
- 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
# 不急,先介绍 redis sentinel 相关命令
- 127.0.0.1:26379> SENTINEL masters #列出所有master状态信息
- 127.0.0.1:26379> SENTINEL master myredis222 #列出某个master状态信息(myredis222为例)
- 127.0.0.1:26379> SENTINEL slaves myredis222 #列出某个master的所有slave和状态信息(myredis222为例)
- 127.0.0.1:26379> SENTINEL sentinels myredis222 #列出某个master的sentinels((myredis222为例)
- 127.0.0.1:26379> SENTINEL get-master-addr-by-name myredis222 #通过某个master获取其IP及port(myredis222为例)
- 127.0.0.1:26379> SENTINEL ckquorum myredis222 #检查某个master是达到投票及故障转移条件(myredis222为例)
- 127.0.0.1:26379> SENTINEL flushconfig #强制重写 SENTINEL 配置到磁盘文件中
- 127.0.0.1:26379> SENTINEL failover myredis222 #这个操作就注意了!强制进行故障转移!慎操作!!!!!!!!!
# 现在测试超时故障转移,因配置文件设置 down-after-milliseconds 为10秒无法连接则认为服务器断开。
# 这里设置连接 15 秒,看是否主从复制角色能否进行故障转移。
- [[email protected] ~]# redis-cli -p 6379 -a 654321 DEBUG sleep 15
- OK
- [[email protected] ~]#
执行结束后,查看各复制状态。
# 222 角色由 master 变为了 slave
- 127.0.0.1:6379> auth 654321
- OK
- 127.0.0.1:6379> info Replication
- # Replication
- role:slave
- master_host:192.168.1.224
- master_port:6379
- master_link_status:up
- master_last_io_seconds_ago:0
- master_sync_in_progress:0
- slave_repl_offset:2422339
- 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:1357484
- repl_backlog_histlen:1048576
# 224 角色由 slave 变为了 master
- [[email protected] ~]# redis-cli
- 127.0.0.1:6379> auth 654321
- OK
- 127.0.0.1:6379> info Replication
- # Replication
- role:master
- connected_slaves:1
- slave0:ip=192.168.1.222,port=6379,state=online,offset=2396492,lag=1
- master_repl_offset:2396637
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:2384854
- repl_backlog_histlen:11784
自动切换了!!!get 和 set 同步正常,224 变成了只读实例。切换成功!!!!
# 现在试试强制故障转移。即把master转移到 myredis222。
- 127.0.0.1:26379> SENTINEL failover myredis222