Keepalived-Nginx-AutoFailover

High Availability Web Server using Keepalived with Nginx AutoFailover

 
 


Overview


In this article we are going to cover -

  • Launching a high availability website which is running on nginx using Keepalived.


Prerequisites


  • Two CentOS/any Linux OS installed machine, one for LB1 and other for LB2.
  • Proper networking access between the machines and outside world.
  • I assume that you have the basic working knowledge of Nginx.

In this article we are using the two machines:

  • LB001 machine which is running on CentOS Linux release 7.2.1511 having ip (192.168.20.22) and VIP (192.168.20.100).
  • LB002 machine which is running on CentOS Linux release 7.2.1511 having ip (192.168.20.23) and VIP (192.168.20.100).

Note: Don't assign VIP initially. It will be automatically assign/take care by keepalived which we will see in coming steps.


Brief Overview of Keepalived


Keepalived is a Linux implementation of the VRRP (Virtual Router Redundancy Protocol) protocol to make IPs highly available - a so called VIP (Virtual IP).


Generally the VRRP protocol take care that one of nodes is master in pool of the servers where the keepalived is running. The backup node(s) always listens for multicast packets from a node which have a higher priority. If the backup node fails to receive VRRP advertisements for a period longer than three times of the advertisement timer, the backup node takes the master state and assigns the configured IP(s) to itself. In case there are more than one backup nodes with the same priority, the one with the highest IP wins the election.


Objective


Our main aim is to run the high availability website with the auto recovery of Nginx in case of failover.

  • Install Nginx web servers
  • Install Keepalived
  • Check the IP failover
  • Auto recovery if Nginx service is down


Initial IP Configuration on both LB's




Nginx and Keepalived Installation & Configuration on both LB


For installation & configuration of Nginx and Keepalived,  please check my previous blog: Keepalived


Keepalived Configuration on LB001


[root@lb001 somesh]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_back

 

[root@lb001 somesh]# vi /etc/keepalived/keepalived.conf
vrrp_script chk_health {

script "/etc/keepalived/curl.sh"     #This script is used to check the status of the webpage
interval 2
fall 3
rise 2

}

 

vrrp_instance VRRP1 {

state MASTER
interface enp0s3
virtual_router_id 51
priority 105
advert_int 1
nopreempt

 

unicast_srcip 192.168.20.22
unicast_peer {

192.168.20.23

}

 

authentication {

auth_type PASS
auth_pass somesh123

}

 

virtual_ipaddress {

192.168.20.100/24 dev enp0s3

}

 

track_script {

chk_health

}

 

# This script is used to check the keepalived server state
notify /etc/keepalived/notify.sh
# This script is used to take the action based on the keepalived server state
notify /etc/keepalived/notify-nginx.sh

}



Keepalived Configuration on LB002


[root@lb002 somesh]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_back

 

[root@lb002 somesh]# vi /etc/keepalived/keepalived.conf
vrrp_script chk_health {

script "/etc/keepalived/curl.sh"   #This script is used to check the status of the webpage
interval 2
fall 3
rise 2

}

vrrp_instance VRRP1 {

state MASTER
interface enp0s3
virtual_router_id 51
priority 105
advert_int 1
nopreempt

unicast_srcip 192.168.20.23
unicast_peer {

192.168.20.22

}

authentication {

auth_type PASS
auth_pass somesh123

}

virtual_ipaddress {

192.168.20.100/24 dev enp0s3

}

track_script {

chk_health

}

# This script is used to check the keepalived server state
notify /etc/keepalived/notify.sh
# This script is used to take the action based on the keepalived server state
notify /etc/keepalived/notify-nginx.sh


}

 

Script to check Health




Script to check Keepalived State




Script to perform action based upon Keepalived State 




Script to read the Keepalived State




Nginx Failover and Autorecovery with KeepAlived


Nginx is running on both the machines as shown below.

 

 

Now we need to start the keepalived on both the machines.

[root@lb001 keepalived]# systemctl start keepalived
 
[root@lb001 keepalived]# systemctl status keepalived
 
[root@lb002 keepalived]# systemctl start keepalived
 
[root@lb002 keepalived]# systemctl status keepalived

As we see below, the VIP is assigned to LB002 machine, means LB002 is current Master.

 

As we mentioned earlier in keepalived configuration that the "notify-nginx.sh" script will perform action based upon keepalived state. So, now we need to check again the status of nginx.

 


In the above snapshot we can see that our "notify-nginx.sh" script is working fine because it's stopped the Nginx on current Backup LB001 machine.


I already mentioned in my previous blog Keepalived that the keepalived take care of our IP failover and provide the high availability. Here we see that the keepalived can also perform certain task based on server state. So the conclusion here is, keepalived can be used for high availability as well as to perform any task based on server state using the notify scripts. It’s all done. Now it’s your time to play around with the keepalived .. 🙂 🙂



Code


Code_@_Github


Leave a Reply

Your email address will not be published. Required fields are marked *