用途
Redis Sentinel 用于为redis提供高可用保证, 稳定版从Redis 2.8开始被引入, 能够检查master和slave的状态是否正常、master失效时提供自动主从切换。
配置文件sentinel.conf
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
只需要指定master即可,slave会通过自动发现获取,然后补充到该配置文件中;
每次当有slave提升到master或者有另外的sentinel加入时,会重写该文件
文件格式
sentinel monitor <master-group-name> <ip> <port> <quorum>
quorum 指检测到失效时,需要多少个sentinel同意时再进行故障切换;
在具体实施故障切换时,如果大部分Sentinel进程失效,将重新选举leader来执行切换
实际情况中, Sentinel never starts a failover if the majority of Sentinel processes are unable to talk.
启动Sentinel
redis-sentinel /path/to/sentinel.conf
or
redis-server /path/to/sentinel.conf --sentinel
说明:
1. 启动时必须指定配置文件,如果文件不可写或者没有指定时, 将拒绝启动。
2. Sentinel 默认监听TCP 26379端口, 其他运行Sentinel的实例会通过该端口进行通信, 端口不通将无法实现故障切换。
部署前需要注意的一些事项
1. 至少需要三个Sentinel实例, 以保证稳定性
2. 各个实例需要部署在不同的虚拟机或物理机上,避免一起挂掉
3. Sentinel + Redis 由于使用异步写,所以并不保证失效时的数据一致性,但可以减少数据丢失的时间窗口
4. 客户端需要有Sentinel支持,当前并非所有客户端都支持Sentinel
5. Docker的端口重新映射会破坏Sentinel对其他Sentinel和slaves的自动发现,需要谨慎使用。
参考