32.9. Virtual-IP Failover with Keepalived

HAProxy is now running on both nodes, but clients still had to choose a node's address. Keepalived fixes that with VRRP: one address that follows a healthy node, decided by a protocol that has been moving default routes since long before clusters were fashionable.

32.9.1. VRRP Instances, Router IDs, Priorities, and Virtual-IP Ownership

The Virtual Router Redundancy Protocol lets several hosts share one address by electing exactly one MASTER at a time. The parameters that define an election are few:

  • virtual_router_id identifies the VRRP group. Every host participating in one election must use the same ID, and two different groups on the same network segment must use different ones. It is a small number, not a secret.
  • priority is the election stake: the highest-priority live host wins. Priorities run 1–254, with 255 reserved for the host that natively owns the address.
  • advert_int is how often the MASTER announces itself. One second is the traditional default; the announcement is not a heartbeat over TCP but a single IP packet of protocol 112.
  • The virtual IP address is the payload: the address, with its mask and interface, that the MASTER configures and advertises.

VRRP election and ownership

Figure 32.5. The VRRP state machine as configured in this section: both directors start as BACKUP, the higher priority wins the election, the MASTER owns the address, and silence - a dead peer or a lost advertisement - is what triggers the transition.

Two properties follow from the design and are worth fixing in mind before any configuration. First, VRRP decides address ownership only; it knows nothing about the service behind the address unless something tells it (section 32.9.3). Second, VRRP has no third-party witness: the protocol's correctness rests on advertisements actually arriving, and section 32.9.5 shows what happens when they are made to vanish in both directions at once.

32.9.2. Configuring Two Directors and Multicast or Unicast Advertisements

Keepalived implements VRRP and owns the configuration both directors share. Install it on both nodes. On ha1, as john:

$ sudo apt install keepalived

The version on the practice hosts:

$ sudo keepalived --version 2>&1 | head -2
Keepalived v2.3.3 (03/30,2025)

Write /etc/keepalived/keepalived.conf on both nodes. The listing below is ha1's; the per-node values that differ are marked and collected in the table after it:

global_defs {
    router_id ha1              # ha2 on the second node
    enable_script_security
    script_user root
}

vrrp_track_process chk_haproxy {
    process haproxy
    weight -60
    delay 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 100               # 90 on the second node
    advert_int 1
    preempt_delay 5
    unicast_src_ip 192.168.232.11   # the node's own address
    unicast_peer {
        192.168.232.12         # ha1 lists ha2; ha2 lists ha1
    }
    virtual_ipaddress {
        192.168.232.150/24 dev eth1
    }
    track_process {
        chk_haproxy
    }
}
Parameter ha1 ha2 Shared meaning
router_id ha1 ha2 unique per host, informational in adverts
priority 100 90 elects ha1 MASTER while it is healthy
unicast_src_ip 192.168.232.11 192.168.232.12 source of this node's adverts
unicast_peer 192.168.232.12 192.168.232.11 where this node sends adverts
virtual_router_id, advert_int, VIP identical identical the election itself

The unicast_src_ip and unicast_peer lines select unicast advertisements, peer to peer. Without them, VRRP's traditional mode is multicast to 224.0.0.18 (vrrp.mcast.net), which works on ordinary switched networks without any configuration but requires that multicast not be filtered between the directors. Unicast is the common choice on isolated or filtered segments exactly like this practice network; pick one mode and use it on both nodes - they must agree.

Start the service on both nodes. On ha1, as john:

$ sudo systemctl restart keepalived

On ha2, the same. Each restart normally prints nothing. The election is immediate; the higher-priority node takes the address. On ha1, as john:

$ ip -4 -br addr show eth1
eth1             UP             192.168.232.11/24 192.168.232.150/24 

The virtual address sits beside the node's own address, owned by ha1. From the independent client lbcli, as john, use the address that can no longer name a single host:

$ curl -s http://192.168.232.150:8080/
backend=lbclient
path=/
x-forwarded-for=192.168.232.15
x-forwarded-proto=http

The request went to the VIP, the VIP's owner proxied it, and the backend reported the forwarding headers - through one address whose physical owner is now an implementation detail.

32.9.3. Interface and Service Tracking and Health-Driven Failover

Section 32.9.1 noted that VRRP knows nothing about the service behind the address. Keepalived's tracking features close that gap. The vrrp_track_process block named chk_haproxy watches for a running haproxy process and, with weight -60, subtracts 60 from the node's effective priority while the process is absent. The delay 2 line grants a two-second grace period at startup so a restarting service is not punished for its first breath.

Kill the MASTER's service. On ha1, as john:

$ sudo systemctl stop haproxy

The command normally prints nothing. Within a couple of seconds - the tracking delay, then the priority recomputation - ha1's effective priority falls to 40, below ha2's 90, and the address moves. On ha2, as john:

$ ip -4 -br addr show eth1
eth1             UP             192.168.232.12/24 192.168.232.100/24 192.168.232.150/24 

The .150 address is ha2's now. (The .100 address beside it is the NFS group's address, inherited by ha2 when the Pacemaker stack stopped on ha1 in section 32.8.2; the two address managers never touch each other's addresses.) The client confirms continuity. On lbcli, as john:

$ curl -s --max-time 5 http://192.168.232.150:8080/
backend=lbclient
path=/
x-forwarded-for=192.168.232.15
x-forwarded-proto=http

The --max-time 5 bounds the attempt - a sensible habit when testing an address that may be moving. The request succeeded through the new owner. Restore the service. On ha1, as john:

$ sudo systemctl start haproxy

With the process back, ha1's priority returns to 100 and the address comes home. On ha1, as john:

$ ip -4 -br addr show eth1
eth1             UP             192.168.232.11/24 192.168.232.150/24 

Health-driven failover with automatic failback, in three commands and about ten seconds of wall time in the practice environment. Whether automatic failback is wanted is the next section's question.

32.9.4. Preemption, Failback, Gratuitous ARP, and IPv6 Neighbor Updates

The configuration sets state BACKUP on both nodes, which looks odd until the semantics are clear: with both nodes starting as BACKUP, the priority alone elects the MASTER, and no node asserts authority it was not granted. Hardcoding state MASTER on one node describes the last election rather than enforcing the next one, and it misbehaves exactly when it matters - a boot race where the "MASTER" node comes up before its peer can outvote it.

The captured failback in section 32.9.3 was preemption at work: ha1 returned to full priority and took the address back. preempt_delay 5 makes that return polite - the higher-priority node waits five seconds after becoming capable before preempting, which damps address flapping when a node's service is cycling rapidly between up and down. The other pole is nopreempt, which tells a healthy higher-priority node never to seize the address back at all; failback then happens only if the current MASTER fails. Between them they express the real design question: is a brief second interruption acceptable in exchange for returning service to the preferred node? For a load balancer pair with identical nodes, nopreempt is often the quieter choice; for asymmetric pairs - the big node should serve when it can - preemption with a delay is the honest one. What is never acceptable is leaving the question unanswered, because the default (preempt, immediately) surprises someone eventually.

Address movement is only useful when neighbors learn about it. On becoming MASTER, Keepalived configures the address and then broadcasts gratuitous ARP - unsolicited ARP replies that tell every host on the segment to update its cache for the VIP. The captured client requests after each transition succeeded precisely because that cache update worked: lbcli re-resolved .150 to ha2's MAC without any client-side action. Keepalived's global vrrp_garp_* parameters tune how many announcements are sent and how often they repeat; the defaults are conservative and rarely need changing.

For a virtual IPv6 address the same machinery speaks Neighbor Discovery instead of ARP: Keepalived accepts IPv6 addresses in virtual_ipaddress and sends unsolicited neighbor advertisements on taking ownership, updating the neighbor caches of the segment the same way. The election parameters are unchanged; only the advertisement family and the update protocol differ. The practice exercises ran IPv4 only, and the IPv6 behavior described here is Keepalived's documented implementation rather than a captured run.

32.9.5. Firewall Rules, Advertisement Loss, and Duplicate Address Ownership

VRRP travels as IP protocol 112 - not TCP, not UDP, so port-based rules cannot describe it. A firewall between two directors must pass protocol 112 explicitly or the election starves. To see why that matters, starve it deliberately: drop VRRP in both directions between the directors, and watch the protocol's missing witness become visible.

On ha1, as john:

$ sudo nft add table inet ch32vrrp
$ sudo nft add chain inet ch32vrrp input "{ type filter hook input priority 0; }"
$ sudo nft add rule inet ch32vrrp input ip protocol 112 drop

On ha2, as john:

$ sudo nft add table inet ch32vrrp
$ sudo nft add chain inet ch32vrrp input "{ type filter hook input priority 0; }"
$ sudo nft add rule inet ch32vrrp input ip protocol 112 drop
$ sudo nft add chain inet ch32vrrp output "{ type filter hook output priority 0; }"
$ sudo nft add rule inet ch32vrrp output ip protocol 112 drop

Each command normally prints nothing. ha1 now drops incoming VRRP - it cannot hear ha2. ha2 drops incoming and outgoing VRRP - it can hear nothing and cannot be heard. Both sides see silence. After the MASTER_DOWN timer expires on each side, both promote themselves. On ha1, as john:

$ ip -4 -br addr show eth1
eth1             UP             192.168.232.11/24 192.168.232.150/24 

On ha2, as john:

$ ip -4 -br addr show eth1
eth1             UP             192.168.232.12/24 192.168.232.100/24 192.168.232.150/24 

Both nodes hold 192.168.232.150. This is duplicate address ownership, and it is the failure state that a real partition or a mis-filtered firewall produces: each MASTER answers ARP for the address, switches hash clients to one MAC or the other by chance, connections split unpredictably between owners, and nothing crashes to announce the problem. On the isolated practice network no client was harmed by the experiment; on a production segment this state is a live incident.

VRRP's own answer to duplicated ownership is the priority field - when advertisements flow again, the lower-priority node demotes itself. Heal the partition. On ha1, as john:

$ sudo nft delete table inet ch32vrrp

On ha2, as john:

$ sudo nft delete table inet ch32vrrp

Within seconds the nodes hear each other again and the election reasserts itself. On ha1, as john:

$ ip -4 -br addr show eth1
eth1             UP             192.168.232.11/24 192.168.232.150/24 

On ha2, as john:

$ ip -4 -br addr show eth1
eth1             UP             192.168.232.12/24 192.168.232.100/24 

ha2 released the address it had wrongly taken. The lesson to take into firewall design: between VRRP peers, permit IP protocol 112 (and, in multicast mode, permit multicast group 224.0.0.18) - the deliberate drop here was a diagnostic, not a configuration to imitate.

32.9.6. Choosing Keepalived or Pacemaker and Avoiding Competing Resource Managers

Keepalived and Pacemaker's IPaddr2 both move addresses, and they are not interchangeable:

Criterion Keepalived (VRRP) Pacemaker (IPaddr2)
Decision speed one to a few seconds, advertisement-driven seconds to tens of seconds, monitor-driven
Quorum none - per-segment election only cluster-wide, witness-backed
Fencing none integrates with the fencing design of section 32.4
Couples address to data state no yes, through groups and ordering
Typical role frontend tier: balancers, routers data services: filesystems, databases, export stacks
Failure mode when unmanaged duplicate address ownership service refuses to start without quorum

The table's middle rows are the decision. An address whose behind lies writable state must be moved by a manager that also controls the state - Pacemaker, with quorum and fencing - because section 32.2.1's three facts must move together. An address whose behind lies stateless or self-checking service is VRRP's natural work: fast, simple, and without cluster machinery to maintain.

What is never acceptable is both managers claiming one address. The failure is not hypothetical: section 32.9.5 needed nftables to force duplicate ownership on VRRP, but two managers on one address produce it without any help, each certain it is right. The practices that keep the boundary clean:

  • One address, one manager, always - the CTDB case of section 32.7.5 is the same rule with a third manager.
  • When both tools run on the same hosts, partition the address space explicitly, and stop one manager entirely when its services are not deployed - exactly what the load-balancing phases did with crm cluster stop in section 32.8.2.
  • Prefer distinct addresses per experiment or per service over "the same address, managed differently this time"; the practice topology's .100, .150, and .160–.162 exist for that reason.