Linux多網卡綁定(bond)及網絡組(team)


Linux多網卡綁定(bond)及網絡組(team)

  很多時候,由於生產環境業務的特殊需求,我們需要對服務器的物理網卡實施特殊的配置,從而來滿足不同業務場景下對服務器網絡的特殊性要求。如高並發的網絡IO型業務,需要高速的網絡IO,即對網卡的收收發包處理能力及網卡最大帶寬速度等性能指標提出了更高的要求;事務處理型的系統,如金融交易系統、電商平台等,對物理網絡線路、網卡等物理設備的穩定可靠性提出了更高的要求。

  Linux系統中,可以通過多網卡綁定(bonding)和網絡組(team)等技術,通過軟件的方式來實現,來滿足不同業務場景下的各種特殊需求。

Linux多網卡綁定(bond)

bond基本原理

  bond技術是將多塊物理網卡綁定同一IP地址對外提供服務,通過不同的模式配置,從而達到高可用、負載均衡及鏈路冗余等效果。我們知道,兩塊物理網卡是不可以直接配置同一個IP地址的,多網卡bond技術的基本原理就是通過虛擬出一塊邏輯網卡對外提供網絡連接。

bond基本工作原理如下圖:

bond七種工作模式及特性詳解

  bond技術提供了(0-6)共7種工作模式,分別如下:

  • mode=0 round-robin輪詢策略(balance-rr)

  • mode=1 active-backup主備策略(active-backup)

  • mode=2 load balancing (xor)異或策略(balance-xor)

  • mode=3 fault-tolerance (broadcast)廣播策略(broadcast)

  • mode=4 lacp IEEE 802.3ad 動態鏈路聚合(IEEE 802.3ad Dynamic link aggregation) (802.3ad)

  • mode=5 transmit load balancing適配器傳輸負載均衡(balance-tlb)

  • mode=6 adaptive load balancing適配器負載均衡(balance-alb)

以上7種工作模式的定義如下:

  • mode=0 round-robin輪詢策略(balance-rr)
cat /proc/net/bonding/bond0 
Bonding Mode: load balancing (round-robin)

此模式下,鏈路處於負載均衡狀態,數據以輪詢方式向每條鏈路發送報文,基於per packet方式發送。即每條鏈路各一個數據包。這模式好處在於增加了帶寬,同時支持容錯能力,當有鏈路出問題,會把流量切換到正常的鏈路上。該模式下,交換機端需要配置聚合口,在cisco交換機上叫port channel。

mode0工作模式圖如下:

  • mode=1 active-backup主備策略(active-backup)
cat /proc/net/bonding/bond0
Bonding Mode: fault-tolerance (active-backup) 

此模式下,一個端口處於主狀態,一個處於備狀態,所有流量都在主鏈路上發出和接收,備用鏈路不會有任何流量。當主鏈路端口down掉時,備用端口立即啟動接管主狀態,從而達到鏈路冗余熱備目的。這種模式接入不需要交換機端支持。

mode1工作模式圖如下:

  • mode=2 load balancing (xor)異或策略(balance-xor)
cat /proc/net/bonding/bond0
Bonding Mode: load balancing (xor) 

在此模式下,通過源和目標mac做hash因子來做xor算法來選擇鏈路,這樣就使得到達特定對端的流量總是從同一個接口上發出。和balance-rr一樣,交換機端口需要配置為“port channel”。需要注意的是,若選擇這種模式,如果所有流量源和目標mac都固定了,例如使用“網關模式”,即所有對外的數據傳輸均固定走一個網關,那么根據該模式的描述,分發算法算出的線路就一直是同一條,另外一條鏈路不會有任何數據流,那么這種模式就沒有多少意義了。

mode2模式拓補圖同mode0、mode1

  • mode=3 fault-tolerance (broadcast)廣播策略
cat /proc/net/bonding/bond0
Bonding Mode: fault-tolerance (broadcast)

這種模式的特點是一個報文會復制兩份往bond下的兩個接口分別發送出去。當有對端交換機失效,我們感覺不到任何丟包。這個模式也需要交換機配置聚合口。如下圖,帶dup的行即是重復發往第2個網卡的數據。

mode3模式的拓撲圖如下所示:

  • mode=4 lacp IEEE 802.3ad 動態鏈路聚合(802.3ad)

該模式拓撲結構與mode1模式相同

cat /proc/net/bonding/bond0
Bonding Mode: IEEE 802.3ad Dynamic link aggregation

該模式是基於IEEE 802.3ad Dynamic link aggregation(動態鏈接聚合)協議。在該模式下,操作系統和交換機都會創建一個聚合組,在同一聚合組下的網口共享同樣的速率和雙工設定。操作系統根據802.3ad 協議將多個slave 網卡綁定在一個聚合組下。聚合組向外發送數據選擇哪一塊兒網卡是基於傳輸hash 策略,該策略可以通過xmit_hash_policy 選項從缺省的XOR 策略改變到其他策略。

該模式的必要條件:

1、ethtool 支持獲取每個slave 的速率和雙工設定;

2、交換機支持IEEE 802.3ad Dynamic link aggregation(動態鏈路聚合)。大多數交換機需要經過特定配置才能支持802.3ad模式。

  • mode=5 transmit load balancing適配器傳輸負載均衡(balance-tlb)
cat /proc/net/bonding/bond0
Bonding Mode: transmit load balancing

這種模式相較mode2異或策略及mode4 LACP模式的hash策略相對智能,會主動根據對端的MAC地址上的流量,智能的分配流量從哪個網卡發出。但不足之處在於,仍使用一塊網卡接收數據。存在的問題與load balancing (xor)也是一樣的一樣,如果對端MAC地址是唯一的,那么策略就會失效。這個模式下bond成員使用各自的mac,而不是上面幾種模式是使用bond0接口的mac。此模式無需交換機支持。

此模式拓撲圖如下:

  • mode=6 adaptive load balancing適配器負載均衡(balance-alb)

此模式拓撲結構與mode5相同。

cat /proc/net/bonding/bond0
Bonding Mode: adaptive load balancing

該模式除了balance-tlb適配器傳輸負載均衡模式的功能外,同時加上針對IPV4流量接收的負載均衡。接收負載均衡是通過ARP協商實現的。在進行ARP協商的過程中,bond模塊將對端和本地的mac地址進行綁定,這樣從同一端發出的數據,在本地也會一直使用同一塊網卡來接收。若是網絡上發出的廣播包,則由不同網卡輪詢的方式來進行接收。通過這種方式實現了接收的負載均衡。該模式同樣無需交換機支持。

注意:在網卡綁定的七種模式下,其中mode=0、2、3需要交換機支持EtherChannel,mode=4需要交換機同時支持EtherChannel和LACP,mode=1、5、6不需要交換機配置支持。

以上7種工作模式,可以根據實際生產環境中的業務場景需求來靈活的選擇配置。

Linux多網卡bond配置

1、手動創建配置文件方式

創建bond0網卡配置文件
[root@centos6 ~]#vim /etc/sysconfig/network-scripts/ifcfg-bond0 
DEVICE=bond0                           #虛擬網卡設備名稱
BOOTPROTO=none                         #使用靜態IP
BONDING_OPTS="mode=1 miimon=100"   #bond模式設定,此例使用mode1模式 miimon=100 為每隔100ms監控主網卡的狀態
IPADDR=192.168.20.100    #IP地址
PREFIX=24                #子網掩碼
GATEWAY=192.168.20.2     #默認網關
DNS1=114.114.114.114     #DNS服務器

修改第一塊網卡配置文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

修改第二塊網卡配置文件
vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

將所有物理網卡啟用
[root@centos6 ~]#cd /etc/sysconfig/network-scripts/
ifconfig eth0 up
ifconfig eth1 up

重啟網絡服務
[root@centos6 ~]#/etc/init.d/network restart
Shutting down interface bond0:                             [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface bond0:  Determining if ip address 192.168.20.100 is already in use for device bond0...
                                                           [  OK  ]
[root@centos6 ~]#

可以看到bond0網絡已成功啟動並獲得IP地址=

查看一下當前bond0網卡的工作狀態

[root@centos6 ~]#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: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

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

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:54:90:fc
Slave queue ID: 0
[root@centos6 ~]#

可以看到當前工作在mode1模式,且eth0網卡為活動的主網卡,eth1處於備用狀態

注意:在centos6系統下,由於NetworkManager服務於network會存在相互干擾問題,建議將NetworkManager服務停止,否則容易導致發生以下錯誤。

禁用NetworkManager服務

[root@centos6 network-scripts]#/etc/init.d/NetworkManager stop
[root@centos6 network-scripts]#chkconfig NetworkManager off

2、通過nmcli命令配置網卡bond

在centos7下,可以通過nmctl命名來創建多網卡bond配置。

創建bond接口並設置IP地址信息
[root@centos7 ~]#nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup ipv4.method manual ipv4.addresses 192.168.20.100/24 ipv4.gateway 192.168.20.2 ipv4.dns 192.168.20.2
Connection 'bond0' (24e90eef-a160-4cc0-af86-9e13259696ba) successfully added.
[root@centos7 ~]#

將物理網卡添加到bond0作為從屬接口
[root@centos7 ~]#nmcli connection add type bond-slave ifname eth0 master bond0 
Connection 'bond-slave-eth0' (bb89e659-a59e-4611-8fbb-a97a99840b66) successfully added.
[root@centos7 ~]#nmcli connection add type bond-slave ifname eth1 master bond0 
Connection 'bond-slave-eth1' (7081209c-09d0-4fb9-aaa9-2564613590b9) successfully added.
[root@centos7 ~]#

啟動從屬接口
[root@centos7 ~]#nmcli connection up bond-slave-eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/46)

[root@centos7 ~]#nmcli connection up bond-slave-eth1 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/50)


啟動bond0
[root@centos7 ~]#nmcli connection up bond0

查看bond連接狀態
[root@centos7 ~]#nmcli connection show 
NAME             UUID                                  TYPE      DEVICE 
bond0            24e90eef-a160-4cc0-af86-9e13259696ba  bond      bond0  
bond-slave-eth0  bb89e659-a59e-4611-8fbb-a97a99840b66  ethernet  eth0   
bond-slave-eth1  7081209c-09d0-4fb9-aaa9-2564613590b9  ethernet  eth1   
virbr0           08a6c628-bae9-4157-a4c3-d954bf07fbb9  bridge    virbr0 
eth0             f82cab29-1dd2-4e17-9c66-4a8726b8dd89  ethernet      

查看一下bond0運行模式狀態
[root@centos7 ~]#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: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:d3:63:8c
Slave queue ID: 0

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:d3:63:96
Slave queue ID: 0
[root@centos7 ~]#

最后通過ifconfig或ip a 命令確認bond0接口IP獲取狀態,ping命令檢測IP能ping通,至此網卡綁定完成。

Linux網絡組(team)

網絡組相關概念:

網絡組:是將多個網卡聚合在一起方法,從而實現冗錯和提高吞吐量

網絡組不同於舊版中bonding技術,提供更好的性能和擴展性

網絡組由內核驅動和teamd守護進程實現.

多種方式runner

broadcast
roundrobin
activebackup
loadbalance
lacp (implements the 802.3ad Link Aggregation Control Protocol)

網絡組相關特性

啟動網絡組接口不會自動啟動網絡組中的port接口

啟動網絡組接口中的port接口總會自動啟動網絡組接口

禁用網絡組接口會自動禁用網絡組中的port接口

沒有port接口的網絡組接口可以啟動靜態IP連接

啟用DHCP連接時,沒有port接口的網絡組會等待port接口的加入

網絡組的相關配置:

1、通過nmctl命令創建

創建網絡接口組
[root@centos7 ~]#nmcli connection add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}'
Connection 'team0' (b59f80dc-c425-4fa6-b1ef-2935d290fa6a) successfully added.

創建port接口
[root@centos7 ~]#nmcli connection add type team-slave con-name team0-eth0 ifname eth0 master team0 
Connection 'team0-eth0' (5d7e5824-4d87-449c-8fff-42b2f20bc877) successfully added.
[root@centos7 ~]#nmcli connection add type team-slave con-name team0-eth1 ifname eth1 master team0 
Connection 'team0-eth1' (920f9f03-9f24-40bc-89eb-509e44ee606f) successfully added.

啟動team接口
[root@centos7 network-scripts]#nmcli connection up team0 
[root@centos7 network-scripts]#nmcli connection up team0-eth0
[root@centos7 network-scripts]#nmcli connection up team0-eth1

查看team連接狀態
[root@centos7 ~]#teamdctl team0 stat
setup:
  runner: activebackup
ports:
  eth0
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
  eth1
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
runner:
  active port: eth1

2、手動創建配置文件

創建team0接口配置文件
vim /etc/sysconfig/network-scripts/ifcfg-team0
DEVICE=team0
DEVICETYPE=Team
TEAM_CONFIG="{\"runner\": {\"name\": \"broadcast\"}}"
BOOTPROTO=none
IPADDR0=172.16.0.100
PREFIX0=24
NAME=team0
ONBOOT=yes

創建port接口配置文件

/etc/sysconfig/network-scripts/ifcfg-team0-eth0
DEVICE=eth0
DEVICETYPE=TeamPort
TEAM_MASTER=team0
NAME=team0-eth0
ONBOOT=yes

/etc/sysconfig/network-scripts/ifcfg-team0-eth1
DEVICE=eth1
DEVICETYPE=TeamPort
TEAM_MASTER=team0
NAME=team0-eth1
ONBOOT=yes

啟動team接口
[root@centos7 ~]#nmcli connection up team0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/69)

說明:本文部分資料參考來自https://blog.51cto.com/2979193/2095134,感謝作者的熱心分享。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM