Skip to main content

HAProxy Management & Operations

Detailed guide on managing HAProxy instances, reloads, monitoring, and APIs.

Documentation

Hitless Reload

To achieve seamless reloads without losing connections using SIGUSR2 or the -x flag.

Configuration

# Enable stats socket for hitless reload
stats socket /var/run/haproxy.sock mode 600 expose-fd listeners level user

Execution

# Graceful restart/reload using the previous process ID and the state socket
haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -sf $(cat /run/haproxy.pid) -x /var/run/haproxy.sock

Monitoring & Stats

Check if Prometheus is supported:

haproxy -vv | grep 'Prometheus'

Stats & Metrics Frontend

frontend stats
bind *:8404
mode http
option http-use-htx

# Prometheus metrics
http-request use-service prometheus-exporter if { path /metrics }

# Traditional stats page
stats enable
stats uri /stats
stats refresh 10s

Runtime Commands

Interact with HAProxy via socket (requires socat):

echo "disable frontend website" | sudo socat stdio /run/haproxy.sock

Data Plane API

The Data Plane API allows dynamic configuration changes via REST.

Example setup

# Create a restart script for the API to call
echo -e '#!/bin/sh\nhaproxy -f haproxy.cfg -p haproxy.pid -sf $(cat haproxy.pid)' > hap-restart.sh
chmod +x hap-restart.sh

# Run Data Plane API
dataplaneapi --port 5555 -b $(which haproxy) -c haproxy.cfg -d 5 -r 'hap-restart.sh' \
-s 'haproxy -f haproxy.cfg -p haproxy.pid' -t /tmp/haproxy --userlist hap-dp

# Test API
curl -X GET --user dataplaneapi:mypassword http://localhost:5555/v2/info