| |

LFCE 107 ๐Ÿง Keepalived โ€” Installation and Config

โฑ๏ธ Estimated read: 10 min | |

Keepalived is the userspace daemon that implements the Virtual Router Redundancy Protocol (VRRP) on Linux. Its purpose is narrow and specific: it manages one or more virtual IP addresses (VIPs) that move between a group of servers when the active node fails. The previous chapter covered the high availability concepts; this chapter covers the package installation and the configuration file that makes the failover work. The configuration is built from a global_defs block and one or more vrrp_instance blocks, and the instance block is where the state, the priority, and the virtual IP are defined.

The installation is a single package-manager command on both the Debian and RHEL families, and the service is managed through systemctl. The configuration lives in /etc/keepalived/keepalived.conf. The primary node is configured with the higher priority, and the backup node with the lower priority. The virtual_router_id and the auth_pass must match on both nodes, or the election fails and the VIP never moves . The firewall must allow the VRRP traffic on the multicast address 224.0.0.18 and the protocol number 112, or both nodes become master, which is the split-brain .

Key point: The state parameter is the initial role, not the running role. The election is decided by the priority, and the node with the higher priority becomes the master. A node declared MASTER claims the VIP at startup without listening for an existing master, which causes an unnecessary VIP move on every restart. The recommended practice is to declare both nodes as BACKUP and let the priority decide the master .


Why Keepalived configuration requires the priority and the router ID

The priority problem.

VRRP elects the master by comparing the priority values. The node with the higher priority becomes the master and holds the VIP. The Oracle documentation states the rule explicitly: “Set the value of priority higher on the master server than on a backup server” . In a two-node cluster, the primary is configured with priority 200 and the backup with priority 100 . If both nodes have the same priority, the one with the higher IP address wins the election, which is unpredictable and difficult to debug .

The router-ID problem.

The virtual_router_id is a number between 1 and 255 that identifies the VRRP group. Every node in the same group must use the same ID, and no two groups on the same network segment can share an ID . The VRID selects the virtual MAC address 00:00:5e:00:01:<vrid>, so two unrelated groups using the same ID on one VLAN fight over the same MAC, and both VIPs become intermittently unreachable . The ID is the mechanism that isolates the VRRP group from the other Keepalived instances on the same link.

The authentication problem.

The VRRP advertisements carry a simple password authentication by default. The auth_type PASS and auth_pass must match on every node in the group . The password is limited to eight characters . The authentication is not a strong security measure, but it prevents the accidental cross-talk between the misconfigured groups.


a. The installation

Keepalived is packaged as keepalived on both the Debian and the RHEL families. The installation is a single command, and the service is managed through systemctl.

# Debian / Ubuntu
sudo apt install keepalived

# RHEL / Fedora / Rocky
sudo dnf install keepalived

The Debian and the RHEL packages both include the keepalived.service systemd unit. After the installation, the service can be enabled and started with systemctl enable --now keepalived. The systemctl status keepalived and the journalctl -u keepalived commands show the state and the logs.


b. The configuration file

The configuration file is /etc/keepalived/keepalived.conf. The global_defs block sets the notification and the router ID, and the vrrp_instance block defines the VIP and the election parameters.

# /etc/keepalived/keepalived.conf (primary node)
global_defs {
    notification_email {
        root@example.com
    }
    notification_email_from keepalived@example.com
    smtp_server localhost
    smtp_connect_timeout 30
    router_id node1
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
    }
    virtual_ipaddress {
        192.168.1.100/24
    }
}

The state MASTER and the priority 200 make this node the preferred master. The interface eth0 is the physical interface that carries the VRRP advertisements and the VIP. The virtual_router_id 51 identifies the VRRP group, and the advert_int 1 sets the advertisement interval to one second . The virtual_ipaddress block defines the VIP that the master holds.

The backup node uses the same configuration with three differences: the state BACKUP, the lower priority, and the different router_id.

# /etc/keepalived/keepalived.conf (backup node)
global_defs {
    router_id node2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
    }
    virtual_ipaddress {
        192.168.1.100/24
    }
}

The virtual_router_id and the auth_pass must match the primary node exactly. The priority 100 is lower than the primary’s 200, so the backup stays in the backup state while the primary is alive .


c. The failover verification and the firewall

The failover is verified by stopping the Keepalived service on the primary and checking that the VIP moves to the backup.

# On the primary: stop keepalived
sudo systemctl stop keepalived

# On the backup: check that the VIP is assigned
ip addr show eth0 | grep 192.168.1.100

When the primary stops, the backup no longer receives the VRRP advertisements, the master-down timer expires, and the backup transitions to the master state and assigns the VIP to its interface . The Oracle documentation states the rule: “If the backup nodes fail to receive three consecutive VRRP advertisements, the backup server with the highest assigned priority takes over as the primary server” . The ip addr show command on the backup confirms the VIP is present.

The VRRP advertisements use the multicast address 224.0.0.18 on the protocol number 112. A firewall that blocks the multicast or the protocol 112 prevents the election, and both nodes remain in the master state, which is the split-brain . The firewall must allow the VRRP traffic on the interface that carries the advertisements.


Complete Example Session

# ============================================
# PART 1: INSTALL KEEPALIVED
# ============================================
sudo apt install keepalived

# ============================================
# PART 2: CREATE THE PRIMARY CONFIG
# ============================================
sudo tee /etc/keepalived/keepalived.conf > /dev/null <<'EOF'
global_defs {
    router_id node1
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
    }
    virtual_ipaddress {
        192.168.1.100/24
    }
}
EOF

# ============================================
# PART 3: START THE SERVICE
# ============================================
sudo systemctl enable --now keepalived
sudo systemctl status keepalived

# ============================================
# PART 4: VERIFY THE VIP ON THE PRIMARY
# ============================================
ip addr show eth0 | grep 192.168.1.100

# ============================================
# PART 5: CONFIGURE THE BACKUP NODE
# ============================================
# On the backup node, use the same config with:
# state BACKUP, priority 100, router_id node2

# ============================================
# PART 6: TEST THE FAILOVER
# ============================================
# On the primary:
sudo systemctl stop keepalived
# On the backup:
ip addr show eth0 | grep 192.168.1.100

# ============================================
# PART 7: RESTORE THE PRIMARY
# ============================================
sudo systemctl start keepalived
# The primary takes the VIP back if preempt is enabled (default)

The seven parts demonstrate the complete configuration: the installation, the primary config, the service start, the VIP verification, the backup config, the failover test, and the primary restore.


Quick Reference

Installation

DistributionCommand
Debian / Ubuntuapt install keepalived
RHEL / Rockydnf install keepalived
Servicesystemctl enable --now keepalived

The vrrp_instance Parameters

ParameterPurposeExample
stateThe initial roleMASTER or BACKUP
interfaceThe physical interfaceeth0
virtual_router_idThe VRRP group ID51
priorityThe election weight200 (primary), 100 (backup)
advert_intThe advertisement interval1 (second)
auth_type / auth_passThe group authenticationPASS / 12345678
virtual_ipaddressThe VIP to manage192.168.1.100/24

The Verification

CommandPurpose
ip addr show <if>Check the VIP presence
journalctl -u keepalivedCheck the state transitions
systemctl stop keepalivedTrigger the failover

Best Practices

โœ… Do This:

# Use the explicit priority difference
priority 200    # primary
priority 100    # backup                                      # โœ…

# Use the same virtual_router_id on all nodes
virtual_router_id 51                                          # โœ…

# Allow the VRRP multicast through the firewall
# Protocol 112, multicast 224.0.0.18                          # โœ…

# Verify the failover after the configuration
systemctl stop keepalived                                     # โœ…

# Use the router_id for the logs
router_id node1                                               # โœ…

โŒ Don’t Do This:

# Use the same priority on both nodes
priority 100    # Both nodes โ†’ ambiguous election              # โŒ

# Use different virtual_router_id values
virtual_router_id 51    # Primary
virtual_router_id 52    # Backup โ†’ different groups            # โŒ

# Block the VRRP multicast
# The election fails โ†’ both nodes stay master                  # โŒ

# Forget the auth_pass match
auth_pass 12345678    # Must be identical                      # โŒ

# Assume the VIP moves instantly
# The failover takes the advert interval ร— 3                    # โŒ

Common Pitfalls

PitfallWhy It HappensFix
Both nodes are masterThe VRRP multicast is blockedAllow the protocol 112 and the 224.0.0.18
The VIP does not moveThe virtual_router_id differsUse the same ID on all nodes
The backup does not take overThe priority is the same or higherSet the lower priority on the backup
The VIP is on both nodesThe split-brainCheck the firewall and the advertisement reachability

Real-World Examples

1. Install Keepalived

sudo apt install keepalived

2. Primary config

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 200
    virtual_ipaddress { 192.168.1.100/24 }
}

3. Backup config

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    virtual_ipaddress { 192.168.1.100/24 }
}

4. Start the service

systemctl enable --now keepalived

5. Check the VIP

ip addr show eth0 | grep 192.168.1.100

6. Test the failover

systemctl stop keepalived

7. Check the logs

journalctl -u keepalived

8. The firewall rule

iptables -A INPUT -p vrrp -j ACCEPT

9. The router ID

router_id node1

10. The authentication

auth_type PASS
auth_pass 12345678

Visual

The VRRP Election

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE HIGHER PRIORITY BECOMES THE MASTER                      โ”‚
โ”‚                                                              โ”‚
โ”‚  Primary (priority 200)                                      โ”‚
โ”‚  โ””โ”€โ”€ Holds the VIP 192.168.1.100                             โ”‚
โ”‚  โ””โ”€โ”€ Sends the VRRP advertisements every 1s                  โ”‚
โ”‚                                                              โ”‚
โ”‚  Backup (priority 100)                                       โ”‚
โ”‚  โ””โ”€โ”€ Listens for the advertisements                          โ”‚
โ”‚  โ””โ”€โ”€ If the advertisements stop for 3ร— advert_int โ†’ master   โ”‚
โ”‚                                                              โ”‚
โ”‚  The election is deterministic because of the priority.      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Failover

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE VIP MOVES WHEN THE MASTER STOPS                         โ”‚
โ”‚                                                              โ”‚
โ”‚  Before:                                                     โ”‚
โ”‚  โ”œโ”€โ”€ Primary: 192.168.1.100 (VIP)                            โ”‚
โ”‚  โ””โ”€โ”€ Backup: no VIP                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  Primary stops                                               โ”‚
โ”‚       โ”‚                                                      โ”‚
โ”‚       โ–ผ                                                      โ”‚
โ”‚  Backup waits 3 ร— advert_int                                 โ”‚
โ”‚       โ”‚                                                      โ”‚
โ”‚       โ–ผ                                                      โ”‚
โ”‚  Backup becomes master                                       โ”‚
โ”‚       โ”‚                                                      โ”‚
โ”‚       โ–ผ                                                      โ”‚
โ”‚  Backup assigns the VIP to its interface                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Configuration Match

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE TWO NODES MUST MATCH ON THE GROUP PARAMETERS            โ”‚
โ”‚                                                              โ”‚
โ”‚  Same:                                                       โ”‚
โ”‚  โ”œโ”€โ”€ virtual_router_id 51                                    โ”‚
โ”‚  โ”œโ”€โ”€ auth_pass 12345678                                      โ”‚
โ”‚  โ””โ”€โ”€ virtual_ipaddress 192.168.1.100/24                      โ”‚
โ”‚                                                              โ”‚
โ”‚  Different:                                                  โ”‚
โ”‚  โ”œโ”€โ”€ state (MASTER vs BACKUP)                                โ”‚
โ”‚  โ”œโ”€โ”€ priority (200 vs 100)                                   โ”‚
โ”‚  โ””โ”€โ”€ router_id (node1 vs node2)                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
Packagekeepalived
Config file/etc/keepalived/keepalived.conf
Primary stateMASTER
Backup stateBACKUP
Primary priorityHigher (e.g., 200)
Backup priorityLower (e.g., 100)
Router IDSame group ID on all nodes
AuthenticationPASS + the matching password
VIPvirtual_ipaddress block
FirewallAllow the protocol 112 and the 224.0.0.18

Key takeaways:

  • Keepalived is installed from the distribution repositories. The apt install keepalived on the Debian family and the dnf install keepalived on the RHEL family are the installation commands, and the service is managed through systemctl .
  • The priority difference is the election mechanism. The primary has the higher priority (e.g., 200) and the backup has the lower (e.g., 100). If the priorities are equal, the election is decided by the IP address, which is unpredictable .
  • The virtual_router_id must match on all nodes. It identifies the VRRP group, and a mismatch puts the nodes in different groups, so the VIP never moves between them .
  • The auth_pass must match on all nodes. The authentication is a simple password that prevents the cross-talk between the misconfigured groups. The password is limited to eight characters .
  • The failover takes three advertisement intervals. When the master stops sending the advertisements, the backup waits three intervals before it transitions to the master state and assigns the VIP .
  • The firewall must allow the VRRP traffic. The protocol is 112 and the multicast address is 224.0.0.18. A firewall that blocks the traffic causes the split-brain, where both nodes believe they are the master .

Remember: Keepalived is the VRRP implementation that manages the floating VIP. The configuration is the vrrp_instance block with the state, the priority, the router ID, and the VIP. The primary has the higher priority and the backup has the lower. The router ID and the password must match. The failover moves the VIP when the master stops, and the firewall must allow the VRRP traffic. The result is the service that survives the failure of the active node without the client noticing the change.


Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!