Operations 9 min read

How to Build a 4‑Layer Reverse Proxy Cluster with Nginx and keepalived

This guide walks through planning a four‑layer reverse‑proxy cluster, installing Nginx, configuring load‑balancing streams, setting up keepalived high‑availability with master/backup modes, testing failover, and troubleshooting common keepalived issues on Linux servers.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a 4‑Layer Reverse Proxy Cluster with Nginx and keepalived

Four‑Layer Reverse Proxy Cluster Planning

mfyxw10.mfyxw.com – 4‑layer load balancer (master) – 192.168.80.10

mfyxw20.mfyxw.com – 4‑layer load balancer (backup) – 192.168.80.20

Two Modes in keepalived

(1) master->backup mode
Once the master fails, the virtual IP automatically moves to the backup; when the master recovers, keepalived will reclaim the virtual IP even if nopreempt is set.
(2) backup->backup mode
When the master fails, the virtual IP moves to the backup, but after the original master recovers it will not preempt the IP, even with higher priority; the repaired master is typically used as the new backup.

1. Install Nginx for Reverse Proxy

# On mfyxw10 host
yum -y install nginx

# On mfyxw20 host
yum -y install nginx

2. Provide Reverse Proxy Configuration

# Append to /etc/nginx/nginx.conf on both hosts
stream {
    upstream kube-apiserver {
        server 192.168.80.30:6443 max_fails=3 fail_timeout=30s;
        server 192.168.80.40:6443 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 7443;
        proxy_connect_timeout 2s;
        proxy_timeout 900s;
        proxy_pass kube-apiserver;
    }
}

3. Verify Configuration and Start Nginx

# Test configuration and enable service on both hosts
nginx -t
systemctl enable --now nginx
systemctl status nginx

4. Install keepalived for High Availability

# Install on both hosts
yum -y install keepalived

5. keepalived Monitoring Script

# /etc/keepalived/check_port.sh
#!/bin/bash
CHK_PORT=$1
if [ -n "$CHK_PORT" ]; then
    PORT_PROCESS=`ss -lnt|grep $CHK_PORT|wc -l`
    if [ $PORT_PROCESS -eq 0 ]; then
        echo "Port $CHK_PORT Is Not Used, End."
        exit 1
    fi
else
    echo "Check Port Cant Be Empty!"
    exit 1
fi

6. keepalived Configuration for Master

# /etc/keepalived/keepalived.conf on mfyxw10
global_defs {
    router_id 192.168.80.10
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_port.sh 7443"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 251
    priority 100
    advert_int 1
    mcast_src_ip 192.168.80.10
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_script { chk_nginx }
    virtual_ipaddress { 192.168.80.100 }
}

7. keepalived Configuration for Backup

# /etc/keepalived/keepalived.conf on mfyxw20
global_defs {
    router_id 192.168.80.20
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_port.sh 7443"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 251
    priority 90
    advert_int 1
    mcast_src_ip 192.168.80.20
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_script { chk_nginx }
    virtual_ipaddress { 192.168.80.100 }
}

8. Start keepalived and Verify

# On each host
systemctl enable --now keepalived
nginx -s reload
netstat -luntp | grep 7443
ip addr

9. Simulate Network Failure

# Stop keepalived on master, check VIP moves to backup
systemctl stop keepalived
ip addr   # on both hosts
# Restart keepalived on master, verify VIP returns
systemctl start keepalived
ip addr

10. Common keepalived Issues

systemctl cannot fully stop keepalived

When using yum to install keepalived (version keepalived‑1.3.5‑16.el7.x86_64), systemctl stop keepalived may fail, leaving the process running and causing “Can’t open PID file /var/run/keepalived.pid (yet?) after start”.

# Edit service file
vi /lib/systemd/system/keepalived.service
# Comment out or remove the line:
# KillMode=process
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.

high availabilityload balancingNginxreverse proxykeepalived
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.