Linux 核心提供的 bonding 模組可將多張實體網卡聚合為一張邏輯介面(bond0),達到負載平衡、容錯移轉或頻寬疊加的效果。共有 7 種模式(mode 0~6),生產環境最常用的是 mode 0、1、6。本文詳細說明各模式特性、適用場景,並提供完整的設定範例。
| Mode | 名稱 | 關鍵特性 | 需交換器支援 | 典型用途 |
|---|---|---|---|---|
| 0 | balance-rr(輪詢) | 封包依序分送各網卡,提供負載平衡與容錯 | 是(需設定 Port Channel/聚合) | 高頻寬、對稱流量 |
| 1 | active-backup(主備) | 僅一張網卡作用,故障時自動切換,MAC 唯一 | 否 | 高可用性、不需負載平衡 |
| 2 | balance-xor(XOR 雜湊) | 依來源/目的 MAC 雜湊分送,同一連線固定走同一網卡 | 是(建議聚合) | 負載平衡、避免封包亂序 |
| 3 | broadcast(廣播) | 所有封包從每張網卡都送一次,極高冗余 | 否 | 特殊高可靠需求 |
| 4 | 802.3ad(LACP 動態聚合) | 符合 IEEE 802.3ad 標準,需交換器支援 LACP | 是(必須設定 LACP) | 企業級聚合、最大頻寬 |
| 5 | balance-tlb(適應性傳輸負載平衡) | 僅需 ethtool 支援,無需交換器設定,只做輸出負載平衡 | 否 | 輸出導向流量 |
| 6 | balance-alb(適應性負載平衡) | 包含 mode 5 並加上 IPv4 接收負載平衡(ARP 協商),無需交換器支援 | 否 | 最通用的無交換器協助負載平衡 |
環境:兩張實體網卡 eth0、eth1,虛擬介面 bond0,IP 192.168.0.100/24。
# /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.0.100
NETMASK=255.255.255.0
NETWORK=192.168.0.0
BROADCAST=192.168.0.255
# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
# /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
# /etc/modprobe.d/bonding.conf
alias bond0 bonding
options bonding mode=1 miimon=100
modprobe bonding
systemctl restart network # 或 /etc/init.d/network restart
cat /proc/net/bonding/bond0
# 輸出應顯示 Bonding Mode: fault-tolerance (active-backup)
# Currently Active Slave: eth0
# 且 bond0、eth0、eth1 三者 MAC 位址相同
在 /etc/rc.d/rc.local(或 systemd 服務)加入:
ifenslave bond0 eth0 eth1
route add default gw 192.168.0.1
拔除作用中網卡(eth0)網路線,觀察 cat /proc/net/bonding/bond0 中的 Active Slave 是否切換至 eth1,並確認服務仍可存取。