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 启动参数

[plain]  view plain  copy
  1. [root@centos222 ~]# vi /etc/redis/6379.conf  
  2.   
  3. bind 127.0.0.1 192.168.1.222    #redis服务器地址  
  4. port 26379          #sentinel端口号  
  5. daemonize yes       #后台启动  
  6. protected-mode no   #关闭保护模式,禁止远程直接访问  
  7. requirepass 654321  #本地访问需要密码  
  8. masterauth 654321   #备端同步访问需要密码  
  9. slave-read-only yes #备端只读  

# 224 配置redis 启动参数

[plain]  view plain  copy
  1. [root@centos224 ~]# vi /etc/redis/6379.conf  
  2.   
  3. bind 127.0.0.1 192.168.1.224    #redis服务器地址  
  4. port 26379          #sentinel端口号  
  5. daemonize yes       #后台启动  
  6. slaveof 192.168.1.222 6379      #设置为192.168.1.222 6379的从库  
  7. protected-mode no   #关闭保护模式,禁止远程直接访问  
  8. requirepass 654321  #本地访问需要密码  
  9. masterauth 654321   #备端同步访问需要密码  
  10. slave-read-only yes #备端只读  

# 配置 Redis Sentinel ,只配置主实例的监控即可。Sentinel 可以通过 master 实例获取从实例的信息。

# 基本参数说明:

[plain]  view plain  copy
  1. port 26379  #Sentinel 服务端口  
  2. sentinel monitor myredis 192.168.1.xxx 6379 1   #监控 人员组名/ip/端口/失效判定数(多少从端判断不同则认为master断开)  
  3. sentinel down-after-milliseconds myredis 10000  #ping 超过 10000 毫秒则认为宕机  
  4. sentinel failover-timeout myredis 900000        #主从切换超过 30000 毫秒则认为失败  
  5. sentinel can-failover myredis yes               #master断开后是否允许判断进行故障转移  
  6. sentinel parallel-syncs myredis 1               #从端继续同步新master的数量  


# 222 redis sentinel 配置

[plain]  view plain  copy
  1. [root@centos222 ~]# vi /etc/redis/sentinel.conf  
  2.   
  3. #ip 192.168.1.222  
  4. port 26379  
  5. bind 0.0.0.0  
  6. daemonize yes  
  7. logfile "/var/log/sentinel_log.log"  
  8. #myredis222  
  9. sentinel monitor myredis222 192.168.1.222 6379 1  
  10. sentinel down-after-milliseconds myredis222 10000  
  11. sentinel failover-timeout myredis222 900000  
  12. sentinel parallel-syncs myredis222 1  
  13. sentinel auth-pass myredis222 654321  
  14. #myredis224  
  15. sentinel monitor myredis224 192.168.1.224 6379 1  
  16. sentinel down-after-milliseconds myredis224 10000  
  17. sentinel failover-timeout myredis224 900000  
  18. sentinel parallel-syncs myredis224 1  
  19. sentinel auth-pass myredis224 654321  

# 224 redis sentinel 配置

[plain]  view plain  copy
  1. [root@centos224 ~]#  vi /etc/redis/sentinel.conf  
  2.   
  3. #ip 192.168.1.224  
  4. port 26379  
  5. bind 0.0.0.0  
  6. daemonize yes  
  7. logfile "/var/log/sentinel_log.log"  
  8. #myredis222  
  9. sentinel monitor myredis222 192.168.1.222 6379 1  
  10. sentinel down-after-milliseconds myredis222 10000  
  11. sentinel failover-timeout myredis222 900000  
  12. sentinel parallel-syncs myredis222 1  
  13. sentinel auth-pass myredis222 654321  
  14. #myredis224  
  15. sentinel monitor myredis224 192.168.1.224 6379 1  
  16. sentinel down-after-milliseconds myredis224 10000  
  17. sentinel failover-timeout myredis224 900000  
  18. sentinel parallel-syncs myredis224 1  
  19. sentinel auth-pass myredis224 654321  

# 服务器 222 和 224 防火墙添加入站规则

[plain]  view plain  copy
  1. iptables -I INPUT -p tcp --dport 26379 -j ACCEPT  

# 启动 sentinel 服务两种方法:

[plain]  view plain  copy
  1. redis-sentinel /etc/redis/sentinel.conf  
  2. redis-server /etc/redis/sentinel.conf --sentinel  

# 222 启动 sentinel 服务

[plain]  view plain  copy
  1. [root@centos222 ~]# redis-sentinel /etc/redis/sentinel.conf  
  2. [root@centos222 ~]# cat /var/log/sentinel_log.log  
  3. 24630:X 26 Nov 18:00:50.496 # Sentinel ID is b0c01ccf191b07bc7f8f42c395b1f45ec3849258  
  4. 24630:X 26 Nov 18:00:50.496 # +monitor master myredis224 192.168.1.224 6379 quorum 1  
  5. 24630:X 26 Nov 18:00:50.496 # +monitor master myredis222 192.168.1.222 6379 quorum 1  
  6. 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 服务

[plain]  view plain  copy
  1. [root@centos224 ~]# redis-sentinel /etc/redis/sentinel.conf  
  2. [root@centos224 ~]# cat /var/log/sentinel_log.log  
  3. 29098:X 26 Nov 07:00:33.795 # Sentinel ID is 2303678ad7c6df83ce9c938ed9341fc33170f843  
  4. 29098:X 26 Nov 07:00:33.795 # +monitor master myredis222 192.168.1.222 6379 quorum 1  
  5. 29098:X 26 Nov 07:00:33.795 # +monitor master myredis224 192.168.1.224 6379 quorum 1  
  6. 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  
  7. 29098:X 26 Nov 07:00:34.560 * +sentinel sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis222 192.168.1.222 6379  
  8. 29098:X 26 Nov 07:00:34.706 * +sentinel sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis224 192.168.1.224 6379  

# 出现类似上面的信息,说明成功了。 

# 连接 Sentinel ,查看信息(以224客户端连接为例)

[plain]  view plain  copy
  1. [root@centos224 ~]# redis-cli -p 26379  
  2. 127.0.0.1:26379> info Sentinel  
  3. # Sentinel  
  4. sentinel_masters:2  
  5. sentinel_tilt:0  
  6. sentinel_running_scripts:0  
  7. sentinel_scripts_queue_length:0  
  8. sentinel_simulate_failure_flags:0  
  9. master0:name=myredis222,status=ok,address=192.168.1.222:6379,slaves=1,sentinels=2  
  10. master1:name=myredis224,status=odown,address=192.168.1.224:6379,slaves=0,sentinels=2  
  11. 127.0.0.1:26379>   

# 可以看到 Sentinel 两个有2个master,其中 192.168.1.22 有1个slave。此时分别连接到 222 和 224 上查看复制信息。


# 在222 服务器连接redis:redis 角色为 master

[plain]  view plain  copy
  1. [root@centos222 ~]# redis-cli  
  2. 127.0.0.1:6379> auth 654321  
  3. OK  
  4. 127.0.0.1:6379> info Replication  
  5. # Replication  
  6. role:master  
  7. connected_slaves:1  
  8. slave0:ip=192.168.1.224,port=6379,state=online,offset=2088480,lag=1  
  9. master_repl_offset:2088623  
  10. repl_backlog_active:1  
  11. repl_backlog_size:1048576  
  12. repl_backlog_first_byte_offset:1040048  
  13. repl_backlog_histlen:1048576  
  14. 127.0.0.1:6379>   

# 在224 服务器连接redis:redis 角色为 slave

[plain]  view plain  copy
  1. [root@centos224 ~]# redis-cli  
  2. 127.0.0.1:6379> auth 654321  
  3. OK  
  4. 127.0.0.1:6379> info Replication  
  5. # Replication  
  6. role:slave  
  7. master_host:192.168.1.222  
  8. master_port:6379  
  9. master_link_status:up  
  10. master_last_io_seconds_ago:1  
  11. master_sync_in_progress:0  
  12. slave_repl_offset:2087322  
  13. slave_priority:100  
  14. slave_read_only:1  
  15. connected_slaves:0  
  16. master_repl_offset:0  
  17. repl_backlog_active:0  
  18. repl_backlog_size:1048576  
  19. repl_backlog_first_byte_offset:0  
  20. repl_backlog_histlen:0  

# 不急,先介绍 redis sentinel 相关命令

[plain]  view plain  copy
  1. 127.0.0.1:26379> SENTINEL masters            #列出所有master状态信息  
  2. 127.0.0.1:26379> SENTINEL master myredis222  #列出某个master状态信息(myredis222为例)  
  3. 127.0.0.1:26379> SENTINEL slaves myredis222 #列出某个master的所有slave和状态信息(myredis222为例)  
  4. 127.0.0.1:26379> SENTINEL sentinels myredis222   #列出某个master的sentinels((myredis222为例)  
  5. 127.0.0.1:26379> SENTINEL get-master-addr-by-name myredis222 #通过某个master获取其IP及port(myredis222为例)  
  6. 127.0.0.1:26379> SENTINEL ckquorum myredis222    #检查某个master是达到投票及故障转移条件(myredis222为例)  
  7. 127.0.0.1:26379> SENTINEL flushconfig            #强制重写 SENTINEL 配置到磁盘文件中  
  8. 127.0.0.1:26379> SENTINEL failover myredis222    #这个操作就注意了!强制进行故障转移!慎操作!!!!!!!!!  

# 现在测试超时故障转移,因配置文件设置 down-after-milliseconds 为10秒无法连接则认为服务器断开。
# 这里设置连接 15 秒,看是否主从复制角色能否进行故障转移。

[plain]  view plain  copy
  1. [root@centos222 ~]# redis-cli -p 6379 -a 654321 DEBUG sleep 15  
  2. OK  
  3. [root@centos222 ~]#   

执行结束后,查看各复制状态。

# 222 角色由 master 变为了 slave

[plain]  view plain  copy
  1. 127.0.0.1:6379> auth 654321  
  2. OK  
  3. 127.0.0.1:6379> info Replication  
  4. # Replication  
  5. role:slave  
  6. master_host:192.168.1.224  
  7. master_port:6379  
  8. master_link_status:up  
  9. master_last_io_seconds_ago:0  
  10. master_sync_in_progress:0  
  11. slave_repl_offset:2422339  
  12. slave_priority:100  
  13. slave_read_only:1  
  14. connected_slaves:0  
  15. master_repl_offset:0  
  16. repl_backlog_active:0  
  17. repl_backlog_size:1048576  
  18. repl_backlog_first_byte_offset:1357484  
  19. repl_backlog_histlen:1048576  

# 224 角色由 slave 变为了 master

[plain]  view plain  copy
  1. [root@centos224 ~]# redis-cli  
  2. 127.0.0.1:6379> auth 654321  
  3. OK  
  4. 127.0.0.1:6379> info Replication  
  5. # Replication  
  6. role:master  
  7. connected_slaves:1  
  8. slave0:ip=192.168.1.222,port=6379,state=online,offset=2396492,lag=1  
  9. master_repl_offset:2396637  
  10. repl_backlog_active:1  
  11. repl_backlog_size:1048576  
  12. repl_backlog_first_byte_offset:2384854  
  13. repl_backlog_histlen:11784  

自动切换了!!!get 和 set 同步正常,224 变成了只读实例。切换成功!!!!


# 现在试试强制故障转移。即把master转移到 myredis222。

[plain]  view plain  copy
  1. 127.0.0.1:26379> SENTINEL failover myredis222  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值