[keepalived] A high-availability artifact that enables automatic active/standby switching of applications

Table of contents

1 Overview

2.Configuration

3. Effect

4.Keepalived active/standby switching principle

5. Contact the author


1 Overview

What is keepalived:

keepalived is a service high availability solution based on VRRP protocol. The VRRP protocol, also known as the Virtual Router Redundancy Protocol, was originally proposed as a routing protocol to solve the problem of a single point of failure when static gateways are configured in a local area network.

In human terms, the VRRP protocol allows a machine to have one or more virtual IPs. This virtual IP will first be bound to an actual IP. Access to this virtual IP will be sent to the actual IP. If the actual IP hangs up, the virtual IP will be re-bound to another available IP (commonly known as IP drift). ).

What is it used for:

For requests, I only need to access a fixed IP address (virtual IP). I don't care who provides the service later, as long as the service can be continuously provided. This naturally achieves high availability.

What this article is going to do:

This article will use keepalived to achieve high availability between two nginx nodes. Realize the effect that when one nginx hangs up, the virtual IP drifts to another nginx that is still alive. As for the automatic switching of active and backup for other types of applications, the usage is similar.

The IP address planning for this example is as follows:

192.168.31.20  

master
192.168.31.30 backup

I won’t go into details about the installation of nginx here. You can read the blogger’s other article:

linx installation nginx__BugMan's blog-CSDN blog

Or an article by another author:

Install Nginx in Linux, very detailed_Detailed steps to install nginx in Linux-CSDN Blog

2.Configuration

There are many ways to install keepalived. Here we use yum installed to install it.

yum install -y keepalived

First, we need to write a check_nginx.sh script under /etc/keepalived/ to check whether nginx is alive. If it hangs, directly stop keepalived on the current node.

#!/bin/bash
counter=$(ps -C nginx --no-headinglwc -1)
  if["${counter]"= "0"]; 
    then systemctl start nginx
    sleep 2
    counter=$(ps -C nginx --no-heading | wc -l)
    if["${counter}"="0"]; 
    then systemctl stop keepalived
  fi
fi

Next we configure keepalived.conf:

Please note that there is a sinkhole here: which brackets in the configuration file should be line-wrapped, and which brackets should have spaces, must be strictly aligned with the following format. Even if a bracket does not have a line-break, an error will be reported in the format!

Keepalived configuration of the master node:

#Global configuration, routing ID remains unchanged
global_defs
{   router_id LVS_DEVEL }

vrrp_script chk_nginx
{   script "/etc/keepalived/check_nginx.sh"   #Time interval, the default unit is seconds   interval 2< /span> }   weight -5   #Weight, when the script execution succeeds or fails, whether the priority of the current node is increased or decreased





vrrp_instance VI_1 {  #Master node  state MASTER  #Bound network card  interface enp0s3  a>  }   auth_pass 1111   auth_type PASS  authentication {  #Security Authentication password  advert_int 2  #Specify the interval for sending VRRP advertisements, in seconds  priority 101  #Priority, the one with higher priority is the main one virtual_router_id 51  #Virtual route id, ensure consistency between master and slave














 #Externally exposed VIP location
 virtual_ipaddress
 {   192.168.31.240  }

 #Specified nginx script
 track_script {   chk_nginx  } }


After configuration, we use systemctl start to start keepalived, and check the startup log of keepalived. The normal startup log will be as follows:

Configure keepalived of the standby node:

The only slight difference from the master node is that the state is switched to BACKUP and the priority is lower than that of the master node.

#Global configuration, routing ID remains unchanged
global_defs 
{   router_id LVS_DEVEL }

vrrp_script chk_nginx 
{   script "/etc/keepalived/check_nginx.sh"   #Time interval, the default unit is seconds   interval 2< /span> }   weight -5   #Weight, when the script execution succeeds or fails, whether the priority of the current node is increased or decreased





vrrp_instance VI_1 {  #Backup node  state BACKUP  #Bound network card  interface enp0s3  a>  }   auth_pass 1111   auth_type PASS  authentication {  #Security Authentication password  advert_int 2  #Specify the interval for sending VRRP advertisements, in seconds  priority 100  #Priority, the one with higher priority is the main one virtual_router_id 51  #Virtual route id, ensure consistency between master and slave














 #Externally exposed VIP location
 virtual_ipaddress 
 {   192.168.31.240  }

 #Specified nginx script
 track_script {   chk_nginx  } }


Start the standby node and view the logs:

3. Effect

Access the virtual IP and you can see that the request is sent to the master node:

Turn off keepalived on the master node, and you can see that the slave node immediately switches to the master node:

When accessing again, the request goes directly to the backup node:

4.Keepalived active/standby switching principle

At this point I believe many students have this doubt:

How does the backup node sense that the master node is down?

Let’s talk about this issue briefly here. During the Keepalived active/standby switchover process, the backup node (Backup) senses the status of the master node (Master) through a protocol called VRRP (Virtual Router Redundancy Protocol).

VRRP is a protocol used to provide redundant routing. Its basic principle is to combine multiple routers into a virtual router. This virtual router has a virtual IP address and a virtual MAC address. In this virtual router, one node is elected as Master, and other nodes become Backup.

The active and standby nodes interact through the VRRP protocol, which mainly includes the following steps:

  1. VRRP router election: In a VRRP group, multiple nodes participate in the election to become the Master node. Through the election algorithm, one of the nodes becomes the Master and is responsible for processing packets of the virtual IP address.

  2. Heartbeat between VRRP routers: VRRP heartbeat messages are sent regularly between Master and Backup to ensure each other’s survival status.

  3. Detect Master node failure: If the Backup node does not receive a heartbeat message from the Master within a certain period of time, it will consider the Master node to be failed.

  4. Backup node takeover: Once the Backup node detects that the Master node has failed, it will quickly take over the virtual IP address and become the new Master.

In Keepalived, the VRRP protocol is used to implement high-availability active-standby switchover. The backup node senses the status of the primary node through the VRRP protocol. When it detects that the primary node is unavailable, the backup node will quickly take over the service to ensure service availability. In this way, even if the master node fails, the entire system can still maintain normal operation.

5. Contact the author

Business cooperation and various exchanges:

Guess you like

Origin blog.csdn.net/Joker_ZJN/article/details/134841903