1. HAProxy基础安装与环境准备
HAProxy作为一款高性能的负载均衡软件,在Linux环境下部署非常简便。以下是在CentOS/RHEL系统上的标准安装流程:
bash复制# 更新系统包缓存
sudo dnf update -y
# 安装HAProxy主程序
sudo dnf install haproxy.x86_64 -y
# 设置开机自启并立即启动服务
sudo systemctl enable --now haproxy
注意:如果使用的是Ubuntu/Debian系统,需要将dnf替换为apt-get,其他命令保持不变。
安装完成后,建议进行以下基础检查:
- 验证安装版本:
haproxy -v - 检查服务状态:
systemctl status haproxy - 查看监听端口:
ss -tulnp | grep haproxy
2. HAProxy核心配置文件解析
2.1 配置文件结构剖析
HAProxy的主配置文件通常位于/etc/haproxy/haproxy.cfg,由以下几个关键部分组成:
code复制global
# 全局配置参数
log /dev/log local0
maxconn 4000
user haproxy
group haproxy
daemon
defaults
# 默认参数设置
mode http
log global
option httplog
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend
# 前端服务定义
bind *:80
default_backend web_servers
backend
# 后端服务器组配置
server server1 192.168.1.10:80 check
server server2 192.168.1.11:80 check
2.2 常用配置参数详解
负载均衡算法配置
roundrobin:轮询调度(默认)static-rr:加权轮询leastconn:最小连接数source:源IP哈希
示例配置:
code复制backend web_servers
balance leastconn
server web1 192.168.1.10:80 check weight 3
server web2 192.168.1.11:80 check weight 1
健康检查参数
check:启用健康检查inter 2000:检查间隔(ms)rise 2:成功次数标记为UPfall 3:失败次数标记为DOWNport 8080:指定检查端口
3. 实战配置案例
3.1 基础HTTP负载均衡
bash复制frontend http_front
bind *:80
mode http
default_backend http_back
backend http_back
mode http
balance roundrobin
server web1 192.168.1.10:80 check inter 2000 rise 2 fall 3
server web2 192.168.1.11:80 check inter 2000 rise 2 fall 3
server web3 192.168.1.12:80 check inter 2000 rise 2 fall 3
3.2 TCP负载均衡配置
bash复制frontend mysql_front
bind *:3306
mode tcp
default_backend mysql_back
backend mysql_back
mode tcp
balance leastconn
server mysql1 192.168.1.20:3306 check inter 2000 rise 2 fall 3
server mysql2 192.168.1.21:3306 check inter 2000 rise 2 fall 3
4. 高级功能配置
4.1 ACL规则应用
bash复制frontend http_front
bind *:80
acl is_static path_beg -i /static /images /javascript
acl is_dynamic path_beg -i /api /service
use_backend static_servers if is_static
use_backend dynamic_servers if is_dynamic
default_backend web_servers
backend static_servers
server static1 192.168.1.30:80 check
backend dynamic_servers
server dynamic1 192.168.1.31:8080 check
4.2 SSL终端配置
bash复制frontend https_front
bind *:443 ssl crt /etc/ssl/certs/mydomain.pem
mode http
default_backend web_servers
backend web_servers
server web1 192.168.1.10:80 check
server web2 192.168.1.11:80 check
5. 日志配置与管理
5.1 配置远程日志
- 修改HAProxy配置:
bash复制global
log 192.168.1.100:514 local0
- 在日志服务器配置rsyslog:
bash复制# 编辑/etc/rsyslog.conf
module(load="imudp")
input(type="imudp" port="514")
# 添加HAProxy日志规则
local0.* /var/log/haproxy.log
- 重启服务:
bash复制systemctl restart rsyslog
systemctl restart haproxy
5.2 日志格式解析
典型HAProxy日志条目:
code复制Nov 12 14:32:45 localhost haproxy[1437]: 10.0.0.1:54321 [12/Nov/2023:14:32:45.123] http_front http_back/web1 0/0/1/12/13 200 1450 - - ---- 1/1/0/0/0 0/0 "GET /index.html HTTP/1.1"
各字段含义:
- 客户端IP和端口
- 请求时间
- 前端名称
- 后端名称/服务器名称
- 各阶段耗时(ms)
- HTTP状态码
- 字节数
- 请求方法、URI和协议
6. 性能调优与监控
6.1 关键性能参数
bash复制global
maxconn 50000 # 最大连接数
nbproc 4 # 工作进程数
nbthread 8 # 每进程线程数
tune.ssl.default-dh-param 2048 # SSL参数
defaults
timeout connect 5s # 连接超时
timeout client 50s # 客户端超时
timeout server 50s # 服务器超时
6.2 监控接口配置
bash复制listen stats
bind *:1936
mode http
stats enable
stats uri /haproxy?stats
stats realm HAProxy\ Statistics
stats auth admin:password
stats hide-version
访问方式:http://your-server:1936/haproxy?stats
7. 常见问题排查
7.1 连接问题排查流程
- 检查HAProxy服务状态:
bash复制systemctl status haproxy
- 验证端口监听:
bash复制ss -tulnp | grep haproxy
- 检查后端服务器连通性:
bash复制curl -v http://backend-server:port
- 查看实时日志:
bash复制tail -f /var/log/haproxy.log
7.2 典型错误解决方案
问题1:503 Service Unavailable
- 检查后端服务器健康状态
- 验证backend配置中的服务器地址和端口
- 检查防火墙规则
问题2:SSL握手失败
- 确保证书路径正确
- 验证证书链完整性
- 检查SSL协议版本兼容性
问题3:性能瓶颈
- 调整maxconn参数
- 增加工作进程数
- 优化超时设置
8. 安全加固建议
- 限制管理接口访问:
bash复制listen stats
bind 127.0.0.1:1936
stats auth admin:complex_password
- 启用连接限制:
bash复制frontend http_front
bind *:80
maxconn 3000
stick-table type ip size 100k expire 30s store conn_rate(3s)
tcp-request connection reject if { src_conn_rate ge 50 }
- 定期更新HAProxy版本:
bash复制dnf update haproxy
- 配置适当的日志轮转:
bash复制# /etc/logrotate.d/haproxy
/var/log/haproxy.log {
daily
rotate 30
compress
missingok
notifempty
sharedscripts
postrotate
/bin/systemctl reload haproxy >/dev/null 2>&1 || true
endscript
}
在实际生产环境中部署HAProxy时,建议先在小规模测试环境验证配置,然后逐步扩展到生产环境。配置变更后,可以使用haproxy -c -f /etc/haproxy/haproxy.cfg命令检查配置文件语法是否正确,然后再重启服务。