現代伺服器通常配備多張網卡,但單張網卡故障就會導致網路中斷。在 Linux 系統中,透過網卡綁定技術將多張實體網卡聚合為一張虛擬網卡(如 bond0),即可實現負載均衡與故障切換,大幅提升網路可用性。本文以 CentOS/RHEL 系列為例,詳細說明如何將 eth0 與 eth1 綁定為 bond0。
vi /etc/sysconfig/network-scripts/ifcfg-bond0
內容如下(請依實際網段調整 IP、Netmask、Gateway):
DEVICE=bond0
BOOTPROTO=static
IPADDR=10.0.11.10
NETMASK=255.255.252.0
GATEWAY=10.0.11.1
ONBOOT=yes
USERCTL=no
TYPE=Ethernet
實體網卡不得設定 IP、Gateway、Netmask 等資訊,僅需指定 MASTER 與 SLAVE 參數:
# ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
USERCTL=no
MASTER=bond0
SLAVE=yes
TYPE=Ethernet
# ifcfg-eth1
DEVICE=eth1
BOOTPROTO=dhcp
ONBOOT=yes
USERCTL=no
MASTER=bond0
SLAVE=yes
TYPE=Ethernet
編輯 /etc/modprobe.d/bonding.conf(舊版為 /etc/modules.conf),加入:
alias bond0 bonding
options bond0 miimon=100 mode=0
參數說明:
miimon=100:每 100ms 檢測一次連結狀態,偵測到斷線即切換。mode=0(balance-rr):負載均衡,兩張網卡皆 Active,流量輪詢分發。mode=1(active-backup):容錯模式,僅一張 Active,另一張備援。若採用此模式,建議加上 primary=eth0 指定主網卡。注意:bonding 僅能監測「主機至交換機」的連結,若交換機上游斷線但交換機本身正常,bonding 仍視為連結正常。
在 /etc/rc.local 加入:
modprobe bonding miimon=100 mode=0
完成後重啟主機或執行 systemctl restart network,若出現 Bringing up interface bond0 OK 即代表成功。
cat /proc/net/bonding/bond0 查看綁定狀態、模式、MII 狀態。ethtool eth0 / ethtool eth1 確認實體連結速率。設定完成後,只要有一張實體網卡存活,虛擬網卡 bond0 即可持續服務,有效避免單點故障導致斷網。
