RHEL7/CentOS7雙網卡綁定-Network Teaming

RHEL7/CentOS7 Configure Network Teaming

Network Teaming可聯合或合併物理網絡接口,形成具有較高吞吐量或冗餘方式的邏輯網絡接口。其作爲網卡綁定的備選方式出現,在系統中和傳統的bonding方式並存。

1. 系統環境

[root@test03 ~]# uname -a
Linux test03 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

[root@test03 ~]# ip addr show 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:56:69:4d brd ff:ff:ff:ff:ff:ff
    inet 192.168.231.13/24 brd 192.168.231.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe56:694d/64 scope link 
       valid_lft forever preferred_lft forever
3: eno33554992: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:56:69:57 brd ff:ff:ff:ff:ff:ff
4: eno50332216: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:56:69:61 brd ff:ff:ff:ff:ff:ff

2. 雙網卡綁定目標

  • 網卡綁定模式:activebackup - 主備模式

    一個網卡處於活躍狀態,另一個處於備份狀態,所有流量都在主鏈路上處理,當活躍網卡down掉時,啓用備份網卡。

  • 綁定網卡:eno33554992+eno50332216=team0

    • 設置eno33554992爲主網卡(優先處於活躍狀態),eno50332216爲輔網卡(備份狀態,主網卡鏈路正常時,輔網卡處於備份狀態);-- 此時無需設置輔網卡粘滯位。

    • 設置eno33554992爲主網卡,eno50332216爲輔網卡,主網卡鏈路故障後,輔網卡切換爲活躍狀態,主網卡鏈路恢復後,輔網卡仍保持活躍狀態直至其鏈路故障。 -- 此時需要設置輔網卡粘滯位{"sticky" true}。

3. 雙網卡綁定步驟

3.1 查看物理網卡信息:

[root@test03 network-scripts]# nmcli device 
DEVICE       TYPE      STATE      CONNECTION  
eno16777736  ethernet  connected  eno16777736 
eno33554992  ethernet  connected  eno33554992 
eno50332216  ethernet  connected  eno50332216 
lo           loopback  unmanaged  --   
# 共四張網卡,lo爲本地迴環網卡,另外三張爲物理網卡。

3.2 查看網卡連接信息

[root@test03 network-scripts]# nmcli connection show 
NAME         UUID                                  TYPE            DEVICE      
eno50332216  36b38556-4bf8-4ce0-8045-4f5465530f67  802-3-ethernet  eno50332216 
eno33554992  46831ccf-905b-4361-b5ba-c5448133e288  802-3-ethernet  eno33554992 
eno16777736  613db14a-2375-4a89-b55a-d2abd8fc65d5  802-3-ethernet  eno16777736 	

3.3 刪除網卡連接信息

本次Network Teaming配置中,需要將eno33554992eno50332216綁定爲team0,並且設置eno33554992爲主網卡,首先需要這兩塊網卡現有的配置信息,否則team0創建完成後,未刪除的網卡配置信息會影響team0的正常工作。

如果nmcli connection show命令輸出中無將要進行配置的網卡連接信息,則無需進行刪除操作。

[root@test03 network-scripts]# nmcli connection delete eno33554992 
[root@test03 network-scripts]# nmcli connection delete eno50332216 
[root@test03 network-scripts]# 
[root@test03 network-scripts]# nmcli connection show 
NAME         UUID                                  TYPE            DEVICE      
eno16777736  613db14a-2375-4a89-b55a-d2abd8fc65d5  802-3-ethernet  eno16777736 
# 網卡連接信息刪除成功。
# 這裏刪除的其實就是/etc/sysconfig/network-scripts目錄下兩塊網卡的配置文件。
[root@test03 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@test03 network-scripts]# ls ifcfg-*
ifcfg-eno16777736  ifcfg-lo

3.4 創建team0

[root@test03 network-scripts]# nmcli connection add type team ifname team0 con-name team0 config '{"runner": {"name":"activebackup"}}' ip4 192.168.231.100/24
Connection 'team0' (dfe2f884-40e0-4686-a7f9-2be4b1a4b5b2) successfully added.

設備類型:team;設備名稱和連接名稱:均爲team0;綁定模式:activebackup - 主備模式;IP及子網掩碼:192.168.231.100/24;

當然,也可以後續設置IP地址,示例:

#創建team0並設置爲主備模式
[root@test03 network-scripts]# nmcli connection add type team ifname team0 con-name team0 config '{"runner": {"name":"activebackup"}}' 

#設置IP地址及子網掩碼
[root@test03 network-scripts]# nmcli connection modify team0 ipv4.addresses 192.168.231.100/24

#設置IP地址獲取方法爲手動
[root@test03 network-scripts]# nmcli connection modify team0 ipv4.method manual

配置完成後,會在/etc/sysconfig/network-scripts目錄下生成ifcfg-team0的配置文件

[root@test03 network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-team0 
DEVICE=team0
TEAM_CONFIG="{\"runner\": {\"name\":\"activebackup\"}}"
DEVICETYPE=Team
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=team0
UUID=dfe2f884-40e0-4686-a7f9-2be4b1a4b5b2
ONBOOT=yes
IPADDR=192.168.231.100
PREFIX=24
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

3.5 將兩塊網卡添加進team0

# 添加網卡eno33554992,設備類型:team-slave;連接名稱:team0-p1;master:team0;優先級:100
[root@test03 network-scripts]# nmcli connection add type team-slave ifname eno33554992 con-name team0-p1 master team0 config '{"prio": 100}'
Connection 'team0-p1' (4c8901c7-0246-47ce-8d5a-272f23f88d70) successfully added.

# 添加網卡eno50332216,設備類型:team-slave;連接名稱:team0-p1;master:team0;優先級:-10; 粘滯位:true(sticky標誌請根據實際需要設置)
[root@test03 network-scripts]# nmcli connection add type team-slave ifname eno50332216 con-name team0-p2 master team0 config '{"prio": -10,"sticky": true}'
Connection 'team0-p2' (d898f1f5-bb0f-496e-8cdd-7f3898c2a482) successfully added.

本次配置中eno33554992爲主網卡,設置其優先級爲100;eno50332216爲輔網卡,設置其優先級爲-10,粘滯位爲true。

prio:優先級。

數字越大,網卡優先級越高,優先爲活躍網卡;若不設置優先級,則默認爲0,先接入網絡的網卡爲活躍網卡,另一塊爲備份網卡,直至活躍網卡斷開連接,備份網卡自動升級爲活躍網卡,兩塊網卡角色互換。

sticky:粘滯位,默認爲false;

  • 輔網卡eno50332216中設置了sticky爲true,代表如果輔網卡連接狀態變爲爲活躍狀態後,無論主網卡連接是否正常,其將會一直保持爲活躍狀態。

  • 若不設置sticky,在輔網卡爲活躍狀態時,若主網卡連接恢復正常,則活躍網卡由主網卡接管,輔網卡會變爲備份狀態。

可根據實際需要進行參數設置。

配置完成後,此時會在/etc/sysconfig/network-scripts目錄下生成ifcfg-team0-p1,ifcfg-team0-p2的配置文件:

[root@test03 network-scripts]# cat ifcfg-team0-p1
TEAM_PORT_CONFIG="{\"prio\": 100}"
NAME=team0-p1
UUID=4c8901c7-0246-47ce-8d5a-272f23f88d70
DEVICE=eno33554992
ONBOOT=yes
TEAM_MASTER=team0
DEVICETYPE=TeamPort

[root@test03 network-scripts]# cat ifcfg-team0-p2
TEAM_PORT_CONFIG="{\"prio\": -10,\"sticky\": true}"
NAME=team0-p2
UUID=d898f1f5-bb0f-496e-8cdd-7f3898c2a482
DEVICE=eno50332216
ONBOOT=yes
TEAM_MASTER=team0
DEVICETYPE=TeamPort

4. 激活team0及其子網卡

# 激活team0-p1 
[root@test03 ~]# nmcli connection up team0-p1 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)

# 激活team0-p2
[root@test03 ~]# nmcli connection up team0-p2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)

# 激活team0
[root@test03 ~]# nmcli connection up team0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)

5. team0狀態查看

5.1 查看team0工作狀態:

# 查看當前活躍網卡
[root@test03 network-scripts]# nmcli connection show --active 
NAME         UUID                                  TYPE            DEVICE      
team0-p1     4c8901c7-0246-47ce-8d5a-272f23f88d70  802-3-ethernet  eno33554992 
eno16777736  613db14a-2375-4a89-b55a-d2abd8fc65d5  802-3-ethernet  eno16777736 
team0        d6e07840-dff8-49e9-a23c-35eb0cc0ec4b  team            team0       
team0-p2     d898f1f5-bb0f-496e-8cdd-7f3898c2a482  802-3-ethernet  eno50332216 

#查看team0端口狀態
[root@test03 network-scripts]# teamnl team0 ports
 4: eno50332216: up 1000Mbit FD 
 3: eno33554992: up 1000Mbit FD 
 
 [root@test03 network-scripts]# teamdctl team0 stat
setup:
  runner: activebackup
ports:
  eno33554992
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
  eno50332216
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
runner:
  active port: eno33554992

5.2 查看team0詳細配置

# 查看team0詳細配置
[root@test03 network-scripts]# teamdctl team0 config dump
{
    "device": "team0",
    "mcast_rejoin": {
        "count": 1
    },
    "notify_peers": {
        "count": 1
    },
    "ports": {
        "eno33554992": {
            "link_watch": {
                "name": "ethtool"
            },
            "prio": 100
        },
        "eno50332216": {
            "link_watch": {
                "name": "ethtool"
            },
            "prio": -10,
            "sticky": true
        }
    },
    "runner": {
        "name": "activebackup"
    }
}

# 單獨查看team0子網卡詳細配置
[root@test03 network-scripts]# teamdctl team0 port config dump eno33554992
{
    "link_watch": {
        "name": "ethtool"
    },
    "prio": 100
}
[root@test03 network-scripts]# teamdctl team0 port config dump eno50332216
{
    "link_watch": {
        "name": "ethtool"
    },
    "prio": -10,
    "sticky": true
}

6. team0切換測試

[root@test03 ~]# teamdctl team0 stat 
setup:
  runner: activebackup
ports:
  eno33554992
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
  eno50332216
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
runner:
  active port: eno33554992

當前活躍子網卡爲eno33554992,ping team0的IP地址一直可以ping通。現在將該網卡斷開連接:

# 斷開team0-p1的網絡連接
[root@test03 ~]# nmcli connection down team0-p1 
Connection 'team0-p1' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)

# 查看team0連接狀態
[root@test03 ~]# teamdctl team0 stat 
setup:
  runner: activebackup
ports:
  eno50332216
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
runner:
  active port: eno50332216
  
# 活躍網卡變爲eno50332216,ping測試短暫中斷後恢復。
來自 192.168.231.100 的回覆: 字節=32 時間<1ms TTL=64
來自 192.168.231.100 的回覆: 字節=32 時間<1ms TTL=64
來自 192.168.231.100 的回覆: 字節=32 時間=1ms TTL=64
來自 192.168.231.100 的回覆: 字節=32 時間<1ms TTL=64
請求超時。
請求超時。
來自 192.168.231.100 的回覆: 字節=32 時間=1ms TTL=64
來自 192.168.231.100 的回覆: 字節=32 時間<1ms TTL=64
來自 192.168.231.100 的回覆: 字節=32 時間<1ms TTL=64

由於本次測試在虛擬機中進行,ping測試會出現較明顯的中斷,在真實的物理機上測試時,ping測試會一直正常或出現短暫中斷後恢復。

官方文檔推薦使用:nmcli device [disconnect|connect] ifname命令來進行網卡的斷開和連接操作,但在虛擬機上使用此方法斷開網卡連接後,恢復網卡連接時會報錯,這裏使用命令nmcli connection [down|up] ifname進行操作。

當前活躍網卡爲輔網卡eno50332216,現在連接主網卡:

# 恢復team0-p1的網絡連接
[root@test03 ~]# nmcli connection up team0-p1 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)

# 查看team0的連接狀態
[root@test03 ~]# teamdctl team0 stat 
setup:
  runner: activebackup
ports:
  eno33554992
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
  eno50332216
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
runner:
  active port: eno50332216

# ping測試短暫中斷後恢復
來自 192.168.231.100 的回覆: 字節=32 時間<1ms TTL=64
請求超時。
請求超時。
來自 192.168.231.100 的回覆: 字節=32 時間=1ms TTL=64
來自 192.168.231.100 的回覆: 字節=32 時間<1ms TTL=64

由於本次測試在虛擬機中進行,ping測試會出現較明顯的中斷,在真實的物理機上測試時,ping測試會一直正常或出現短暫中斷後恢復。

在主網卡連接恢復正常後,查看team0發現當前活躍網卡仍爲輔網卡eno50332216,沒有將活躍網卡切換爲主網卡eno33554992,原因是在輔網卡中設置了粘滯位sticky=true,sticky標誌使輔網卡爲活躍狀態後,若其網絡連接不發生故障,則會一直保持其爲活躍網卡。

如果在輔網卡配置中不設置sticky標誌,主網卡連接恢復正常後,活躍網卡由主網卡接管,輔網卡會變爲備份狀態。

7. 雙網卡綁定各模式配置文件示例

對於各種雙網卡綁定配置模式,請參考系統中的示例文件,文件位置:

/usr/share/doc/teamd-xx/example_configs/

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