Redis 主从切换的集群管理之redis-sentinel

2018年3月13日

当我们使用 Redis 做缓存策略。刚开始的时候,redis是一个单点,此时如果redis机器岩机,redis的 服务就完全停止,这时就会影响其站点的正常运行。 下面利用自带的一个集群管理工具  redis sentinel 做一个主从切换的集群管理,下面介绍我的做法。

这里我准备了三台机器  VirtualBOX 的虚拟机

Systen  : Centos 6

IP:192.168.11.249   (redis master)  port:6379
IP:192.168.11.249   (redis slave3)  port:6380  一台机器多实例redis
IP:192.168.11.250   (redis slave 1) port:6379
IP:192.168.11.251   (redis slave 2) port:6379

做好主从配置

vim /etc/redis.conf

 

slaveof 192.168.11.249 6379
masterauth redispass
requirepass redispass

 

IP:192.168.11.249  master

 

127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:3
slave0:ip=127.0.0.1,port=6380,state=online,offset=53535,lag=1
slave1:ip=192.168.11.250,port=6379,state=online,offset=53549,lag=0
slave2:ip=192.168.11.251,port=6379,state=online,offset=53549,lag=0
master_repl_offset:53549
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:53548

 

IP:192.168.11.250 slave

127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:192.168.11.249
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:101631
slave_priority:99
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

IP:192.168.11.251  slave

127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:192.168.11.249
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:15300
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配置

主页地址:http://redis.io/topics/sentinel

sentinel主要功能就是为Redis M-S(master,slaves)集群提供了

1)master存活检测

2)集群中M-S服务监控

3) 自动故障转移,M-S角色转换等能力,

从一个方面说是提高了redis集群的可用性.
#添加并编辑配置文件/etc/sentinel.conf,新增内容如下,也可在安装文件中负责sentinel配置文件并作适当修改:
# port 可以一样也可以向下面所示 不同

vim /etc/redis/sentinel.conf

 

[root@localhost ~]# cat /etc/sentinel.conf

 

port 26379
sentinel monitor mymaster 192.168.11.249 6379 2
sentinel auth-pass mymaster redispass
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 900000
sentinel parallel-syncs mymaster 1

 

[root@localhost ~]# cat /etc/sentinel.conf

 

port 26479
sentinel monitor mymaster 192.168.11.249 6379 2
sentinel auth-pass mymaster redispass
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 900000
sentinel parallel-syncs mymaster 1

 

[root@localhost ~]# cat /etc/sentinel.conf

 

port 26579
sentinel monitor mymaster 192.168.11.249 6379 2
sentinel auth-pass mymaster redispass
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 900000
sentinel parallel-syncs mymaster 1

#在三个服务器中以sentinel模式启动redis-server

[root@localhost ~]# service redis-sentinel start
[root@localhost ~]# service redis-sentinel start
[root@localhost ~]# service redis-sentinel start

此时可以看看日志

vim  /var/log/redis/sentinel.log

#最后三台服务器sentinel输出如下:(注意每个机器输出对应的都是另外两台机器的IP)

版本信息
......
16166:X 19 Feb 20:23:30.721 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
16166:X 19 Feb 20:23:30.721 # Sentinel runid is f76f2e2ea561611ffa4d6e43e457a35a8f6ae495
16166:X 19 Feb 20:23:30.721 # +monitor master mymaster 192.168.11.249 6379 quorum 2
16166:X 19 Feb 20:23:30.725 * +slave slave 127.0.0.1:6688 127.0.0.1 6688 @ mymaster 192.168.11.249 6379
16166:X 19 Feb 20:23:30.736 * +slave slave 127.0.0.1:6687 127.0.0.1 6687 @ mymaster 192.168.11.249 6379
16166:X 19 Feb 20:23:30.738 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 192.168.11.249 6379
16166:X 19 Feb 20:23:30.740 * +slave slave 192.168.11.250:6379 192.168.11.89 6379 @ mymaster 192.168.11.249 6379
16166:X 19 Feb 20:23:30.742 * +slave slave 192.168.11.251:6379 192.168.11.250 6379 @ mymaster 192.168.11.249 6379
16166:X 19 Feb 20:24:00.803 # +sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 192.168.11.249 6379
16166:X 19 Feb 20:27:48.735 # User requested shutdown...
16166:X 19 Feb 20:27:48.735 * Removing the pid file.
16166:X 19 Feb 20:27:48.735 # Sentinel is now ready to exit, bye bye...

测试

 #关掉192.168.11.250 上的redis-server服务

 service  redis  stop

#每个sentinel将输出如下信息,表示认为该 slave已经宕机,剔除

30141:X 20 Feb 09:55:33.570 # -sdown slave 192.168.11.250:6379 192.168.11.250 6379 @ mymaster 192.168.11.249 6379
30141:X 20 Feb 09:55:43.544 * +convert-to-slave slave 192.168.11.250:6379 192.168.11.250 6379 @ mymaster 192.168.11.249 6379

在启动 redis  查看 sentinel
service redis  start

30141:X 20 Feb 10:26:49.249 # +sdown slave 192.168.11.250:6379 192.168.11.250 6379 @ mymaster 192.168.11.249 6379
30141:X 20 Feb 10:28:11.776 * +reboot slave 192.168.11.250:6379 192.168.11.250 6379 @ mymaster 192.168.11.249 6379
30141:X 20 Feb 10:28:11.828 # -sdown slave 192.168.11.250:6379 192.168.11.250 6379 @ mymaster 192.168.11.249 6379
"/var/log/redis/sentinel.log" 431L, 40764C

如果关掉主redis 那么  sentinel 就会选择一台接收 master  并将其他 slave  的master 修改到这个上面

参考 http://mrcto.blog.51cto.com/1923168/1319542

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注