Docker + kept + nginx build high availability cluster

Time:2021-4-29

Using docker + kept + nginx to build high availability


1. In the production environment, nginx can be used as a load balancing server. In the case of a single node, nginx or this server is likely to hang up. In order to ensure high availability, the first thought is to set up another nginx server. But the IP addresses of two nginx are different on different servers, so how to ensure that when one server fails, the other server will be used automatically? The solution is to use kept.

Keepalived is a service software based on VRRP protocol (virtual routing Redundancy Protocol). VRRP protocol is to form a router group with n routers providing the same function. In this group, there is a master and multiple backups. There is a VIP (virtual IP) on the master to provide services to the outside. The master will send multicast. When the backup fails to receive the VRRP packet, it is considered that the master is down, At this time, you need to choose a backup as the master according to the priority of VRRP. In this way, the business can be switched to other devices in time.

Docker + kept + nginx build high availability cluster

2. I use docker environment to simulate multi server environment.

docker run -d --name web1 --privileged=true centos /usr/sbin/init
docker exec -it web1 /bin/bash

Tip: because I can’t bind VIP when running docker, I always add these two parameters

  • –Privileged = true: you can really have root permission in docker
  • /Usr / SBIN / init: you can use the systemctl command in docker

According to the upper operation, start three containers: web1, web2 and WebTest. Two are used to build high availability, and one is used for client connection test.

Docker + kept + nginx build high availability cluster

3. Enter the web1 and web2 containers respectively and execute the following steps:

yum install -y nginx keepalived vim net-tools  initscripts
  • VIM: the CentOS container started by docker does not have VIM, so you need to install it yourself
  • Net tools: you can use the ifconfig command to view the network card
  • Initscripts: you can use the service command

4. Modify the default index.html file of nginx to distinguish which nginx is being accessed. You can mark one as master and one as backup.

cd /usr/share/nginx/html
vim index.html

Docker + kept + nginx build high availability cluster

Docker + kept + nginx build high availability cluster

5. Create a check in the / etc / kept directory of the two containers_ Nginx.sh file. And add the execution permission: Chmod + X check_ Nginx.sh (not required here, because it is posted under the root permission for the sake of safety). check_ The nginx.sh script file is used to check whether niginx is started.

cd /etc/keepalived
touch check_nginx.sh
chmod +x check_nginx.sh
vim check_nginx.sh

check_ Nginx.sh script file content:

#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ];then
    /usr/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

6. Configure the keepalived.conf file in the two containers. The file is in the / etc / keepalived directory. The configuration is as follows:

Configuration in master:

global_defs {
   router_ id LVS_ Level # node name, generally use the default
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_ Nginx. Sh "# check the path of nginx script
    Interval 2 # the interval between running the script
    Weight - 20 ᦇ weight of change
}

vrrp_instance VI_1 {
    State master # this node is a master. If this node goes down, it will immediately preempt the master node when it is restarted
    #mcast_ src_ IP 172.17.0.2 native IP. If it is not declared, the native IP will be bound by default
    Interface eth0 # binding network card, you can view the network card information through ifconfig
    virtual_ router_ ID 70 # virtual route ID, which must be the same in the same group
    Priority 100 # weight
    Nopreempt # is defined as non preemptive, because multiple active standby handoffs are not good for servers with large number of requests
    advert_ Int 1 # interval between sending heartbeat and other nodes in the group: S
    virtual_ipaddress {
        172.17.0.200
    }# bound virtual IP group
    track_script {
        chk_nginx
    }Execute the previously declared script
}

Configuration in backup:

global_defs {
   router_ id LVS_ Level # node name, generally use the default
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_ Nginx. Sh "# check the path of nginx script
    Interval 2 # the interval between running the script
    Weight - 20 ᦇ weight of change
}

vrrp_instance VI_1 {
    State backup # this node is backup
    # mcast_ src_ IP 172.17.0.3 native IP. If it is not declared, the native IP will be bound by default
    Interface eth0 # binding network card, you can view the network card information through ifconfig
    virtual_ router_ ID 70 # virtual route ID, which must be the same in the same group
    The weight of the standby node is smaller than that of the primary node
    advert_ Int 1 # the interval between sending heartbeat and other nodes in the group: s. The interval within the group must be the same
    virtual_ipaddress {
        172.17.0.200
    }# bound virtual IP group
    track_script {
        chk_nginx
    }Execute the previously declared script
}

Tip: the interface items need to be checked by ifconfig, and mine are eth0( Note part deleted by itself (ha ha ha ha)

7. Start the test

Start nginx in two containers respectively

nginx

Start the two containers respectively

systemctl start keepalived.service

Check whether VIP is bound in master. You can find that 172.17.0.200 has been bound

ip a

Docker + kept + nginx build high availability cluster

At this time, the third docker, WebTest, is used to test

curl 172.17.0.200

Docker + kept + nginx build high availability cluster

It can be found that the current visit is master.

At this point, close kept in web1, that is, close the master node, and then in WebTest, curl:

systemctl stop keepalived.service
curl 172.17.0.200

Docker + kept + nginx build high availability cluster

Ok! be accomplished!