Back to articles
DevOps & Systems

Optimizing and Configuring a High Availability Reverse Proxy Cluster with HAProxy and Keepalived on VPS

This guide walks through setting up a high availability reverse proxy cluster using HAProxy and Keepalived on two VPS servers to ensure maximum service uptime. It covers protocol...

DPTCloud Engineering5 min read
Optimizing and Configuring a High Availability Reverse Proxy Cluster with HAProxy and Keepalived on VPS

This guide walks through setting up a high availability reverse proxy cluster using HAProxy and Keepalived on two VPS servers to ensure maximum service uptime. It covers protocol analysis, virtual IP setup, HAProxy load balancing, and Keepalived failover management. The setup applies to NVMe Cloud VPS environments for Docker, Nginx & Databases NVMe Cloud VPS for Docker, Nginx & Databases.

Overview of the Solution

A reverse proxy acts as an intermediary between users and backend servers, enabling load balancing, server protection, and improved performance. In a VPS environment, deploying a high availability (HA) reverse proxy cluster prevents single points of failure. HAProxy is widely used for load balancing HTTP/HTTPS and other protocols, while Keepalived manages the virtual IP (VIP) to enable fast failover between servers.

The solution uses two VPS servers (VPS1 and VPS2), each with a public IP address. A virtual IP (VIP) is assigned to the active server. Keepalived monitors the HAProxy status on both servers and transfers the VIP during failures. HAProxy distributes traffic to backend servers (e.g., Nginx, Apache, or Docker applications).

Requirements and Preparation

To deploy this solution, you need:

  • Two VPS servers (minimum 2GB RAM, 1 CPU, 20GB SSD) with Ubuntu 20.04 or 22.04.
  • A public IP address for each VPS server.
  • A virtual IP (VIP) that is not in use, managed by the VPS provider (or a private IP if supported).
  • SSH access to both servers.
  • Network connectivity between the two servers (typically over the Internet or private network).

Assume the server IP addresses are:

  • VPS1: 192.168.1.10
  • VPS2: 192.168.1.11
  • VIP: 192.168.1.100

Configuring HAProxy and Keepalived

Perform the following steps on both servers (VPS1 and VPS2).

1. Install and Configure HAProxy

On each server, install HAProxy:

sudo apt update
sudo apt install haproxy -y

Modify the HAProxy configuration file at `/etc/haproxy/haproxy.cfg`:

global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5000
    timeout client  50000
    timeout server  50000

frontend http_front
    bind *:80
    bind *:443 ssl crt /etc/haproxy/certs/
    default_backend http_back

backend http_back
    option httpchk GET /health
    http-check expect status 200
    server server1 192.168.1.20:80 check
    server server2 192.168.1.21:80 check

Replace `192.168.1.20` and `192.168.1.21` with your backend server IPs. If no backend is present, HAProxy can serve as a simple reverse proxy.

2. Install and Configure Keepalived

On each server, install Keepalived:

sudo apt install keepalived -y

Modify the Keepalived configuration file at `/etc/keepalived/keepalived.conf`.

On VPS1 (master):

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.1.100
    }
}

On VPS2 (backup):

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.1.100
    }
}

Ensure `interface eth0` is the correct network interface. The priority on VPS1 is higher to make it the master. The `virtual_ipaddress` is the shared VIP.

Testing and Verification

After configuration, restart the services:

sudo systemctl restart haproxy
sudo systemctl restart keepalived

Check Keepalived status:

sudo keepalived -v
sudo systemctl status keepalived

Verify the VIP assignment:

ip addr show

The virtual IP (192.168.1.100) should appear on the server with the MASTER state.

Troubleshooting

If the VIP is not assigned, check the following:

  • Ensure both servers can communicate over the network.
  • Verify Keepalived configuration for syntax errors.
  • Run `sudo journalctl -u keepalived` to check logs.
  • Confirm HAProxy is running on both servers.
  • Some VPS providers do not support virtual IPs on public networks. In such cases, consider using a provider's VIP service or DNS failover.

References

For more information on HAProxy and Keepalived, refer to:

Related infrastructure & engineering guides: Optimizing CI/CD Development Process for iOS Apps with macOS on Proxmox KVM

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478