当前位置:网站首页>Redis Cluster - Sentinel Mode Principle (Sentinel)
Redis Cluster - Sentinel Mode Principle (Sentinel)
2022-07-31 09:32:00 【IT-Lao Niu】
文章目录
1.哨兵模式(Sentinel)
哨兵模式是Redis的高可用性解决方案:由一个或多个Sentinel实例组成的SentinelThe system can monitor any number of master servers at the same time,and all slaves under each master.When the primary server is monitored to enter the offline state,自动将下线主服务器属下的某个从服务器升级为新的主服务器,然后由新的主服务器代替已下线的主服务器继续处理命令请求.
示意图
- The double ring represents the master serverserver1
- A single ring represents three slavesserver2、server3、server4
- server2、server3、server4Three slaves are replicating the masterserver1,而sentinelThe system is listening on all four servers
It can be seen that the sentinel mode is a sentinel system added to the master-slave replication mode,In order to achieve automatic transfer of failures
故障转移
如上图16-3所示,主服务server1挂掉了,处于下线状态,那么server2、server3、server4Replication operations to the primary server will be terminated,and after a whilesentinelThe system will also noticeserver1的下线,Let's talk about the failover process first:
- Sentinel系统会挑选server1属下的其中一个从服务器,And the selected slave server is upgraded to the new master server
- Sentinel系统会向server1All subordinate slaves send new replication commands,Make them slaves to the new master,当所有从服务器都开始复制新的主服务器时,故障转移操作执行完毕
- SentinelThe system will continue to monitor the offlineserver1,if it comes back online,will set it as a slave to the new master
2.SentinelCommunication between the system and each node
2.2.1.SentinelHow to determine if the main server is offline
主观下线
在默认情况下,Sentinel系统会以每秒一次The frequency is sent to all instances with which a command connection is createdPING命令,And judge whether the instance is online or not by the result returned by the instance.
如果一个实例在down-after-milliseconds毫秒内(默认30s),连续向Sentinel返回无效回复,那该SentinelIt will be marked as a subjective offline state.
Sentinal配置文件中的down-after-milliseconds
选项指定了Sentinel判断实例进入主观下线所需的时间长度
客观下线
当Sentinel将一个主服务器判断为主观下线之后,为了确认这个主服务器是否真的下线了,它会向同样监视这一主服务器的其他Sentinel进行询问,See if they also think the master is down(It can be subjective downline or objective downline),当Sentinel从其他SentinelAfter receiving a sufficient number of offline judgments there,Sentinel就会将从服务器判定为客观下线,and start a failover operation on the primary server.
客观下线状态的判断条件:当认为主服务器已经进入下线状态的Sentinel的数量,超过Sentinel配置中设置的quorum
参数的值,那么该Sentinel就会认为主服务器已经进入客观下线状态.
上面配置的含义:包括当前Sentinel在内,只要总共有两个SentinelThe main server is considered offline,那么当前Sentinel就将主服务器判断为客观下线.
当Sentinel将一个主服务器判断为主观下线后,It will tell others that are also monitoring the master serverSentinel进行询问,看它们是否同意这个主服务器已经进入主观下线状态.
3 .SentinelHow to determine if the main server is offline
3.1.选举领头Sentinel
当一个主服务器被判断为客观下线时,监视这个下线主服务器的各个Sentinel会进行协商,选举出一个领头Sentinel,并由领头Sentinel对下线主服务器执行故障转移操作.
选举领头Sentinel的规则:
1.所有在线的Sentinel都有被选为领头Sentinel的资格,换句话说,监视同一个主服务器的 多个在线Sentinel中的任意一个都有可能成为领头Sentinel
2.每次进行领头Sentinel选举之后,不论选举是否成功,所有Sentinel的配置纪元 (configuration epoch)的值都会自增一次. 配置纪元实际上就是一个计数器,并没有什么特 别的.
3.在一个配置纪元里面, 所有Sentinel都有一次将某个Sentinel设置为局部领头Sentinel的机会 并且局部领头一旦设置,在这个配置每个发现主服务器进入客观下线的Sentinel都会要求其他Sentinel将自己设置为局部领头Sentinel.
4.当一个Sentinel(源Sentinel)向另一个Sentinel(目标Sentinel)发送SENTINEL ismaster-down-by-addr命令,并且命令中的runid参数不是*符号而是源Sentinel的运行ID时,这表示源Sentinel要求目标Sentinel将前者设置为后者的局部领头Sentinel
5.Sentinel设置局部领头Sentinel的规则是先到先得:最先向目标Sentinel发送设置要求的源Sentinel将成为目标Sentinel的局部领头Sentinel,而之后接收到的所有设置要求都会被目标Sentinel拒绝
6.目标Sentinel在接收到SENTINEL is-master-down-by-addr命令之后,将向源Sentinel返回 一条命令回复,回复中的leader_runid参数和leader_epoch参数分别记录了目标Sentinel的局部领头Sentinel的运行ID和配置纪元
7.源Sentinel在接收到目标Sentinel返回的命令回复之后,会检查回复中leader_epoch参数 的值和自己的配置纪元是否相同,如果相同的话,那么源Sentinel继续取出回复中的 leader_runid参数, 如果leader_runid参数的值和源Sentinel的运行ID一致,那么表示目标 Sentinel将源Sentinel设置成了局部领头Sentinel
8.如果有某个Sentinel被半数以上的Sentinel设置成了局部领头Sentinel,那么这个Sentinel成 为领头Sentinel.举个例子,在一个由10个Sentinel组成的Sentinel系统里面,只要有大于等于 10/2+1=6个Sentinel将某个Sentinel设置为局部领头Sentinel,那么被设置的那个Sentinel就会成 为领头Sentine
9.因为领头Sentinel的产生需要半数以上Sentinel的支持,并且每个Sentinel在每个配置纪元里面只能设置一次局部领头Sentinel,所以在一个配置纪元里面,只会出现一个领头 Sentinel
10.如果在给定时限内,没有一个Sentinel被选举为领头Sentinel,那么各个Sentinel将在一段时间之后再次进行选举,直到选出领头Sentinel为止
演示案例
假设现在有三个Sentinel
正在监视同一个主服务器, 并且这三个Sentinel
之前已经通过 SENTINEL is-master-down-by -addr
命令确认主服务器进入了客观下线状态,如下图所示
那么为了选出领头Sentinel,三个Sentinel将再次向其他Sentinel发送SENTINEL is-masterdown-by-addr
命令,如下图所示
和检测客观下线状态时发送的SENTINEL is-master-down-by-addr
命令不同,SentinelThe command sent this time will have Sentinel自己的运行ID,
例如:
如果接收到这个命令的Sentinel还没有设置局部领头Sentinel的话,它就会将运行ID为e955b4c85598ef5b5f055bc7ebfd5e828dbed4fa的Sentinel设置为自己的局部领头Sentinel,并返 回类似以下的命令回复:
然后接收到命令回复的Sentinel就可以根据这一回复,统计出有多少个Sentinel将自己设置成了局部领头Sentinel
根据命令请求发送的先后顺序不同,可能会有某个Sentinel的SENTINEL is-master-downby -addr命令比起其他Sentinel发送的相同命令都更快到达,并最终胜出领头Sentinel的选举, 然后这个领头SentinelThe failover operation on the primary server is ready to begin.
3.2.Election of a new master server&故障转移
Election to leadSentinel之后,领头SentinelA failover operation will be performed on the offline server.
第一步:First it will go offline from the main server(server1)All subordinates pick one from the server in good condition、数据完整的从服务器,并发送SLAVE no one
命令.
第二步:lead at this timeSentinelwill be at the rate of once per second(Usually every ten seconds)To the upgraded slave server(server2)发送INFO
command and observe what is returnedrole是否已经变成了master,变成master说明升级成功.
第三步:领头Sentinelto the offline primary server(server1)的两个从服务器(server3、server4)发送SLAVEOF
命令,Have them replicate the new master(server2).
第四步:当server1重新上线时,Sentinel就会向它发送SLAVEOF
命令,Make it the new master(server2)的从服务器.
至此,The entire failover is complete.
边栏推荐
猜你喜欢
模块八
Come n times - 06. Print the linked list from end to end
Are postgresql range queries faster than index queries?
Andoird开发--指南针(基于手机传感器)
服务器上解压文件时提示“gzip: stdin: not in gzip format,tar: Child returned status 1,tar: Error is not recovera“
安装sambe
PyQt5快速开发与实战 9.4 Matplotlib在PyQt中的应用
js实现2020年元旦倒计时公告牌
安装gnome-screenshot截图工具
富文本编辑器Tinymce
随机推荐
Splunk Workflow action 给我们带来的好处
loadrunner脚本--添加检查点
Kotlin 优点
js空气质量aqi雷达图分析
零代码工具推荐 八爪鱼采集器
@RequestBody和@RequestParam区别
Come n times with the sword--05. Replace spaces
Redis Sentinel原理
OpenGL es 导读篇
Kotlin—基本语法 (四)
业务-(课程-章节-小节)+课程发布一些业务思路
Canvas particles change various shapes js special effects
作为面试官,关于线程池的问题我一般这样套路...
让动画每次重复前都有延迟
js右侧圆点单页滚动介绍页面
JSP config对象的简介说明
canvas粒子变幻各种形状js特效
二叉树的搜索与回溯问题(leetcode)
【TCP/IP】Network Model
【TCP/IP】网络模型