Skip to content
Free Delivery on Orders Above Rs 999/- Pan-India
Cash on Delivery Available for Orders above Rs.500/- and Upto Rs 3000/-
SAVE more when you BUY more. Upto 30% Off on BULK PURCHASE
GST Invoices for Your Business
Dedicated Technical Support Team
Safely Delivering Genuine Products PAN INDIA

Load Balancing and High Availability in Linux Using Keepalived

Load Balancing and High Availability in Linux Using Keepalived
D
Written By Daniel D'Souza
📅 Updated on 14 Aug 2026
Summarize with AI
✅ Prompt copied

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

Raspberry Pi Zero 2 W – Ultra-efficient Raspberry Pi Board with upgraded processing power. -RobocrazeRaspberry Pi Zero 2 W – Ultra-efficient Raspberry Pi Board with upgraded processing power. -Robocraze

Raspberry Pi Zero 2 W

Raspberry Pi Zero 2 W The Raspberry Pi Zero 2 W is a compact yet powerful single-board computer (SBC), ideal for DIY electronics, automation, and embedded systems. It is widely regarded as the best Raspberry Pi Zero 2 W for IoT and smart home...
Rs 2,210/-
Rs 2,210/-
Rs 2,799/-
Save Rs 589/-
Raspberry Pi 4 Model B 2GB RAM – Well-balanced Raspberry Pi Board with 2GB RAM for diverse applications. -RobocrazeRaspberry Pi 4 Model B 2GB RAM – Well-balanced Raspberry Pi Board with 2GB RAM for diverse applications. -Robocraze

Raspberry Pi 4 Model B 2GB RAM Single Board Computer

Raspberry Pi 4 Model B 2GB RAM The Raspberry Pi 4 Model B 2GB RAM is a powerful single-board computer designed for programming, IoT, robotics, home automation, media centers, and embedded system development. Powered by the Broadcom BCM2711 quad-core Cortex-A72 processor running at 1.5GHz,...
Rs 7,091/-
Rs 7,091/-
Rs 7,999/-
Save Rs 908/-
Raspberry Pi 3B – Versatile Raspberry Pi Board for a range of DIY & development projects. -RobocrazeRaspberry Pi 3B – Versatile Raspberry Pi Board for a range of DIY & development projects. -Robocraze

Raspberry Pi 3B

Raspberry Pi 3B The power source has been upgraded to 2.5A instead of 2A, allowing the Pi to power even more powerful devices over USB ports. The Pi 3 has four built-in USB ports for connectivity to a mouse, keyboard, or anything with a...
Rs 4,790/-
Rs 4,790/-
Rs 5,999/-
Save Rs 1,209/-
Raspberry Pi 3B+ – Reliable Raspberry Pi Board with enhanced processing power & connectivity. -RobocrazeRaspberry Pi 3B+ – Reliable Raspberry Pi Board with enhanced processing power & connectivity. -Robocraze

    Raspberry Pi 3B+

    Raspberry Pi 3B+ The Raspberry Pi 3 Model B+ is the latest product in the Raspberry Pi 3 range and the best low power single-board computer for beginners and makers, making it an ideal single board computer for Raspberry Pi projects, boasting a 64-bit...
    Rs 4,999/-
    Rs 4,999/-
    Rs 6,999/-
    Save Rs 2,000/-

    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.

     

    Excerpt

    Learn how to implement load balancing and high availability in Linux systems using Keepalived to ensure reliability, failover, and uptime.
    Prev Post
    Next Post

    Leave a comment

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

    Thanks for subscribing!

    This email has been registered!

    Shop the look

    Choose Options

    Edit Option
    Back In Stock Notification
    Compare
    Product SKU Description Collection Availability Product Type Other Details

    Choose Options

    this is just a warning
    Login
    Shopping Cart
    0 items
    FREE SHIPPING!
    ₹100 OFF
    ₹200 OFF
    ₹999
    ₹2500
    ₹4900
    WhatsApp Chat Chat