Keepalived combined with nginx status detection script to achieve high availability for web server cluster

lab environment

Two CentOS-7.5 virtual machines
web1: 10.0.11.203
web2: 10.0.11.204
VIP: 10.0.11.210
web type: nginx
client: self-use notebook (win10)
nginx status detection script: ck_nginx.sh

Experiment one, use keepalived to simply realize the high availability function of the web cluster

1. Prepare two web servers

1) Web1 network card situation
[root @ CentOS ~] #
[root @ CentOS ~] # ip a
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

2) Web2 network card situation
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # ip a
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

3) Install nginx

web1 end:
[root @ CentOS ~] #
[root @ CentOS ~] # yum install nginx
[root @ CentOS ~] # rpm -qa nginx
nginx-1.16.1-1.el7.x86_64
[root @ CentOS ~] #
[ root @ CentOS ~] #
[root @ CentOS ~] #> /usr/share/nginx/html/index.html #Empty the default webpage
[root @ CentOS ~] # echo web1> / usr / share / nginx / html / index .html #Let the webpage display web1
[root @ CentOS ~] # cat /usr/share/nginx/html/index.html
web1
[root @ CentOS ~] #
[root @ CentOS ~] # systemctl start nginx #Start nginx
[root @CentOS ~] # systemctl enable nginx #Set nginx to start automatically after booting
C: \ Users \ Administrator> curl 10.0.11.203
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

web2端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# yum install nginx
[root@CentOS-2 ~]# rpm -qa nginx
nginx-1.16.1-1.el7.x86_64
[root@CentOS-2 ~]#
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# > /usr/share/nginx/html/index.html
[root@CentOS-2 ~]# echo web2 > /usr/share/nginx/html/index.html
[root@CentOS-2 ~]# cat /usr/share/nginx/html/index.html
web2
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl start nginx
[root@CentOS-2 ~]# systemctl enable nginx
C:\Users\Administrator>curl 10.0.11.204
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

4)
Instructions for installing keepalived : both web servers need to install keepalived service

web1端:
[root@CentOS ~]#
[root@CentOS ~]# yum install keepalived
[root@CentOS ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[root@CentOS ~]#

web2端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# yum install keepalived
[root@CentOS-2 ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[root@CentOS-2 ~]#

5) Configure two keepalived and start

web1 end:
[root @ CentOS ~] #
[root @ CentOS ~] #> /etc/keepalived/keepalived.conf #Empty the original configuration
[root @ CentOS ~] # vim /etc/keepalived/keepalived.conf #Edit configuration File, custom keepalived configuration file
[root @ CentOS ~] # cat /etc/keepalived/keepalived.conf
Keepalived combined with nginx status detection script to achieve high availability for web server cluster
[root @ CentOS ~] #
[root @ CentOS ~] # systemctl start keepalived #start
[root @ CentOS ~] # systemctl enable keepalived #Set boot start

web2 end:
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] #> /etc/keepalived/keepalived.conf
[root @ CentOS-2 ~] # vim /etc/keepalived/keepalived.conf
[root @ CentOS-2 ~] # cat /etc/keepalived/keepalived.conf
Keepalived combined with nginx status detection script to achieve high availability for web server cluster
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # systemctl start keepalived #Start
[root @ CentOS-2 ~] # systemctl enable keepalived #Set boot

3. Test access
method: use the client to access the web through the VIP test

C: \ Users \ Administrator> curl 10.0.11.210 #Personal notebook accesses the web through VIP test
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

Unplug the network cable of the web1 server and use the client to access the web through the VIP test:
C: \ Users \ Administrator> curl 10.0.11.210
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

Connect the network cable of the web1 server again, and use the client to access the web through the VIP test:
C: \ Users \ Administrator> curl 10.0.11.210
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

4. The conclusion of the experiment:
1) The high availability of the web server cluster was successfully achieved using keepalived
2) Although the high availability of the web can be achieved in the case of abnormal high failure of the server, unfortunately, it is impossible to achieve the exception for the web service at this time The high-availability switchover caused by the fault; keepalived cannot perceive the running status and health status of the nginx service.


Experiment two, use keepalived combined with nginx monitoring and detection script to achieve a more flexible and highly available cluster function

Experimental environment: Based on the environment of
experiment one, the only difference between experiment one and experiment one is that the nginx health status detection script needs to be prepared

1. Write nginx health detection script

Execute on the web1 end:
[root @ CentOS ~] #
[root @ CentOS ~] # vim /etc/keepalived/ck_nginx.sh #Write a keepalived script to detect the health status of nginx

#! / bin / bash
#Check if ngin process exists
counter = $ (ps -C nginx --no-heading | wc -l)
if ["$ {counter}" = "0"]; then
#Try to start ngnx once , After 5 seconds stop, check again
systemctl start nginx
sleep 5
counter = $ (ps -C nginx --no-heading | wc -l)
if ["$ {counter}" = "0"]; then #if the
startup is not successful , Just kill keepave to trigger the master-slave switch
systemctl stop nginx
fi
fi
[root @ CentOS ~] #
[root @ CentOS ~] # cat /etc/keepalived/ck_nginx.sh #View script content
Keepalived combined with nginx status detection script to achieve high availability for web server cluster
[root @ CentOS ~] #
[root @CentOS ~] # chmod + x /etc/keepalived/ck_nginx.sh #Grant script executable permissions
[root @ CentOS ~] #

Web2 end execution:
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # vim /etc/keepalived/ck_nginx.sh

#! / bin / bash
#Check if ngin process exists
counter = $ (ps -C nginx --no-heading | wc -l)
if ["$ {counter}" = "0"]; then
#Try to start ngnx once , After 5 seconds stop, check again
systemctl start nginx
sleep 5
counter = $ (ps -C nginx --no-heading | wc -l)
if ["$ {counter}" = "0"]; then #if the
startup is not successful , Kill keepave to trigger the master-standby switch
systemctl stop nginx
fi
fi

[root@CentOS-2 ~]#
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# cat /etc/keepalived/ck_nginx.sh
Keepalived combined with nginx status detection script to achieve high availability for web server cluster
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# chmod +x /etc/keepalived/ck_nginx.sh
[root@CentOS-2 ~]#

2. Edit and modify the nginx configuration file
web1 end:
[root @ CentOS ~] #
[root @ CentOS ~] # vim /etc/keepalived/keepalived.conf #Custom configuration file

! Configuration File for keepalived

global_defs {
notification_email {br/>[email protected]
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id 1
}

vrrp_script chk_nginx {
script "/etc/keepalived/ck_nginx.sh"
interval 2
weight -5
fail 3
}

vrrp_instance VI_1 {
state MASTER
interface ens32
mcast_src_ip 10.0.11.203
virtual_router_id 55
priority 100
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
    10.0.11.210/24
}

track_script {
     chk_nginx
}

}

[root @ CentOS ~] #
[root @ CentOS ~] # systemctl restart keepalived #Restart keepalived to make the configuration take effect
[root @ CentOS ~] #

web2 end:
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # vim /etc/keepalived/keepalived.conf #Custom configuration file

! Configuration File for keepalived

global_defs {
notification_email {
br/>[email protected]
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id 1
}

vrrp_script chk_nginx {
script "/etc/keepalived/ck_nginx.sh"
interval 2
weight -5
fail 3
}

vrrp_instance VI_1 {
state MASTER
interface ens32
mcast_src_ip 10.0.11.204
virtual_router_id 55
priority 95
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
    10.0.11.210/24
}

track_script {
     chk_nginx
}

}

[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # systemctl restart keepalived #Restart keepalived to make the configuration take effect

Test the keepalived function:
due to the content of the script, the nginx service will start immediately after stopping, so we simulate the abnormal shutdown of the server and cause the nginx service to have no way to start automatically
[root @ CentOS ~] #
[root @ CentOS ~] # init 0 # Shut down the web1 server

[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # ping 10.0.11.203 #Use web2 to pingweb1 and find that web1 has been shut down
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

At this time, use the client to test and verify the keepalived function:
C: \ Users \ Administrator> curl 10.0.11.210 #Use the client to access the web server through VIP
Keepalived combined with nginx status detection script to achieve high availability for web server cluster

3. Conclusion of experiment 2
Using keepalived service combined with nginx health status detection script successfully achieved high availability for web server cluster

Guess you like

Origin blog.51cto.com/14783377/2486411