LOAD BALANCING TO LINUX SYSTEM USING KEEPALIVED

LOAD BALANCING TO LINUX SYSTEM USING KEEPALIVED - Robocraze

Raspberry Pi’s make a good server for deploying backend API’s, hosting websites etc. But what happens when for any of the numerous reasons it fails. Being left without a backend is a highly unfavorable scenario.

 

 

To mitigate this a high availability cluster server is employed. Basically what this means is that we put multiple Raspberry Pi’s hosting the same backend and make them share a single virtual IP address so that the end user can open a single address without the hassle of going through multiple IP’s and be served with the webpage or the API every time. If one of the Raspberry Pi’s goes down the virtual IP will be grabbed by the next Raspberry Pi and operations will continue unhindered.

 

 

But how to make the Raspberry Pi’s share a virtual IP you ask? Keepalived. Keepalived is a routing software written in C. The main goal of this project is to provide simple and robust facilities for load balancing and high-availability to Linux system and Linux based infrastructures.

 

Let’s get right into installing and configuring keepalived on the Raspberry Pi. Please note this method works only on the Raspberry Pi 3b+.

 

For testing keepalived, we shall be using nginx which basically helps in hosting a website on the pi to test the virtual IP. We shall be using 2 R-Pi’s for this but you can use more than that too.

 

Before starting with the installation its best practice to run the following two commands

 

sudo apt-get update

sudo apt-get upgrade

 

Step 1: Installing nginx

This step is fairly simple, just type the following command into the terminal on both the raspberry pi’s

 

sudo apt-get install nginx

 

And that should handle the entire installation process (one thing which makes linux my favourite OS).

 

Step 2: Configuring nginx

In order to identify the Raspberry Pi to which the virtual IP is assigned, let’s modify the default web page such that we can identify the Raspberry Pi from which this page is being served from. On the first pi that you want to set as master, modify the default page to say “Hi you have reached Pi 1” and so on for the rest of the Raspberry Pi’s. To modify the default page enter the following command

 

cd /var/www/html

 

There should be a file named index.html, open it using your favourite text editor, for me that would be nano. Put the following into it. Be sure to open it with root privileges otherwise you will not be able to save the file.

 

sudo nano index.html

 

<html>

<head>

<title> RPi 1 </title>

</head>

<body>

<h1> Hi you have reached Pi 1 </h1>

</body>

</html>

And then save it, for nano that would be ctrl + o and then press enter to save it and ctrl + x to close the nano editor. Make similar changes to the rest of the R-Pi’s substituting 1 with 2, 3 and so on. Once nginx is configured you can open a web browser and type in the IP of the Raspberry Pi to check if nginx is working properly.

 

Once nginx is up and running we can head over to keepalived.

 

Step 3: Installing keepalived

For installing keepalived type into the terminal the following command on all the R-Pi’s

 

sudo apt-get install keepalived

 

And that takes care of the keepalived installation (isn’t linux just awesome).

 

Step 4: Configuring keepalived

Type the following command to open the keepalived configuration file

 

sudo nano /etc/keepalived/keepalived.conf

 

This file might not exist the first time you open it. Also feel free to replace nano with your favourite editor such as gedit or vim, but make sure you open it with root privileges otherwise linux won’t let you save it.

 

Enter the following into the keepalived.conf file on the R-Pi with the maximum priority

 

vrrp_instance VI_1 {

   state MASTER

   interface eth0

   priority 100

   virtual_router_id 250

   advert_int 1

   use_vmac

   virtual_ipaddress {

       172.29.189.30

   }

}

On the other R-Pi add the following configuration

vrrp_instance VI_1 {

   state BACKUP

   interface eth0

   priority 150

   virtual_router_id 250

   advert_int 1

   use_vmac

   virtual_ipaddress {

       172.29.189.30

   }

}

 

The virtual router-id and virtual IP address need to be the same on all the nodes. Now restart the service on all the R-Pi’s using the following command

 

sudo service keepalived restart

 

To see if the Raspberry Pi captured the IP type

 

ifconfig

 

It should show something similar to what is shown below

 

 

Here you can see that there is another interface called vrrp.250. This is generated due to the line use_vmac and 250 is the virtual router-id. Advert int is basically how many seconds between each health check broadcast.

 

To test if the system is working, type the following command on the Raspberry Pi with the highest priority

 

sudo service keepalived stop

 

And then IP should pass onto the next Raspberry Pi’s and so on.

 

Components and Supplies

You may also like to read

Frequently Asked Questions

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.

Components and Supplies

You may also like to read