Linux Bonding 多网卡汇聚

Posted by Sunday on 2018-07-24

什么是Bonding

早期由于网卡的速度还不够快,那如果你的服务器需要比较大的频宽使用时,就得要购买更昂贵的万兆网卡才行。那为什么不能将几张网卡合并成为一张来扩大频宽呢?此外,对于重要的服务来说,网络是不能中断的!所以,能不能使用两条以上的线路连接到我的服务器呢?因此,合并网卡的频宽让网卡具有容错能力(容错)就成了Linux bonding最主要的考量了!目前Linux bonding功能已经加入核心,所以你只要启动它即可!不需要额外安装其他软件呦!

Bonding的模式与功能

bonding根据当初设计理念的不同,有许多常用的模式可以参考:

  • 模式0,循环负载平衡合并频宽(Round-robin,balance-rr):
    Linux bonding会将发送的封包循序的从任何可用的网卡上面循环发送,因为是循环发送的,所以每张网卡就能够充分利用〜不过,就鸟哥的经验来看,这种模式的负载平衡方面似乎没有想像中的好!所以鸟哥不建议使用。

  • 模式1,自动备援模式(Active-backup):
    假设有三个网卡做成这种bonding模式的话,那么永远都只有一个网卡会运作,我们称之为主要网卡(primary)!除非这个主要网卡连线失败了,另一张网卡(slave)才会扶正而成为下一个主要网卡,并持续提供服务。这种模式并不会合并频宽,只会用在连线的容错而已。

  • 模式4,LACP链路聚合模式(IEEE 802.3ad Dynamic link aggregation):
    这种模式算是业界的标准支援吧!你的网卡启动在这种模式下,然后让串接到这些网卡的交换机端口设定好LACP群组,这样就能够达成与LACP相同的频宽倍增功能!这种模式得要有支持网管且提供LACP设定的交换机才行!这种模式可以同时提供合并频宽与网络容错!

  • 模式5,自动调整传输负载平衡(Adaptive transmit load balancing,balance-tlb):
    在传送方面,这种模式会将封包分散在各个可用的bonding网卡上送出,因此传送才能够达到合并频宽。不过在接收封包时,由于考虑到交换机的MAC记忆能力,因此仅有一个网卡的MAC会被记录于交换机上头,简单的说,就是仅有一张网卡会用于接收!除非该接收网卡挂点,否则其他网卡不会被用于接收上。这种模式也算挺适合用在「主要在发送资料给用户端的服务器」上,如果服务器也要负责大量资料的接收,恐怕就不是这么适合了。

  • 模式6,自动调整全负载平衡(Adaptive transmit load balancing,balance-alb):
    这个模式包括了模式5,除了传送可以合并频宽之外,连接收也可以合并频宽了。当服务器要传资料出去给用户端时,bonding模组会主动的拦截封包,并透过ARP协商机制(ARP就是透过IP去找出MAC的通讯协定),将不同的网卡要送出到同一个用户端的封包,都改写成单一一个固定的发送端MAC位址,如此一来,发送有合并频宽的功能了,但接收却还是只有一个MAC而已对吧?那怎么说接收有合并频宽的负载平衡机制呢?

    原因是这样的:当有资料封包要送出到多个不同的用户端时,此模式的bonding模组就会透过ARP协商机制,找出bonding管理的比较闲置的网卡MAC分配给下个用户端,如此一来,不同的用户端回传给服务器的资料,就可以透过不同的网卡来接收,就能达到接收也合并频宽的功能了。

    这个模式不需要特别的交换机支援,而且设定简单,可以在接收,传送都达成合并频宽的能力,且也具有基本的网路容错功能,是目前鸟哥最爱使用的绑定模式啦!

常用的三种Bonding模式

mode=0:平衡负载模式,有自动备援,但需要交换机支支援及设定。
mode=1:自动备援模式,其中一条线若断线,其他线路将会自动备援。
mode=6:平衡负载模式,有自动备援,不需要交换机支援及设定。

Bond 应用场景

你得要知道为什么要做bonding呢?可能的原因有两个:

  • 为了让网卡连线有备援,因此断了一条网线的时候,另一条网卡会主动接管
  • 为了增加频宽

如果是为了第一点,那使用bonding模式1(Active-backup)的功能最好!
如果是为了第二点,那你得要注意,就是你服务器提供的资料速度要比两张网卡更快,所以你的硬盘不能太慢(当然,如果传输的资料是在内存上,那就无关硬盘)
所以,如果是为了增加频宽,那你就应该要有这样的想法:

  • 你的服务器总体读/写速度要高于所有网卡的频宽总和才好
  • 你的网卡所连接的交换机总背板频宽(bandwidth)要能够负荷邦定的最高流量
  • 用户端的数量要比网卡数还要多才合理

环境说明

模式:mode=6
eth0:私有地址
bond: eth1 eth2 双网卡绑定

一些设置

1
2
3
4
5
6
/etc/init.d/NetworkManager stop
chkconfig NetworkManager off
/etc/init.d/network restart
/etc/init.d/iptables stop
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

Bond 配置

查看网卡状态

1
2
3
4
5
6
nmcli conn show

NAME UUID TYPE DEVICE
Wired connection 1 fe17083e-df87-3723-bdb9-010cf77fa1db ethernet eth1
Wired connection 2 175c3f7f-ab9a-31d2-b86d-03e64c383830 ethernet eth2
eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet eth0

建立bonding网卡

1
nmcli connection add con-name bond0 ifname bond0 type bond bond.options "miimon=100,mode=6"

查看状态

1
2
3
4
5
6
7
nmcli connection show 

NAME UUID TYPE DEVICE
Wired connection 1 fe17083e-df87-3723-bdb9-010cf77fa1db ethernet eth1
Wired connection 2 175c3f7f-ab9a-31d2-b86d-03e64c383830 ethernet eth2
bond0 8bda5484-3841-4318-8642-995d6a2faeaf bond bond0
eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet eth0

将eth1和eth2 绑定到bond0

1
2
nmcli connection add con-name eth1 ifname eth1 type bond-slave master bond0
nmcli connection add con-name eth2 ifname eth2 type bond-slave master bond0

查看状态

1
2
3
4
5
6
7
8
9
nmcli connection show

NAME UUID TYPE DEVICE
Wired connection 1 fe17083e-df87-3723-bdb9-010cf77fa1db ethernet eth1
Wired connection 2 175c3f7f-ab9a-31d2-b86d-03e64c383830 ethernet eth2
bond0 8bda5484-3841-4318-8642-995d6a2faeaf bond bond0
eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet eth0
eth1 5f5c0a0e-a0e4-430a-95de-5178126c6932 ethernet --
eth2 b573daec-ab0b-4f86-839b-ca2536bcfc99 ethernet --

设置静态IP

1
2
nmcli connection modify bond0 ipv4.method manual ipv4.addresses 192.168.8.10/24 ipv4.gateway 192.168.8.2
nmcli connection modify bond0 ipv4.method manual

激活bond0 eth1 eth2

1
2
3
nmcli connection up bond0
nmcli connection up eth1
nmcli connection up eth2

查看状态

1
2
3
4
5
6
7
8
9
nmcli connection show

NAME UUID TYPE DEVICE
bond0 8bda5484-3841-4318-8642-995d6a2faeaf bond bond0
eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet eth0
eth1 5f5c0a0e-a0e4-430a-95de-5178126c6932 ethernet eth1
eth2 b573daec-ab0b-4f86-839b-ca2536bcfc99 ethernet eth2
Wired connection 1 fe17083e-df87-3723-bdb9-010cf77fa1db ethernet --
Wired connection 2 175c3f7f-ab9a-31d2-b86d-03e64c383830 ethernet --

查看bonding状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cat /proc/net/bonding/bond0 

Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: adaptive load balancing
Primary Slave: None
Currently Active Slave: eth1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:03:24:4a
Slave queue ID: 0

Slave Interface: eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:03:24:54
Slave queue ID: 0

故障测试

1
2
cat /proc/net/bonding/bond0  | grep "Currently Active Slave"
Currently Active Slave: eth1

ping bond0 ip,拔掉eth1网线,会自动切换到eth2,ping不中断
或命令

1
2
ip link set eth1 down
#ip link set eth1 up

1
2
3
4
5
6
7
8
9
10
11
12
ping 192.168.8.10

PING 192.168.8.10 (192.168.8.10) 56(84) bytes of data.
64 bytes from 192.168.8.10: icmp_seq=1 ttl=64 time=0.352 ms
64 bytes from 192.168.8.10: icmp_seq=2 ttl=64 time=0.179 ms
64 bytes from 192.168.8.10: icmp_seq=3 ttl=64 time=0.157 ms
64 bytes from 192.168.8.10: icmp_seq=4 ttl=64 time=0.197 ms
64 bytes from 192.168.8.10: icmp_seq=5 ttl=64 time=0.197 ms
64 bytes from 192.168.8.10: icmp_seq=6 ttl=64 time=0.205 ms
64 bytes from 192.168.8.10: icmp_seq=7 ttl=64 time=0.298 ms
64 bytes from 192.168.8.10: icmp_seq=8 ttl=64 time=0.281 ms
64 bytes from 192.168.8.10: icmp_seq=9 ttl=64 time=0.285 ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cat /proc/net/bonding/bond0 

Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: adaptive load balancing
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: eth1
MII Status: down
Speed: Unknown
Duplex: Unknown
Link Failure Count: 1
Permanent HW addr: 00:0c:29:03:24:4a
Slave queue ID: 0

Slave Interface: eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:03:24:54
Slave queue ID: 0

删除bonding

1
2
3
nmcli connection delete eth1
nmcli connection delete eth2
nmcli connection delete bond0

脚本1 NetworkManager命令

1
2
3
4
5
6
7
8
9
10
nmcli connection add con-name bond0 ifname bond0 type bond bond.options "miimon=100,mode=6"
nmcli connection add con-name eth1 ifname eth1 type bond-slave master bond0
nmcli connection add con-name eth2 ifname eth2 type bond-slave master bond0
nmcli connection modify bond0 ipv4.method manual ipv4.addresses 192.168.8.10/24 ipv4.gateway 192.168.8.2
nmcli conn modify bond0 ipv4.dns 192.168.8.2
nmcli connection modify bond0 ipv4.method manual
nmcli connection up bond0
nmcli connection up eth1
nmcli connection up eth2
cat /proc/net/bonding/bond0

脚本2 Shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
cat << EOF >/etc/sysconfig/network-scripts/ifcfg-bond0
BONDING_OPTS="miimon=100 mode=balance-alb"
TYPE=Bond
BONDING_MASTER=yes
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=bond0
DEVICE=bond0
ONBOOT=yes
IPADDR=192.168.8.10
PREFIX=24
GATEWAY=192.168.8.2
DNS1=192.168.8.2
EOF

cat << EOF >/etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
NAME=eth1
DEVICE=eth1
ONBOOT=yes
MASTER=bond0
SLAVE=yes
EOF

cat << EOF >/etc/sysconfig/network-scripts/ifcfg-eth2
TYPE=Ethernet
NAME=eth2
DEVICE=eth2
ONBOOT=yes
MASTER=bond0
SLAVE=yes
EOF

systemctl restart network
cat /proc/net/bonding/bond0

参考:
http://dic.vbird.tw/linux_server/unit03.php