跳至正文
Wener Notes

HAProxy Management & Operations

约 2 分钟阅读
标签:DevOps网站HAProxy运维DashboardPrometheus

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

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

Execution

bash
# 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:

bash
haproxy -vv | grep 'Prometheus'

Stats & Metrics Frontend

text
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):

bash
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

bash
# 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

关联信息

反向链接、本文链接的其他页面和外部资料。

参考资料

GitHub1 条
其他外链8 条
最近更新commit d77e4c2编辑