Keepalived+Nginx dual machine hot standby experiment

Article directory

  • foreword
  • 1. The working process of Keepalived
  • 2. Keepalived + Nginx dual-machine hot backup experiment
    • 1. Basic environment
      • 1.1, turn off the firewall and selinux
    • 2. Install nginx
      • 2.1. Some default configuration file paths of nginx
      • 2.2. Modify the web page of nginx
    • 3. Install Keepalived
      • 3.1. Modify the configuration file
      • 3.2. Modify the configuration files of nginx-1 and nginx-2 respectively
      • 3.3, view ip
      • 3.4, write nginx survival script
    • 4. Client testing
      • 4.1. Access to two nginx servers respectively
      • 4.2. Access to VIP
      • 4.3, fault test
  • Summarize

Foreword

  • Keepalived is a high-availability service that can help you achieve load balancing and failover on cloud servers. This service implements failover and load balancing between active and standby nodes through the VRRP protocol.
  • VRRP is a virtual router redundancy protocol, which can automatically transfer the workload to a standby node when a node in the network fails, thereby ensuring high availability of the network. Keepalive software combined with VRRP protocol can achieve more reliable and stable high availability.
  • The main functions of Keepalived include managing LVS load balancing software, realizing the function of LVS cluster node health check, high availability function as system network service, and high availability failover transfer principle. When the Keppalived service is working normally, the active Master node will continuously send heartbeat messages to the standby node, telling the standby Backup node that it is still alive. When the active Master node fails, the standby node will invoke its own takeover program to take over the IP resources and services of the active Master node. When the primary Master node recovers, the standby Backup node will release the IP resources and services it took over when the primary node failed, and restore to the original standby role.

1. The working process of Keepalived

The function of Keepalived is to detect the status of the server. If there is a web server down or the work fails, Keepalived will detect it and remove the faulty server from the system, and use other servers to replace the work of the server. Keepalived will automatically add the server to the server group after the work is normal. All these tasks are automatically completed without manual intervention. What needs manual work is to repair the faulty server.

2. Keepalived + Nginx dual-machine hot standby experiment

1. Basic environment

Machine Primary IP address
nginx-1 main 192.168.222.143
nginx-2 backup backup td>

192.168.222.167
client/windows / 192.168.222.1

1.1, turn off the firewall and selinux

The configuration of nginx-1 and nginx-2 is as follows:

[root@nginx-1 ~]# systemctl stop firewalld
[root@nginx-1 ~]# systemctl disable firewalld
[root@nginx-1 ~]# setenforce 0
[root@nginx-1 ~]# getenforce
Disabled

2. Install nginx

Configure Ali’s yum source and use yum to install nginx.
nignx-1 and nginx-2 are configured as follows:

nginx-1:
[root@nginx-1 ~]# yum install nginx -y
[root@nginx-1 ~]# nginx -v
nginx version: nginx/1.20.1
[root@nginx-1 ~]# systemctl enable nginx # Set boot up automatically
[root@nginx-1 ~]# systemctl start nginx # Start nginx

nginx-2:
[root@nginx-2 ~]# yum install nginx -y
[root@nginx-2 ~]# nginx -v
nginx version: nginx/1.20.1
[root@nginx-2 ~]# systemctl enable nginx # Set boot up
[root@nginx-2 ~]# systemctl start nginx # Start nginx

2.1, some default configuration file paths of nginx

  • /etc/nginx/nginx.conf # The default configuration file of nginx
  • /etc/nginx/conf.d # Custom configuration file for nginx
  • /usr/share/nginx/html/ # nginx’s default web page storage directory
  • /var/log/nginx/ # nginx log file storage directory

2.2. Modify the nginx web page

Modify the default page file of nginx so that the client can test and view the effect
The configuration of nginx-1 and nginx-2 is as follows:

nginx-1 configuration:
[root@nginx-1 ~]# cd /usr/share/nginx/html/
[root@nginx-1 html]# ls
404.html en-US img nginx-logo.png
50x.html icons index.html poweredby.png
[root@nginx-1 html]# echo "<h1>This is nginx-1</h1>" > index.html
[root@nginx-1 html]# cat index.html
<h1>This is nginx-1</h1>

nginx-2 configuration:
[root@nginx-2 ~]# cd /usr/share/nginx/html/
[root@nginx-2 html]# ls
404.html en-US img nginx-logo.png
50x.html icons index.html poweredby.png
[root@nginx-2 html]# echo "<h1>This is nginx-2</h1>" > index.html
[root@nginx-2 html]# cat index.html
<h1>This is nginx-2</h1>

3. Install Keepalived

nginx-1 and nginx-2 are installed using yum

[root@nginx-1 ~]# yum install keepalived -y
[root@nginx-2 ~]# yum install keepalived -y

3.1, modify the configuration file

The default configuration file is in the /etc/keepalived directory

[root@nginx-1 ~]# cd /etc/keepalived/
[root@nginx-1 keepalived]# ls
keepalived.conf
[root@nginx-1 keepalived]# cp keepalived.conf keepalived.conf.bak # There are a lot of configuration files, you can back them up just in case

3.2. Modify the configuration files of nginx-1 and nginx-2 respectively

nginx-1 (main) configuration:

[root@nginx-1 keepalived]# > keepalived.conf
[root@nginx-1 keepalived]# vim keepalived.conf
!Configuration File for keepalived

global_defs {<!-- -->
   vrrp_mcast_group4 224.100.100.100 # Specify a broadcast address
}


# Add the relevant configuration of periodically detecting nginx service scripts
vrrp_script check_nginx{<!-- -->
    script "/etc/keepalived/check_nginx.sh" # script executed by heartbeat to detect whether nginx is started
    interval 1 # detection script execution interval, the unit is second
}


vrrp_instance VI_1 {<!-- -->
    state MASTER # identifies the state as MASTER
    interface ens33 # Modify the network card name
    virtual_router_id 51 # Define the group vriid, the same virtual_router_id must be the same
    priority 100 # priority, MASTER weight is higher than BACKUP
    advert_int 1 # Interval between synchronization checks between MASTER and BACKIP load balancer, in seconds
    authentication {<!-- -->
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {<!-- -->
        192.168.222.220 # Set virtual IP address
    }
     track_script{<!-- -->
        check_nginx
    }

}



restart service
[root@nginx-1 keepalived]# systemctl restart keepalived

nginx-2 (standby) configuration:
Because the master and backup configurations are the same, only some parameters need to be modified, and the configuration file of nginx-1 can be transferred to nginx-2 through the scp command

[root@nginx-1 keepalived]# scp keepalived.conf 192.168.222.167:`pwd`
keepalived.conf 100% 1044 887.9KB/s 00:00

[root@nginx-2 keepalived]# vim keepalived.conf
!Configuration File for keepalived

global_defs {<!-- -->
   vrrp_mcast_group4 224.100.100.100 # Specify a broadcast address
}


# Add the relevant configuration of periodically detecting nginx service scripts
vrrp_script check_nginx{<!-- -->
    script "/etc/keepalived/check_nginx.sh" # script executed by heartbeat to detect whether nginx is started
    interval 1 # detection script execution interval, the unit is second
}


vrrp_instance VI_1 {<!-- -->
    state BACKUP # identifies the state as MASTER
    interface ens33 # Modify the network card name
    virtual_router_id 51 # Define the group vriid, the same virtual_router_id must be the same
    priority 99 # Priority, MASTER weight is higher than BACKUP
    advert_int 1 # Interval between synchronization checks between MASTER and BACKIP load balancer, in seconds
    authentication {<!-- -->
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {<!-- -->
        192.168.222.220 # Set virtual IP address
    }
     track_script{<!-- -->
        check_nginx
    }

}


restart service
[root@nginx-2 keepalived]# systemctl restart keepalived

3.3, view ip

At this time, use ip a to view the two nginx servers. Only nginx-1 master and backup have virtual ip addresses. Only after nginx-1 goes down will they be transferred to nginx-2
nginx-1:

nginx-2:

3.4, write nginx survival script

  • Because keepalived judges whether the active and standby servers are online through kernel forwarding requests, and nginx is an application program, it has the possibility of process exiting unexpectedly and does not involve the kernel, so nginx cannot make a corresponding judgment to switch the standby server if keepalived is hung up. When you need to use a script to monitor whether the nginx process exists in real time, if it does not exist, it will be restarted, if it cannot be restarted, it will kill the keepalived service in the current host to achieve failover. The script is automatically executed after keepalived is enabled
#Create nginx survival detection script in /etc/keepalived directory
[root@localhost keepalived]# vim check_nginx.sh
#!/bin/bash
#Check if nginx is alive script
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ];then #Start nginx if nginx is not started
      systemctl start nginx #restart nginx
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx fails to restart, then stop the keepalived service and perform VIP transfer
              killall keepalived
      the fi
the fi
 
 
Give the script execute permission
[root@localhost keepalived]# chmod 755 check_nginx.sh

And pass the script to the nginx-2 server
[root@nginx-1 keepalived]# scp check_nginx.sh 192.168.222.167:`pwd`
check_nginx.sh

4. Client testing

4.1, access to two nginx servers respectively

4.2. Access to VIP


The result you see is the nginx-1 server, because the nginx-1 server is the master server

4.3, failure test

  • Simulate a server failure. If the nginx-1 server suddenly goes down, let keepalived realize automatic failover, and let the page accessed by the client go to the backup server
  • At this time, try to access Refresh VIP to view the results again

    The page accessed at this time is provided by the nginx-2 server, and the VIP has also arrived on the ens33 network card of the nginx-2 server

Summary

Tip: This article is for learning and reference only, welcome to pay attention to my blog and my blog website.