Linux 支持單網卡配置多 IP(別名接口)及多網卡綁定(Bonding)實現高可用與負載均衡。本文提供臨時命令、永久配置文件、Bonding 模式選擇、驗證測試全流程。
ifconfig eth0:0 192.168.0.1 netmask 255.255.255.0 ifconfig eth0:1 192.168.0.2 netmask 255.255.255.0 ifconfig eth0:2 192.168.0.3 netmask 255.255.255.0 ifconfig eth0:3 192.168.0.4 netmask 255.255.255.0
cd /etc/sysconfig/network-scripts/ifcfg-eth0:0 至 ifcfg-eth0:3:DEVICE=eth0:0 BOOTPROTO=static ONBOOT=yes IPADDR=192.168.0.1 NETMASK=255.255.255.0 HWADDR=00:0c:29:18:6e:35 # 必須與主接口 MAC 一致
systemctl restart network 或 ifdown eth0 && ifup eth0# /etc/modprobe.d/bonding.conf alias bond0 bonding options bonding mode=1 miimon=100 primary=eth0
modprobe bonding
# /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 MASTER=bond0 SLAVE=yes ONBOOT=yes BOOTPROTO=none # ifcfg-eth1 同理,DEVICE=eth1
# /etc/sysconfig/network-scripts/ifcfg-bond0 DEVICE=bond0 BOOTPROTO=static IPADDR=192.168.1.100 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 ONBOOT=yes BONDING_OPTS="mode=1 miimon=100 primary=eth0"
| 模式 | 名稱 | 特點 | 適用場景 |
|---|---|---|---|
mode=0 | balance-rr | 輪詢負載均衡,需交換機支持聚合 | 高吞吐、交換機支持 LACP |
mode=1 | active-backup | 主備冗余,單活,無需交換機支持 | 高可用、最常用 |
mode=2 | balance-xor | 基於 MAC/XOR 負載均衡 | 特定負載分發 |
mode=3 | broadcast | 廣播傳輸,極高冗余 | 金融/軍工級冗余 |
mode=4 | 802.3ad (LACP) | 動態鏈路聚合,需交換機配置 | 專業網絡環境 |
mode=5 | balance-tlb | 適應性負載均衡(發送端) | 無需交換機支持 |
mode=6 | balance-alb | 適應性負載均衡(發送+接收) | 無需交換機支持 |
# 查看 bonding 狀態 cat /proc/net/bonding/bond0 # 關鍵輸出:Bonding Mode、MII Status、Active Slave、Partner MAC # 測試故障切換 ifdown eth0 # 觀察備用接口接管 # 恢復 ifup eth0
HWADDR 需與實際 MAC 一致miimon 建議 100ms,過小誤觸發,過大切換慢primary 確保優先使用高性能網卡