Ubuntu服务器使用动态链接聚合模式进行网口绑定

Ubuntu服务器使用动态链接聚合模式进行网口绑定

简介

在这次阿拉善的出差过程中,自己也在疲惫的工作中学到一些可以在以后的工作中使用或者自己觉得重要的东西,昨天晚上已经总结过XShell快速命令配置使用,在阿拉善出差的9天中,还有另外两个主题的工作内容需要总结。

在9天的出差过程中,虽然被搞得很疲惫,但自己依然从过程中学到了一些东西,通过把这些东西总结,并输出博客上,加深理解,防止在以后的工作中遇到相同的场景而束手无策。毕竟经验是需要积攒的。不要让一个问题在第一次遇到时是一个问题,在第二次遇到时,依然是一个问题,这样,我们便在不同的不断的小进步,当积少成多,量变导致质变,会很客观的。

在操作是,主要是参考Ubuntu 网口绑定进行实践的,实现了active-backup模式

为什么要进行网口绑定

绑定,也称为端口聚合或链路聚合,意味着将多个网络接口(NIC)组合到单个链路,从而提供高可用性(容错),负载平衡,最大吞吐量或这些组合。 说白了,也是客户要求,通过这种网口绑定的机制实现高性能、可靠性,没有办法,只能去实现了。

网口绑定的模式

  • 0:(balance-rr) Round-robin policy: (平衡轮询策略):传输数据包顺序是依次传输,直到最后一个传输完毕,此模式提供负载平衡和容错能力。
  • 1:(active-backup) Active-backup policy:(活动备份策略):只有一个设备处于活动状态。一个宕掉另一个马上由备份转换为主设备。mac地址是外部可见得。此模式提供了容错能力
  • 2:(balance-xor) XOR policy:(平衡策略):传输根据[(源MAC地址xor目标MAC地址)mod设备数量]的布尔值选择传输设备。 此模式提供负载平衡和容错能力。
  • 3:(broadcast) Broadcast policy:(广播策略):将所有数据包传输给所有设备。此模式提供了容错能力。
  • 4:(802.3ad) IEEE 802.3ad Dynamic link aggregation. IEEE 802.3ad 动态链接聚合:创建共享相同的速度和双工设置的聚合组。此模式提供了容错能力。每个设备需要基于驱动的重新获取速度和全双工支持;如果使用交换机,交换机也需启用 802.3ad 模式。
  • 5:(balance-tlb) Adaptive transmit load balancing(适配器传输负载均衡):通道绑定不需要专用的交换机支持。发出的流量根据当前负载分给每一个设备。由当前设备处理接收,如果接受的设 备传不通就用另一个设备接管当前设备正在处理的mac地址。
  • 6:(balance-alb) Adaptive load balancing: (适配器负载均衡):包括mode5,由 ARP 协商完成接收的负载。bonding驱动程序截获 ARP在本地系统发送出的请求,用其中之一的硬件地址覆盖从属设备的原地址。就像是在服务器上不同的人使用不同的硬件地址一样。

材料

ifenslave

负载均衡软件fenslave,它是一款linux下的负载均衡工具,可以将数据包有效的分配到bonding驱动。

交换机

动态链接聚合的网口绑定模式需要交换机进行支持,由于交换机的配置不是本文的重点,因此不再重点强调。可以参考文章Ubuntu及RHEL linux环境下多端口捆绑实战验证(基于模式4和1)

网口绑定配置过程

总览

配置的主要步骤如下所示:

配置步骤

安装ifenslave

负载均衡软件fenslave,它是一款linux下的负载均衡工具,可以将数据包有效的分配到bonding驱动。

安装如果在联网环境下,可以采用

apt-get install ifenslave

如果是在离线环境下,可以在ifenslave 2.7ubuntu1 (amd64 binary) in ubuntu xenial下载ifenslave的安装包和源码包。

在此需要简单的说明一下,在安装该可执行程序时,由于自己不知道ifenslave是否依赖其他文件,因此在网站上查看该软件包,不知为何就觉得一定依赖很多文件,因此就直接下载了源码包(并不明白为什么自己会先入为主),然后自己花时间通过光盘把源码包导入到服务器上,虽然解压就可以使用,但由于不知道应该把可执行文件放置在何处,这种方式不得不作罢。

我同事说,或许可以直接安装了,这样可以按照程序自己设定的位置去自动放置应该位于的位置。因此自己就转向了这个思路,自己采用源码安装的思路是因为自己觉得很有可能,这个软件在安装时会需要依赖其他的软件,一旦产生依赖树,依赖爆炸的问题就没有办法解决了。然而实际上,这种问题并不存在,而是自己这种自己吓唬自己的思维方式限制了自己,导致自己浪费了一些时间,自己很明显应该先去验证是否真的存在依赖爆炸的问题,就像上图中,ifenslave2.7依赖了命令ifupdown(>=0.7.46),自己可以通过

dpkg -l | grep ifupdown

sqh@sqh-virtual-machine:~$ dpkg -l | grep -E "ifupdown|iproute2"
ii  ifupdown  0.8.10ubuntu1.4         amd64   high level tools to configure network interfaces
ii  iproute2  4.3.0-1ubuntu3.16.04.4  amd64   networking and traffic control tools
sqh@sqh-virtual-machine:~$

这就表明,ifupdown所需要的依赖,系统已经预先安装,因此可以放心的直接下载可执行deb包进行安装。

离线压缩包

可以在ifenslave 2.7ubuntu1 (amd64 binary) in ubuntu xenial下载ifenslave的安装包和源码包。

在获取离线压缩包ifenslave_2.7ubuntu1_all.deb,使用dpkg -i 即可实现安装。

dpkg -i ifenslave_2.7ubuntu1_all.deb

配置内核管理

介绍一下几个命令lsmodmodinfomodprobe

lsmod主要是显示系统当前加载了那些内核模块。

NAME
       lsmod - Show the status of modules in the Linux Kernel

SYNOPSIS
       lsmod

DESCRIPTION
       lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded.
       简言之,lsmod就是表示系统加载了那些内核。
       

modinfo则用于提取内核模块的信息。可以通过该命令查看内核模块是否已经存在,并且被系统所支持

MODINFO(8)                                         modinfo                                        MODINFO(8)

NAME
       modinfo - Show information about a Linux Kernel module

SYNOPSIS
MODINFO(8)                                         modinfo                                        MODINFO(8)

NAME
       modinfo - Show information about a Linux Kernel module

SYNOPSIS
       modinfo [-0] [-F field] [-k kernel] [modulename|filename...]

       modinfo -V

       modinfo -h

DESCRIPTION
       modinfo extracts information from the Linux Kernel modules given on the command line. If the module
       name is not a filename, then the /lib/modules/version directory is searched, as is also done by
       modprobe(8) when loading kernel modules.

       modinfo by default lists each attribute of the module in form fieldname : value, for easy reading.
       The filename is listed the same way (although it's not really an attribute).

       This version of modinfo can understand modules of any Linux Kernel architecture.

modprobe的主要作用从Linux内核中添加和移除模块。

MODPROBE(8)                                       modprobe                                       MODPROBE(8)

NAME
       modprobe - Add and remove modules from the Linux Kernel

因此我的思路也是一样的,首先判断内核模块是否存在。

注意:通过modinfo可以查看内核模块支持的参数,从下属的命令中,可以看到bonding的重要参数包括

miimon(Link check interval in milliseconds (int))、mode(Mode of operation; 0 for balance-rr, 1 for active-backup, 2 for balance-xor, 3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, 6 for balance-alb (charp))、primary(Primary network device to use (charp))等重要参数

sqh@sqh-virtual-machine:~$ modinfo bonding
filename:       /lib/modules/4.15.0-46-generic/kernel/drivers/net/bonding/bonding.ko
author:         Thomas Davis, [email protected] and many others
description:    Ethernet Channel Bonding Driver, v3.7.1
version:        3.7.1
license:        GPL
alias:          rtnl-link-bond
srcversion:     10478C9FE27D36218C487F5
depends:        
retpoline:      Y
intree:         Y
name:           bonding
vermagic:       4.15.0-46-generic SMP mod_unload 
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 peer notifications to send on failover event (alias of num_unsol_na) (int)
parm:           num_unsol_na:Number of peer notifications to send on failover event (alias of num_grat_arp) (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 for 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 of 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:802.3ad aggregation selection logic; 0 for stable (default), 1 for bandwidth, 2 for count (charp)
parm:           min_links:Minimum number of available links before turning on carrier (int)
parm:           xmit_hash_policy:balance-xor and 802.3ad hashing method; 0 for layer 2 (default), 1 for layer 3+4, 2 for layer 2+3, 3 for encap layer 2+3, 4 for encap layer 3+4 (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 all (charp)
parm:           arp_all_targets:fail on any/all arp targets timeout; 0 for any (default), 1 for all (charp)
parm:           fail_over_mac:For active-backup, do not set all slaves to the same MAC; 0 for none (default), 1 for active, 2 for follow (charp)
parm:           all_slaves_active:Keep all frames received on an interface by setting active flag for all slaves; 0 for never (default), 1 for always. (int)
parm:           resend_igmp:Number of IGMP membership reports to send on link failure (int)
parm:           packets_per_slave:Packets to send per slave in balance-rr mode; 0 for a random slave, 1 packet per slave (default), >1 packets per slave. (int)
parm:           lp_interval:The number of seconds between instances where the bonding driver sends learning packets to each slaves peer switch. The default is 1. (uint)

由此,我们可以看到,Ubuntu 16.04支持模块bonding。

可以执行命令

sqh@sqh-virtual-machine:~$ lsmod | grep modinfo
sqh@sqh-virtual-machine:~$ lsmod | grep bonding
sqh@sqh-virtual-machine:~$ modprobe bonding
modprobe: ERROR: could not insert 'bonding': Operation not permitted
sqh@sqh-virtual-machine:~$ su 
Password: 
root@sqh-virtual-machine:/home/sqh# modprobe bonding
root@sqh-virtual-machine:/home/sqh# lsmod | grep bonding
bonding               163840  0
root@sqh-virtual-machine:/home/sqh# 

从上面可以看到,确实只有在使用root用户加载了bonding模块之后,lsmod才能看到该模块。

配置开机自动加载模块

配置系统开机自动加载模块

sudo vim /etc/modules

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
bonding

配置interfaces

配置网口绑定一个较为重要的内容,就是在配置文件**/etc/network/interfaces**中配置网口绑定的模式和检测率

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto enp96s0f0
iface enp96s0f0 inet manual
bond-master bond0

auto enp96s0f1
iface enp96s0f1 inet manual
bond-master bond0


auto bond0
iface bond0 inet static
address 66.10.66.168
netmask 255.255.255.0
gateway 66.10.66.1
bond-mode 4
bond-miimon 100
bond-slaves none

重启网络

/etc/init.d/networing restart
/etc/init.d/network-manager restart

如果能够正常的重启网络,则不需要重启,否则需要通过reboot命令重启网络。

查看结果

/proc/net/bonding/bond0

在配置完interfaces之后,通过重启网络或者重启服务器,如果能够正常运行,可以看到下面的结果

root@aaron-desktop:~# cat /proc/net/bonding/bond0

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

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
Min links: 0
Aggregator selection policy (ad_select): stable
Active Aggregator Info:
        Aggregator ID: 1
        Number of ports: 2
        Actor Key: 17
        Partner Key: 1
        Partner Mac Address: f0:9e:63:f2:ec:80
 Slave Interface: enp96s0f0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:11:0a:57:5f:4e
Aggregator ID: 1
Slave queue ID: 0

Slave Interface: enp96s0f1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:11:0a:57:5f:4f
Aggregator ID: 1
Slave queue ID: 0

ifconfig命令

root@ubuntu-desktop:~# ifconfig

bond0     Link encap:Ethernet  HWaddr 00:11:0a:57:5f:4e  

          inet addr:192.168.4.200  Bcast:192.168.4.255  Mask:255.255.255.0

          inet6 addr: fe80::211:aff:fe57:5f4e/64 Scope:Link

          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1

          RX packets:78 errors:0 dropped:2 overruns:0 frame:0

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

          collisions:0 txqueuelen:0

          RX bytes:7645 (7.6 KB)  TX bytes:18267 (18.2 KB)

enp96s0f0 Link encap:Ethernet  HWaddr 00:11:0a:57:5f:4e  

          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:7525 (7.5 KB)  TX bytes:18267 (18.2 KB)

enp96s0f1 Link encap:Ethernet  HWaddr 00:11:0a:57:5f:4e  

          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:120 (120.0 B)  TX bytes:0 (0.0 B)

通过对命令可以看到,虚拟出来的网卡bond0和两个物理网卡的Mac地址都变成同一个了,

00:11:0a:57:5f:4e

这也就表示在Ubuntu16.04服务器使用Dynamic link aggregation动态链接聚合模式配置端口绑定已经成功了。

验证

验证的方式较为简单,两个网卡同时插上网线,并连接到交换机上,断掉其中任何一个,不影响使用。

总结

通过本文,详细介绍了使用Dynamic link aggregation模式配置Ubutun服务器的过程,算是情景在现吧,这样当再次出现相同情景时,只要按部就班,就能自信的完成网口绑定的工作了。虽然现在总结了一上午挺累的,但还是挺开心的。

参考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章