Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

keepalived

keepalived moves a virtual IP between servers with VRRP, so one address stays up when a server dies — classic HA for load balancers.

Level
DevSecOps service
Group
Network services
Needs root
Yes
Package
keepalived

What it does

keepalived runs on two or more servers. With the VRRP protocol they elect a MASTER that holds the virtual IP (VIP); the others are BACKUP. If the master stops sending adverts (server down) or a track script fails (for example HAProxy died), a backup takes the VIP within seconds. Clients always use the VIP. It can also run IPVS load balancing.

Typical uses: two HAProxy nodes in front of web apps, or in front of the Kubernetes API (port 6443).

How to write it

keepalived [-f FILE] [-t] [-D] [-n]
systemctl {start|reload|status} keepalived

Main parts

Config parts

global_defs
Router ID, script security.
vrrp_script NAME { script … interval … }
Health check.
vrrp_instance NAME { … }
One VIP group.
state MASTER|BACKUP
Start role.
priority N
Higher wins (100 master, 90 backup).
virtual_router_id N
Same on all nodes of one group (1–255).
virtual_ipaddress { … }
The VIP(s).
authentication / unicast_peer
Simple auth / unicast instead of multicast.
track_script { NAME }
Lower priority when the check fails.

Options 5

Most used

Flags

-t, --config-test
Check the config and exit.
-f FILE
Config file.
-D
Detailed log.
-n
Stay in the foreground.
-l
Log to the console.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Install on both nodes

    dnf install -y keepalived haproxy
  2. 2Master node config

    cat /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb1
        enable_script_security
        script_user root
    }
    vrrp_script chk_haproxy {
        script "/usr/bin/systemctl is-active --quiet haproxy"
        interval 2
        fall 2
        rise 2
    }
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 51
        priority 100
        advert_int 1
        unicast_src_ip 192.168.122.21
        unicast_peer { 192.168.122.22 }
        virtual_ipaddress { 192.168.122.20/24 }
        track_script { chk_haproxy }
    }
  3. 3Backup node: same file with these changes

    grep -E "router_id|state|priority|unicast" /etc/keepalived/keepalived.conf
        router_id lb2
        state BACKUP
        priority 90
        unicast_src_ip 192.168.122.22
        unicast_peer { 192.168.122.21 }
  4. 4Allow VRRP in the firewall

    firewall-cmd --permanent --add-rich-rule='rule protocol value="vrrp" accept' && firewall-cmd --reload
  5. 5Check and start

    keepalived -t -f /etc/keepalived/keepalived.conf
    systemctl enable --now keepalived
  6. 6Who has the VIP?

    ip -br addr show eth0
    eth0  UP  192.168.122.21/24 192.168.122.20/24
  7. 7Test a failover

    systemctl stop haproxy
    journalctl -u keepalived -n 5
    ip -br addr show eth0
  8. 8Watch VRRP traffic

    tcpdump -ni eth0 vrrp
  9. 9Allow binding to the VIP on the backup (HAProxy)

    sysctl -w net.ipv4.ip_nonlocal_bind=1
    echo "net.ipv4.ip_nonlocal_bind = 1" > /etc/sysctl.d/90-vip.conf
  10. 10Read the state changes

    journalctl -u keepalived | grep -E "MASTER|BACKUP|FAULT"

Watch out

  • Use a different virtual_router_id per group on the same network, or groups fight.
  • In most clouds (Azure, AWS) VRRP/multicast and moving IPs do not work — use the cloud load balancer.
  • Two masters at once (split brain) happen when nodes cannot see each other; check the firewall.

Files

/etc/keepalived/keepalived.conf
Configuration.

Tips to remember

  • Use unicast peers in VMs and labs; multicast is often blocked.

Exit codes

0Success (config OK).
1Error (config invalid).

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man 8 keepalived