Keepalived

On this page 7

Keepalived is an open-source Linux daemon that provides high availability through VRRP failover of virtual IP addresses and health checking for the kernel IPVS load balancer. In load balancing and networking, it is the classic way to remove the single point of failure from a pair of load balancers: two or more nodes share a floating virtual IP, and when the active node fails, a standby node claims that address within seconds.

Keepalived is written in C, configured through a single file (usually /etc/keepalived/keepalived.conf), and commonly deployed in front of HAProxy, NGINX, database replicas and Kubernetes control planes. It does not proxy traffic itself; it decides which machine owns the address and, optionally, which real servers receive connections.

How Keepalived implements VRRP failover

Keepalived implements the Virtual Router Redundancy Protocol (VRRP), defined in RFC 3768 for version 2 and RFC 5798 for version 3. Nodes that protect the same address form a virtual router, identified by a virtual router ID from 1 to 255 that must be unique on the network segment. Each node has a priority, and the node with the highest priority becomes MASTER while the others remain BACKUP.

The master sends VRRP advertisements as IP protocol 112 to the multicast group 224.0.0.18, by default once per second. When backups stop hearing advertisements for roughly three advertisement intervals plus a skew time, the highest-priority backup promotes itself, adds the virtual IP to its interface and sends gratuitous ARP (or IPv6 unsolicited neighbor advertisements) so that switches and hosts update their tables. A typical failover therefore completes in about 3 to 4 seconds with default timers.

The following table summarizes the parameters that define a VRRP instance in Keepalived and what each one controls.

ParameterTypical valueWhat it controls
virtual_router_id1-255, unique per segmentWhich nodes form one virtual router
priority1-254 (255 for address owner)Which node becomes master
advert_int1 secondAdvertisement frequency and failover speed
stateMASTER or BACKUPInitial role at startup
nopreemptOff by defaultWhether a recovered node takes the address back
unicast_peerPeer IP listAdvertisements without multicast
authenticationPASS or AH (VRRPv2 only)Basic advertisement validation
Core VRRP instance parameters

Health checking and the IPVS integration

The second half of Keepalived manages Linux Virtual Server (LVS), the kernel IPVS module that performs Layer 4 load balancing. In virtual_server blocks, an administrator defines a service address, a scheduling algorithm such as round robin, weighted round robin or least connections, a forwarding method (NAT, direct routing or tunneling) and a list of real servers.

A checker process probes each real server and removes it from the IPVS table after a configurable number of failures, then restores it after recovery. This keeps the server pool accurate without manual intervention. The available checkers differ in depth, as the next table shows.

CheckerWhat it testsTypical use
TCP_CHECKTCP handshake on a portGeneric services, databases
HTTP_GETHTTP status and optional content digestWeb applications
SSL_GETSame as HTTP_GET over TLSHTTPS back ends
SMTP_CHECKSMTP greeting and HELOMail relays
DNS_CHECKAnswer to a DNS queryResolvers
MISC_CHECKExit code of a custom scriptAny service-specific logic
Keepalived real server checkers

Separately, vrrp_script and track_script blocks let a VRRP instance watch a local process. A script such as pidof haproxy or a curl against a local health endpoint runs every few seconds; on failure it either forces the instance into a FAULT state or subtracts a weight from the priority, which triggers failover to a healthier node.

Keepalived solves address ownership, not traffic distribution at Layer 7, which places it beside rather than against proxies such as HAProxy or Envoy Proxy. The table contrasts it with neighboring approaches.

ApproachPrimary roleFailover scopeTypical trade-off
KeepalivedVRRP virtual IP plus IPVS checksOne Layer 2 segmentSimple, fast, but needs shared broadcast domain
Pacemaker with CorosyncGeneral cluster resource managerAny resource with agentsPowerful fencing, far more complex
HAProxy aloneLayer 7 and Layer 4 proxyNone for itselfStill a single point of failure
DNS load balancingSpreads clients across addressesGlobal, slow to convergeDepends on TTL and client caching
Cloud load balancerManaged virtual IP and health checksProvider regionNo VRRP needed, less control
Keepalived and alternative availability approaches

A frequent pattern combines the tools: two HAProxy nodes share one Keepalived virtual IP, HAProxy performs Layer 7 load balancing and TLS termination, and Keepalived moves the address when HAProxy or the host fails.

Deploying Keepalived in production

A sound deployment starts with clear priorities and explicit preemption behavior. Many operators set both nodes to BACKUP with different priorities and enable nopreempt, so a recovered node does not cause a second, unnecessary failover. Kernel settings such as net.ipv4.ip_nonlocal_bind=1 allow services to bind to the virtual IP before the node owns it.

Typical deployment decisions depend on the environment, as summarized below.

SituationRecommended choiceReason
Multicast blockedunicast_src_ip and unicast_peerAdvertisements reach peers directly
Public cloud networkUnicast plus notify script calling provider APIGratuitous ARP is ignored by the virtual network
Two load balancer nodesTrack the proxy process with vrrp_scriptFail over on service failure, not only host failure
Active-active neededTwo instances with two virtual IPsEach node masters one address
Stateful servicesnotify_master scripts and conntrackdRestore state or sessions after failover
Configuration choices by environment

Notify scripts (notify_master, notify_backup, notify_fault) run on state transitions and are the hook for alerting, route updates or cloud API calls that reassign an elastic address.

Limitations and common mistakes

The most serious risk is split brain, where both nodes believe they are master and announce the same address. It usually results from a firewall dropping protocol 112, a broken link between nodes, or mismatched virtual router IDs. Keepalived has no built-in fencing, so it cannot power off a misbehaving peer the way Pacemaker can.

  • Duplicate virtual router IDs on one VLAN cause unrelated clusters to interfere with each other.
  • VRRPv2 password authentication is plain text and was removed in VRRPv3, so it provides no real security.
  • Overly aggressive timers below one second can produce flapping on busy or virtualized hosts.
  • Checking only the host, not the proxy process, leaves traffic on a node whose service has crashed.
  • Existing TCP connections are lost on failover unless state is synchronized, for example with IPVS sync daemons.

The simple explanation

Keepalived lets two servers share one public address so that visitors never notice when one of them breaks. Only one server holds the address at a time, and it keeps telling its partner that it is alive.

If those messages stop, the partner takes over the address within a few seconds. It works like a relay team where the second runner is already standing on the track, ready to grab the baton the moment the first runner stumbles.

How it is used in practice

An online retailer runs two HAProxy servers in one data center, both connected to the same VLAN. The address 203.0.113.10, which its DNS record points to, is a Keepalived virtual IP. Node A has priority 150, node B has priority 100, advertisements run every second, and a vrrp_script checks every two seconds that HAProxy is running, subtracting 60 from the priority on failure.

During a routine kernel update, HAProxy on node A stops. Its effective priority drops to 90, node B becomes master, sends gratuitous ARP and starts answering on 203.0.113.10. Checkout traffic resumes in about three seconds, only a few in-flight requests are retried by browsers, and the team completes the update without a maintenance window.