Operations 9 min read

Implementing Nginx High Availability with Keepalived on Two Virtual Machines

This article explains how to set up a high‑availability Nginx service using Keepalived on two CentOS virtual machines, covering installation, configuration of keepalived, health‑check scripts, virtual IP management, and failover testing to ensure continuous service availability.

Top Architect
Top Architect
Top Architect
Implementing Nginx High Availability with Keepalived on Two Virtual Machines

The guide starts by preparing two virtual machines (192.168.16.128 and 192.168.16.129) with Nginx installed. It updates the yum repository and installs Nginx using the following commands:

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install nginx

After installation, Nginx can be started or stopped with:

systemctl start nginx   # start Nginx
systemctl stop nginx    # stop Nginx

High availability (HA) is introduced as a design goal to minimize service downtime. The article proposes using Keepalived, originally designed for LVS, to provide HA for Nginx via VRRP.

Keepalived is installed with:

yum -y install keepalived

The master node (192.168.16.128) is configured with a keepalived.conf file that defines a health‑check script, VRRP instance, authentication, priority, and a virtual IP (192.168.16.130):

#检测脚本
vrrp_script chk_http_port {
    script "/usr/local/src/check_nginx_pid.sh"   # check Nginx status
    interval 2
    weight 2
}
#vrrp 实例定义部分
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script { chk_http_port }
    virtual_ipaddress { 192.168.16.130 }
}

The backup node (192.168.16.129) uses a similar configuration with state BACKUP and a slightly lower priority:

#检测脚本
vrrp_script chk_http_port {
    script "/usr/local/src/check_nginx_pid.sh"
    interval 2
    weight 2
}
#vrrp 实例定义部分
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script { chk_http_port }
    virtual_ipaddress { 192.168.16.130 }
}

The health‑check script check_nginx_pid.sh monitors the Nginx process and attempts to restart it; if restart fails, it stops Keepalived to trigger VIP takeover:

#!/bin/bash
#检测nginx是否启动了
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ]; then
    systemctl start nginx
    if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then
        killall keepalived
    fi
fi

After setting executable permission ( chmod 775 check_nginx_pid.sh ), the article demonstrates failover: accessing the virtual IP (192.168.16.130) initially returns the page from the master server; stopping Nginx on the master triggers the script to restart it, and if the master goes down completely, the backup automatically takes over the virtual IP, confirming a successful HA setup.

high availabilityLinuxnginxHAkeepalived
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.