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 雙網卡 Bonding 綁定實現負載均衡與故障轉移

編輯:Linux 教學

網卡綁定 將多張實體網卡聚合為單一邏輯介面,提供頻寬疊加 (負載均衡) 或連線備援 (故障轉移) 功能。Linux 核心 2.4.12+ 內建 bonding 模組,早期版本可透過 patch 實現。本文以雙網卡 (eth0、eth1) 綁定為 bond0 為例說明配置流程。

一、原理概述

  • 兩張網卡設定相同 MAC 位址,以混雜模式接收封包
  • bonding 驅動程式處理封包分發與鏈路監測
  • 對上層應用呈現為單一網路介面

二、建立虛擬介面配置檔

cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0 ifcfg-bond0
vi ifcfg-bond0

修改內容如下:

DEVICE=bond0
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
BROADCAST=192.168.1.255
GATEWAY=192.168.1.1
ONBOOT=yes
TYPE=Ethernet

三、配置實體網卡為從屬介面

ifcfg-eth0:

DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=yes

ifcfg-eth1:

DEVICE=eth1
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=yes

四、載入 bonding 核心模組

編輯 /etc/modprobe.conf:

alias bond0 bonding
options bond0 miimon=100 mode=1

參數說明:

  • mode=0:負載均衡 (Round-robin),兩張網卡同時工作
  • mode=1:主備容錯,單張工作、另一張備援 (推薦生產環境)
  • miimon=100:每 100ms 檢測一次鏈路狀態

五、加入開機啟動腳本

編輯 /etc/rc.d/rc.local (若網卡配置已含 MASTER/SLAVE 可略過):

ifenslave bond0 eth0 eth1

六、重啟網路服務驗證

service network restart
# 或
reboot

七、故障測試驗證

  1. 持續 ping 目標主機確認連通
  2. 拔除網卡 A 網線,觀察 ping 是否中斷
  3. 重新插入網卡 A,等待 2 分鐘
  4. 拔除網卡 B 網線,觀察 10-30 秒內是否自動切換恢復
  5. 若 ping 持續不中斷,證明故障轉移運作正常

注意: bonding 僅監測本機至交換機的鏈路,若交換機上游鏈路中斷但交換機本身正常,bonding 可能不會切換。

Copyright © Windows教程網 All Rights Reserved