1. 首页 > IT综合教程 > 正文

258. HAProxy负载均衡培训

一、HAProxy概述

HAProxy是高性能的负载均衡软件,支持TCP和HTTP两种代理模式,广泛用于Web应用和数据库负载均衡场景。

from 培训视频:www.itpux.com

1.1 HAProxy核心特性

  • 高性能:单机支持数万并发连接
  • 多协议支持:TCP和HTTP两种代理模式
  • 丰富调度算法:支持多种负载均衡算法
  • 健康检查:支持TCP、HTTP等多种检查方式

二、HAProxy安装部署

2.1 YUM安装

# 安装HAProxy
yum install -y haproxy
# 查看版本
haproxy -v
# 启动服务
systemctl start haproxy
systemctl enable haproxy
# 查看状态
systemctl status haproxy
# 输出示例
HAProxy 2.4.22-f8e4215 2023/12/15 – https://haproxy.org/
Build options :
TARGET = linux-glibc
CPU = generic
CC = cc
CFLAGS = -O2 -g -Wall -Wextra -Wdeclaration-after-statement

2.2 源码编译安装

# 安装依赖
yum install -y gcc pcre-devel zlib-devel openssl-devel
# 下载源码
wget https://www.haproxy.org/download/2.8/src/haproxy-2.8.3.tar.gz
tar -xzf haproxy-2.8.3.tar.gz
cd haproxy-2.8.3
# 编译安装
make TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1
make install PREFIX=/usr/local/haproxy
# 创建配置目录
mkdir -p /usr/local/haproxy/conf
mkdir -p /var/lib/haproxy
# 创建用户
useradd -r -s /sbin/nologin haproxy
# 创建systemd服务
cat > /etc/systemd/system/haproxy.service << 'EOF'
[Unit]
Description=HAProxy Load Balancer
After=network.target
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -c
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /usr/local/haproxy/conf/haproxy.cfg
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
User=haproxy
Group=haproxy
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload

三、主配置文件

3.1 基本配置结构

# /etc/haproxy/haproxy.cfg
global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    stats socket /var/lib/haproxy/stats mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon
    maxconn 4000
defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5000
    timeout client  50000
    timeout server  50000
    retries 3
    option redispatch
    maxconn 3000
# 统计页面
frontend stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 10s
    stats admin if LOCALHOST

四、HTTP负载均衡

4.1 基本HTTP配置

# HTTP负载均衡配置
frontend http_front
    bind *:80
    mode http
    
    # ACL规则
    acl url_static path_beg -i /static /images /javascript /stylesheets
    acl url_static path_end -i .jpg .gif .png .css .js
    
    # 使用不同后端
    use_backend static_servers if url_static
    default_backend web_servers
backend web_servers
    balance roundrobin
    option httpchk GET /health
    http-check expect status 200
    
    server web1 192.168.1.101:80 check inter 2000 rise 2 fall 3 weight 3
    server web2 192.168.1.102:80 check inter 2000 rise 2 fall 3 weight 2
    server web3 192.168.1.103:80 check inter 2000 rise 2 fall 3 weight 1
backend static_servers
    balance roundrobin
    
    server static1 192.168.1.201:80 check
    server static2 192.168.1.202:80 check

4.2 HTTPS配置

# HTTPS配置
frontend https_front
    bind *:443 ssl crt /etc/haproxy/ssl/fgedu.net.cn.pem alpn h2,http/1.1
    mode http
    
    # HTTP/2支持
    option http-use-htx
    
    # HSTS
    http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains"
    
    default_backend web_servers
# HTTP重定向HTTPS
frontend http_redirect
    bind *:80
    mode http
    redirect scheme https code 301 if !{ ssl_fc }

五、TCP负载均衡

5.1 MySQL负载均衡

# MySQL负载均衡
listen mysql_cluster
    bind *:3306
    mode tcp
    balance roundrobin
    option tcp-check
    tcp-check connect
    tcp-check send PING\r\n
    tcp-check expect string +PONG
    
    server mysql1 192.168.1.101:3306 check inter 2000 rise 2 fall 3
    server mysql2 192.168.1.102:3306 check inter 2000 rise 2 fall 3
    server mysql3 192.168.1.103:3306 check inter 2000 rise 2 fall 3 backup

5.2 Redis负载均衡

# Redis负载均衡
listen redis_cluster
    bind *:6379
    mode tcp
    balance roundrobin
    option tcp-check
    tcp-check send PING\r\n
    tcp-check expect string +PONG
    
    server redis1 192.168.1.101:6379 check inter 2000
    server redis2 192.168.1.102:6379 check inter 2000
    server redis3 192.168.1.103:6379 check inter 2000

六、调度算法

6.1 算法说明

算法 说明 适用场景
roundrobin 轮询,支持权重 一般场景
static-rr 静态轮询 权重不变场景
leastconn 最少连接 长连接场景
source 源地址哈希 会话保持
uri URI哈希 缓存服务器

6.2 算法配置

# 轮询
backend web_servers
    balance roundrobin
    server web1 192.168.1.101:80 check
    server web2 192.168.1.102:80 check
# 最少连接
backend db_servers
    balance leastconn
    server db1 192.168.1.101:3306 check
    server db2 192.168.1.102:3306 check
# 源地址哈希
backend session_servers
    balance source
    server app1 192.168.1.101:8080 check
    server app2 192.168.1.102:8080 check

七、健康检查

7.1 HTTP健康检查

backend web_servers
    option httpchk GET /health HTTP/1.1\r\nHost:\ fgedu.net.cn
    http-check expect status 200-399
    
    server web1 192.168.1.101:80 check inter 3000 rise 2 fall 3
    server web2 192.168.1.102:80 check inter 3000 rise 2 fall 3
# 参数说明
# inter: 检查间隔(毫秒)
# rise: 连续成功次数判定为健康
# fall: 连续失败次数判定为不健康
# check: 启用健康检查

7.2 TCP健康检查

backend tcp_servers
    option tcp-check
    tcp-check connect port 80
    tcp-check send GET\ /\ HTTP/1.0\r\n\r\n
    tcp-check expect string OK
    
    server srv1 192.168.1.101:80 check
    server srv2 192.168.1.102:80 check

八、ACL访问控制

8.1 ACL规则配置

frontend http_front
    bind *:80
    
    # 基于路径的ACL
    acl is_api path_beg /api
    acl is_admin path_beg /admin
    acl is_static path_end .css .js .png .jpg
    
    # 基于域名的ACL
    acl is_www hdr_beg(host) -i www.
    acl is_api_domain hdr_beg(host) -i api.
    
    # 基于IP的ACL
    acl is_internal src 192.168.1.0/24
    acl is_allowed src 10.0.0.0/8
    
    # 基于方法的ACL
    acl is_get method GET
    acl is_post method POST
    
    # 使用ACL
    use_backend api_servers if is_api
    use_backend admin_servers if is_admin is_internal
    use_backend static_servers if is_static
    default_backend web_servers
backend api_servers
    server api1 192.168.1.101:8080 check
backend admin_servers
    server admin1 192.168.1.201:8080 check

九、监控与统计

9.1 统计页面配置

# 统计页面
frontend stats
    bind *:8404
    mode http
    stats enable
    stats uri /haproxy-stats
    stats refresh 10s
    stats admin if LOCALHOST
    stats auth admin:admin123
    stats show-legends
    stats show-node
# 访问统计页面
# http://fgedudb:8404/haproxy-stats

9.2 Prometheus监控

# 启用Prometheus指标
frontend prometheus
    bind *:8405
    mode http
    http-request use-service prometheus-exporter if { path /metrics }
# prometheus.yml配置
scrape_configs:
  - job_name: 'haproxy'
    static_configs:
      - targets: ['fgedudb:8405']
# 关键指标
haproxy_frontend_current_sessions
haproxy_backend_current_sessions
haproxy_server_current_queue
haproxy_server_response_time_average_seconds

十、运维命令

10.1 常用管理命令

# 测试配置
haproxy -c -f /etc/haproxy/haproxy.cfg
# 启动服务
systemctl start haproxy
# 停止服务
systemctl stop haproxy
# 重载配置
systemctl reload haproxy
# 查看状态
systemctl status haproxy
# 查看进程
ps -ef | grep haproxy
# 使用socket管理
echo "show stat" | socat stdio /var/lib/haproxy/stats
echo "show info" | socat stdio /var/lib/haproxy/stats
echo "show servers state" | socat stdio /var/lib/haproxy/stats
# 动态调整权重
echo "set weight web_servers/web1 50" | socat stdio /var/lib/haproxy/stats
# 启用/禁用服务器
echo "enable server web_servers/web1" | socat stdio /var/lib/haproxy/stats
echo "disable server web_servers/web1" | socat stdio /var/lib/haproxy/stats

十一、最佳实践

配置项 建议值 说明
maxconn 根据内存计算 避免连接过多
timeout 合理设置 避免连接堆积
健康检查 启用 自动剔除故障节点
统计页面 启用认证 安全访问
注意事项:

  • 配置文件修改后需重载
  • 监控连接数和响应时间
  • 定期检查后端健康状态
  • 统计页面需设置认证

十二、总结

HAProxy是功能强大的负载均衡软件。通过本培训文档,您应该掌握了:

  • HAProxy的安装部署
  • HTTP和TCP负载均衡配置
  • 调度算法的选择
  • 健康检查配置
  • ACL访问控制
  • 监控统计和运维管理
IT运维培训文档系列 | 第258篇 | HAProxy负载均衡培训

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息