1. 程式人生 > >centos多網絡卡繫結(匯聚),實現冗餘和負載均衡

centos多網絡卡繫結(匯聚),實現冗餘和負載均衡

說明:
繫結多塊網絡卡為一個虛擬ip,類似csico的etherchannel,實現冗餘或負載均衡和增加頻寬的功能。
核心需要bonding的支援,察看是否掛在bonding,lsmod命令。預設2.6核心中bonding已經被編譯為M的選項,不需重新編譯核心。

其實Redhat關於bond,在kernel-doc裡有一篇文件,講述得非常詳細,可以先看看/usr/share/doc/kernel-doc- 2.6.18/Documentation/networking/bonding.txt

一:不需重起的配置方法。
1 modprobe bonding miimon=100
2 ifconfig bond0 192.168.0.1 netmask 255.255.255.0
3 ifenslave bond0 eth0 eth1


二:重起仍然生效的配置方法一。

1關閉要繫結的物理網絡卡
修改ifcfg-eth0和ifcfg-eth1的啟動項

BOOTPROTO=none
ONBOOT=no

2建立虛擬網絡卡

在/etc/sysconfig/network-scripts/ 目錄下建立 ifcfg-bond0,並修改 /etc/modprobe.conf檔案實現開機自動掛載。

/etc/sysconfig/network-scripts/ifcfg-bond0 配置如下:

DEVICE=bond0
IPADDR=192.168.0.193
NETMASK=255.255.255.0
BOOTPROTO=static
ONBOOT=yes
GATEWAY=192.168.0.3

/etc/modprobe.conf 配置如下:
alias bond0 bonding
options bonding miimon=100 mode=0(miimon是用來進行鏈路監測的。
比如:miimon=100,那麼系統每100ms監測一次鏈路連線狀態,如果有一條線路不通就轉入另一條線路。模式1為主備模式,模式0為負載均衡與增加頻寬的模式)
注:以上為只做一組bonding的方式,如果做多組的話可以更改為以下的方式:
alias eth0 bnx2
alias eth1 bnx2
alias eth2 e1000
alias eth3 e1000
install bond0 /sbin/modprobe -a eth0 eth1 && /sbin/modprobe bonding
alias bond0 bonding
install bond1 /sbin/modprobe -a eth2 eth3 && /sbin/modprobe bonding
alias bond1 bonding
options bonding mode=0 miimon=100 max_bonds=2

最後執行測試, REBOOT確認bond0是否啟動,如果啟動,配置成功。


檢視bonding狀態
cat /proc/net/bonding/bond0


三:重起仍然生效的配置方法二

1 vi /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
IPADDR=192.168.0.193
NETMASK=255.255.255.0
BOOTPROTO=static
ONBOOT=yes
GATEWAY=192.168.0.3


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

3 vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes

4 vi /etc/modprobe.conf

alias bond0 bonding
options bonding miimon=100 mode=0

5 reboot




四: Linux 的 BONDING 模式

bonding 的應用分為合併網絡卡提高頻寬與冗餘兩種功能。

在 linux kernel bonding 的 kernel module 內,可以依據傳入 mode=X 的方式來決定執行模式,其中數值可能結果依據官方檔案說明如下:

mode=0 (balance-rr)

Round-robin policy: Transmit packets in sequential order from the first available
slave through the last. This mode provides load balancing and fault tolerance.

mode=1 (active-backup)

Active-backup policy: Only one slave in the bond is active. A different slave
becomes active if, and only if, the active slave fails. The bond's MAC address is externally visible on only one port

(network adapter) to avoid confusing the switch. This mode provides fault tolerance. The primary option affects the behavior

of this mode.

mode=2 (balance-xor)

XOR policy: Transmit based on [(source MAC address XOR'd with destination
MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load

balancing and fault tolerance.

所以可以依據實際需求決定要使用哪種模式來提供 bonding 功能。一般所謂合併與平衡負載功能部份,選擇 mode=0,而若是要達成 active-backup 架構的話,則選擇使用 mode=1 即可,注意,網絡卡需要支援mii-too
l

設定配置檔案, /etc/modprobe.conf 放置如下內容即可決定型別:

alias bond0 bonding
options bond0 miimon=100 mode=0

說明:miimon 是用來進行鏈路監測的。如果miimon=100,那麼系統每100ms 監測一次鏈路連線狀態,如果有一條線路不通就轉入另一條線路;mode 的值表 示工作模式,它共有0,1,2,3四種模式,常用的為0,1兩種。
mode=0 表示load balancing (round-robin)為負載均衡方式,兩塊網絡卡都工作。
mode=1 表示fault tolerance (active-backup)提供冗餘功能,工作方式是主備的工作方式,也就是說預設情況下只有一塊網絡卡工作,另一塊做備份。
max_bonds=2 表示最大的網絡卡繫結數量為2。