Redis哨兵(Sentinel)
在了解Redis哨兵模式之前,需要提前了解一下Redis主从复制,本例是基于本人上一篇博客Redis主从复制写的
概述
Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:
- 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
- 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
- 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 Sentinel 进程(progress), 这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息, 并使用投票协议(agreement protocols)来决定是否执行自动故障迁移, 以及选择哪个从服务器作为新的主服务器。
虽然 Redis Sentinel 释出为一个单独的可执行文件 redis-sentinel , 但实际上它只是一个运行在特殊模式下的 Redis 服务器, 你可以在启动一个普通 Redis 服务器时通过给定 –sentinel 选项来启动 Redis Sentinel 。
sentinel 配置文件
在Redis源代码中已经给出了一个sentinel.conf的文件,如图:
sentinel配置文件最少配置如下:
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5
所以需要在sentinel.conf中增加这些配置
启动sentinel
使用以下命令启动sentinel
[root@localhost src]# pwd
/opt/redis/src
[root@localhost src]# ./redis-sentinel ../sentinel6370.conf
启动成功后日志如下:
[root@localhost src]# ./redis-sentinel ../sentinel6370.conf
13882:X 27 Jul 15:48:23.688 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 13882
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
13882:X 27 Jul 15:48:23.689 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
13882:X 27 Jul 15:48:23.689 # Sentinel ID is 66467dc5418f2dbdf6414dadbfa22e7b51b0dd2c
13882:X 27 Jul 15:48:23.689 # +monitor master mymaster 192.168.44.129 6381 quorum 1
日志分析:
13882:X 27 Jul 15:48:23.689 # +monitor master mymaster 192.168.44.129 6381 quorum 1
通过观察日志可知,当前sentinel正在监听着一台名为mymaster,ip地址:192.168.44.129,端口:6381的redis服务的主机。
接下来,我们分别启动redis主从服务,并查看一下主从信息。如图:
上图中可以看出目前port=6381的Redis服务是主机,另外两台是从机。
现在我们把主机服务杀掉,再来看sentinel的日志变化。
当sentinel监控到Redis服务主机挂掉后,日志中出现了以下日志:
13906:X 27 Jul 16:10:47.516 # +switch-master mymaster 192.168.44.129 6381 192.168.44.129 6380
13906:X 27 Jul 16:10:47.516 * +slave slave 192.168.44.129:6382 192.168.44.129 6382 @ mymaster 192.168.44.129 6380
13906:X 27 Jul 16:10:47.516 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 192.168.44.129 6380
13906:X 27 Jul 16:10:47.516 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 192.168.44.129 6380
13906:X 27 Jul 16:10:47.516 * +slave slave 192.168.44.129:6381 192.168.44.129 6381 @ mymaster 192.168.44.129 6380
从switch-master mymaster 192.168.44.129 6381 192.168.44.129 6380
这句日志中分析得知,目前的redis主机已经从之前的6381切换到了6380。
我们再通过127.0.0.1:6380> info replication
来查看6380这个redis服务主从信息。执行命令后,出现以下信息:
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:28120
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:28119
127.0.0.1:6380>
这说明,sentinel已经可以监控Redis主从服务,且当主机因意外挂机后,可以自动切换到一台从机做为最新的Redis服务主机。
之前挂机的主机在重新启动后会做为当前服务主机的一个从机。
参考链接:
http://www.redis.cn/topics/sentinel.html
http://doc.redisfans.com/topic/sentinel.html#id7