Centos7 / RHEL 7 雙網卡綁定

1. 簡要
    雙網卡綁定技術在centos7中使用了teaming技術,而在rhel6/centos7中使用的是bonding技術,
    在centos7中雙網卡綁定既能使用teaming也可使用bonding,這裏推薦使用teaming技術,方便與查看和監控。

2. 原理
    這裏介紹兩種最多見的雙網卡綁定模式:
        (1) roundrobin - 輪詢模式
            全部鏈路處於負載均衡狀態,這種模式的特色增長了帶寬,同時支持容錯能力。
        (2) activebackup - 主備模式
            一個網卡處於活動狀態,另外一個處於備份狀態,全部流量都在主鏈路上處理,當活動網卡down掉時,啓用備份網卡。linux

3. 基礎環境
    系統:centos7

    在centos7中,關閉虛擬機添加網卡,再開啓虛擬機,系統是不會自動生成網卡的配置文件:

    在centos7中要生成網卡配置文件的方法很簡單,使用以下命令:centos

    # 查看物理網卡信息
[root@localhost ~]# nmcli dev
DEVICE       TYPE      STATE      CONNECTION         
eno16777736  ethernet  connected  eno16777736        
eno33554984  ethernet  connected  Wired connection 1 
eno50332208  ethernet  connected  Wired connection 2 
lo           loopback  unmanaged  --

上面的信息能夠看到,一共有四張網卡,其中lo是本地迴環網卡,另外三張爲物理網卡。服務器

    # 查看網卡鏈接信息
[root@localhost ~]# nmcli con sh
NAME                UUID                                  TYPE            DEVICE      
Wired connection 2  9a1314bf-d273-406a-930a-dc4160dee4ec  802-3-ethernet  eno50332208 
Wired connection 1  47eef4a3-7fa4-4a73-83ef-1485bda9b950  802-3-ethernet  eno33554984 
eno16777736         51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736

上面的信息能夠查看到一共有3個鏈接,其中兩個有線鏈接,表示沒有設置過的網卡鏈接,還有一個enoxx鏈接。接下來要修改兩個有線鏈接的命名方法,並生成網卡配置文件:網絡

    # 首先,刪除兩個無用的有線鏈接
[root@localhost ~]# nmcli con del 9a1314bf-d273-406a-930a-dc4160dee4ec
Connection 'Wired connection 2' (9a1314bf-d273-406a-930a-dc4160dee4ec) successfully deleted.
[root@localhost ~]# nmcli con del 47eef4a3-7fa4-4a73-83ef-1485bda9b950
Connection 'Wired connection 1' (47eef4a3-7fa4-4a73-83ef-1485bda9b950) successfully deleted.
    # 再次建立新的鏈接並生成配置文件

[root@localhost ~]# nmcli conn add type ethernet con-name eno50332208 ifname eno50332208 
Connection 'eno50332208' (eea2a115-63f8-4f33-be84-12559f02542c) successfully added.
[root@localhost ~]# nmcli conn add type ethernet con-name eno33554984 ifname eno33554984
Connection 'eno33554984' (32d2389e-4e82-4419-9d18-5aff8f0be003) successfully added.
    # 最後,查看下

[root@localhost ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE      
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736 
eno33554984  32d2389e-4e82-4419-9d18-5aff8f0be003  802-3-ethernet  eno33554984 
eno50332208  eea2a115-63f8-4f33-be84-12559f02542c  802-3-ethernet  eno50332208

[root@localhost ~]# ll /etc/sysconfig/network-scripts/ifcfg-eno*
-rw-r--r--. 1 root root 394 Nov 26 05:59 /etc/sysconfig/network-scripts/ifcfg-eno16777736
-rw-r--r--. 1 root root 288 Dec 26 08:44 /etc/sysconfig/network-scripts/ifcfg-eno33554984
-rw-r--r--. 1 root root 288 Dec 26 08:44 /etc/sysconfig/network-scripts/ifcfg-eno50332208

網絡鏈接和配置文件都成功生成了。 接下來經過eno33554984 和 eno50332208 使用teaming模式進行雙網卡綁定負載均衡

4. roundrobin模式:

    roundrobin模式也稱爲輪詢模式,它 基於每個包 ,當某一臺服務器的兩張網卡設置爲roundrobin模式teaming,此時服務器發出的數據包,就會在兩個物理
    網卡上進行輪詢,即第一個數據包走一張網卡,第二個數據包走第二張網卡,依次輪詢。
    注意:
        (1)roundrobin具備容錯性,當一張網卡down掉,數據包依然發送成功。
        (2)在使用roundrobin模式必需要在交換機上作以太通道,否則會出現網絡沒法連通。oop

    實際操做:測試

    # 使用nmcli命令操做,建立team接口team0,同時設置teaming模式爲roundrobin
[root@localhost ~]# nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name": "roundrobin"}}'
Connection 'team0' (9dc48bd6-54b9-48db-b7dd-ecf34ae9196f) successfully added.
    # 給接口team0設置ip地址
[root@localhost ~]# nmcli con modify team0 ipv4.address '10.0.0.2/24' ipv4.gateway '10.0.0.1' 
    # 設置爲手動模式,取消DHCP
[root@localhost ~]# nmcli con modify team0 ipv4.method manual
    # 將兩張物理網卡加入到team中
[root@localhost ~]# nmcli con add type team-slave con-name team0-port1 ifname eno33554984 master team0
Connection 'team0-port1' (22d3640e-f5c8-499c-b0dd-913c8970d800) successfully added.
[root@localhost ~]# nmcli con add type team-slave con-name team0-port2 ifname eno50332208 master team0
Connection 'team0-port2' (12660d2a-0e1f-41e6-96b2-53de4fc6b78c) successfully added.
[root@localhost ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE      
team0-port2  12660d2a-0e1f-41e6-96b2-53de4fc6b78c  802-3-ethernet  --          
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736 
team0-port1  22d3640e-f5c8-499c-b0dd-913c8970d800  802-3-ethernet  --          
team0        9dc48bd6-54b9-48db-b7dd-ecf34ae9196f  team            team0       
eno33554984  32d2389e-4e82-4419-9d18-5aff8f0be003  802-3-ethernet  eno33554984 
eno50332208  eea2a115-63f8-4f33-be84-12559f02542c  802-3-ethernet  eno50332208
    # 查看team0的狀態

[root@localhost ~]# teamdctl team0 st
setup:
  runner: roundrobin

 

4.1 roundrobin問題排查centos7

第一次查看出現的是這樣的結果,檢查下配置:spa

    # 經過直接查看網卡發現team0並無啓動
[root@localhost ~]# ip a | grep team0
5: team0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN

    # 使用ifup 啓動team0並無成功,繼續排查
[root@localhost ~]# ifup team0
[root@localhost ~]# ip a | grep team0
5: team0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN

直接sysetmctl restart network 也沒有成功。3d

    # 發現team0-port1 team0-port2沒有添加成功物理網卡,怎麼回事呢?
[root@localhost ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE      
team0-port2  ec96c1e1-d268-4ba1-a2f9-0133a9f02655  802-3-ethernet  --          
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736 
team0-port1  22d3640e-f5c8-499c-b0dd-913c8970d800  802-3-ethernet  --          
team0        9dc48bd6-54b9-48db-b7dd-ecf34ae9196f  team            team0       
eno33554984  32d2389e-4e82-4419-9d18-5aff8f0be003  802-3-ethernet  eno33554984 
eno50332208  eea2a115-63f8-4f33-be84-12559f02542c  802-3-ethernet  eno50332208 
    # 刪除掉
[root@localhost ~]# nmcli con del team0-port1
Connection 'team0-port1' (22d3640e-f5c8-499c-b0dd-913c8970d800) successfully deleted.
[root@localhost ~]# nmcli con del team0-port2
Connection 'team0-port2' (ec96c1e1-d268-4ba1-a2f9-0133a9f02655) successfully deleted.


    # 刪除兩張物理網卡的鏈接
[root@localhost ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE      
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736 
team0        9dc48bd6-54b9-48db-b7dd-ecf34ae9196f  team            team0       
eno33554984  32d2389e-4e82-4419-9d18-5aff8f0be003  802-3-ethernet  eno33554984 
eno50332208  eea2a115-63f8-4f33-be84-12559f02542c  802-3-ethernet  eno50332208 
[root@localhost ~]# nmcli con del eno33554984 eno50332208
Connection 'eno33554984' (32d2389e-4e82-4419-9d18-5aff8f0be003) successfully deleted.
Connection 'eno50332208' (eea2a115-63f8-4f33-be84-12559f02542c) successfully deleted.
    # 刪除成功
[root@localhost ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE      
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736 
team0        9dc48bd6-54b9-48db-b7dd-ecf34ae9196f  team            team0
    # 再次將兩張物理網卡添加到team
[root@localhost ~]# nmcli con add type team-slave con-name team0-port1 ifname eno33554984 master team0
Connection 'team0-port1' (0331ea43-95c0-4106-b388-8484d4c3940d) successfully added.
[root@localhost ~]# nmcli con add type team-slave con-name team0-port2 ifname eno50332208 master team0
Connection 'team0-port2' (880f2146-fe93-42dd-8d28-7d0dd50b4011) successfully added.
[root@localhost ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE      
team0-port1  0331ea43-95c0-4106-b388-8484d4c3940d  802-3-ethernet  eno33554984 
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736 
team0        9dc48bd6-54b9-48db-b7dd-ecf34ae9196f  team            team0       
team0-port2  880f2146-fe93-42dd-8d28-7d0dd50b4011  802-3-ethernet  eno50332208

如今成功將兩張物理網卡綁定到了team組中

    # 如今查看team0的狀態就出現了,模式是:roundrobin
[root@localhost ~]# teamdctl team0 st 
setup:
  runner: roundrobin
ports:
  eno33554984
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
  eno50332208
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0

這樣,team模式的roundrobin搭建成功。

總結:
    在作鏈路聚合的時候,所要使用到的物理網卡不能獨立存在鏈接,也就是在nmcli con sh查看時,不能獨立存在,不然就沒法綁定到team模式中。

4.2 測試

由於這裏是虛擬機環境,只能經過down掉網卡進行查看。

[root@localhost ~]# teamdctl team0 st
setup:
  runner: roundrobin
ports:
  eno33554984
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
  eno50332208
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0

兩張網卡同時在線,如今down掉其中一張網卡,

[root@localhost ~]# nmcli con del team0-port1
Connection 'team0-port1' (0331ea43-95c0-4106-b388-8484d4c3940d) successfully deleted.
[root@localhost ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE      
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736 
team0        9dc48bd6-54b9-48db-b7dd-ecf34ae9196f  team            team0       
team0-port2  880f2146-fe93-42dd-8d28-7d0dd50b4011  802-3-ethernet  eno50332208

[root@localhost ~]# teamdctl team0 st
setup:
  runner: roundrobin
ports:
  eno50332208
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0

這樣就發現eno33554984網卡已經down掉了。

[root@localhost ~]# nmcli con add type team-slave con-name team0-port1 ifname eno33554984 master team0
Connection 'team0-port1' (59acd6bf-affc-4994-a050-83c45816859a) successfully added.
[root@localhost ~]# 
[root@localhost ~]# teamdctl team0 st
setup:
  runner: roundrobin
ports:
  eno33554984
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
  eno50332208
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0

    虛擬機環境就這樣測試了吧,至於生產環境就直接拔網線,ping測試。

資料有說linux下roundrobin具備容錯的效果,爲了測試是否有容錯的效果,虛擬機作了下更改來驗證:

這一次網卡所有改爲橋接模式,爲了更好的測試是否具備容錯性。

roundrobin已經配置完畢。接下來咱們down掉一張網卡檢查是否具備容錯性。

經過虛擬機測試,容錯性是存在的。

 

5. 雙網卡綁定activebackup的實現

[root@localhost ~]# nmcli con sh 
NAME         UUID                                  TYPE            DEVICE      
team0-port2  2a1aa534-e021-4452-bb0a-2002dabcbc08  802-3-ethernet  eno50332208 
team0-port1  9a10c9ef-ee9c-4151-9a47-59b4668e8934  802-3-ethernet  --          
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736 
team0        c2278f3f-5c6b-4e1d-b925-514d3c3832b6  team            team0       
[root@localhost ~]# nmcli con del team0 team0-port1 team0-port2
Connection 'team0' (c2278f3f-5c6b-4e1d-b925-514d3c3832b6) successfully deleted.
Connection 'team0-port1' (9a10c9ef-ee9c-4151-9a47-59b4668e8934) successfully deleted.
Connection 'team0-port2' (2a1aa534-e021-4452-bb0a-2002dabcbc08) successfully deleted.
[root@localhost ~]# nmcli con sh 
NAME         UUID                                  TYPE            DEVICE      
eno16777736  51d00668-2cf5-41da-ad8d-5019d62e98ad  802-3-ethernet  eno16777736

# 以上操做就刪除了roundrobin模式,恢復到初始狀態。
# 添加邏輯網卡team0,並啓用activebackup模式
[root@localhost ~]# nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}'
Connection 'team0' (f0794e59-419d-4c2a-96b9-464610b6321a) successfully added.

# 綁定ip
[root@localhost ~]# nmcli con modify team0  ipv4.address 192.168.118.122/24 ipv4.gateway 192.168.118.1
[root@localhost ~]# nmcli con modify team0 ipv4.method manual


# 添加物理網卡到activebackup模式
[root@localhost ~]# nmcli con add type team-slave con-name team0-port1 ifname eno33554984 master team0
Connection 'team0-port1' (52656904-6e85-4a04-98b4-2b19773fa290) successfully added.
[root@localhost ~]# nmcli con add type team-slave con-name team0-port2 ifname eno50332208 master team0
Connection 'team0-port2' (44d96e3a-014a-481f-9122-4ca2d3be9f7a) successfully added.
[root@localhost ~]# teamdctl team0 st
setup:
  runner: activebackup
ports:
  eno33554984
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
  eno50332208
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
runner:
  active port: eno33554984

成功,處於活動中的網卡爲 eno33554984

5.1 測試

經過down掉活動網卡測試網絡是否仍然處於連通狀態

 

測試成功。注意: 在作網卡綁定的時候,若是發現物理網卡老是沒法綁定到team0,請檢查該物理網卡是否處於up狀態。

相關文章
相關標籤/搜索