Keepalived role election and keepalived role election
Introduction:
1. In a Keepalived cluster, the MASTER and slave nodes are not strictly involved. Although the state option can be defined as the MASTER state in keepalived. conf,
However, this does not mean that this node has always been a MASTER role. The master node role is the priority value in keepalived. conf, and the weight value in the vrrp_script module,
The two options correspond to an integer. The weight value can be a positive integer or a negative integer. The role of a node in the cluster is controlled by these two values.
2. In a Keepalived cluster with one MASTER node and multiple slave nodes, the MASTER node with the highest priority value will be the MASTER node in the cluster, and all others will be the BACKUP node.
When the MASTER node fails, the BACKUP nodes will elect roles, calculate the node priority and weight, and select a new MASTER node to take over the service.
3. In the vrrp_script module, if the weight value is not set, the cluster priority is determined by the priority value in the keepalived. conf configuration file.
Weight Value Setting:
Nodes A and B form Keepalived clusters. The priority value of A is 100, and the priority value of B is 80.
Under normal circumstances, A is the MASTER/B is the BACKUP
Now we have added the resource monitoring vrrp_script module to monitor nginx.
The value of weight must be greater than 20 in the case of a positive integer.
When Nginx in node A fails to provide services, the script detection of node A fails and 1 is returned.
At this time, the weight of node A will keep the priority value 100
The weight of Node B will change to the sum of priority + weight, that is, 80 + weight. If the sum of weight and weight must be greater than 100, the master-slave switchover will occur.
Two cases of weight:
1. Positive Integer
When the vrrp_script script of the MASTER node fails to detect, the priority value of the MASTER node is smaller than the priority + weight Value of the BACKUP node, and the MASTER-slave switchover is performed.
When the vrrp_script script of the MASTER node is detected successfully, the value of priority + weight of the MASTER node is greater than that of priority + weight of the BACKUP node, and the MASTER node is still the MASTER node, without switching
2. negative integer
When the vrrp_script of the MASTER node fails to be detected, the MASTER node priority-weight is less than the value of the BACKUP node priority.
When the vrrp_script script of the MASTER node is detected successfully, if the priority value of the MASTER node is greater than the priority value of the BACKUP node, the MASTER node is still the MASTER node and no switchover occurs.
Conclusion: the absolute value of weight must be greater than the priority of the MASTER node-BACKUP node!