HAProxy for Heimdall Proxy Load Balancing

Introduction

HAProxy is a reliable and high-performance TCP/HTTP load balancer that can be used to distribute traffic across multiple backend servers like Heimdall Proxy. This guide provides a step-by-step approach to installing and configuring HAProxy on an Ubuntu system for Heimdall Proxies load balancing.

Prerequisites

  • A system running Linux distribution.
  • Heimdall Proxy installed on the backend servers.

Step 1: Install HAProxy

First, update the package list and install HAProxy using the following command:

sudo apt update && sudo apt upgrade -y
sudo apt install haproxy -y

Verify the installation:

haproxy -v

Enable HAProxy to start on system boot:

sudo systemctl enable haproxy

Step 2: Configure HAProxy for Heimdall Load Balancing

Edit HAProxy Configuration File

Open the HAProxy configuration file:

sudo nano /etc/haproxy/haproxy.cfg

Replace the existing content or append the following configuration (adjusting for hosts and IP addresses):

defaults
    mode    tcp

frontend ft_postgres
    bind *:5432
    default_backend bk_postgres

frontend stats
    mode http
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 10s

backend bk_postgres
    balance roundrobin
    option httpchk GET /status
    http-check expect status 200
    server pg-proxy1 192.168.90.4:5432 check port 80
    server pg-proxy2 192.168.90.5:5432 check port 80

global
    stats socket /var/run/haproxy.sock mode 600 level admin

Explanation:

  • mode tcp ensures HAProxy works in TCP mode, required for PostgreSQL connections.
  • bind *:5432 instructs HAProxy to listen on port 5432.
  • balance roundrobin enables equal distribution of connections across backend servers.
  • option httpchk GET /status set the HTTP healthcheck URL
  • check port <port> enables health checks to monitor backend server availability using the http health check on the specified port (as configured in the vdb advanced settings).

Save the file (CTRL + X, then Y and Enter).

Step 3: Restart and Verify HAProxy

Restart HAProxy to apply the new configuration:

sudo systemctl restart haproxy

Check HAProxy’s status:

sudo systemctl status haproxy

Ensure HAProxy is listening on port 5432:

ss -tulnp | grep haproxy

Step 4: Test Heimdall Proxy Connection via HAProxy

To verify the setup, try connecting to Heimdall Proxy through HAProxy:

psql -h <haproxy_server_ip> -p 5432 -U <db_user> -d <database_name>

Replace <haproxy_server_ip> with the HAProxy host IP.

Replace <db_user> and <database_name> accordingly.

Note:

Heimdall Proxy instances running on 192.168.88.121 and 192.168.88.120, ensuring high availability.

Conclusion

This guide provides the essential steps to install and configure HAProxy for PostgreSQL load balancing with Heimdall Proxy. By following these steps, you can ensure high availability, efficient load distribution, and failover support for your PostgreSQL database cluster.