linux多網卡綁定相關教程

來源:互聯網
上載者:User

linux多網卡綁定相關教程

實驗環境:Redhat Enterprise Linux 5.2 VMware workstation 7


圖一 實驗環境網卡配置:三塊虛擬網卡,工作在NAT模式

  一、配置三個網卡及bond0

[root@redhat5 ~]# cd /etc/sysconfig/network-scripts
[root@redhat5 network-scripts]# ls | grep ifcfg
ifcfg-bond0
ifcfg-eth0
ifcfg-eth1
ifcfg-eth2
ifcfg-lo
[root@redhat5 network-scripts]#

  用vi編輯器編輯ifcfg-eth0,在原因的基礎上保保留三個參數的配置即可:DEVICE=eth0,BOOTPROTO=static及ONBOOT=yes,其它的
參數值用#號注釋掉或直接刪除。

[root@redhat5 network-scripts]# vi ifcfg-eth0

# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
#BROADCAST=192.168.249.255
#HWADDR=00:0C:29:42:42:61
#IPADDR=192.168.249.2
#IPV6INIT=yes
#IPV6_AUTOCONF=yes
#NETMASK=255.255.255.0
#NETWORK=192.168.249.0
ONBOOT=yes
~
~
~

"ifcfg-eth0" 11L, 248C

  其它的兩個網卡eth1與eth2的配置與eth0的一樣:

[root@redhat5 network-scripts]# cat ifcfg-eth1
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
#HWADDR=00:0c:29:42:42:6b
[root@redhat5 network-scripts]# cat ifcfg-eth2
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth2
ONBOOT=yes
BOOTPROTO=static
#HWADDR=00:0c:29:42:42:75

  以某個網卡為模板建立bond0的設定檔ifcfg-bond0:

[root@redhat5 network-scripts]# cp ifcfg-eth0 bond0

  用vi編輯修改ifcfg-bond0,內容如下,保留DEVICE=bond0,BOOTPROTO=static,IPADDR=192.168.249.2,NETMASK=255.255.2550即可。
[root@redhat5 network-scripts]# cat ifcfg-bond0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=bond0
BOOTPROTO=static
#BROADCAST=192.168.249.255
#HWADDR=00:0C:29:42:42:61
IPADDR=192.168.249.2
#IPV6INIT=yes
#IPV6_AUTOCONF=yes
NETMASK=255.255.255.0
#NETWORK=192.168.249.0
ONBOOT=yes

  二、編輯/etc/modprobe.conf檔案

  在/etc/modprobe.conf檔案中添加兩行內容:
alias bond0 bonding
options bond0 miimon=100 mode=1

[root@redhat5 /]# cat /etc/modprobe.conf
alias eth0 pcnet32
alias scsi_hostadapter mptbase
alias scsi_hostadapter1 mptspi
alias scsi_hostadapter2 ata_piix
alias snd-card-0 snd-ens1371
options snd-card-0 index=0
options snd-ens1371 index=0
remove snd-ens1371 { /usr/sbin/alsactl store 0 >/dev/null 2>&1 || : ; }; /sbin/modprobe -r --ignore-remove snd-ens1371
alias eth1 pcnet32
alias eth2 pcnet32
alias bond0 bonding
options bond0 miimon=100 mode=1
[root@redhat5 /]#

  miimon=100表示網卡狀態的檢測時間是100毫秒。 mode參數表網路綁定的模式,模式1是三個網卡中只有一個網卡工作,其它的兩個做備份。

  三、最後一步讓綁定生效

  service network restart對網卡及bond0介面等配置生效
[root@redhat5 /]# service network restart
Shutting down interface bond0: [ OK ]
Shutting down interface eth0: [ OK ]
Shutting down interface eth1: [ OK ]
Shutting down interface eth2: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface bond0: [ OK ]
Bringing up interface eth0: [ OK ]
Bringing up interface eth1: [ OK ]
Bringing up interface eth2: [ OK ]
[root@redhat5 /]#

  運行ifenslave bond0 eth0 eth1 eth2

[root@redhat5 /]# ifenslave bond0 eth0 eth1 eth2

為了讓下次重啟系統能夠自動運行ifenslave命令,要執行下面命令:
[root@redhat5 init.d]# echo 'ifenslave bond0 eth0 eth1 eth2' >> /etc/rc.local

檢驗一下:
[root@redhat5 /]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
ifenslave bond0 eth0 eth1 eth2

  上面這條命令的意思是bond0為主網卡,其它的為從網卡,其中eth0是當前活動的從網卡,即eth0用來通訊。查看bond0資訊:
[root@redhat5 /]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.2.4 (January 28, 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
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:61

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:6b

Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:75

  檢查綁定效果,用host機不停地向虛擬機器發送icmp資料包,依次down eth0、 eth1,看看網路是否還能連通。


圖二 ping虛擬機器


圖三 斷開虛擬機器的eth0、eth1的網卡



圖四 每斷開一個網卡會丟一個icmp資料包

  當把eth0、eth1斷開後,當前的活動網卡就是eth2了,
[root@redhat5 /]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.2.4 (January 28, 2008)

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

Slave Interface: eth0
MII Status: down
Link Failure Count: 1
Permanent HW addr: 00:0c:29:42:42:61

Slave Interface: eth1
MII Status: down
Link Failure Count: 1
Permanent HW addr: 00:0c:29:42:42:6b

Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:75
[root@redhat5 /]#

  四、多網卡綁定,實現負載平衡

  修改/etc/modprobe.conf檔案,mode=0,把繫結模式修改為負載平衡(round-robin)。

[root@redhat5 ~]# cat /etc/modprobe.conf
alias eth0 pcnet32
alias scsi_hostadapter mptbase
alias scsi_hostadapter1 mptspi
alias scsi_hostadapter2 ata_piix
alias snd-card-0 snd-ens1371
options snd-card-0 index=0
options snd-ens1371 index=0
remove snd-ens1371 { /usr/sbin/alsactl store 0 >/dev/null 2>&1 || : ; }; /sbin/modprobe -r --ignore-remove snd-ens1371
alias eth1 pcnet32
alias eth2 pcnet32
alias bond0 bonding
options bond0 miimon=100 mode=0

[root@redhat5 ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.2.4 (January 28, 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
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:61

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:6b

Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:75

  其它模式:
mode=2
[root@redhat5 ~]# cap /proc/net/bonding/bond0
-bash: cap: command not found
[root@redhat5 ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.2.4 (January 28, 2008)

Bonding Mode: load balancing (xor)
Transmit Hash Policy: layer2 (0)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:61

Slave Interface: eth1
MII Status: down
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:6b

Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:75
mode=3
[root@redhat5 ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.2.4 (January 28, 2008)

Bonding Mode: fault-tolerance (broadcast)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:61

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:6b

Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:75

mode=4
[root@redhat5 ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.2.4 (January 28, 2008)

Bonding Mode: IEEE 802.3ad Dynamic link aggregation
Transmit Hash Policy: layer2 (0)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

802.3ad info
LACP rate: slow
Active Aggregator Info:
Aggregator ID: 1
Number of ports: 1
Actor Key: 9
Partner Key: 1
Partner Mac Address: 00:00:00:00:00:00

Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:61
Aggregator ID: 1

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:6b
Aggregator ID: 2

Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:42:42:75
Aggregator ID: 3

  需要注意的是:當mode=0可以實現負載平衡但不能實現容錯。這些機制是需要交換器支援的。比如:IEEE鏈路彙總協議。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.