插件窝 干货文章 如何在Linux上配置软件负载均衡(如HAProxy)

如何在Linux上配置软件负载均衡(如HAProxy)

HAProxy HTTP 配置 stats 328    来源:    2025-04-16

在Linux上配置HAProxy实现软件负载均衡

HAProxy是一款高性能的TCP/HTTP负载均衡器,广泛用于高可用性和负载均衡场景。以下是详细的配置步骤:

1. 安装HAProxy

Ubuntu/Debian系统

sudo apt update
sudo apt install haproxy

CentOS/RHEL系统

sudo yum install haproxy

从源码编译安装(最新版本)

wget http://www.haproxy.org/download/2.8/src/haproxy-2.8.3.tar.gz
tar -xzvf haproxy-2.8.3.tar.gz
cd haproxy-2.8.3
make TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1
sudo make install

2. 基本配置

编辑主配置文件(通常位于/etc/haproxy/haproxy.cfg):

sudo nano /etc/haproxy/haproxy.cfg

最小配置示例

global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5000
    timeout client 50000
    timeout server 50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

frontend http_front
    bind *:80
    stats uri /haproxy?stats
    default_backend http_back

backend http_back
    balance roundrobin
    server server1 192.168.1.101:80 check
    server server2 192.168.1.102:80 check
    server server3 192.168.1.103:80 check

3. 高级配置选项

负载均衡算法

backend http_back
    balance roundrobin     # 轮询(默认)
    # balance leastconn    # 最少连接
    # balance source       # 源IP哈希
    # balance uri          # URI哈希

SSL终止

frontend https_front
    bind *:443 ssl crt /etc/ssl/private/example.com.pem
    http-request redirect scheme https unless { ssl_fc }
    default_backend http_back

健康检查

backend http_back
    option httpchk GET /health
    http-check expect status 200
    server server1 192.168.1.101:80 check inter 2000 rise 2 fall 3

会话保持

backend http_back
    cookie SERVERID insert indirect nocache
    server server1 192.168.1.101:80 cookie s1 check
    server server2 192.168.1.102:80 cookie s2 check

4. 启动和管理HAProxy

启动服务

sudo systemctl start haproxy

设置开机启动

sudo systemctl enable haproxy

检查状态

sudo systemctl status haproxy

重新加载配置(不中断服务)

sudo systemctl reload haproxy
# 或
sudo haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -sf $(cat /run/haproxy.pid)

5. 监控和管理

访问统计页面

在浏览器中访问:http://your-haproxy-server/haproxy?stats

日志配置

确保rsyslog配置中包含:

local0.* /var/log/haproxy.log

然后重启rsyslog:

sudo systemctl restart rsyslog

6. 安全建议

  1. 限制统计页面的访问:
frontend stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 10s
    stats auth admin:securepassword
    acl network_allowed src 192.168.1.0/24
    http-request deny unless network_allowed
  1. 启用基本认证:
userlist AuthUsers
    user admin insecure-password yourpassword
  1. 使用ACL限制访问:
acl restricted_page path_beg /admin
acl allowed_ips src 192.168.1.100 192.168.1.200
http-request deny if restricted_page !allowed_ips

7. 性能调优

  1. 增加最大连接数(在global部分):
maxconn 50000
  1. 调整内核参数:
echo "net.ipv4.tcp_max_syn_backlog = 10240" >> /etc/sysctl.conf
echo "net.core.somaxconn = 10240" >> /etc/sysctl.conf
sysctl -p
  1. 启用多线程(HAProxy 1.8+):
global
    nbthread 4

通过以上配置,您可以在Linux系统上搭建一个高性能的软件负载均衡器,根据实际需求调整配置参数以获得最佳性能。