如何进行Linux下双网卡绑定bond的配置

如何进行Linux下双网卡绑定bond的配置,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。


一、什么是bond?

网卡bond是通过多张网卡绑定为一个逻辑网卡,实现本地网卡冗余,带宽扩容和负载均衡,在生产场景中是一种常用的技术。kernels 2.4.12及以后的版本均供bonding模块,以前的版本可以通过patch实现。

二、bond的配置实例

1、测试环境

[root@wjq2 ~]# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 6.4 (Santiago)

[root@wjq2 ~]# uname -r

2.6.32-358.el6.x86_64

2、查看linux是否支持bonding,大部分发行版都支持

[root@wjq2 ~]# cat /boot/config-2.6.32-358.el6.x86_64 | grep -i bonding

CONFIG_BONDING=m

或者

[root@wjq2 ~]# modinfo bonding | more

filename:       /lib/modules/2.6.32-358.el6.x86_64/kernel/drivers/net/bonding/bonding.ko

author:         Thomas Davis, tadavis@lbl.gov and many others

description:    Ethernet Channel Bonding Driver, v3.6.0

version:        3.6.0

license:        GPL

srcversion:     990DDFEBDBA03F305236885

depends:        8021q,ipv6

vermagic:       2.6.32-358.el6.x86_64 SMP mod_unload modversions

parm:           max_bonds:Max number of bonded devices (int)

parm:           tx_queues:Max number of transmit queues (default = 16) (int)

parm:           num_grat_arp:Number of gratuitous ARP packets to send on failover event (int)

parm:           num_unsol_na:Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event (int)

parm:           miimon:Link check interval in milliseconds (int)

parm:           updelay:Delay before considering link up, in milliseconds (int)

parm:           downdelay:Delay before considering link down, in milliseconds (int)

parm:           use_carrier:Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default) (int)

parm:           mode:Mode of operation; 0 for balance-rr, 1 for active-backup, 2 for balance-xor, 3 for broadcast, 4 fo

r 802.3ad, 5 for balance-tlb, 6 for balance-alb (charp)

parm:           primary:Primary network device to use (charp)

parm:           primary_reselect:Reselect primary slave once it comes up; 0 for always (default), 1 for only if speed o

f primary is better, 2 for only on active slave failure (charp)

parm:           lacp_rate:LACPDU tx rate to request from 802.3ad partner; 0 for slow, 1 for fast (charp)

parm:           ad_select:803.ad aggregation selection logic; 0 for stable (default), 1 for bandwidth, 2 for count (cha

rp)

parm:           xmit_hash_policy:balance-xor and 802.3ad hashing method; 0 for layer 2 (default), 1 for layer 3+4, 2 fo

r layer 2+3 (charp)

parm:           arp_interval:arp interval in milliseconds (int)

parm:           arp_ip_target:arp targets in n.n.n.n form (array of charp)

parm:           arp_validate:validate src/dst of ARP probes; 0 for none (default), 1 for active, 2 for backup, 3 for al

l (charp)


如输出以上信息,则说明支持bonding,如果没有,说明内核不支持bonding,需要重新编译内核

3、查看现有网卡的配置信息

[root@wjq2 ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:50:56:B5:4F:F5 

          inet addr:10.1.1.58  Bcast:10.1.1.255  Mask:255.255.255.0

          inet6 addr: fe80::250:56ff:feb5:4ff5/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:1302 errors:0 dropped:0 overruns:0 frame:0

          TX packets:300 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:105872 (103.3 KiB)  TX bytes:38427 (37.5 KiB)

eth2      Link encap:Ethernet  HWaddr 00:50:56:B5:2D:0F 

          inet addr:10.1.1.59  Bcast:10.1.1.255  Mask:255.255.255.0

          inet6 addr: fe80::250:56ff:feb5:2d0f/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:923 errors:0 dropped:0 overruns:0 frame:0

          TX packets:18 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:69365 (67.7 KiB)  TX bytes:1240 (1.2 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:8 errors:0 dropped:0 overruns:0 frame:0

          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:440 (440.0 b)  TX bytes:440 (440.0 b)

4、修改物理网卡配置文件

两个物理网口分别是:eth0,eth2 绑定后的虚拟口是:bond0

[root@wjq2 network-scripts]# vim ifcfg-eth0

DEVICE=eth0

TYPE=Ethernet

UUID=adc59d0e-8c6c-4d03-8f98-d77ef5131208

ONBOOT=yes

NM_CONTROLLED=no

BOOTPROTO=none

MASTER=bond0

SLAVE=yes   #可以没有此字段,就需要开机执行ifenslave bond0 eth0 eth2命令了

DEFROUTE=yes

IPV4_FAILURE_FATAL=yes

IPV6INIT=no

NAME="System eth0"

HWADDR=00:50:56:b5:4f:f5

[root@wjq2 network-scripts]# vim ifcfg-eth2

DEVICE=eth2

TYPE=Ethernet

UUID=3fca3e29-7fd1-4355-86c2-2f76e43f4d9c

ONBOOT=yes

NM_CONTROLLED=no

BOOTPROTO=none

HWADDR=00:50:56:b5:2d:0f

MASTER=bond0

SLAVE=yes  #可以没有此字段,就需要开机执行ifenslave bond0 eth0 eth2命令了

DEFROUTE=yes

IPV4_FAILURE_FATAL=yes

IPV6INIT=no

NAME="System eth2"

5、配置逻辑网卡bond0

ifcfg-bond0配置文件需要我们手工创建

[root@wjq2 network-scripts]# vim ifcfg-bond0

DEVICE=bond0

NAME='System bond0'

TYPE=Ethernet

NM_CONTROLLED=no

USERCTL=no

ONBOOT=yes

BOOTPROTO=none

IPADDR=10.1.1.100

NETMASK=255.255.255.0

GATEWAY=10.1.1.249

6、加载模块,让系统支持bonding

/etc/modprobe.d/modprobe.conf不存在的话,可以手动创建或者使用/etc/modprobe.d/dist.conf文件也可以

[root@wjq2 ~]# vim /etc/modprobe.d/modprobe.conf  

alias bond0 bonding

options bond0 miimon=100 mode=0

说明:配置bond0的链路检查时间为100ms,模式为0。(详细的bond七种模式在最后面详细讲解)

7、加载bond module

[root@wjq2 ~]# modprobe bonding

8、查看绑定结果

[root@wjq2 ~]# cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)

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: 10000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:50:56:b5:4f:f5

Slave queue ID: 0

Slave Interface: eth2

MII Status: up

Speed: 10000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:50:56:b5:2d:0f

Slave queue ID: 0

查看物理网卡和逻辑网卡的信息

[root@wjq2 ~]# ifconfig

bond0     Link encap:Ethernet  HWaddr 00:50:56:B5:4F:F5 

          inet addr:10.1.1.100  Bcast:10.1.1.255  Mask:255.255.255.0

          inet6 addr: fe80::250:56ff:feb5:4ff5/64 Scope:Link

          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1

          RX packets:903 errors:0 dropped:0 overruns:0 frame:0

          TX packets:158 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:68972 (67.3 KiB)  TX bytes:17037 (16.6 KiB)

eth0      Link encap:Ethernet  HWaddr 00:50:56:B5:4F:F5 

          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1

          RX packets:452 errors:0 dropped:0 overruns:0 frame:0

          TX packets:79 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:34507 (33.6 KiB)  TX bytes:6935 (6.7 KiB)

eth2      Link encap:Ethernet  HWaddr 00:50:56:B5:4F:F5 

          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1

          RX packets:451 errors:0 dropped:0 overruns:0 frame:0

          TX packets:79 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:34465 (33.6 KiB)  TX bytes:10102 (9.8 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:16 errors:0 dropped:0 overruns:0 frame:0

          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)

9、测试bond

由于使用的是mode=0,负载均衡的方式,这时我们ping114.114.114.114,然后手动断开一个网卡,此时ping不会中断

[root@wjq2 ~]# ping 114.114.114.114

PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.

64 bytes from 114.114.114.114: icmp_seq=1 ttl=59 time=4.46 ms

64 bytes from 114.114.114.114: icmp_seq=1 ttl=59 time=4.56 ms (DUP!)

64 bytes from 114.114.114.114: icmp_seq=2 ttl=59 time=2.21 ms

64 bytes from 114.114.114.114: icmp_seq=2 ttl=59 time=2.28 ms (DUP!)

64 bytes from 114.114.114.114: icmp_seq=3 ttl=59 time=2.23 ms

64 bytes from 114.114.114.114: icmp_seq=3 ttl=59 time=2.24 ms (DUP!)

64 bytes from 114.114.114.114: icmp_seq=4 ttl=59 time=1.94 ms

64 bytes from 114.114.114.114: icmp_seq=4 ttl=59 time=1.94 ms (DUP!)

64 bytes from 114.114.114.114: icmp_seq=5 ttl=59 time=1.95 ms

64 bytes from 114.114.114.114: icmp_seq=5 ttl=59 time=1.95 ms (DUP!)

64 bytes from 114.114.114.114: icmp_seq=6 ttl=59 time=2.00 ms

64 bytes from 114.114.114.114: icmp_seq=6 ttl=59 time=2.01 ms (DUP!)

……

用另一个终端手动关闭eth0网卡,ping并没有中断

[root@wjq2 ~]# ifdown eth0

64 bytes from 114.114.114.114: icmp_seq=10 ttl=59 time=1.96 ms

64 bytes from 114.114.114.114: icmp_seq=11 ttl=59 time=2.12 ms

64 bytes from 114.114.114.114: icmp_seq=12 ttl=59 time=2.30 ms

64 bytes from 114.114.114.114: icmp_seq=13 ttl=59 time=2.23 ms

64 bytes from 114.114.114.114: icmp_seq=14 ttl=59 time=2.16 ms

64 bytes from 114.114.114.114: icmp_seq=15 ttl=59 time=2.04 ms

64 bytes from 114.114.114.114: icmp_seq=16 ttl=59 time=1.88 ms

查看bond0状态,发现eth0没有了,但是bond正常(只显示处于UP状态的网卡信息)

[root@wjq2 ~]# cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)

Bonding Mode: load balancing (round-robin)

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

Slave Interface: eth2

MII Status: up

Speed: 10000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:50:56:b5:2d:0f

Slave queue ID: 0

然后启动eth0网卡,发现有恢复up状态

[root@wjq2 ~]# ifup eth0

[root@wjq2 ~]#

[root@wjq2 ~]# cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)

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: 10000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:50:56:b5:4f:f5

Slave queue ID: 0

Slave Interface: eth2

MII Status: up

Speed: 10000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:50:56:b5:2d:0f

Slave queue ID: 0

三、扩展(多网卡绑定多个bond

上边是两个网卡(eth0、eth2)绑定成一个bond0,如果我们要设置多个bond口,比如物理网口eth0和eth2组成bond0,eth3和eth4组成bond1,eth5和eth6组成bond2,那么网口设置文件的设置方法和上面,是一样的,只是/etc/modprobe.d/dist.conf文件就不能叠加了。正确的设置方法有两种:

1、第一种

alias bond0 bonding

alias bond1 bonding

alias bond2 bonding

options bonding max_bonds=3 miimon=100 mode=0

这样所有的绑定只能使用一个mode了。

 

2、第二种
alias bond0 bonding

options bond0 miimon=100 mode=0 max_bonds=3

alias bond1 bonding

options bond1 miimon=100 mode=1 max_bonds=3

alias bond2 bonding

options bond2 miimon=100 mode=2 max_bonds=3

说明:这种方式不同的bond口可以设定为不同的mode,注意开机自动启动/etc/rc.d/rc.local文件的设置

特别注意:如果在配置文件的options中没有max_bonds参数,就会出现下面的报错信息:Master 'bond1': Error: handshake with driver failed.因为在用modprobe加载bonding模块时,加上参数指定创建多少个bonding device,否则默认只会创建一个,就是bond0。

[root@wjq2 ~]# ifenslave bond1 eth3 eth4
Master 'bond1': Error: handshake with driver failed. Aborting

多个bond的结果如下所示:
[root@wjq2 ~]# ifconfig
bond0     Link encap:Ethernet  HWaddr 00:50:56:B5:4F:F5 
          inet addr:10.1.1.100  Bcast:10.1.1.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:feb5:4ff5/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:2012 errors:0 dropped:0 overruns:0 frame:0
          TX packets:160 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:153964 (150.3 KiB)  TX bytes:20786 (20.2 KiB)

bond1     Link encap:Ethernet  HWaddr 00:50:56:B5:5A:25 
          inet addr:10.1.1.101  Bcast:10.1.1.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:feb5:5a25/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:1806 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:131984 (128.8 KiB)  TX bytes:536 (536.0 b)

bond2     Link encap:Ethernet  HWaddr 00:50:56:B5:0B:EB 
          inet addr:10.1.1.102  Bcast:10.1.1.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:feb5:beb/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:1806 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:131920 (128.8 KiB)  TX bytes:606 (606.0 b)

eth0      Link encap:Ethernet  HWaddr 00:50:56:B5:4F:F5 
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:1007 errors:0 dropped:0 overruns:0 frame:0
          TX packets:81 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:77033 (75.2 KiB)  TX bytes:10939 (10.6 KiB)

eth2      Link encap:Ethernet  HWaddr 00:50:56:B5:4F:F5 
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:1005 errors:0 dropped:0 overruns:0 frame:0
          TX packets:80 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:76931 (75.1 KiB)  TX bytes:10929 (10.6 KiB)

eth3      Link encap:Ethernet  HWaddr 00:50:56:B5:5A:25 
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:903 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:65983 (64.4 KiB)  TX bytes:252 (252.0 b)

eth4      Link encap:Ethernet  HWaddr 00:50:56:B5:5A:25 
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:903 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:66001 (64.4 KiB)  TX bytes:284 (284.0 b)

eth5      Link encap:Ethernet  HWaddr 00:50:56:B5:0B:EB 
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:903 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:65951 (64.4 KiB)  TX bytes:342 (342.0 b)

eth6      Link encap:Ethernet  HWaddr 00:50:56:B5:0B:EB 
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:903 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:65969 (64.4 KiB)  TX bytes:264 (264.0 b)

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:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b)

四、bond的七种模式

像samba、nfs这种共享文件系统,网络的吞吐量非常大,就造成网卡的压力很大,网卡bond是通过把多个物理网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡,具体的功能取决于采用的哪种模式。  
1、mode=0(balance-rr)(平衡抡循环策略)

链路负载均衡,增加带宽,支持容错,一条链路故障会自动切换正常链路。交换机需要配置聚合口,思科叫port channel。
特点:传输数据包顺序是依次传输(即:第1个包走eth0,下一个包就走eth2….一直循环下去,直到最后一个传输完毕),此模式提供负载平衡和容错能力;但是我们知道如果一个连接或者会话的数据包从不同的接口发出的话,中途再经过不同的链路,在客户端很有可能会出现数据包无序到达的问题,而无序到达的数据包需要重新要求被发送,这样网络的吞吐量就会下降

2、mode=1(active-backup)(主-备份策略)

这个是主备模式,只有一块网卡是active,另一块是备用的standby,所有流量都在active链路上处理,交换机配置的是捆绑的话将不能工作,因为交换机往两块网卡发包,有一半包是丢弃的。
特点:只有一个设备处于活动状态,当一个宕掉另一个马上由备份转换为主设备。mac地址是外部可见得,从外面看来,bond的MAC地址是唯一的,以避免switch(交换机)发生混乱。
此模式只提供了容错能力;由此可见此算法的优点是可以提供高网络连接的可用性,但是它的资源利用率较低,只有一个接口处于工作状态,在有 N 个网络接口的情况下,资源利用率为1/N

3、mode=2(balance-xor)(平衡策略)

表示XOR Hash负载分担,和交换机的聚合强制不协商方式配合。(需要xmit_hash_policy,需要交换机配置port channel)
特点:基于指定的传输HASH策略传输数据包。缺省的策略是:(源MAC地址 XOR 目标MAC地址) % slave数量。其他的传输策略可以通过xmit_hash_policy选项指定,此模式提供负载平衡和容错能力