Nginx + maintained dual hot standby (dual main mode)

Nginx + maintained dual hot standby (dual main mode)

A previous article has been written about how to use keepalived to realize the high availability of nginx. In this way, a machine has been used for backup, and 50% of the resources are wasted.
Nginx + maintained dual machine hot standby (master-slave mode)
Please check the keepalived installation
Nginx + maintained dual machine hot standby (master-slave mode)

Next, configure the dual host dual machine hot standby of nginx + preserved. In this configuration, there are two virtual IPS. The two machines are primary and secondary to each other. Finally, we can resolve the domain name DNS server to two virtual IPS.

Environment introduction

Two servers

Server1: 192.168.70.132
Server2: 192.168.70.133

Two virtual IP S to set up

Virtual IP1: 192.168.70.130
Virtual IP2: 192.168.70.129

Resolve the DNS domain name to two virtual IP:192.168.30.51 and 192.168.30.52

Network topology

Configure the first server

The first server ip is 192.168.70.132

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id NodeA
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {   #Check nginx process script is not started here
   script "/etc/keepalived/nginx_check.sh"  
   interval 2  
   weight -20  
} 

#Virtual IP1, native as Master
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {  # Execute the script above to detect nginx process
       chk_nginx  
    } 
    virtual_ipaddress {
        192.168.70.130
    }
}


#Virtual IP2, native as Backup
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 52
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {  # Execute the script above to detect nginx process
       chk_nginx  
    } 
    virtual_ipaddress {
        192.168.70.129
    }
}

Configure the second server

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id NodeB #Custom host name
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {   #Check nginx process script is not started here
   script "/etc/keepalived/nginx_check.sh"  
   interval 2  
   weight -20  
} 

#Virtual IP1, local as BACKUP
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {  # Execute the script above to detect nginx process
       chk_nginx  
    } 
    virtual_ipaddress {  #Virtual ip
        192.168.70.130
    }
}


#Virtual IP2, native as Master
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {  # Execute the script above to detect nginx process
       chk_nginx  
    } 
    virtual_ipaddress {  #Virtual ip
        192.168.70.129
    }
}

Monitoring script

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

test

Modify nginx default access page

# nginx default access page
# $NGINX_HOME/html/index.html
vim /usr/share/nginx/html/index.html

Modify the server1 page and add IP: 192.168.70.132

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
<p>IP:192.168.70.132</p>
</body>
</html>

Modify the server2 page and add IP: 192.168.70.133

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
<p>IP:192.168.70.133</p>
</body>
</html>

Both machines start keepalived and nginx

Access virtual ip1
Enter 192.168.30.130 in the browser to access the page

Access virtual ip2
Enter 192.168.30.129 in the browser to access the page

Prove that both VIP s are working

Kill the keepalived of server2

killall keepalived

Visit ip2

It is proved that server1 is the standby machine of server2. After server2 is down, server1 takes over the nginx access of server2

Kill the keepalived of server1 and start the keepalived of server2

Kill server1

killall keepalived

Start server2

service keepalived start

Visit ip1

It is proved that server2 is the standby machine of server1. After server1 is down, server2 takes over the nginx access of server1

Tags: Nginx firewall vim DNS

Posted on Sun, 03 May 2020 09:38:01 -0400 by olly79