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.conf syntax: vrrp_instance, virtual_ipaddress, priority, state

  • Configure track_script for application-aware health checking

  • Write notify scripts for state change events

  • Understand preempt_delay and non-preemptive failover


Step 1: Install Keepalived

apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y keepalived iproute2

Verify installation:

📸 Verified Output:

View all available options:

📸 Verified Output:

💡 Tip: Use keepalived -t to 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:

Parameter
Description

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 weight in vrrp_script adjusts priority dynamically. If chk_haproxy fails 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: nopreempt on 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 run systemctl restart keepalived on 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_script must be executable and must exit with code 0 (success/healthy) or non-zero (failure). The weight adjusts 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_peer instead 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

Concept
Config Key
Description

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