keepalived highly available nginx

Posted by sunnyk on Sat, 08 Jan 2022 04:06:29 +0100

Previously, we used the dr model of the lvs with kept high availability, which is realized by adding ipvs rules. Let's implement high availability for nginx and dual master high availability.

Delete the previously configured vip and execute the following script.

#!/bin/bash
#
vip="192.168.2.246"
vip2="192.168.2.244"
eth="lo"
host="-host"
case $1 in
start)
	echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
	echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
	echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
	echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        ifconfig $eth:1 $vip/32 broadcast $vip up
        route add $host $vip dev $eth:1
        ifconfig $eth:2 $vip2/32 broadcast $vip2 up
        route add $host $vip2 dev $eth:2
        ;;
stop)

	echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
	echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
	echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
	echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        ifconfig $eth:1 down
        ifconfig $eth:2 down
        ;;
esac

High availability nginx

Installing Nginx on node1 and node2

# yum install nginx -y

Next, configure nginx so that nginx can reverse proxy to node1 and node4

upstream webservers {
  server 192.168.2.11:80 weight=1;
  server 192.168.2.14:80 weight=1;
}

location   /    {
    proxy_pass  http://webservers;  
}

node2 has the same configuration

To enable keepalived to detect whether nginx is online, we can manually write a script:

vrrp_script chk_nginx {
    script "killall -0 nginx"
    interval 1
    weight -10
}
stay vrrp_instance Test in:
vrrp_instance VI_1 {
  track_script {
    chk_nginx
  }
}

The contents of Virtual server can be deleted because the rules of ipvs are not used
Note: both node1 and node2 should be implemented

At this time, the browser accesses: http://192.1682.110 ; It can realize the effect of load balancing; If we stop the nginx service on node1, browser access can still achieve the effect of load balancing. If we let nginx on the node restart the service, the virtual address will flow to the node again.

If we want the virtual address on which node, which node will run nginx; Nodes without virtual addresses stop nginx.

1. If a fault occurs, that is, nginx does not start, the priority will be reduced by 20:

Initial priority:
MASTER:100
BACKUP:99
MASTER start-up nginx When, the standby node does not start nginx
MASTER:100
BACKUP: 99 --> 79 
When MASTER Node failure:
MASTER: 100 -->80
BACKUP: 79  //Because the priority of the standby node is lower than that of the MASTER, the virtual address cannot be robbed

If different values are subtracted from the priority in case of failure, so that one is more and the other is less, there will always be a priority problem and the virtual address can't be robbed.

At this time, we can let the primary and standby nodes run nginx all the time. As long as the primary node is online, we can let it serve. When the high priority node is gone, we can let the low priority node serve. When the high priority node comes back, we can flow the address and let the high priority node serve. So we can do this:

In the previous notify SH add:

#!/bin/bash
# Author: 
# Description: An example of notify script
#

vip="192.168.2.18"
contact="root@localhost"

notify() {
    mailsubject="$(hostname) to be $1: $vip floating"
   mailbody="$(date +'%F %H:%M:%S'): vrrp transition,$(hostname) changed to be $1"
  echo $mailbody | mail -s "$mailsubject" $contact
}

case $1 in
master)
        notify master
      service nginx restart  //Become the master node and let nginx restart
         exit 0
        ;;
backup)
        notify backup
     service nginx restart  //As the standby node, we also restart nginx
        exit 0
        ;;
fault)
        notify fault
        exit 0
        ;;
*)
        echo "Usage:$(basename $0) {master|backup|fault}"
 exit 1
        ;;
esac

In this way, we can ensure that nginx is always online. This is a single master mode. We want to implement the dual master mode so that nginx can receive requests at the same time. How to implement it?

Next, we implement the application of dual master mode.

Just add one instance in the keepalived configuration file.

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1122
    }
    virtual_ipaddress {
        192.168.2.28/32 dev eth0 label eth0:2
    }
    track_script {
        chk_nginx
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

  

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1122
    }
    virtual_ipaddress {
        192.168.2.28/32 dev eth0 label eth0:2
    }
    track_script {
        chk_nginx
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

And in notify The command to restart nginx is removed from the SH script. In our practical application, we should not use the script to control the startup of nginx, but let other monitoring software realize this function, such as zabbix.

In this way, we can realize the dual active mode. Both active and standby nodes are equipped with VIPs, and the browser can get a response when accessing these two VIPs. One active and two standby VIPs can still work. Manually start the offline nginx, and the vip will flow to the started node.