Operations 8 min read

Configuring Keepalived High Availability with Nginx Load Balancing

This article explains how to install and configure Keepalived for high‑availability VRRP failover, set up master and backup configuration files, integrate with two Nginx load balancers, and test failover scenarios using scripts and command‑line tools.

Top Architect
Top Architect
Top Architect
Configuring Keepalived High Availability with Nginx Load Balancing

Keepalived is a high‑availability solution originally designed for LVS but also provides VRRP‑based failover for generic services.

VRRP (Virtual Router Redundancy Protocol) eliminates single‑point failures by allowing a backup node to take over the virtual IP when the master node stops sending heartbeat messages.

Installation on a Linux system is as simple as running yum install keepalived -y , then starting the service and enabling it at boot.

Master node configuration (example) defines global settings, VRRP instance parameters such as state MASTER , interface eth1 , virtual_router_id 55 , priority 150 , authentication, and the virtual IP address 192.168.31.5/24 dev eth1 label eth1:1 .

Backup node configuration mirrors the master but sets state BACKUP and a lower priority 100 , keeping the same virtual_router_id and virtual IP.

Key configuration parameters are explained: router_id must be unique, vrrp_instance defines role, interface, priority, authentication, and virtual IP address.

After editing the configuration files, restart Keepalived; the master will acquire the virtual IP within minutes.

Failover testing involves stopping the master service and verifying that the backup node acquires the virtual IP, then restarting the master to see it reclaim the address.

To combine with Nginx load balancing, two Nginx instances are placed behind the virtual IP; client requests are directed to the VIP, and logs show which backend handles the traffic.

A monitoring script checks whether Nginx is listening on port 80 every two seconds; if Nginx stops, the script stops Keepalived to force the backup node to take over.

#!/bin/bash
while true
do
    if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
    then
        /etc/init.d/keepalived stop
    fi
    sleep 2
done
High Availabilityload balancinglinuxNginxvrrpKeepalived
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.