Fedora allows administrators to bind multiple network interfaces together into a single
channel using the bonding
kernel
module and a special network interface called a channel bonding
interface. Channel bonding enables two or more network interfaces to act as one,
simultaneously increasing the bandwidth and providing redundancy.
To
create a channel bonding interface, create a file in the /etc/sysconfig/network-scripts/
directory
called ifcfg-bond
, replacing N
N
with
the number for the interface, such as 0
.
After
the channel bonding interface is created, the network interfaces to be bound together must be configured by adding the MASTER=
and SLAVE=
directives
to their configuration files. The configuration files for each of the channel-bonded interfaces can be nearly identical.
For a channel bonding interface to be valid, the kernel module must be loaded. To ensure
that the module is loaded when the channel bonding interface is brought up, create a new file as root
named
in
the bonding
.conf/etc/modprobe.d/
directory.
Note that you can name this file anything you like as long as it ends with a .conf
extension.
Insert the following line in this new file: alias bondN bonding; Replace N
with
the interface number, such as 0
.
For each configured channel bonding interface, there must be a corresponding entry in your new /etc/modprobe.d/
file.bonding
.conf
對於bonding的網絡負載均衡是我們在文件服務器中常用到的,比如把三塊網卡,當做一塊來用,解決一個IP地址,流量過大,服務器網絡壓力過大的問 題。對於文件服務器來說,比如NFS或SAMBA文件服務器,沒有任何一個管理員會把內部網的文件服務器的IP地址弄很多個來解決網絡負載的問題。如果在 內網中,文件服務器爲了管理和應用上的方便,大多是用同一個IP地址。對於一個百M的本地網絡來說,文件服務器在多個用戶同時使用的情況下,網絡壓力是極 大的,特別是SAMABA和NFS服務器。爲了解決同一個IP地址,突破流量的限制,畢竟網線和網卡對數據的吞吐量是有限制的。如果在有限的資源的情況
下,實現網絡負載均衡,最好的辦法就是 bonding ;
網絡冗餘;
對於服務器來說,網絡設備的穩定也是比較重要的,特別是網卡。在生產型的系統中,網卡的可靠性就更爲重要了。在生產型的系統中,大多通過硬件設備的冗餘來 提供服務器的可靠性和安全性,比如電源。bonding 也能爲網卡提供冗餘的支持。把網個網卡綁定到一個IP地址,當一塊網卡發生物理性損壞的情況下,另一塊網卡也能提供正常的服務。
雙網卡綁定設置
一、編輯虛擬網絡接口配置文件,指定網卡IP
假設eth0是對外服務的網卡,已經調試好網絡;eth1是希望與eth0同時對外提供服務的網卡
# cd /etc/sysconfig/network-scripts/
#vi ifcfg-bond0
寫入如下信息和原來 ifcfg-eth0 的配置其實差不多。
所以我建議執行如下語句,將ifcfg-eth0複製一份再改。
# cp ifcfg-eth0 ifcfg-bon0
將ifcfg-bon0的信息修改大致如下:
DEVICE=bond0
BOOTPROTO=static
IPADDR=[IP]
NETMASK=[MASK]
BROADCAST=[BROADCAST]
GATEWAY=[GATEWAY]
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
默認情況下,內核已支持bonding,只需要簡單修改/etc/modprobe.conf 這個配置文檔就可以了:添加兩行
alias bond0 bonding
options bond0 miimon=100 mode=1
說明:
mode指定了bond0的工作模式,在redhat中有0-6共7種工作模式,常用的是0和1。
mode=0 表示 load balancing (round-robin)爲負載均衡方式,兩塊網卡都工作。
mode=1 表示 fault-tolerance (active-backup)提供冗餘功能,工作方式是主 從的工作方式,也就是說默認情況下只有一塊網卡工作,另一塊做備份。
mode=2 表示 XOR policy 爲平衡策略。此模式提供負載平衡和容錯能力
mode=3 表示 broadcast 爲廣播策略。此模式提供了容錯能力
mode=4 表示 IEEE 802.3ad Dynamic link aggregation 爲 IEEE 802.3ad 爲 動態鏈接聚合。該策略可以通過 xmit_hash_policy 選項從缺省的 XOR 策略改變到其他策略。
mode=5 表示 Adaptive transmit load balancing 爲適配器傳輸負載均衡。該 模式的必要條件:ethtool 支持獲取每個 slave 的速率
mode=6 表示 Adaptive load balancing 爲適配器適應性負載均衡。該模式包含 了 balance-tlb 模式,同時加上針對 IPV4 流量的接收負載均衡(receive load balance, rlb),而且不需要任何 switch(交換機)的支持。
bonding 只能提供鏈路監測,即從主機到交換機的鏈路是否接通。如果只是交換機對 外的鏈路 down 掉了,而交換機本身並沒有故障,那麼 bonding 會認爲鏈路沒有問題而繼 續使用。
關於綁定網卡的詳細參數可參見筆者的另一篇文章
http://czmmiao.iteye.com/admin/blogs/1044031
四、增加開機啓動腳本
在 /etc/rc.d/rc.local里加上
#ifenslave bond0 eth0 eth1
如果eth0和eth1都寫了MASTER和SLAVE,則上面的步驟做不做都無所謂。
五、重啓
reboot或者service network restart 都可以看到結果。
六、查看綁定在哪張網卡上
# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.0.3 (March 23, 2006)
Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 500
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:01:4f:77
Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:01:4f:8b
六、測試
ping着某個地址,當然是能ping通的地址啦。如果發現網絡不通,請檢查ifcfg-bond0的網絡設置。
然後拔掉一根網線,如果ping沒斷,證明拔了一根backup的線,不是主線,重新插上等兩分鐘。
此時拔掉另一根網線,估計現在可以看到ping超時或者卡在那裏,稍等10~30秒,ping繼續連同。
測試成功。
七、交換機配置
conf t
interf port-channel 1
switchport mode access
inter range fa0/1-2
channel-group 1 mode on