简述
虽然Redis的主从复制可以实现数据的备份,保障数据的安全,但是还不能实现高可用,需要手动故障转移,因此Master仍然可能存在单点故障,为此Redis提供了Sentinel(哨兵)模式来实现高可用。
Redis Sentinel 提供以下功能:
- Monitoring(监控):Sentinel会不断检查Master和Slave是否正常工作;
- Notification(通知):Sentinel所监控的任意一个Redis实例出现问题,都可以以API的方式告知系统管理员或者开发人员;
- Automatic failover(自动故障转移):如果Master出现故障,Sentinel会自动选择一个Slave升级为Master,并且修改其他Slave的配置,指向新的Master,最后告知应用程序新Master地址;
- Configuration provider(配置提供者):Sentinel会充当服务发现客户端,应用程序通过Sentinel获取Master地址,如果发生故障转移,Sentinel会报告新地址。
架构
Redis Sentinel是一个分布式系统,多个Sentinels协同工作:
- 当Master无法正常提供服务时,多个Sentinel均可检测Master是否真的故障,降低单个Sentinel误报的可能性
- 即使一个Sentinel出现问题,其他Sentinel仍可以继续提供服务,防止Sentinel出现单点故障问题
Redis Sentinel是Redis Server的一个组件,需要单独部署,建议至少使用三个Sentinel来保证高可用。
配置
修改sentinel.conf文件,配置如下内容:
sentinel monitor <master-group-name> <ip> <port> <quorum>
master-group-name、ip、port都很容易理解,配置Master的ip地址与端口号,并指定名称。quorum是指参与检测Master故障的sentinel个数,如2表示为:至少两个sentinel检测到Master故障,才进行故障转移,切换Master。
使用 redis-sentinel 便可以简单的运行Sentinel,注意要指定配置文件
redis-sentinel /path/to/sentinel.conf
或者添加sentinel参数来启动redis-server
redis-server /path/to/sentinel.conf --sentinel