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).
ย
Components and Supplies
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.
ย



