Operations 5 min read

How to Install and Configure Keepalived for Single‑VIP High Availability on CentOS 7

This guide walks through downloading the Keepalived source, installing required dependencies, compiling and installing the software on a CentOS 7.9 server, then configuring a preemptive single‑VIP setup with a health‑check script for Nginx, while addressing common pitfalls.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Install and Configure Keepalived for Single‑VIP High Availability on CentOS 7

Background : Server runs CentOS 7.9.

1. Install Keepalived from source

Download the source package from the official site:

https://www.keepalived.org/download.html

Extract it on the server: tar -xf keepalived-2.2.8.tar.gz Install required build tools: yum -y install gcc gcc-c++ Configure the build with custom directories:

./configure --sysconfdir=/etc \
            --prefix=/usr/local/keepalived \
            --sbindir=/usr/sbin \
            --bindir=/usr/bin

Compile and install:

make && make install

2. Configure Keepalived for a single‑VIP preemptive setup

The configuration below assumes two virtual machines share one virtual IP (VIP). When the primary host fails, the VIP migrates to the backup; when the primary recovers, it re‑acquires the VIP.

global_defs {
    router_id logincas1   # unique identifier for this node
    enable_script_security
    script_user root      # user that runs scripts
    vrrp_version 2
}

vrrp_script chk_nginx {
    script "/usr/local/nginx-check.sh"
    interval 2           # check interval (seconds)
    timeout 4            # fail after two missed checks
    rise 4               # consider healthy after four successes
    fall 2               # consider failed after two failures
    weight -50           # reduce weight if script reports failure
}

vrrp_instance VI_1 {
    version 2
    state MASTER         # primary host; backup hosts use BACKUP
    interface eth0       # network interface for the VIP
    virtual_router_id 51
    priority 100         # backup should have a lower priority
    advert_int 1         # VRRP advertisement interval
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
        172.29.184.220/24
    }
    track_script {
        chk_nginx
    }
}

nginx‑check.sh script

This script restarts Nginx if it is not running; if Nginx remains absent after a short wait, it stops Keepalived so the VIP can fail over.

#!/bin/bash
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then
    systemctl restart nginx
    sleep 1
    if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then
        systemctl stop keepalived
    fi
fi

Common issues

Using ps aux | grep nginx inside the script may fail due to permission restrictions of the Keepalived user; Keepalived warns if the script is made world‑writable (777).

The single‑VIP architecture can be extended to a dual‑VIP setup where each node has its own VIP, or both VIPs listen to traffic depending on business needs.

To keep the VIP on the primary after it recovers, set state BACKUP on the primary but give it a higher priority than the backup.

Source: https://www.cnblogs.com/iamxiaofu/p/17848680.html (© original author)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

VRRPkeepalived
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.