# Keepalived — High Availability and Load Balancing for Linux > A robust VRRP and health-checking daemon that provides IP failover and real server load balancing for Linux infrastructure. ## Install Save as a script file and run: # Keepalived — High Availability and Load Balancing for Linux ## Quick Use ```bash apt install keepalived # /etc/keepalived/keepalived.conf cat < /etc/keepalived/keepalived.conf vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 virtual_ipaddress { 192.168.1.100 } } EOF systemctl start keepalived ``` ## Introduction Keepalived is a Linux system daemon that provides high availability via VRRP (Virtual Router Redundancy Protocol) and load balancing through the Linux Virtual Server (LVS/IPVS) framework. It is widely used to create active-passive or active-active failover clusters for critical services like load balancers, database proxies, and web servers. ## What Keepalived Does - Implements VRRP for automatic virtual IP failover between multiple servers - Manages Linux Virtual Server (IPVS) rules for Layer 4 load balancing - Performs health checks on real servers using TCP, HTTP, SSL, and custom scripts - Supports BFD (Bidirectional Forwarding Detection) for sub-second failure detection - Triggers notification scripts on state transitions for custom automation ## Architecture Overview Keepalived runs as a multi-process daemon with a parent watchdog and child processes for VRRP and health checking. The VRRP process sends multicast advertisements and manages virtual IP assignment via Netlink. The health checker process monitors real servers and updates IPVS tables accordingly. Both processes communicate state changes to the parent, which can invoke notification scripts. The entire system integrates directly with the Linux kernel's networking stack for minimal overhead. ## Self-Hosting & Configuration - Install via package manager on all major Linux distributions - Configuration in `/etc/keepalived/keepalived.conf` defines VRRP instances and virtual servers - Set `priority` values to control which node becomes MASTER (higher wins) - Use `track_script` to tie VRRP state to application health checks - Enable `unicast_peer` for environments where multicast is not available ## Key Features - Sub-second failover with configurable advertisement intervals - SNMP integration for monitoring VRRP state and statistics - IPv4 and IPv6 dual-stack support for virtual IP addresses - Preemption control to prevent flapping between MASTER and BACKUP nodes - Sync groups for coordinating failover of multiple VRRP instances together ## Comparison with Similar Tools - **HAProxy** — Layer 7 load balancer and proxy; Keepalived provides Layer 4 LVS load balancing and VIP failover that is often used alongside HAProxy - **Pacemaker/Corosync** — Full cluster resource manager for complex HA setups; Keepalived is simpler for IP failover and LVS scenarios - **MetalLB** — Kubernetes-specific load balancer implementation; Keepalived works at the OS level outside of Kubernetes - **UCARP** — Lightweight VRRP alternative; Keepalived adds LVS management and richer health checking ## FAQ **Q: Can Keepalived run in containers?** A: Yes, but the container needs NET_ADMIN capability and host networking to manage virtual IPs and VRRP multicast. **Q: How do I prevent split-brain?** A: Use unicast peers instead of multicast, configure fencing scripts, and ensure reliable network connectivity between nodes. **Q: Does Keepalived work with HAProxy?** A: Yes, a common pattern is using Keepalived for VIP failover between two HAProxy instances for a fully redundant load balancer. **Q: What is the minimum setup for simple failover?** A: Two servers with Keepalived configured as MASTER and BACKUP for the same virtual_router_id, sharing a virtual IP address. ## Sources - https://github.com/acassen/keepalived - https://keepalived.readthedocs.io/ --- Source: https://tokrepo.com/en/workflows/asset-069396be Author: Script Depot