Skip to content

日志与监控

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 2048

defaults 中的日志选项

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)
%STHTTP 状态码
%B字节数
%hr后端重试次数
%HMHTTP 方法
%HUHTTP URI
%HVHTTP 版本
%sslcSSL 相关信息

示例: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 rsyslog

HAProxy 中的 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_name

Prometheus 监控

HAProxy 支持 Prometheus 格式指标导出:

haproxy
listen prometheus_metrics
    bind *:8405
    mode http
    http-request use-service prometheus-exporter if { path /metrics }
    no log

Prometheus 抓取配置:

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 工具