Skip to content
masdzub.com
~/articles/linux — one from the notebook

Configure Virtual IP Failover with Keepalived and VRRP

Set up High Availability with Keepalived VRRP. Master-Backup failover logic, health checks, kernel tuning, and troubleshooting for Linux.

masdzub@website:~$ cat ~/articles/configure-virtual-ip-failover-with-keepalived-and-vrrp.aspx.md
# 1 file · 9 min read · topic: linux · status: published

Introduction

High availability on Linux servers requires a mechanism to transfer network resources without manual intervention. Keepalived implements the Virtual Router Redundancy Protocol (VRRP) to manage a Virtual IP (VIP) address. The Master node holds the VIP and advertises its presence to the Backup nodes. If the Master fails or the health check detects a critical service down, the Backup node with the highest priority assumes the VIP. This guide details the configuration of this topology, including VRRP script integration and kernel tuning required for reliable failover.

Architectural Overview

VRRP operates by creating a virtual router group. One node acts as the Master and advertises its priority to the group members via multicast UDP packets on port 1128. The Backup nodes listen for these advertisements. If the Master stops sending advertisements, the Backup with the highest priority transitions to Master state and starts answering ARP requests for the VIP.

The core components are:

  1. Virtual IP: The address clients use to reach the service.
  2. VRRP Instance: A logical group of routers sharing a VIP.
  3. vrrp_script: A monitoring block that tracks a local service. The script’s success or failure influences the Master node’s priority.

Prerequisites

We assume a standard Linux distribution (e.g., Ubuntu 22.04 or CentOS 8) with root access.

Network Topology

Two servers, node1 and node2.

  • node1 IP: 192.168.1.10
  • node2 IP: 192.168.1.11
  • Virtual IP (VIP): 192.168.1.100
  • Interface: eth0

Installation

Install the keepalived package using the package manager.

Terminal window
# Debian/Ubuntu
apt update && apt install -y keepalived
# RHEL/CentOS
yum install -y keepalived

Verify the installation and check the service status.

Terminal window
systemctl status keepalived

System Kernel Tuning

VRRP requires specific kernel parameters to function correctly. The default kernel settings often block VRRP advertisements or drop multicast packets.

Create a configuration file for sysctl settings.

Terminal window
cat > /etc/sysctl.d/99-vrrp.conf << EOF
# Enable IP forwarding (required for VIP to pass through)
net.ipv4.ip_forward = 1
# Disable Source Routed Packets (security hardening)
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
# Disable Reverse Path Filtering (often blocks VRRP)
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
# Ignore IGMP queries (keepalived will handle IGMP)
net.ipv4.conf.all.igmp_max_memberships = 10
EOF

Apply the settings immediately.

Terminal window
sysctl -p /etc/sysctl.d/99-vrrp.conf

Configuration: Master Node

The configuration file resides at /etc/keepalived/keepalived.conf. We define the VRRP instance, the interface, and the virtual IP.

global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100/24 dev eth0 label eth0:1
}
}

Configuration Breakdown

  • state MASTER: The initial state. This is overridden by priority logic during failover. The node with the highest priority becomes Master.
  • interface eth0: The physical network interface bound to the VIP.
  • virtual_router_id 51: All nodes in the same failover group must share the same ID. This ID is not the IP address.
  • priority 100: The Master has a higher priority than the Backup. Valid range is 1 to 254.
  • auth_pass 1111: Simple plaintext authentication. While not cryptographically secure, it prevents unauthorized nodes from hijacking the VIP.

Configuration: Backup Node

The Backup node configuration is nearly identical. The only difference is the priority, which must be lower than the Master, and often the state, though the state is effectively ignored by the protocol.

global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100/24 dev eth0 label eth0:1
}
}

Integrating Health Checks

A static VIP assignment is dangerous. If the Master node is up but the web server (Nginx/Apache) is down, the VIP should move to the Backup. Keepalived supports vrrp_script blocks to achieve this.

We will create a script that checks if Nginx is listening on port 80.

Create the script directory and file.

mkdir -p /usr/local/bin
cat > /usr/local/bin/check_nginx.sh << 'EOF'
#!/bin/bash
# Check if Nginx is running and responding on port 80
if ! systemctl is-active --quiet nginx; then
exit 1
fi
if ! nc -z localhost 80; then
exit 1
fi
exit 0
EOF

Make the script executable.

Terminal window
chmod +x /usr/local/bin/check_nginx.sh

Update the Master configuration to include this script and link its success/failure to the priority.

vrrp_script check_nginx {
script "/usr/local/bin/check_nginx.sh"
interval 10
weight -20
fall 2
rise 2
}
vrrp_instance VI_1 {
# ... existing config ...
virtual_ipaddress {
192.168.1.100/24 dev eth0 label eth0:1
}
track_script {
check_nginx
}
}

Behavior:

  • interval 10: Run the check every 10 seconds.
  • weight -20: If the check fails, subtract 20 from the priority.
  • fall 2: If the check fails twice in a row, transition to Backup state.
  • rise 2: If the check succeeds twice in a row, transition back to Master state (if priority is higher).

Service Deployment

Start the keepalived service on both nodes.

Terminal window
systemctl daemon-reload
systemctl enable keepalived
systemctl start keepalived

Check the logs for errors.

Terminal window
journalctl -u keepalived -f

Verification

1. IP Address Verification

On the Master node, verify that the VIP is assigned.

Terminal window
ip addr show eth0

You should see eth0:1 with the IP 192.168.1.100.

2. VRRP Advertisement Check

Use tcpdump to inspect VRRP traffic. You should see Master sending advertisements to 224.0.0.18 (VRRP Multicast group).

Terminal window
tcpdump -i eth0 -n host 224.0.0.18

3. Ping Test

Ping the VIP from another machine on the network.

Terminal window
ping 192.168.1.100

Success indicates the ARP resolution is working and traffic is flowing through the Master.

Troubleshooting

Issue: VIP does not appear on Master

Symptoms:

  • Run ip addr show on Master. No VIP.
  • Backup also has no VIP.

Diagnosis:

  1. Interface Mismatch: Ensure interface eth0 matches the actual interface name. Use ip link show to check.
  2. Priority: The Backup might have a higher priority than the Master.
  3. VRRP ID: Ensure virtual_router_id is identical on both nodes.

Logs: Check /var/log/syslog or /var/log/messages for: Keepalived_vrrp: VRRP_Instance(II) Entering BACKUP STATE

Action: Ensure priority 100 is higher than priority 90 on the Master.

Issue: VIP moves immediately after setting up

Symptoms: The VIP appears on Master, then vanishes, then reappears.

Diagnosis: This is usually due to the health check script failing immediately.

Logs: Keepalived_vrrp: Script /usr/local/bin/check_nginx failed for execution

Action: Check permissions on the script. Ensure nc (netcat) is installed. Run the script manually to verify it returns 0.

Terminal window
/usr/local/bin/check_nginx.sh
echo $?

Issue: VIP moves only during network spikes

Symptoms: The VIP is stable under normal load but drops when network latency spikes.

Diagnosis: The advert_int interval is too long, or the network is dropping packets.

Action: Reduce advert_int to 0.5 or 1. Adjust priority and weight parameters to be more forgiving.

Issue: VRRP packet blocked by Firewall

Symptoms: tcpdump shows no traffic, but systemctl status keepalived says Master.

Diagnosis: The firewall is dropping UDP port 1128.

Action: Allow VRRP traffic.

Terminal window
# UFW
ufw allow from any to any port 1128 proto udp
# iptables
iptables -A INPUT -p udp --dport 1128 -j ACCEPT

Advanced Configuration: Non-Default VRRP Port

In environments with strict firewall policies (like AWS or corporate VPNs), multicast (224.0.0.18) might be blocked. Keepalived supports Unicast mode to communicate directly with the Backup node’s IP address.

Configure the Master to send unicast advertisements to the Backup IP.

vrrp_instance VI_1 {
# ... existing config ...
# Unicast Source and Destination
unicast_src_ip 192.168.1.10
unicast_peer {
192.168.1.11
}
# Disable Multicast
mcast_src_ip 192.168.1.10
# No virtual_ipaddress defined here, it is defined globally or per instance
}

Note: Some distributions or kernel versions require specific flags to disable multicast fallback behavior. This configuration is advanced and rarely needed unless strict network segmentation prohibits Layer 2 broadcasts.

Security Considerations

VRRP is a public protocol. Without authentication, any node on the network can claim the VIP.

  1. Authentication: Use auth_type PASS with a strong password (at least 8 characters). Note that this is cleartext.
  2. Network Segmentation: Place the Master and Backup nodes in the same VLAN to ensure VRRP traffic is not blocked by routers.
  3. Secure Boot: Ensure the OS boots securely to prevent a malicious user from modifying the keepalived.conf file to steal the VIP.

Performance Trade-offs

Advantages:

  • Zero Downtime: Automatic failover without client intervention.
  • Simplicity: No complex load balancer configuration required for basic VIP failover.
  • Cost: Runs on commodity hardware.

Disadvantages:

  • Split Brain Risk: If both Master and Backup lose network connectivity between them, they cannot communicate. If both detect a failure in a way that prevents VRRP messages, both might try to claim the VIP. This is mitigated by track_script and careful priority settings.
  • No Load Balancing: Keepalived alone does not distribute traffic. It only provides a single point of entry (the VIP). For load balancing, pair Keepalived with HAProxy or Nginx.

Troubleshooting with keepalived -D

The daemon mode (-D) provides verbose debug output. This is the most effective tool for diagnosing state changes.

Terminal window
keepalived -D -f /etc/keepalived/keepalived.conf

This will print every priority change, script execution result, and interface state change to the console. Use this to trace exactly when and why the VIP moves.

Summary

Configuring Keepalived for VRRP failover provides a robust high-availability solution for Linux servers. By tuning the Linux kernel (sysctl) and defining strict health checks, administrators can ensure that services remain reachable even if the primary hardware fails. The key to a stable deployment lies in the virtual_router_id synchronization and the reliability of the underlying monitoring script.

found this useful? share it →
Written by

Dzubayyan Ahmad

System Administrator, SRE & Technical Support Engineer specialising in web hosting infrastructure — Linux servers, automation, and monitoring. Writing these notes since 2015 so future-me can find the fix again.