温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

HAProxy高可用代理怎样配置

发布时间:2026-10-15 07:44:50 来源:亿速云 阅读:105 作者:小樊 栏目:系统运维

下面给你一个常见、实用的 HAProxy 高可用代理配置方案,分为 架构说明 → 安装 → 基础配置 → 高可用(Keepalived)→ 健康检查 → 常用优化 几个部分。


一、典型高可用架构

        VIP (虚拟IP)
         |
   +-----+------+
   | Keepalived |
   +-----+------+
         |
   +-----+------+       +-----+------+
   | HAProxy 1  |<----->| HAProxy 2  |
   +-----+------+       +-----+------+
         |                    |
   +-----+------+       +-----+------+
   | 后端服务1  |       | 后端服务2  |
   +------------+       +------------+
  • HAProxy:做负载均衡和反向代理
  • Keepalived:做 VIP 漂移,实现 HAProxy 高可用
  • 后端:Web / API / 数据库代理等

二、安装 HAProxy(以 CentOS / Rocky / Ubuntu 为例)

CentOS / Rocky

yum install -y haproxy

Ubuntu

apt update
apt install -y haproxy

三、HAProxy 基础配置示例

配置文件:/etc/hapee-2.8/haproxy.cfg 或 /etc/haproxy/haproxy.cfg

1️⃣ 全局配置

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

2️⃣ 默认配置

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5s
    timeout client  30s
    timeout server  30s

3️⃣ 前端(监听端口)

frontend http_front
    bind *:80
    default_backend http_back

4️⃣ 后端(真实服务器)

backend http_back
    balance roundrobin
    option httpchk GET /healthz

    server web1 192.168.1.11:8080 check
    server web2 192.168.1.12:8080 check

✅ check:开启健康检查
✅ /healthz:后端健康检查接口


四、启用 HAProxy 状态页(推荐)

listen stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 10s
    stats admin if TRUE

浏览器访问:

http://IP:8404/stats

五、Keepalived 实现高可用(VIP 漂移)

1️⃣ 安装

yum install -y keepalived
# 或
apt install -y keepalived

2️⃣ Keepalived 配置(主节点)

/etc/keepalived/keepalived.conf

vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1

    virtual_ipaddress {
        192.168.1.100
    }

    track_script {
        chk_haproxy
    }
}

3️⃣ 备节点

state BACKUP
priority 100

4️⃣ 启动

systemctl enable haproxy keepalived
systemctl start haproxy keepalived

VIP:192.168.1.100
客户端只访问 VIP,不直接访问 HAProxy 节点。


六、HTTPS / TLS 配置(可选)

frontend https_front
    bind *:443 ssl crt /etc/haproxy/certs/example.pem
    default_backend http_back

七、常见优化建议

✅ 使用 leastconn 替代 roundrobin(长连接场景)
✅ 后端开启 check port
✅ 限制连接数防止雪崩
✅ 开启 tcpka / http-keep-alive
✅ 多节点 Keepalived + 单 VIP


八、典型故障排查

# 查看状态
systemctl status haproxy

# 检查配置
haproxy -c -f /etc/haproxy/haproxy.cfg

# VIP 是否漂移
ip a | grep 192.168.1.100

如果你愿意,我可以:

  • ✅ 给你 MySQL / Redis 的 HAProxy 配置
  • ✅ 给你 Kubernetes Ingress + HAProxy 方案
  • ✅ 按你的 业务类型(Web / API / WebSocket)定制配置

你可以直接说:「我想代理 XXX 服务」。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手