Lab 03: Keepalived — VRRP Failover
Time: 45 minutes | Level: Architect | Docker: docker run -it --rm --privileged ubuntu:22.04 bash
Overview
Keepalived implements VRRP (Virtual Router Redundancy Protocol) on Linux to provide automatic failover of virtual IP addresses between servers. Combined with health scripts, it enables highly available services without a full cluster stack. Keepalived is widely used to provide VIP failover for HAProxy, Nginx, and database clusters.
Learning Objectives:
Understand VRRP protocol and its operation
Install and configure Keepalived
Master
keepalived.confsyntax:vrrp_instance,virtual_ipaddress, priority, stateConfigure
track_scriptfor application-aware health checkingWrite
notifyscripts for state change eventsUnderstand
preempt_delayand non-preemptive failover
Step 1: Install Keepalived
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y keepalived iproute2Verify installation:
📸 Verified Output:
View all available options:
📸 Verified Output:
💡 Tip: Use
keepalived -tto validate configuration syntax without starting the daemon. Essential before applying changes in production.
Step 2: VRRP Protocol Concepts
VRRP (Virtual Router Redundancy Protocol — RFC 5798):
Key VRRP parameters:
virtual_router_id
VRRP group ID (1-255). Must match on all nodes in group
priority
Higher wins MASTER role (1-254). MASTER must have highest
advert_int
Advertisement interval in seconds (default: 1)
authentication
Shared password for VRRP packet authentication
virtual_ipaddress
VIP(s) assigned to the MASTER node
preempt / nopreempt
Whether recovered MASTER reclaims the VIP
preempt_delay
Seconds to wait before preempting (avoids flapping)
Step 3: Basic Keepalived Configuration — MASTER Node
📸 Verified Output:
💡 Tip: The
weightinvrrp_scriptadjusts priority dynamically. Ifchk_haproxyfails and weight is-20, node1's effective priority drops from 150 to 130. If node2 has priority 140, node2 wins MASTER — automatic failover without hard node failure!
Step 4: BACKUP Node Configuration
📸 Verified Output:
💡 Tip:
nopreempton the BACKUP node means that even if node1 recovers with higher priority, node2 will NOT give up the VIP. This prevents flapping. For planned maintenance, manually runsystemctl restart keepalivedon node1 to trigger re-election.
Step 5: Notify Scripts
Create notification scripts that execute on state transitions:
📸 Verified Output:
Step 6: Advanced Track Script — Custom Health Check
📸 Verified Output:
💡 Tip: Scripts used in
track_scriptmust be executable and must exit with code 0 (success/healthy) or non-zero (failure). Theweightadjusts priority dynamically; if weight causes effective priority to drop below the BACKUP node's priority, automatic failover occurs.
Step 7: Validate Configuration
📸 Verified Output:
📸 Verified Output:
Step 8: Capstone — HA Load Balancer with VRRP
Scenario: Design a complete Keepalived+HAProxy HA solution for a production web platform with:
VRRP VIP for client connection endpoint
HAProxy health-check-driven VRRP priority adjustment
State-change notifications via webhook
Non-preemptive failover to prevent VIP flapping
Dual network interface (separate management/data)
📸 Verified Output:
💡 Tip: Use
unicast_peerinstead of multicast VRRP in cloud environments (AWS, Azure, GCP) where multicast is not supported. Unicast VRRP sends advertisements directly between peer IPs — faster and more reliable in virtualized environments.
Summary
VRRP instance
vrrp_instance VI_1 {}
Defines a VRRP failover group
Role assignment
state MASTER/BACKUP
Initial node role
Election priority
priority 1-254
Higher = preferred MASTER
Group identifier
virtual_router_id 1-255
Must match across all nodes
Advertisement rate
advert_int 1
Seconds between VRRP hellos
Virtual IP
virtual_ipaddress { ... }
Floating IP(s)
Health scripts
vrrp_script + track_script
App-aware failover
Priority tuning
weight -N in script
Adjust priority on failure
Preemption
preempt_delay N
Delay before MASTER reclaim
No reclaim
nopreempt
Prevent automatic re-election
State hook
notify_master/backup/fault
Execute on state change
Unicast mode
unicast_src_ip / unicast_peer
Cloud-compatible VRRP
Config test
keepalived -t -f
Validate before applying
Last updated
