Back to articles
DevOps & Systems

Guide to Configuring Keepalived and HAProxy for VIP on Two Hetzner VPS Without Shared Storage

This guide details configuring Keepalived and HAProxy to create a virtual IP (VIP) for two Hetzner VPS servers without shared storage, ensuring high availability and fault tolerance...

DPTCloud Engineering4 min read
Guide to Configuring Keepalived and HAProxy for VIP on Two Hetzner VPS Without Shared Storage

This guide details configuring Keepalived and HAProxy to create a virtual IP (VIP) for two Hetzner VPS servers without shared storage, ensuring high availability and fault tolerance using VRRP and HTTP/HTTPS load balancing. The setup allows one VPS to act as the master and the other as backup, automatically failing over the VIP if the master fails.

Introduction and Objective

In Hetzner VPS environments, shared storage is not supported, making it challenging to maintain a single virtual IP (VIP) for services like web servers or databases. Keepalived, combined with HAProxy, enables the creation of a single access point (VIP) shared between two VPS instances. Using the VRRP protocol, Keepalived synchronizes the VIP state, while HAProxy handles HTTP/HTTPS load balancing. This guide focuses on configuring two Hetzner VPS instances without shared storage, using Keepalived for VIP management and HAProxy for load balancing.

Prerequisites

To follow this guide, you need:

  • Two Hetzner VPS servers (labeled VPS-A and VPS-B) running Linux (Ubuntu 20.04/22.04 or CentOS 8/9).
  • At least two unique IP addresses (one physical IP per VPS and one virtual IP to be created).
  • SSH access to both VPS instances.
  • The public IP addresses of each VPS (e.g., 10.0.0.1 and 10.0.0.2).
  • The virtual IP (VIP) to be used (e.g., 10.0.0.100).

Configuring Keepalived

Keepalived uses the VRRP protocol to manage the virtual IP (VIP). The configuration on both VPS instances must be synchronized, differing only in the `priority` parameter to designate the master VPS.

Install Keepalived on both VPS instances:

# Ubuntu/Debian
sudo apt update
sudo apt install keepalived -y

# CentOS/RHEL
sudo yum install keepalived -y

Modify the `/etc/keepalived/keepalived.conf` file on each VPS.

On VPS-A (master, priority = 100):

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 {
        10.0.0.100/24
    }
}

On VPS-B (backup, priority = 90):

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 {
        10.0.0.100/24
    }
}

Restart the Keepalived service:

sudo systemctl restart keepalived
sudo systemctl enable keepalived

Configuring HAProxy

HAProxy will balance traffic to the VIP. Install HAProxy on both VPS instances:

# Ubuntu/Debian
sudo apt install haproxy -y

# CentOS/RHEL
sudo yum install haproxy -y

Modify the `/etc/haproxy/haproxy.cfg` file:

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
    default_backend http_back

backend http_back
    balance roundrobin
    server server1 10.0.0.1:80 check
    server server2 10.0.0.2:80 check

Restart HAProxy:

sudo systemctl restart haproxy
sudo systemctl enable haproxy

Verification and Testing

Run the following commands to verify the setup:

  • On both VPS instances, check if the VIP is active on the master VPS:
  • Run: ip addr show. The IP 10.0.0.100 should appear on the VPS with the higher priority.
  • On VPS-A, check Keepalived status: sudo systemctl status keepalived.
  • On VPS-B, check Keepalived status: sudo systemctl status keepalived. The state should be BACKUP.
  • Access the VIP (10.0.0.100) via a browser or curl to verify HAProxy is functioning.

Troubleshooting

  • VIP not appearing on master VPS: Verify the `priority`, `state`, and `virtual_router_id` settings. Ensure the `auth_pass` matches on both VPS instances.
  • HAProxy not responding: Check for syntax errors in `haproxy.cfg`. Use haproxy -c -f /etc/haproxy/haproxy.cfg to validate the configuration.
  • Cannot access VIP: Check firewall rules (iptables/nftables) on both VPS instances to ensure ports 80 and 443 are open.

References

For further reading on Keepalived and HAProxy, refer to:

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