跳转至

Rocky Linux 9.6 Bonding Mode 6 (balance-alb) 配置指南

环境信息

项目
操作系统 Rocky Linux 9.6
网络管理 NetworkManager (nmcli)
网卡 eth0、eth1(千兆)
Bond 模式 balance-alb (mode 6)
网络拓扑 br0 → bond0 → eth0 + eth1
IP 地址 172.16.1.23/24
网关 172.16.1.1

什么是 Bonding Mode 6

Bash
balance-alb (Adaptive Load Balancing) - 自适应负载均衡

├── tlb: 发送负载均衡(Transmit Load Balancing)
   根据实时负载动态分配发送流量到不同网卡
└── rlb: 接收负载均衡(Receive Load Balancing)
    通过 ARP 协商实现接收端负载均衡

特点:
- 不需要交换机支持任何聚合协议
- 两台独立交换机也能正常工作
- 发送和接收双向负载均衡
- 链路故障自动切换,恢复自动回连

拓扑结构

Text Only
                    br0 (Linux Bridge)
                   IP: 172.16.1.23/24
                   GW: 172.16.1.1
                    ┌────▼────┐
                    │  bond0  │  mode=balance-alb
                    │ miimon= │  100ms
                    └────┬────┘
               ┌─────────┴─────────┐
               │                   │
         ┌─────▼────┐      ┌──────▼─────┐
         │   eth0   │      │    eth1    │
         │ 1Gbps    │      │  1Gbps     │
         └─────┬────┘      └──────┬─────┘
               │                  │
         ┌─────▼────┐      ┌──────▼─────┐
         │  交换机 A │      │  交换机 B  │
         │ (Access) │      │ (Access)   │
         └──────────┘      └────────────┘

配置前准备

备份现有网络配置

Bash
cp -r /etc/NetworkManager/system-connections/ \
  /etc/NetworkManager/system-connections.bak.$(date +%F_%T)

确认网卡名称

Bash
nmcli device status
ip link show

确认两个物理网卡名称为 eth0eth1

清理旧连接

确保 nmcli connection show 输出为空,或已删除 eth0、eth1 的旧连接配置:

Bash
1
2
3
nmcli connection show
# 如有旧连接,先删除
nmcli connection delete "旧连接名"

配置步骤

配置顺序很重要

必须按以下顺序执行:先创建 br0,再创建 bond0 并挂到 br0 下,最后将物理网卡加入 bond0。

第 1 步:创建 br0 网桥

Bash
1
2
3
4
5
6
7
8
9
nmcli connection add type bridge \
  ifname br0 \
  con-name br0 \
  ipv4.method manual \
  ipv4.addresses 172.16.1.23/24 \
  ipv4.gateway 172.16.1.1 \
  ipv4.dns "172.16.1.16" \
  ipv6.method disabled \
  bridge.stp no
参数 说明
ipv4.addresses 172.16.1.23/24 网桥 IP 地址和子网掩码
ipv4.gateway 172.16.1.1 默认网关
ipv4.dns 172.16.1.16 DNS 服务器
ipv6.method disabled 禁用 IPv6
bridge.stp no 关闭 STP(br0 只有一个端口 bond0,无环路风险)

为什么 br0 不开 STP

br0 只有一个桥接端口(bond0),不存在环路。开启 STP 会导致 30 秒启动延迟,KVM 虚机重启后 30 秒内网络不通。

第 2 步:创建 bond0 并挂到 br0 下

Bash
1
2
3
4
5
nmcli connection add type bond \
  ifname bond0 \
  con-name bond0 \
  master br0 \
  bond.options "mode=balance-alb,miimon=100,updelay=200"
参数 说明
master br0 将 bond0 作为 br0 的桥接端口
mode=balance-alb 6 自适应负载均衡模式
miimon=100 100ms MII 链路监测间隔,每 100ms 检测一次物理链路状态
updelay=200 200ms 链路恢复后等待 200ms 再启用,防止端口抖动

注意事项

  • slave 连接(bond0 挂在 br0 下)不能设置 ipv4.method,会自动从 br0 继承 IP 配置
  • arp_intervalarp_validatearp_ip_target 等 ARP 相关参数与 balance-alb 模式不兼容,nmcli 会报错
  • 所有 bond 参数统一写在 bond.options 里,逗号分隔,用等号赋值

第 3 步:将物理网卡加入 bond0

Bash
1
2
3
4
5
6
7
8
9
nmcli connection add type ethernet \
  ifname eth0 \
  con-name eth0 \
  master bond0

nmcli connection add type ethernet \
  ifname eth1 \
  con-name eth1 \
  master bond0

第 4 步:重载并激活

Bash
1
2
3
4
5
6
7
nmcli connection reload

# 按从底层到顶层的顺序激活
nmcli connection up eth0
nmcli connection up eth1
nmcli connection up bond0
nmcli connection up br0

激活顺序

先激活底层物理网卡(eth0、eth1),再激活 bond0,最后激活 br0。从下往上激活避免中间状态断网。

验证

查看连接列表

Bash
nmcli connection show

预期输出(4 个连接):

连接名 类型 关系
br0 bridge 带 IP: 172.16.1.23/24
bond0 bond master=br0
eth0 ethernet master=bond0
eth1 ethernet master=bond0

查看 Bonding 状态

Bash
cat /proc/net/bonding/bond0

预期输出:

Bash
Ethernet Channel Bonding Driver: v5.14.0-xxx.el9_6.x86_64

Bonding Mode: adaptive load balancing
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 200
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0

查看网络接口状态

Bash
ip addr show br0
ip addr show bond0

测试连通性

Bash
ping -c 3 172.16.1.1

故障切换测试

测试 eth0 断线

Bash
1
2
3
4
5
# 终端 1:保持 ping 不断
ping 172.16.1.1

# 终端 2:断开 eth0
nmcli device disconnect eth0

期望结果:

  • ping 丢 1~2 个包后自动恢复
  • cat /proc/net/bonding/bond0 显示 eth0 MII Status 为 down
  • eth1 继续正常工作,网络不断

恢复 eth0

Bash
nmcli device connect eth0

期望结果:

  • eth0 在约 200ms(updelay)后重新 up
  • cat /proc/net/bonding/bond0 显示两个 slave 都 up
  • 负载均衡恢复

参数详解

核心参数(必须配置)

参数 说明
mode=balance-alb 6 自适应负载均衡,包含发送(tlb)+接收(rlb)负载均衡
miimon=100 100 链路监测间隔,默认 0 关闭,不设置则断网了 bond 不知道

建议配置

参数 说明
updelay=200 200 链路恢复后延迟 200ms 启用,防止端口抖动频繁切换

不兼容参数(balance-alb 模式下禁止使用)

参数 原因
arp_interval > 0 与 balance-alb 模式不兼容,nmcli 会直接拒绝
arp_ip_target 配合 arp_interval 使用,同样不兼容
arp_validate mode 1 专用参数,nmcli 不识别

默认值参数(可省略)

参数 默认值 说明
downdelay 0 链路断开后立即切换
tlb_dynamic_lb 1 动态发送负载均衡
primary_reselect always 主网卡恢复后切回
fail_over_mac none 不切换 MAC 地址

常见问题

Q: br0 需要开 STP 吗?

不需要。br0 只有一个端口(bond0),不存在环路。开 STP 只会增加 30 秒启动延迟。

Q: mode 6 需要交换机配合吗?

不需要。balance-alb 模式完全由 Linux 内核处理,交换机端保持普通 Access 端口配置即可。

Q: bond0 需要配置 IP 吗?

不需要。IP 配置在 br0 上,bond0 作为 br0 的桥接端口,IP 自动继承。

Q: 单个 TCP 连接会拆分到两个网卡吗?

不会。mode 6 按连接分配流量,单个 TCP 连接走一个网卡,不同连接会分配到不同网卡。

Q: nmcli bond.options 里参数用下划线还是连字符?

下划线。如 miimon=100arp_interval=1000,这是内核 bonding 模块的原始参数名。

Q: slave 连接能设置 ipv4.method 吗?

不能。slave 连接(bond0、eth0、eth1)的 IP 配置自动从 master 继承,nmcli 会拒绝在 slave 上设置 ipv4/ipv6 属性。

配置文件位置

所有连接配置文件保存在:

Bash
/etc/NetworkManager/system-connections/

对应文件:

Bash
1
2
3
4
br0.nmconnection           网桥,带 IP
bond0.nmconnection         bond,挂到 br0 eth0.nmconnection   物理网卡 eth0
eth1.nmconnection   物理网卡 eth1

查看原始配置:

Bash
cat /etc/NetworkManager/system-connections/bond0.nmconnection