Appearance
日志与监控
HAProxy 提供多层次的日志和监控能力,是生产环境不可或缺的一环。
日志配置
全局日志配置
haproxy
global
log stdout local0 # 输出到 stdout(systemd 环境)
log stdout local1 # 额外日志
log /var/run/log local0 # 输出到 rsyslog socket
maxconn 4096
tune.ssl.default-dh-param 2048defaults 中的日志选项
haproxy
defaults
log global
mode http
option httplog # 启用详细 HTTP 日志
option dontlognull # 不记录空连接
option log-separate-failures # 只记录失败日志
option log-health-checks # 记录健康检查日志详细 HTTP 日志格式(custom log)
haproxy
log-format %ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tr %Tq %ST %B %cc %cs %tsc %rt/%hr/%hs %hcs "%HM" "%HU" "%HV" "%[ssl_c_ver]" "%[ssl_c_sess]"常用日志变量:
| 变量 | 说明 |
|---|---|
%ci | 客户端 IP |
%cp | 客户端端口 |
%t | 请求时间 |
%ft | 前端名称 |
%b | 后端名称 |
%s | 服务器名称 |
%Tw | 等待时间(ms) |
%Tc | 连接建立时间(ms) |
%Tr | 响应时间(ms) |
%ST | HTTP 状态码 |
%B | 字节数 |
%hr | 后端重试次数 |
%HM | HTTP 方法 |
%HU | HTTP URI |
%HV | HTTP 版本 |
%sslc | SSL 相关信息 |
示例:JSON 格式日志
haproxy
log-format {"timestamp":"%t","client_ip":"%ci","method":"%HM","uri":"%HU","status":"%ST","backend":"%b","server":"%s","response_time":"%Tr","bytes":"%B","request_id":"%ID"}'Syslog 转发
配置 rsyslog 接收 HAProxy 日志
bash
# /etc/rsyslog.d/99-haproxy.conf
$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP 127.0.0.1
local0.* /var/log/haproxy/haproxy.log
local0.* @@10.0.0.100:514 # 转发到远程日志服务器重启 rsyslog:
bash
systemctl restart rsyslogHAProxy 中的 syslog 配置
haproxy
global
log /var/run/log local0 local1 info # info 级别以上
# log /var/run/log local0 local1 debug # debug 记录最详细Stats 页面
Stats 页面是 HAProxy 内置的实时监控界面:
haproxy
listen stats
bind *:8404
stats enable
stats uri /stats
stats refresh 30s
stats auth admin:yourpassword
stats realm HAProxy\ Statistics
# stats admin if LOCALHOST # 仅本地可访问管理功能访问 http://your-ip:8404/stats,用设置的账号密码登录。
Stats 页面完整配置
haproxy
listen stats
bind *:8404
stats enable
stats uri /haproxy?stats
stats refresh 10s
stats auth admin:Str0ngPass
stats realm Production\ HAProxy
stats admin if TRUE # 启用管理功能(慎用)
stats show-legends # 显示详细图例
# 只显示特定 frontend/backend
# stats scope front_name backend_namePrometheus 监控
HAProxy 支持 Prometheus 格式指标导出:
haproxy
listen prometheus_metrics
bind *:8405
mode http
http-request use-service prometheus-exporter if { path /metrics }
no logPrometheus 抓取配置:
yaml
# prometheus.yml
scrape_configs:
- job_name: 'haproxy'
static_configs:
- targets: ['haproxy-ip:8405']
metrics_path: /metrics常用 Prometheus 指标
| 指标名 | 说明 |
|---|---|
haproxy_backend_up | 后端是否上线(1=上线) |
haproxy_backend_current_sessions | 当前连接数 |
haproxy_backend_total_requests | 总请求数 |
haproxy_backend_response_errors_total | 响应错误数 |
haproxy_frontend_total_requests | 前端总请求数 |
haproxy_server_check_failed_total | 健康检查失败次数 |
Unix Socket 运维接口
通过 Unix Socket 可以动态查询状态和执行命令:
haproxy
global
stats socket /var/run/haproxy.sock mode 600 level admin连接 Socket
bash
# 使用 socat
echo "show info" | socat stdio /var/run/haproxy.sock
echo "show stat" | socat stdio /var/run/haproxy.sock
echo "show backend" | socat stdio /var/run/haproxy.sock
# 动态禁用/启用服务器
echo "disable server web_pool/web1" | socat stdio /var/run/haproxy.sock
echo "enable server web_pool/web2" | socat stdio /var/run/haproxy.sock
# 查看会话
echo "show sess" | socat stdio /var/run/haproxy.sock常用 Socket 命令
| 命令 | 说明 |
|---|---|
show info | 显示 HAProxy 进程信息 |
show stat | 显示所有 frontend/backend 统计 |
show backend | 列出所有后端 |
show servers state <backend> | 后端服务器实时状态 |
show sess | 当前所有会话 |
disable server <backend>/<server> | 禁用服务器 |
enable server <backend>/<server> | 启用服务器 |
get map | 查看 ACL/map 表 |
clear counters | 清零计数器 |
Grafana 看板
结合 Prometheus + Grafana 可视化 HAProxy 指标:
推荐面板 ID:**367`(HAProxy official dashboard)
常用图表:
- Backend Up/Down:各后端健康状态
- Requests/s:QPS 趋势
- Response Time (p95/p99):响应时间分布
- Error Rate:4xx/5xx 占比
- Active Connections:实时并发连接
- Bytes In/Out:流量带宽
日志分析与故障排查
bash
# 查看错误日志
grep -i error /var/log/haproxy/haproxy.log
# 查看 5xx 错误
grep " 5[0-9][0-9] " /var/log/haproxy/haproxy.log
# 查看某 IP 的所有请求
grep "192.168.1.100" /var/log/haproxy/haproxy.log
# 查看后端故障记录
grep -i "backend.*down\|server.*fail" /var/log/haproxy/haproxy.log
# 实时查看请求
tail -f /var/log/haproxy/haproxy.log | httpry # 需要 httpry 工具