Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 實現單網卡多 IP 與多網卡 Bonding 負載均衡/冗余完整配置

編輯:Linux 教學

Linux 支持單網卡配置多 IP(別名接口)及多網卡綁定(Bonding)實現高可用與負載均衡。本文提供臨時命令、永久配置文件、Bonding 模式選擇、驗證測試全流程。

單網卡多 IP(別名接口)

臨時生效(立即生效,重啟失效)

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

永久生效(配置文件)

  1. cd /etc/sysconfig/network-scripts/
  2. 創建 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 一致
  3. 重啟網絡:systemctl restart network 或 ifdown eth0 && ifup eth0

多網卡 Bonding(內核 bonding 模塊)

加載模塊並持久化

# /etc/modprobe.d/bonding.conf
alias bond0 bonding
options bonding mode=1 miimon=100 primary=eth0
modprobe bonding

配置物理接口 (eth0, eth1)

# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
MASTER=bond0
SLAVE=yes
ONBOOT=yes
BOOTPROTO=none

# ifcfg-eth1 同理,DEVICE=eth1

配置 Bond 接口

# /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"

Bonding 模式選擇指南

模式名稱特點適用場景
mode=0balance-rr輪詢負載均衡,需交換機支持聚合高吞吐、交換機支持 LACP
mode=1active-backup主備冗余,單活,無需交換機支持高可用、最常用
mode=2balance-xor基於 MAC/XOR 負載均衡特定負載分發
mode=3broadcast廣播傳輸,極高冗余金融/軍工級冗余
mode=4802.3ad (LACP)動態鏈路聚合,需交換機配置專業網絡環境
mode=5balance-tlb適應性負載均衡(發送端)無需交換機支持
mode=6balance-alb適應性負載均衡(發送+接收)無需交換機支持

驗證與監控

# 查看 bonding 狀態
cat /proc/net/bonding/bond0
# 關鍵輸出:Bonding Mode、MII Status、Active Slave、Partner MAC

# 測試故障切換
ifdown eth0  # 觀察備用接口接管
# 恢復
ifup eth0

注意事項

  • 物理接口 HWADDR 需與實際 MAC 一致
  • miimon 建議 100ms,過小誤觸發,過大切換慢
  • mode=1 必須指定 primary 確保優先使用高性能網卡
  • 交換機端需配置對應端口聚合(mode=0/4)或啟用 PortFast(mode=1)
Copyright © Windows教程網 All Rights Reserved