歡迎您光臨本站 註冊首頁

Linux bonding研究及實現

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

Linux Bonding

保持伺服器的高可用性是企業級IT環境的首要因素,其中最主要的一點是伺服器網路連接的高可用性.在這樣的背景下,單網卡已是捉襟見肘,於是冗餘技術層出不窮.如果資源有限,網卡(NIC)綁定技技術將有助於保證高可用性特徵並提供其他優勢以提高網路性能.本文介紹Linux(具體發行版本是CentOS5.6)下的雙卡綁定的原理及實現.

Linux雙網卡綁定實現是使用兩塊網卡虛擬成為一塊網卡設備,這簡單來說,就是兩塊網卡具有相同的IP地址而并行鏈接聚合成一個邏輯鏈路工作.這項技術在Sun和Cisco中分別被稱為Trunking和Etherchannel技術,在Linux的2.4.x及其以後的內核則稱為Bonding技術.

bonding的前提條件:網卡應該具備自己獨立的BIOS晶元,並且各網卡晶元組型號相同.同一台伺服器上的網卡毫無疑問是是符合這個條件的.

Bonding原理

bonding的原理:在正常情況下,網卡只接收目的硬體地址(MAC Address)是自身MAC的乙太網幀,過濾別的數據幀,以減輕驅動程序的負擔;但是網卡也支持另外一種被稱為混雜promisc的模式,可以接收網路上所有的幀,bonding就運行在這種模式下,而且修改了驅動程序中的mac地址,將兩塊網卡的MAC地址改成相同,可以接收特定MAC的數據幀.然後把相應的數據幀傳送給bond驅動程序處理.

為方便理解bonding的配置及實現,順便闡述一下Linux的網路介面及其配置文件.在 Linux 中,所有的網路通訊都發生在軟體介面與物理網路設備之間.與網路介面配置相關的文件,以及控制網路介面狀態的腳本文件,全都位於 /etc/sysconfig/netwrok-scripts/ 目錄下.網路介面配置文件用於控制系統中的軟體網路介面,並通過這些介面實現對網路設備的控制.當系統啟動時,系統通過這些介面配置文件決定啟動哪些介面,以及如何對這些介面進行配置.介面配置文件的名稱通常類似於 ifcfg-<name>,其中 <name> 與配置文件所控制的設備的名稱相關. 在所有的網路介面中,最常用的就是乙太網介面ifcfg-eth0,它是系統中第一塊網卡的配置文件.雖然在不同的系統之間,文件的類型和數量並不一定相同,但各種的網路設備都需要用到這些文件保證設備的正常運作.bonding技術,就是通過配置文件建立綁定介面,從而實現多個物理網卡綁定到一個虛擬網卡.

測試環境

本次作業環境中,使用的是CentOS5.6,其系統信息如下:
[root@support ~]# cat /etc/issue
CentOS release 5.6 (Final)
Kernel r on an m
[root@support ~]# uname -a
Linux support 2.6.18-238.9.1.el5 #1 SMP Tue Apr 12 18:10:13 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

可以通過modinfo bonding命令查看Linux是否支持bonding,據資料介紹,RHEL4已默認支持了,RHEL5、CentOS5毫無疑問也是支持的.系統有類似下面的信息輸出,就說明已支持了.
[root@support ~]# modinfo bonding
filename: /lib/modules/2.6.18-238.9.1.el5/kernel/drivers/net/bonding/bonding.ko
author: Thomas Davis, tadavis@lbl.gov and many others
description: Ethernet Channel Bonding Driver, v3.4.0-1
version: 3.4.0-1
license: GPL
srcversion: 358EAAF5610876F44387AEF
depends: ipv6
vermagic: 2.6.18-238.9.1.el5 SMP mod_unload gcc-4.1
parm: max_bonds:Max number of bonded devices (int)
…… ……
parm: debug:Print debug messages; 0 for off (default), 1 for on (int)
module_sig: 883f3504da4d65c2ddc58dbbe9356811249c0a0993d9c1c5b9055a7e4f2b4d86e86ebb2c5fbdbf09e2c41b93ca13b6b809914bc6a201c3cf694855143

Bonding配置

通過在 /etc/sysconfig/network-scripts/ 目錄下創建 ifcfg-bond<N> 配置文件,就能夠創建綁定介面.文件名稱中的 <N> 為介面的編號,如第一個通道綁定介面的名稱一般為 ifcfg-bond0.ifcfg-bond<N> 文件的內容與乙太網介面的配置文件(如 ifcfg-eth0)基本相同,只是 DEVICE 選項的值,應該為 bond<N>.

Bonding介面創建以後,被綁定的網卡必須在他們的設置文件裡面添加MASTER和SLAVE兩個參數.每個bonding介面的設置基本相同,具體配置如下:

1、新建/etc/sysconfig/network-scripts/ifcfg-bond0文件,輸入以下配置信息:
[root@support ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
BOOTPROTO=static
IPADDR=10.8.0.231
NETMASK=255.255.0.0
NETWORK=10.8.0.0
BROADCAST=10.8.255.255
ONBOOT=yes
USERCTL=NO
BONDING_OPTS="mode=0 miimon=100"

說明:這裡使用了BONDING_OPTS選項,則不需要再使用 /etc/modprobe.conf 配置文件對綁定設備進行配置.參數mode=0,指負載均衡模式,詳見下文.miimon是用來進行鏈路監測的,其原理是檢測網上的鏈路狀態,一般將miimon值設為100,表示系統每100ms監測一次鏈路連接狀態,如果有一條線路不通就轉入另一條線路.

2、修改 /etc/sysconfig/network-scripts/ifcfg-eth0網卡屬性
[root@support ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
HWADDR=00:30:48:56:DA:72
ONBOOT=yes
MASTER=bond0
SLAVE=yes

3、修改 /etc/sysconfig/network-scripts/ifcfg-eth1網卡屬性
[root@support ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
HWADDR=00:30:48:56:DA:73
ONBOOT=yes
HOTPLUG=no
DHCP_HOSTNAME=support
MASTER=bond0
SLAVE=yes

說明:修改單個網卡配置,主要是去掉IP 地址、子網掩碼等信息.同時添加MASTER及SLAVE兩項參數.
  MASTER=<bond-interface>:<bond-interface> 的值是乙太網卡連接到的通道綁定介面的名稱,這個指令與 SLAVE 指令配合使用.
  SLAVE=<yes|no>:yes - 表示此設備可以由 MASTER 指令中配置的通道綁定介面進行控制. no - 表示此設備不能由 MASTER 指令中配置的通道綁定介面進行控制.

4、編輯 /etc/modules.conf 文件,添加:alias bond0 bonding,以使系統在啟動時載入bonding模塊,對外虛擬網路介面設備為 bond0


[root@support ~]# cat /etc/modprobe.conf
alias bond0 bonding
alias eth0 e1000
alias eth1 e1000
alias scsi_hostadapter ata_piix
alias scsi_hostadapter1 usb-storage

註:在linux6下測試時,發現沒有modprobe.conf這個文件,需要進入modprobe.d文件夾創建bond0.conf,再將“alias bond0 bonding”添加到裡面
[root@ara ~]# cat /etc/modprobe.d/bond0.conf
alias bond0 bonding

5、重啟網路服務,以使用bond生效
[root@support ~]# service network restart

在網路設置可以看到bond0與eth0、eth1之間的主從關係
[root@support ~]# ifconfig
bond0 Link encap:Ethernet HWaddr 00:30:48:56:DA:72
inet addr:10.8.0.231 Bcast:10.8.255.255 Mask:255.255.0.0
inet6 addr: fe80::230:48ff:fe56:da72/64 Scope:Link
UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
RX packets:19015 errors:0 dropped:0 overruns:0 frame:0
TX packets:4714 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2557139 (2.4 MiB) TX bytes:469585 (458.5 KiB)

eth0 Link encap:Ethernet HWaddr 00:30:48:56:DA:72
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:10592 errors:0 dropped:0 overruns:0 frame:0
TX packets:2011 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:1295667 (1.2 MiB) TX bytes:209014 (204.1 KiB)

eth1 Link encap:Ethernet HWaddr 00:30:48:56:DA:72
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:8423 errors:0 dropped:0 overruns:0 frame:0
TX packets:2705 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:1261472 (1.2 MiB) TX bytes:260887 (254.7 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1178 errors:0 dropped:0 overruns:0 frame:0
TX packets:1178 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2054820 (1.9 MiB) TX bytes:2054820 (1.9 MiB)

至此,完成了bonding配置,系統已經在網卡的負載均衡模式下運行,隨意拔掉一個網卡口的網線,網線依然正常.

Bonding工作模式

bonding的模式一共有7種,常用的為0、1兩種:
round-robin(balance-rr) 0 網卡的負載均衡模式
active-backup 1 網卡的容錯模式
balance-xor 2 需要交換機支持
broadcast 3 廣播模式
ieee802.3ad 4 動態鏈路聚合模式,需要交換機支持
mode-tlb 5 自適應模式
mode-alb 6 網卡虛擬化方式

mode=0表示load balancing(round-robin)為負載均衡方式,兩塊網卡都工作.在這種模式下,能在提供帶寬的負載均衡的同時提供失效保護.B

mode=1表示fault-tolerance(active-backup)提供冗餘功能,工作方式是主備的工作方式,也就是說默認情況下只有一塊網卡工作,另一塊做備份.bonding定義了網卡的4個鏈路狀態:正常狀態(BOND_LINK_UP)、網卡出現故障(BOND_LINK_FAIL)、失效狀態(BOND_LINK_DOWN)及網上恢復狀態(BOND_LINK_BACK).mii的作用就是依次檢查網卡鏈路狀態是否處於這些狀態,然後通過標記某個變數來說明當前是否需要切換slave網卡.在這種模式下,兩塊網卡有一塊是不工作的,同時,bond虛設備的MAC地址均一致,這張備用網卡不會被外界察覺,交換機也不存在向該埠發包的情況.當bond的mii檢測到當前的active設備失效了以後,bonding會迅速將另外一塊網卡設置為首選slave設備.

在以上模式中,虛擬網卡的MAC地址始終是第一個slave網卡的MAC.由於外界學習到的伺服器MAC地址始終是不變的,在網路上確定了IP和MAC的唯一對應關係,保證了上層業務傳輸的邏輯一致性,鏈路的狀態不會受很大的影響.

Bonding工作情況

1、工作在模式0時

通過查看bond0的工作狀態查詢能詳細的掌握bonding的工作情況,正常狀態下的/proc/net/bonding/bond0文件:
[root@support ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.4.0-1 (October 7, 2008)

Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 100 Mbps
Duplex: full
Link Failure Count: 1
Permanent HW addr: 00:30:48:56:da:72

Slave Interface: eth1
MII Status: up
Speed: 100 Mbps
Duplex: full
Link Failure Count: 2
Permanent HW addr: 00:30:48:56:da:73

從win7 ping,通過拔插網線查看結果,不存在丟包

通過日誌查看網卡工作情況
[root@support ~]# tail -f /var/log/messages
May 29 08:01:41 support kernel: e1000: eth0 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX
May 29 08:01:41 support kernel: bonding: bond0: link status definitely up for interface eth0.
May 29 08:01:48 support kernel: e1000: eth1 NIC Link is Down
May 29 08:01:48 support kernel: bonding: bond0: link status definitely down for interface eth1, disabling it
May 29 08:02:18 support kernel: e1000: eth1 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX
May 29 08:02:18 support kernel: bonding: bond0: link status definitely up for interface eth1.
May 29 08:08:49 support kernel: e1000: eth0 NIC Link is Down
May 29 08:08:49 support kernel: bonding: bond0: link status definitely down for interface eth0, disabling it


May 29 08:08:56 support kernel: e1000: eth0 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX
May 29 08:08:56 support kernel: bonding: bond0: link status definitely up for interface eth0.

2、工作在模式1時

正常狀態下的/proc/net/bonding/bond0文件:
[root@support ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.4.0-1 (October 7, 2008)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 100 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:30:48:56:da:72

Slave Interface: eth1
MII Status: up
Speed: 100 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:30:48:56:da:73

從win7 ping,通過拔插網線查看結果,發現在拔網線時,偶爾會存在丟掉一個包的情況.

通過日誌查看網卡工作情況
[root@support ~]# tail -f /var/log/messages
May 29 08:25:24 support kernel: e1000: eth0 NIC Link is Down
May 29 08:25:24 support kernel: bonding: bond0: link status definitely down for interface eth0, disabling it
May 29 08:25:24 support kernel: bonding: bond0: making interface eth1 the new active one.
May 29 08:25:32 support kernel: e1000: eth0 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX
May 29 08:25:32 support kernel: bonding: bond0: link status definitely up for interface eth0.
May 29 08:27:02 support kernel: e1000: eth1 NIC Link is Down
May 29 08:27:02 support kernel: bonding: bond0: link status definitely down for interface eth1, disabling it
May 29 08:27:02 support kernel: bonding: bond0: making interface eth0 the new active one.


May 29 08:27:48 support kernel: e1000: eth1 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX
May 29 08:27:48 support kernel: bonding: bond0: link status definitely up for interface eth1.

小結

在測試過程中,發現丟包情況與設備有關,老式的網新易得伺服器上做測試時,兩個網卡無論是接到同一個二層交換機,還是分別接到兩個二層交換機,在拔插網線,偶爾會出現丟掉一個包的情況.但是在戴爾R710上,當使用模式1時,丟包比較多,甚至在被其他計算機ping時,會出現ping不同的情況.

另外,模式0隻是實現了簡單的負載均衡及冗餘備份.如果要實現高效的負載均衡,提高網路流量,需設置成模式4,而這需要交換機的支持,牽涉到交換機的鏈路聚合(LACP)功能.有待日後再研究.

註:本文有部分資料來自網路.

本文出自 「一樹清勁」 博客,請務必保留此出處http://sunshyfangtian.blog.51cto.com/1405751/577762


[火星人 ] Linux bonding研究及實現已經有1073次圍觀

http://coctec.com/docs/linux/show-post-48482.html