Implementing High Availability and Load Balancing with Keepalived

Overview

Keepalived is a free, open-source routing software written in C that provides load balancing and high-availability capabilities. It operates at layers 3, 4, and 7 of the OSI model, functioning similarly to layer 3/4/7 switches. The software primarily offers two key features: load balancing (implemented through Linux's IP Virtual Server kernel module) and high availability (achieved via the VRRP protocol for failover between multiple systems).

Keepalived Architecture

The Keepalived architecture is divided into two main components:

  • Kernel Space: Contains IPVS (IP Virtual Server for load balancing network services) and NETLINK (providing advanced routing and other network functions).
  • User Space: Includes several components:
    • WatchDog: Monitors the status of checkers and VRRP processes
    • VRRP Stack: Handles failover between load balancers (optional if using only one load balancer)
    • Checkers: Performs health checks on real servers (essential component)
    • IPVS wrapper: Sends configured rules to the kernel IPVS code
    • Netlink Reflector: Configures VRRP VIP addresses and other network settings

All Keepalived functionality is configured through the keepalived.conf file.

Installation and Configuration

Installation Process

Download the latest version of Keepalived from the official website. For this example, we'll use version 1.2.24:

shell> cd /usr/local/src
shell> wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz
shell> tar -zxvf keepalived-1.2.24.tar.gz
shell> cd keepalived-1.2.24
shell> ./configure --prefix=/usr/local/keepalived
shell> make && make install

Configuration Setup

After installation, you need to copy the configuration files to the appropriate system directories. Keepalived looks for its configuration file in /etc/keepalived/keepalived.conf at startup. The directory structure in the installation folder looks like this:

shell> tree -l /usr/local/keepalived/etc
-- keepalived
|   |-- keepalived.conf
|   `-- samples
|       |-- keepalived.conf.status_code
|       |-- keepalived.conf.track_interface
|       |-- keepalived.conf.vrrp
|       |-- ...
|-- rc.d
|   `-- init.d
|       `-- keepalived
`-- sysconfig
    `-- keepalived

Copy the necessary files to their system locations:

shell> mkdir /etc/keepalived
shell> cp /usr/local/keepalived/etc/keepalived.conf /etc/keepalived/keepalived.conf
shell> cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/keepalived
shell> cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived

Enable the Keepalived service to start automatically on boot:

shell> chkconfig keepalived on
shell> service keepalived start   # Start the service
shell> service keepalived stop    # Stop the service
shell> service keepalived restart # Restart the service

Important: Keepalived doesn't validate configuration file syntax on startup, so ensure your configuration is correct to avoid unexpected behavior.

When starting Keepalived with the service command, it uses the KEEPALIVED_OPTIONS parameter from /etc/sysconfig/keepalived and loads the configuration from /etc/keepalived/keepalived.conf (or a file specified with the -f parameter).

# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#
KEEPALIVED_OPTIONS="-D"

After starting Keepalived, you should see three processes running: a parent process that monitors the child processes, a VRRP child process, and a health checker child prrocess:

shell> ps -ef | grep keepalived
root       831     1  0 11:22 ?        00:00:00 keepalived -D
root       840   831  0 11:22 ?        00:00:00 keepalived -D
root       841   831  0 11:22 ?        00:00:00 keepalived -D

Keepalived Configuration File Explained

Global Definitions Block

This section contains global settings for the Keepalived instance:

global_defs {            
   notification_email {  # Email recipients for alerts
    admin@example.com
   }
   notification_email_from keepalived@example.com # Sender email
   smtp_server mail.example.com # SMTP server address
   smtp_connect_timeout 30 # SMTP connection timeout
   router_id LB_1 # Unique identifier for this node
}

VRRP Synchronization Group

This section defines how failover is handled between multiple instances:

vrrp_sync_group VG_1{ # Monitor multiple network instances
group {
    VI_1 # Instance name
    VI_2
    # ...
}
notify_master /path/to/master.sh # Script to run when becoming master
notify_backup /path/to/backup.sh # Script to run when becoming backup
notify_fault "/path/to/fault.sh VG_1" # Script to run on fault
smtp_alert # Send email notifications using global settings
}

VRRP Instance Configuration

This section defines a specific VRRP instance:

vrrp_instance VI_1 {
    state BACKUP # Initial state (MASTER or BACKUP)
    interface eth0 # Network interface to bind to
    dont_track_primary # Ignore VRRP interface errors
    track_interface{ # Additional interfaces to monitor
        eth0
        eth1
    }
    mcast_src_ip 192.168.1.100 # Source IP for multicast packets
    garp_master_delay 5 # Delay for gratuitous ARP after becoming master
    virtual_router_id 50 # Unique VRID for the VRRP instance
    priority 100 # Priority for master election (higher is better)
    advert_int 1 # Advertisement interval in seconds
    nopreempt # Disable preemption (only effective on BACKUP nodes)
    preempt_delay 300 # Delay before attempting to preempt
    authentication { # Authentication settings
        auth_type PASS # Authentication type
        auth_pass securepassword # Authentication password
    }
    virtual_ipaddress { # Virtual IP addresses
        192.168.1.200/24 dev eth0
    }
}

Virtual Server Configuration

This section defines the virtual server for load balancing:

virtual_server 192.168.1.200 80 { # VIP and port
    delay_loop 6 # Health check interval
    lb_algo wrr # Scheduling algorithm (rr|wrr|lc|wlc|lblc|sh|dh)
    lb_kind DR # Forwarding method (NAT|DR|TUN)
    persistence_timeout 50 # Session persistence timeout
    protocol TCP # Protocol to use
    persistence_granularity 255.255.255.0 # Persistence granularity
    sorry_server 192.168.1.99 80 # Backup server

    real_server 192.168.1.10 80 { # First real server
        weight 3 # Server weight
        inhibit_on_failure # Mark server down instead of removing it
        notify_up /path/to/up.sh # Script to run when server comes up
        notify_down /path/to/down.sh # Script to run when server goes down
        
        TCP_CHECK { # Health check method
            connect_timeout 3 # Connection timeout
            nb_get_retry 3 # Number of retries
            delay_before_retry 3 # Delay between retries
            connect_port 80 # Port to check
        }
    }

    real_server 192.168.1.11 80 { # Second real server
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

Production Configuration Example

! Configuration File for keepalived

global_defs {
    notification_email {
        admin@example.com
    }
    notification_email_from keepalived@example.com
    smtp_server mail.example.com
    smtp_connect_timeout 30
    router_id LB_2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 55
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass securepass
    }
    virtual_ipaddress {
        192.168.1.110/24
    }
}

virtual_server 192.168.1.110 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 300
    protocol TCP
    
    real_server 192.168.1.20 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 8
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    
    real_server 192.168.1.21 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 8
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

Configuring Keepalived Logging

By default, Keepalived logs to /var/log/messages. To configure separate logging:

# Edit /etc/sysconfig/keepalived
#KEEPALIVED_OPTIONS="-D"  # Comment out this line
KEEPALIVED_OPTIONS="-D -d -S 0"  # Add this line

# Edit /etc/rsyslog.conf
local0.*        /var/log/keepalived.log  # Add this line

# Restart services
shell> /etc/init.d/rsyslog restart
shell> /etc/init.d/keepalived restart

Now Keepalived will log to /var/log/keepalived.log instead of the system messages file.

Tags: Keepalived load-balancing high-availability VRRP Linux

Posted on Tue, 20 Oct 2026 16:25:51 +0000 by matstuff