一、基础环境准备
-
服务器规划 67
-
HAProxy调度器:1台 (2核4G+,CentOS 7/8)
-
Web服务器:至少2台(如Nginx/Apache,建议192.168.1.101-102)
-
客户端测试机:1台(Windows/Linux)
-
网络:所有服务器需在同一局域网,关闭防火墙/SELinux
-
-
Web服务器配置
# 在每台Web服务器安装Nginx并创建测试页 yum install -y nginx echo "Server 1" > /usr/share/nginx/html/index.html # 服务器1标识 echo "Server 2" > /usr/share/nginx/html/index.html # 服务器2标识 systemctl start nginx
二、HAProxy安装与核心配置
-
安装依赖与源码编译 78
yum install -y gcc pcre-devel bzip2-devel wget http://www.haproxy.org/download/2.8/src/haproxy-2.8.3.tar.gz tar -zxvf haproxy-2.8.3.tar.gz cd haproxy-2.8.3 make TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 make install
-
配置文件详解 (
/etc/haproxy/haproxy.cfg
)global log /dev/log local0 info # 分离日志级别 maxconn 10000 # 最大连接数 :cite[3] user haproxy group haproxy daemon # 守护进程模式 defaults mode http timeout connect 5s # 连接超时 timeout client 50s # 客户端超时 timeout server 50s # 后端超时 :cite[2] option httplog # 记录HTTP日志 option dontlognull # 忽略空请求 retries 3 # 节点失败重试次数 frontend http-in bind *:80 # 监听所有IP的80端口 acl static path_beg -i /static # ACL规则示例 use_backend static_servers if static default_backend web_servers # 默认后端组 backend web_servers balance leastconn # 最小连接调度算法 :cite[5]:cite[8] server web1 192.168.1.101:80 check inter 2000 rise 2 fall 3 server web2 192.168.1.102:80 check inter 2000 rise 2 fall 3 # check: 健康检查,inter 2000ms检测一次,rise 2次成功标记存活,fall 3次失败标记宕机 :cite[7] backend static_servers server static1 192.168.1.103:80 check
-
创建系统服务
cp examples/haproxy.init /etc/init.d/haproxy chmod +x /etc/init.d/haproxy systemctl daemon-reload
三、关键优化配置
-
日志分离 37
-
修改
/etc/rsyslog.d/haproxy.conf
:if $programname == 'haproxy' then { if $syslogseverity-text == 'info' then /var/log/haproxy-info.log if $syslogseverity-text == 'notice' then /var/log/haproxy-notice.log stop }
-
重启服务:
systemctl restart rsyslog haproxy
-
-
性能调优参数 3
参数 推荐值 作用 maxconn
10000+ 最大并发连接数 nbproc
CPU核数 进程数(需绑定CPU) timeout http-request
5s-10s 防止HTTP慢连接攻击 option httpclose
- 主动关闭HTTP连接释放资源
四、高级功能实现
-
健康检查强化
backend web_servers option httpchk GET /healthcheck # 自定义检查路径 http-check expect status 200 # 要求返回200状态码 :cite[7] server web1 192.168.1.101:80 check port 8080 # 指定检查端口
-
SSL终端卸载
frontend https-in bind *:443 ssl crt /etc/haproxy/certs/example.com.pem redirect scheme https if !{ ssl_fc } # HTTP强制跳转HTTPS default_backend web_servers
-
高可用方案 36
-
部署双HAProxy节点 + Keepalived
-
虚拟IP(VIP)如192.168.1.100
-
配置Keepalived组播地址:224.0.0.18
-
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 virtual_ipaddress { 192.168.1.100 } }
-
五、验证与维护
-
功能测试
# 连续访问测试负载均衡 for i in {1..10}; do curl http://haproxy-ip; done # 模拟节点故障(观察日志) systemctl stop nginx@web1 tail -f /var/log/haproxy-info.log # 查看健康检查日志
-
监控命令
echo "show stat" | socat stdio /var/run/haproxy/admin.sock # 实时状态:cite[2] watch -n 1 "echo 'show info' | socat stdio /var/run/haproxy/admin.sock"
六、故障排查要点
-
常见问题 3
-
503错误:检查后端服务是否存活,
nbproc
是否超限 -
日志不记录:确认rsyslog配置权限,检查
/dev/log
设备存在 -
性能瓶颈:调整
maxconn
,启用option http-server-close
-
生产环境建议:
重要配置变更前执行语法检查:
haproxy -c -f /etc/haproxy/haproxy.cfg
启用多进程时绑定CPU核心:
nbproc 4 cpu-map 1 0 cpu-map 2 1 ...
对于会话保持场景使用
balance source
或cookie SERVERID insert
8