当前位置:网站首页>Keepalived中Master和Backup角色选举策略
Keepalived中Master和Backup角色选举策略
2022-07-03 11:01:00 【星哥玩云】
在Keepalived集群中,其实并没有严格意义上的主、备节点,虽然可以在Keepalived配置文件中设置“state”选项为“MASTER”状态,但是这并不意味着此节点一直就是Master角色。控制节点角色的是Keepalived配置文件中的“priority”值,但并它并不控制所有节点的角色,另一个能改变节点角色的是在vrrp_script模块中设置的“weight”值,这两个选项对应的都是一个整数值,其中“weight”值可以是个负整数,一个节点在集群中的角色就是通过这两个值的大小决定的。
在一个一主多备的Keepalived集群中,“priority”值最大的将成为集群中的Master节点,而其他都是Backup节点。在Master节点发生故障后,Backup节点之间将进行“民主选举”,通过对节点优先级值“priority”和““weight”的计算,选出新的Master节点接管集群服务。
在vrrp_script模块中,如果不设置“weight”选项值,那么集群优先级的选择将由Keepalived配置文件中的“priority”值决定,而在需要对集群中优先级进行灵活控制时,可以通过在vrrp_script模块中设置“weight”值来实现。下面列举一个实例来具体说明。
假定有A和B两节点组成的Keepalived集群,在A节点keepalived.conf文件中,设置“priority”值为100,而在B节点keepalived.conf文件中,设置“priority”值为80,并且A、B两个节点都使用了“vrrp_script”模块来监控MySQL服务,同时都设置“weight”值为10,那么将会发生如下情况。
在两节点都启动Keepalived服务后,正常情况是A节点将成为集群中的Master节点,而B自动成为Backup节点,此时将A节点的mysql服务关闭,通过查看日志发现,并没有出现B节点接管A节点的日志,B节点仍然处于Backup状态,而A节点依旧是Master状态,在这种情况下整个HA集群将失去意义。
下面就分析一下产生这种情况的原因,这也就是Keepalived集群中主、备角色选举策略的问题。下面总结了在Keepalived中使用vrrp_script模块时整个集群角色的选举算法,由于“weight”值可以是正数也可以是负数,因此,要分两种情况进行说明。
1. “weight”值为正数时
在vrrp_script中指定的脚本如果检测成功,那么Master节点的权值将是“weight值与”priority“值之和,如果脚本检测失败,那么Master节点的权值保持为“priority”值,因此切换策略为:
Master节点“vrrp_script”脚本检测失败时,如果Master节点“priority”值小于Backup节点“weight值与”priority“值之和,将发生主、备切换。
Master节点“vrrp_script”脚本检测成功时,如果Master节点“weight”值与“priority”值之和大于Backup节点“weight”值与“priority”值之和,主节点依然为主节点,不发生切换。
2. “weight”值为负数时
在“vrrp_script”中指定的脚本如果检测成功,那么Master节点的权值仍为“priority”值,当脚本检测失败时,Master节点的权值将是“priority“值与“weight”值之差,因此切换策略为:
Master节点“vrrp_script”脚本检测失败时,如果Master节点“priority”值与“weight”值之差小于Backup节点“priority”值,将发生主、备切换。
Master节点“vrrp_script”脚本检测成功时,如果Master节点“priority”值大于Backup节点“priority”值时,主节点依然为主节点,不发生切换。
在熟悉了Keepalived主、备角色的选举策略后,再来分析一下刚才实例,由于A、B两个节点设置的“weight”值都为10,因此符合选举策略的第一种,在A节点停止Mysql服务后,A节点的脚本检测将失败,此时A节点的权值将保持为A节点上设置的“priority”值,即为100,而B节点的权值将变为“weight”值与“priority”值之和,也就是90(10+80),这样就出现了A节点权值仍然大于B节点权值的情况,因此不会发生主、备切换。
对于“weight”值的设置,有一个简单的标准,即“weight”值的绝对值要大于Master和Backup节点“priority”值之差。对于上面A、B两个节点的例子,只要设置“weight”值大于20即可保证集群正常运行和切换。由此可见,对于“weight值的设置,要非常谨慎,如果设置不好,将导致集群角色选举失败,使集群陷于瘫痪状态。
边栏推荐
猜你喜欢

2022 东北四省赛 VP记录/补题

Understand go language context in one article

鸿蒙第四次培训

Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests

Numpy np.max和np.maximum实现relu函数

ASP.NET-酒店管理系统

STL教程9-容器元素深拷贝和浅拷贝问题

Matlab extracts numerical data from irregular txt files (simple and practical)

鸿蒙第三次培训(项目实训)

Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
随机推荐
2022 northeast four provinces match VP record / supplementary questions
ASP. Net hotel management system
The R language uses the hist function in the native package (basic import package, graphics) to visualize the histogram plot
VS2015的下载地址和安装教程
Phpcms prompt message page Jump showmessage
Stm32hal library upgrades firmware based on flash analog U disk (detailed explanation)
Unity3D学习笔记5——创建子Mesh
JGG专刊征稿:时空组学
Driver development based on I2C protocol
VPP three-layer network interconnection configuration
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
Slam mapping and autonomous navigation simulation based on turnlebot3
R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
Excel表格转到Word中,表格不超边缘纸张范围
Viewing binary bin files with notepad++ editor
Dynamic programming (interval DP)
How to make others fear you
剑指offer专项32-96题做题笔记