Sentinel哨兵是redis官方提供的高可用方案,可以用它来监控多个Redis服务实例的运行情况。说白了就是24小时不间断服务。
一、监控
Sentinel会不断检查Master和Slave是否正常
如果Sentinel挂了,就无法监控,所以需要多个哨兵,组成Sentinel网络
监控同一个Master的Sentinel会自动连接,组成一个分布式的Sentinel网络,互相通信并交换彼此关于被监控服务器的信息
当一个Sentinel认为被监控的服务器已经下线时,它会向网络中的其它Sentinel进行确认,判断该服务器是否真的已经下线
如果下线的服务器为主服务器,那么Sentinel网络将对下线主服务器进行自动故障转移,通过将下线主服务器的某个从服务器提升为新的主服务器,并让其从服务器转移到新的主服务器下,以此来让系统重新回到正常状态
下线的旧主服务器重新上线,Sentinel会让它成为从,挂到新的主服务器下
二、Sentinel配置
复制三份sentinel.conf文件:
sentinel26380.conf、sentinel26381.conf、sentinel26382.conf
三份sentinel配置文件修改:
1、修改 port 26380、 port 26381、 port 26382
2、修改 sentinel monitor mymaster 127.0.0.1 6380 2
对于配置二 格式:Sentinel monitor <name> <masterIP> <masterPort><Quorum投票数>
端口代表监视的主服务器, 2代表法庭人数,当主服务器坏了,哨兵会经过投票决定是否切换备用服务器。
Sentinel会根据Master的配置自动发现Master的Slave,Sentinel默认端口号为26379
三、启动Sentinel
redis安装时make编译后就产生了redis-sentinel程序文件,可以在一个redis中运行多个sentinel进程
执行以下三条命令,将创建三个监视主服务器的Sentinel实例:
./redis-sentinel ../sentinel26380.conf
./redis-sentinel ../sentinel26381.conf
./redis-sentinel ../sentinel26382.conf
启动一个运行在Sentinel模式下的Redis服务实例
./redis-sentinel ../sentinel26381.conf
然后我们来测试Sentinel:
首先关闭主服务器
然后观察哨兵界面:
他们开始投票,商量,决定是否且换主机。
而且其中有一句:已经告知新的主服务器是哪台。
四、Sentinel总结
主从复制,解决了读请求的分担,从节点下线,会使得读请求能力有所下降,Master下线,写请求无法执行
Sentinel会在Master下线后自动执行故障转移操作,提升一台Slave为Master,并让其它Slave成为新Master的Slave