A Journey Into KeepAlived With NGINX on Ubuntu

Morning!

Today I wanted to share a recent journey I had with getting NGINX setup in a redundant way. I affectionately call this ’A Journey Into KeepAlived With NGINX on Ubuntu’ as it’s certainly not about the destination. I learnt a boat load on this topic!

So lets say I have a server that I want people to get to, it can be any server. But I want a proxy so that people don’t directly to that server. You might think this is just an unnecessary middle man but it does add a ton of benefits.

The main benefit we are interested in is redundancy. So in the event of one system behind unavailable, the traffic is automatically routed to another system. This also aids scalability…

So this is the setup:

A Journey Into KeepAlived With NGINX on Ubuntu

In this example, we only need to represent the NGINX servers with an IP address since we are only interested in the KeepAlived config.

First things first, install NGINX on your system. This is different for every OS so I recommend looking at the official docs or finding a tutorial. It’s super easy though, only gets more complex when you get picky about your version…

Now we can hop into KeepAlived. Install it on both servers using:

sudo apt install keepalived

Now we can create a new keepalived.conf file for each system:

sudo nano /etc/keepalived/keepalived.conf

The config required on each system is fairly similar. So you can just copy the templated one below onto both systems and edit the values as required:

global_defs {
# Keepalived process identifier
 router_id nginx
}

# Script to check whether Nginx is running or not
vrrp_script check_nginx {
script "/bin/check_nginx.sh"
interval 2
weight 50
}

# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_01 {
state MASTER
interface enp0s3 #CHANGE THIS TO BE YOUR NETWORK INTERFACE
virtual_router_id 151
priority 110
virtual_ipaddress {
#the IP you want to share between both NGINX servers
    192.168.0.3/24
}
track_script {
check_nginx
}
authentication {
auth_type AH
auth_pass secret
}
}

Okay, now come the specifics.

On the MASTER NGINX instance, edit the above config to:

  1. Update the virtual_address to the value you want, this is the address that will be shared by both NGINX instances to facilitate the redundancy.
  2. Update the interface in vrrp_instance VI_01 to be your network adapter. FYI to get your network adapter, just type sudo ip a into the console.

On the SLAVE NGINX instance, edit the above config to:

  1. Same as the master, update the virtual_address to the value you want, this is the address that will be shared by both NGINX instances to facilitate the redundancy.
  2. Again, same as the master, update the interface in vrrp_instance VI_01 to be your network adapter. FYI to get your network adapter, just type sudo ip a into the console.
  3. Update the state in vrrp_instance VI_01 to be ‘BACKUP’ instead of ‘MASTER’
  4. Lower the priority to 100 instead of 110

Now that KeepAlived is setup, we need to create a script so that KeepAlived is updated when NGINX isn’t running. On both systems run:

sudo nano /bin/check_nginx.sh

And add the following content:

#!/bin/sh
if [ -z "`pidof nginx`" ]; then
  exit 1
fi

Now we need to update the permissions so that the system sees it as a file that can be ran:

sudo chmod 0755 /bin/check_nginx.sh

Finally, we will restart NGINX and KeepAlived, and also tell both services to start automatically at system start:

sudo systemctl restart nginx
sudo systemctl restart keepalived
sudo systemctl enable nginx
sudo systemctl enable keepalived

All done!

Obviously, this guide doesn’t go into the config of NGINX which is a whole different area, but at least you can see how KeepAlived would work to keep your services online in the event of an outage.

Before I go, you can check that KeepAlived is using the correct virtual address using:

sudo ip add show

And you can test KeepAlived is running by stopping NGINX on either host and the services behind the proxy will remain accessible.

Enjoy! 🎉

1 thought on “A Journey Into KeepAlived With NGINX on Ubuntu”

  1. prix du alonest sur ordonnance

    It’s actually a nice and useful piece of information. I am satisfied that you just
    shared this helpful information with us. Please
    stay us up to date like this. Thank you for sharing.

Leave a Comment

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

email popup image
Mark Harwood
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Subscribe
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Fill Out This Form, And I Will Be In Touch Shortly
Contact form image
I'll Be In Touch Soon