HAPROXY安装,双网卡负载均衡实战指南

HAPORXY 实验环境设定

本文档整理了 Haproxy 在实验环境(双网卡调度器 + 两台后端 Web 服务器)下的安装、配置、网络与验证步骤,包含示例配置与常见排查要点。

原始图片已保留并嵌入文档中。


目录

  • 简介与拓扑
  • 环境准备
  • Haproxy 安装
  • Haproxy 配置示例(frontend/backend 与 listen)
  • 主机网络配置(haproxy 与 webserver)
  • Web 服务准备(webserver1 / webserver2)
  • 验证方法
  • 参数说明与常见故障排查
  • 附:完整示例配置与常用命令

简介与拓扑

  • 拓扑:一台 haproxy(具有两块网卡:外网 eth0:172.25.254.100,内网 eth1:192.168.0.100)作为负载均衡器,后端两台 webserver(192.168.0.10、192.168.0.20)提供 HTTP 服务。
  • haproxy 监听外网 IP(例如 172.25.254.100:80),将请求转发到内网的两个后端服务器。

环境准备

  • 假设三台主机:

    • haproxy:eth0 (外网) 172.25.254.100/24,eth1 (内网) 192.168.0.100/24
    • webserver1:eth0 192.168.0.10/24
    • webserver2:eth0 192.168.0.20/24
  • 确保实验网络连通(内网互通或 haproxy 可访问后端 IP)。


1. Haproxy 安装

在 haproxy 主机上执行:

bash 复制代码
# 在调度器(双网卡主机中)
[root@haproxy ~]# dnf install haproxy.x86_64 -y
[root@haproxy ~]# systemctl enable --now haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.

2. Haproxy 基础配置示例

推荐把前端(frontend)和后端(backend)分开写,或使用 listen 模式。

示例 A --- frontend / backend 写法:

haproxy 复制代码
frontend webcluster
    bind            *:80
    mode            http
    default_backend webserver-80

backend webserver-80
    mode http
    balance roundrobin
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5

示例 B --- listen 写法(功能相同):

haproxy 复制代码
listen webcluster
    bind        *:80
    mode        http
    balance     roundrobin
    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5
    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5

编辑完成后重启 haproxy:

bash 复制代码
[root@haproxy ~]# systemctl restart haproxy.service

测试(从能访问 haproxy 的主机上):

bash 复制代码
[root@haproxy ~]# curl 172.25.254.100
# 可能返回
webserver2 - 192.168.0.20

[root@haproxy ~]# curl 172.25.254.100
# 轮询到另一台
webserver1 - 192.168.0.10

3. 主机网络配置(示例命令)

在你的实验中使用了 vmset.sh 辅助脚本来配置网卡。示例:

haproxy(外网 eth0):

bash 复制代码
[root@haproxy ~]# vmset.sh eth0 172.25.254.100 haproxy
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/7)
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:0c:6f:ee brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 172.25.254.100/24 brd 172.25.254.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe0c:6fee/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever
haproxy

haproxy(内网 eth1):

bash 复制代码
[root@haproxy ~]# vmset.sh eth1 192.168.0.100 haproxy norouter
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/8)
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:0c:6f:f8 brd ff:ff:ff:ff:ff:ff
    altname enp19s0
    altname ens224
    inet 192.168.0.100/24 brd 192.168.0.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::4ca7:8cde:1244:8df/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever
haproxy

启用内核转发(若 haproxy 作为路由/转发节点需要):

bash 复制代码
[root@haproxy ~]# echo net.ipv4.ip_forward=1 > /etc/sysctl.conf
[root@haproxy ~]# sysctl -p
net.ipv4.ip_forward = 1

webserver1:

bash 复制代码
[root@webserver1 ~]# vmset.sh eth0 192.168.0.10 webserver1 noroute
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:8c:96:72 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 192.168.0.10/24 brd 192.168.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8c:9672/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever
webserver1

webserver2(示例输出也类似):

bash 复制代码
[root@webserver2 ~]# vmset.sh eth0 192.168.0.20 webserver2 noroute
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:8c:96:72 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 192.168.0.20/24 brd 192.168.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8c:9672/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever
webserver2

4. Web 服务准备(两台后端)

在每台 webserver 上安装并启动 httpd(Apache),并写一个简单的 index.html:

webserver1:

bash 复制代码
[root@webserver1 ~]# dnf install httpd -y
[root@webserver1 ~]# echo "webserver1 - 192.168.0.10" > /var/www/html/index.html
[root@webserver1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

(原笔记中的图片)

webserver2:

bash 复制代码
[root@webserver2 ~]# dnf install httpd -y 
[root@webserver2 ~]# echo "webserver2 - 192.168.0.20" > /var/www/html/index.html
[root@webserver2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

(原笔记中的图片)


5. 验证环境

在 haproxy 中访问后端直接验证 web 服务:

bash 复制代码
# 在 haproxy 中访问
[root@haproxy ~]# curl 192.168.0.10
webserver1 - 192.168.0.10
[root@haproxy ~]# curl 192.168.0.20
webserver2 - 192.168.0.20

通过 haproxy 访问验证负载均衡:

bash 复制代码
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 192.168.0.20
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 192.168.0.10

(原笔记中的验证截图)


6. 参数说明(简要)

  • check:启用健康检查
  • inter 3s:每 3 秒进行一次检查
  • fall 3:连续 3 次失败则认为后端 DOWN
  • rise 5:连续 5 次成功才认为后端 UP
  • balance roundrobin(或其它算法):轮询、最少连接等

7. 常见故障排查

  • haproxy 未启动:systemctl status haproxy / journalctl -u haproxy -e
  • 后端不可达:在 haproxy 主机上使用 curl 192.168.0.10:80telnet 192.168.0.10 80 测试连通性
  • 防火墙/SELinux:
    • 查看防火墙:firewall-cmd --list-all;必要时开放端口 firewall-cmd --add-service=http --permanent; firewall-cmd --reload
    • SELinux 可能阻止 httpd / 端口访问:sestatus 或临时 permissive 测试
  • IP 转发:如果网络需要通过 haproxy 转发,请确认 net.ipv4.ip_forward=1
  • haproxy 配置语法错误:haproxy -c -f /etc/haproxy/haproxy.cfg 校验配置
  • health check 配置过于严格/宽松会导致后端频繁上下线,调整 inter/fall/rise 参数以稳定检测

8. 完整示例 haproxy.cfg(可直接替换或参考)

haproxy 复制代码
global
    log /dev/log    local0
    maxconn 4096
    daemon

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

frontend webcluster
    bind *:80
    default_backend webserver-80

backend webserver-80
    balance roundrobin
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5

常用命令速查

  • 重启 haproxy:systemctl restart haproxy
  • 校验 haproxy 配置:haproxy -c -f /etc/haproxy/haproxy.cfg
  • 查看 haproxy 状态:systemctl status haproxy
  • 查看日志:journalctl -u haproxy -f
  • 查看某台后端是否可达:curl -I http://192.168.0.10/telnet 192.168.0.10 80

fall 3 rise 5

复制代码
---

## 常用命令速查
- 重启 haproxy:`systemctl restart haproxy`
- 校验 haproxy 配置:`haproxy -c -f /etc/haproxy/haproxy.cfg`
- 查看 haproxy 状态:`systemctl status haproxy`
- 查看日志:`journalctl -u haproxy -f`
- 查看某台后端是否可达:`curl -I http://192.168.0.10/` 或 `telnet 192.168.0.10 80`
相关推荐
淡泊if2 分钟前
1.2GB → 98MB,我的 Docker 镜像瘦身实战记录
运维·docker·容器
Sst的头号粉丝4 分钟前
Docker——cgroups
运维·docker·容器
JuckenBoy1 小时前
Linux环境安装SGLang框架运行自选大模型(以Rocky9.7为例)
linux·运维·大模型·qwen·rocky·deepseek·sglang
十巷无终1 小时前
Kali Virtual Machines(虚拟机镜像)安装后问题及解决办法
linux·运维·服务器
架构指南1 小时前
Centos上安装Claude Code报GLIBC_2.27 not found
linux·运维·centos
Predestination王瀞潞1 小时前
4.3.1 存储->微软文件系统标准(微软,自有技术标准):exFAT(Extended File Allocation Table)扩展文件分配表系统
linux·运维·microsoft·exfat·ex4
你有按下913的勇气吗1 小时前
【Agent,RAG,Transform】
linux·运维·服务器
last demo1 小时前
docker存储
运维·docker·容器
小义_2 小时前
随笔 1(Linux)
linux·运维·服务器·网络·云原生·红帽
HalvmånEver3 小时前
Linux:socket套接字编程的基础概念
linux·运维·服务器