Ubuntu服務器使用active-backup模式進行網口綁定

Ubuntu服務器使用active-backup模式進行網口綁定

簡介

在這次阿拉善的出差過程中,自己也在疲憊的工作中學到一些可以在以後的工作中使用或者自己覺得重要的東西,昨天晚上已經總結過XShell快速命令配置使用,在阿拉善出差的9天中,還有另外兩個主題的工作內容需要總結。

  • Ubuntu服務器使用active-backup(模式1)模式進行網口綁定
  • Ubuntu服務器使用動態鏈接聚合(模式4)模式進行網口綁定
  • CPU核數變爲1如何解決

在9天的出差過程中,雖然被搞得很疲憊,但自己依然從過程中學到了一些東西,通過把這些東西總結,並輸出博客上,加深理解,防止在以後的工作中遇到相同的場景而束手無策。畢竟經驗是需要積攢的。不要讓一個問題在第一次遇到時是一個問題,在第二次遇到時,依然是一個問題,這樣,我們便在不同的不斷的小進步,當積少成多,量變導致質變,會很客觀的。

在操作是,主要是參考Ubuntu 網口綁定進行實踐的

爲什麼要進行網口綁定

綁定,也稱爲端口聚合或鏈路聚合,意味着將多個網絡接口(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驅動。

網口綁定配置過程

總覽

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

配置步驟

安裝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 static
#address 66.10.66.168
#netmask 255.255.255.0
#gateway 66.10.66.1

#auto enp96s0f1
#iface enp96s0f1 inet static
#address 9.9.9.246
#netmask 255.255.255.0
#gateway 9.9.9.1

auto enp96s0f0
iface enp96s0f0 inet manual
bond-master bond0
bond-primary enp96s0f0

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 active-backup
bond-miimon 100
bond-slaves none

重啓網絡

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

如果能夠正常的重啓網絡,則不需要重啓,否則需要通過reboot命令重啓網絡。

查看結果

/proc/net/bonding/bond0

在配置完interfaces之後,通過重啓網絡或者重啓服務器,如果能夠正常運行,可以看到下面的結果

root@ubuntu-desktop:~# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
Bonding Mode: fault-tolerance (active-backup)(容錯能力)
Primary Slave: None
Currently Active Slave: enp96s0f0

MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: enp96s0f1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:11:0a:57:5f:4e
Slave queue ID: 0
Slave Interface: eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:11:0a:57:5f:4f
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服務器使用active-backup模式配置端口綁定已經成功了。

驗證

驗證的方式較爲簡單,當enp96s0f1和enp96s0f0兩個網卡組合成模式1的網口綁定時,表示兩個網卡形成主備模式,當網卡enp96s0f0故障時,可以直接把網線插入另一個網卡,系統仍然工作,就好像兩個網卡是一個一樣。

總結

通過本文,詳細介紹了使用active-backup模式配置Ubutun服務器的過程,算是情景在現吧,這樣當再次出現相同情景時,只要按部就班,就能自信的完成網口綁定的工作了。雖然現在總結了一上午挺累的,但還是挺開心的。

參考

  2020年5月24日 12點37分於奧克斯時代未來之城
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章