Sentinel之间互相监控
Sentinel是特殊的Redis节点,也能发布订阅;
Sentinel没有主从之分;
Sentinel订阅所有Redis节点的_sentinel_:hello频道,并在上线时给所有Redis节点的_sentinel_:hello频道发送消息,包括自己的host、进程ID(runid)、以及Master配置,让其他Sentinel感知自己,更新存储的Sentinel列表(如果是新的host、新的进程号,则进行添加;如果已经有host相同,但进程ID不同的Sentinel,代表原有的Sentinel重启了,会进行替换),如果Master配置不同,代表其他的Sentinel的Master配置的版本低,也会进行替换。
Sentinel上线后会每隔1秒给其他Sentinel发送消息。
Sentinel会询问Master,了解Master的Slave的路由。
备注:runid:Redis Server启动时创建的40位随机字符串,重复概率非常小。
Sentinel判断Master是否下线
Sentinel每隔1秒向Master发送PING命令,如果30秒内(down-after-milliseconds)没有收到+PONG 、 -LOADING或者-MASTERDOWN,认为Master主观下线,然后会询问其他Sentinel,超过半数Sentinel认为Master下线,Master就是客观下线,进行新Master的选举。
处理Master故障(Automatic Failover:自动故障转移)
建议拜读:
总结:https://www.cnblogs.com/kevingrace/p/9004460.html
Sentinel选择Leader
基于Raft算法:
Master客观下线后,各Sentinel谁先请求投票,就把票投给谁;谁得到的票多,就选举谁成为Leader。
Leader不会通知其他Sentinel自己成为Leader,Leader选举出Master后,其他Sentinel会删除Master客观下线的标记。
Raft算法和Sentinel Leader Election差异:
Raft是Follower在150ms到300ms之间各自随机的election timeout时间过后开始选举;
Leader会通知其他节点自己成为了Leader。
重选Master
Redis Server与Sentinel断开连接超过阈值,失去选举权。
如果有选举权,比较优先级:replica-priority,默认100,值越小优先级越高,选择优先级最高的。
如果优先级相同,比较当前复制Master的Offset(replication offset),选择大的,也就是复制数据最多的。
如果Offset相同,比较进程ID(runid),选择最小的。
Sentinel Leader切换Master
Sentinel Leader向选出的Master发送命令:slaveof no one,抹去Master之前的Slave印记。
Sentinel Leader向Slave发送命令:slaveof host port,host、port是新的Master的主机(IP或主机名)、端口,确定Slave的Master。
Sentinal总结
官网阐述:https://redis.io/topics/sentinel
This is the full list of Sentinel capabilities at a macroscopical level (i.e. the big picture):
- Monitoring. Sentinel constantly checks if your master and replica instances are working as expected.
- Notification. Sentinel can notify the system administrator, or other computer programs, via an API, that something is wrong with one of the monitored Redis instances.
- Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a replica is promoted to master, the other additional replicas are reconfigured to use the new master, and the applications using the Redis server are informed about the new address to use when connecting.
- Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address.
翻译:
以下是宏观层面(即宏观层面)上的所有前哨能力:
监控。Sentinel经常检查主实例和副本实例是否按预期工作。
通知。Sentinel可以通过API通知系统管理员或其他计算机程序,其中一个被监控的Redis实例出现了问题。
自动故障转移。如果一个主服务器不能正常工作,哨兵可以启动一个故障转移过程,其中一个副本被提升为主服务器,其他额外的副本被重新配置为使用新的主服务器,并且使用Redis服务器的应用程序被告知在连接时使用的新地址。
配置提供者。哨兵充当客户端服务发现的权威来源:客户端连接到哨兵,以询问负责给定服务的当前Redis主服务器的地址。如果发生故障转移,哨兵将报告新地址。