What is Keepalived?
In a high-availability (HA) cluster, if your primary load balancer or web server crashes, users will experience an outage until you manually update DNS or re-route traffic. Keepalived solves this by using the Virtual Router Redundancy Protocol (VRRP). It allows two Linux servers to share a single “Virtual IP” (VIP) address. The primary server holds the VIP, and if it goes offline, Keepalived instantly detects the failure and floats the VIP over to the secondary backup server, ensuring zero downtime for your users.
Step 1: Install Keepalived on Both Servers
You need two Ubuntu servers on the same local subnet (e.g., Node-1 at 192.168.1.10 and Node-2 at 192.168.1.11). Open a terminal on both servers and install the package:
sudo apt updatesudo apt install keepalived -y
Step 2: Configure the Primary Node (Master)
Keepalived does not create a default configuration file upon installation. You must create one on your primary server (Node-1):
sudo nano /etc/keepalived/keepalived.conf
Add the following basic VRRP configuration. We will assign 192.168.1.100 as the highly-available Virtual IP address:
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass SecretPassword123
}
virtual_ipaddress {
192.168.1.100
}
}
state MASTER: Tells this node it is the primary.priority 100: The higher number wins the master election.virtual_router_id 51: Must be identical on both nodes.
Step 3: Configure the Secondary Node (Backup)
Log in to your secondary server (Node-2). Create the exact same file:
sudo nano /etc/keepalived/keepalived.conf
Paste the configuration, but you must change two critical lines: set the state to BACKUP, and lower the priority to 90.
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass SecretPassword123
}
virtual_ipaddress {
192.168.1.100
}
}
Step 4: Start the Services
On both servers, start the Keepalived service and enable it to start on boot:
sudo systemctl enable --now keepalived
Because Node-1 has a higher priority (100), it will instantly claim the Virtual IP address. You can verify this by running ip a on Node-1; you should see the 192.168.1.100 address dynamically attached to the eth0 interface.
Step 5: Test the Failover
From a third computer on the network, start a continuous ping to the Virtual IP:
ping 192.168.1.100
Now, simulate a catastrophic hardware failure by powering off Node-1 or stopping its service (sudo systemctl stop keepalived). Within one second, Node-2 will realize the master is dead and take over the IP address. You will see the ping drop perhaps one single packet before continuing smoothly. When Node-1 comes back online, it will automatically preempt Node-2 and take the IP address back.