Keepalived high availability software

Keepalived was originally designed for LVS load balancing software to manage and monitor the status of each service node in an LVS cluster system, and then added with VRRP for high availability. Therefore, Keepalived can be used as a highly available solution software for other services besides managing LVS software.

Keepalived software mainly realizes high availability function through VRRP protocol. VRRP, which stands for Virtual Router Redundancy Protocol, is designed for the single point of failure of static routes, ensuring that the entire network runs continuously when individual nodes fail. Therefore, Keepalived has the function of configuration management LVS on the one hand, and also has the function of health check on the nodes below LVS, on the other hand, it can also realize the high availability of system network services.

2. Keepalived high availability failover transfer principle

Keepalived Failover between high availability service pairs is implemented through VRRP. While keepalived is working, the Master node sends heartbeat messages (multicast) to the standby node to tell the standby node that it is still alive. When the active node fails, heartbeat messages cannot be sent, and the standby node cannot continue to detect the heartbeat of the active node. It then invokes its own receiver to take over IP resources and services of the master node. When the active node is restored, the standby node releases the IP resources and services that the active node took over when the active node is faulty and restores to the original standby role.

3. Keepalived high availability experimental environment description

As shown in the figure below, there are two Nginx load balancers on the front end that distribute requests received from clients. Nginx01 is configured in the previous section, and Nginx02 is configured in the same way. Now you need to do a high availability configuration on two Nginx load balancers, with Nginx01 as the primary node and Nginx02 as the standby node.


4. Install and enable Keepalived

Keepalived is easy to install using YUM.

 yum install keepalived -yCopy the code

Once installed, start keepalived and write Keepalived into the boot script.

/etc/init.d/keepalived star
echo "/etc/init.d/keepalived start" >>/etc/rc.localCopy the code

Once started, there will be three processes. After that, keepalived software can be turned off. Next, keepalived configuration files need to be modified.

5. Modify the Keepalived profile and restart the Keepalived service

/etc/init.d/keepalived stop    # Turn off keepalived service
vim /etc/keepalived/keepalived.conf  Open edit with VimCopy the code

Configuration file of the primary node

Configuration file of the standby node

Note: Modifying the configuration file is mainly a few areas in bold. Here are the meanings of those parameters:

  • Router_id is a route id and should be unique within a LAN.
  • Vrrp_instance VI_1 {… } this is a VRRP instance, which defines keepalived state, interface, priority, authentication and IP information;
  • Virtual_router_id indicates the ID of the virtual route. A set of Keepalived configurations have the same Settings for the master and slave. Priority indicates the priority. A larger number indicates the higher priority. Auth_type indicates the authentication mode, and auth_pass indicates the authentication password
  • Virtual_ipaddress {… } Define the virtual IP address. Multiple IP addresses can be configured. Here I define 192.168.31.5 and bind the network interface eth1, virtual interface eth1:1

After modifying the master node, save and exit, then start Keepalived and within a few minutes a virtual IP address is generated: 192.168.31.5

Then modify the configuration file of the standby node, save and exit and start Keepalived, it will not generate virtual IP, if generated that is the configuration file error. The standby node and the primary node compete for IP resources. This phenomenon is called “split brain”.

6. Perform an ha switchover between the active and standby servers

Disable keepalived on the active node and check whether VIP: 192.168.31.5 is generated on the standby node

Start the keepalived service for the master node and then check the VIPs for the master and standby nodes. The master node should grab the VIPs back:

7. Test with Nginx load balancing

Modify the Hosts file of Windows to point the domain name to VIP

Then, open a browser to www.pcm.com, and view the CLIENT IP address recorded in access.log on web01

The IP address of the client recorded in logs is 192.168.31.1, and the reverse proxy server is 192.168.31.3. Let’s stop the Keepalived service and see if the standby node takes over the VIP and service from the primary node.

As you can see, the standby node does take over the work of the primary node. Re-enable the master node and the results of the experiment are not validated.

8. Write daemon scripts for Nginx Web services

One of the problems with the test above is that we used Nginx to load balance the packets of requests. If the Keepalived service on the primary node is running properly and Nginx is not running properly, then the Nginx load balancing service will fail and cannot be switched to Nginx load balancer 02, and the backend Web server will not be able to receive requests. Therefore, we should check if Nginx service is running properly, if not, we should stop Keepalived service, so that the automatic switch to the standby node.

We can check whether port 80 is enabled to determine the health of Nginx, every 2 seconds, the script below

#! /bin/bash
while true
do
if [ $(netstat -tlnp|grep nginx|wc -l) -ne1]then
    /etc/init.d/keepalived stop
fi
sleep 2
doneCopy the code

The results of the experiment can be executed in the background and then stopped Nginx service validation