HAProxy provides a powerful Runtime API that allows administrators to dynamically modify settings without restarting the service. This is particularly useful for managing backend servers during deployments or maintenance windows.
You can interact with the HAProxy Runtime API through a UNIX socket or TCP socket. First, ensure your HAProxy configuration has the stats socket enabled:
global
stats socket /var/run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
Here are the essential commands for managing backend servers:
# Disable a server
echo "disable server backend_name/server_name" | socat stdio /var/run/haproxy/admin.sock
# Enable a server
echo "enable server backend_name/server_name" | socat stdio /var/run/haproxy/admin.sock
# Put server in maintenance mode (drain)
echo "set server backend_name/server_name state maint" | socat stdio /var/run/haproxy/admin.sock
For deployment workflows, you can create simple shell scripts:
#!/bin/bash
# disable_server.sh
BACKEND=$1
SERVER=$2
SOCKET="/var/run/haproxy/admin.sock"
echo "disable server $BACKEND/$SERVER" | socat stdio $SOCKET
Combine these commands with health checks for more robust automation:
#!/bin/bash
# rolling_deploy.sh
BACKEND="web_servers"
SOCKET="/var/run/haproxy/admin.sock"
for SERVER in server1 server2 server3; do
echo "disable server $BACKEND/$SERVER" | socat stdio $SOCKET
# Run deployment commands here
sleep 30 # Wait for connections to drain
echo "enable server $BACKEND/$SERVER" | socat stdio $SOCKET
done
When implementing these commands in automation:
- Restrict socket permissions (mode 660)
- Use proper authentication if using TCP socket
- Implement error handling in your scripts
You can check server status using:
echo "show servers state" | socat stdio /var/run/haproxy/admin.sock
HAProxy provides a powerful runtime API that lets you dynamically modify server states without restarting the service. This is particularly useful for automation scenarios like deployments or maintenance windows.
The primary method involves sending commands to HAProxy's UNIX socket. First ensure your HAProxy configuration has admin socket enabled:
global stats socket /var/run/haproxy.sock mode 660 level admin stats timeout 30s
For disabling a backend server (maintenance mode):
echo "disable server backend_name/server_name" | socat stdio /var/run/haproxy.sock
To re-enable the server:
echo "enable server backend_name/server_name" | socat stdio /var/run/haproxy.sock
Here's a complete bash script for deployment automation:
#!/bin/bash BACKEND="web_servers" SERVER="web1" SOCKET="/var/run/haproxy.sock" # Put server in maintenance echo "Disabling $SERVER for deployment..." echo "disable server $BACKEND/$SERVER" | socat stdio $SOCKET # Perform deployment tasks here sleep 30 # Simulate deployment # Bring server back online echo "Re-enabling $SERVER..." echo "enable server $BACKEND/$SERVER" | socat stdio $SOCKET
You can check current server states with:
echo "show servers state" | socat stdio /var/run/haproxy.sock
For JSON output (HAProxy 2.4+):
echo "show servers state" | socat stdio /var/run/haproxy.sock | jq .
If using HAProxy Enterprise or compiled with Prometheus support:
curl "http://localhost:9999/v2/services/haproxy/runtime/servers/web1/state" \ -X PUT \ -H "Content-Type: application/json" \ -d '{"admin_state": "maint"}
Always implement proper error handling in your scripts:
if ! echo "disable server $BACKEND/$SERVER" | socat stdio $SOCKET; then echo "ERROR: Failed to disable server" >&2 exit 1 fi