Skip to content
Due to large volume of orders, all the orders placed/confirmed after 1pm on 19th Oct will only be shipped on 21st Oct. We apologize for the inconvenience and appreciate your patience

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,699/-
    Rs 4,699/-
    Rs 6,999/-
    Save Rs 2,300/-

    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