Centos7双网卡绑定

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7双网卡绑定相关的知识,希望对你有一定的参考价值。

(一)简述
1.双网卡绑定技术在centos7中使用了teaming技术,而在rhel6/centos7中使用的是bonding技术,
在centos7中双网卡绑定既能使用teaming也可以使用bonding,这里推荐使用teaming技术,方便与查看和监控。

  1. 原理
    这里介绍两种最常见的双网卡绑定模式:
    (1) roundrobin - 轮询模式
    所有链路处于负载均衡状态,这种模式的特点增加了带宽,同时支持容错能力。
    (2) activebackup - 主备模式
    一个网卡处于活动状态,另一个处于备份状态,所有流量都在主链路上处理,当活动网卡down掉时,启用备份网卡。
    3.查看服务器的相关信息:
    [[email protected] ~]# nmcli dev
    DEVICE  TYPE      STATE        CONNECTION  
    em1     ethernet  connected   -- 
    em2     ethernet  connected    --    
    em3     ethernet  unavailable  --          
    em4     ethernet  unavailable  --          
    lo      loopback  unmanaged    --                   
    [[email protected] ~]# ip a           
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master team0 state UP qlen 1000
    link/ether 36:3c:c4:19:d0:ef brd ff:ff:ff:ff:ff:ff
    3: em2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master team0 state UP qlen 1000
    link/ether 36:3c:c4:19:d0:ef brd ff:ff:ff:ff:ff:ff
    4: em3: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 24:6e:96:a2:dd:ae brd ff:ff:ff:ff:ff:ff
    5: em4: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 24:6e:96:a2:dd:af brd ff:ff:ff:ff:ff:ff
    18: team0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 36:3c:c4:19:d0:ef brd ff:ff:ff:ff:ff:ff
    inet 10.128.105.156/24 brd 10.128.105.255 scope global team0
       valid_lft forever preferred_lft forever
    inet6 fe80::718:e575:f031:47d4/64 scope link 
       valid_lft forever preferred_lft forever

    ###########其中state UP 为网卡有网线或启用的网口,有上边的参数可知,em1和em2有网线接口,其他的网口没有。lo为本地回环网卡

(二)Roundrobin(轮询)安装步骤
1,查看网卡的连接信息:nmcli con sh

[[email protected] ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE  
em1          4c8a8aee-327b-4f29-9ec4-ea2b2551420b  802-3-ethernet  --     
em2          62bde3ef-9f40-42aa-9b3a-4fe8b1aabf42  802-3-ethernet  --     
em3          56ef9d05-3a5d-4cd6-af5b-3d39adce619f  802-3-ethernet  --     
em4          761f7809-d10c-4e3b-bc06-c0906ca55d1c  802-3-ethernet  --  

2,删除无用的网卡:nmcli con del em3

[[email protected] ~]# nmcli con del em3
Connection ‘em3‘ (56ef9d05-3a5d-4cd6-af5b-3d39adce619f) successfully deleted.
[[email protected] ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE  
em1          4c8a8aee-327b-4f29-9ec4-ea2b2551420b  802-3-ethernet  --     
em2          62bde3ef-9f40-42aa-9b3a-4fe8b1aabf42  802-3-ethernet  --     
em4          761f7809-d10c-4e3b-bc06-c0906ca55d1c  802-3-ethernet  --  

3,# 使用nmcli命令操作,创建team接口team0,同时设置teaming模式为roundrobin

[[email protected] ~]# nmcli con add type team con-name team0 ifname team0 config ‘{"runner":{"name": "roundrobin"}}‘
[[email protected] ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE 
team0        5a1976d7-2c91-456e-8dda-0b12c6ef0c04  team            team0  
em1          4c8a8aee-327b-4f29-9ec4-ea2b2551420b  802-3-ethernet  --     
em2          62bde3ef-9f40-42aa-9b3a-4fe8b1aabf42  802-3-ethernet  --     
em4          761f7809-d10c-4e3b-bc06-c0906ca55d1c  802-3-ethernet  -- 

4,给接口team0设置ip地址和网关,并取消手动模式:

[[email protected] ~]# nmcli con modify team0 ipv4.address ‘10.128.105.157/24‘ ipv4.gateway ‘10.128.105.254‘ 
###设置为手动模式,取消DHCP
[[email protected] ~]# nmcli con modify team0 ipv4.method manual

5,使用ifup team0 启用team0网卡,然后重启网络服务。

[[email protected] ~]# ifup team0
[[email protected] ~]# /etc/init.d/network restart
Restarting network (via systemctl):                        [  OK  ]
[[email protected] ~]# ip a |grep team0
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master team0 state UP qlen 1000
10: team0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    inet 10.128.105.157/24 brd 10.128.105.255 scope global team0

6,再次将两张物理网卡添加到team

[[email protected] ~]# nmcli con add type team-slave con-name team0-port2 ifname em1 master team0
Connection ‘team0-port2‘ (e01c37f5-b3a1-4e6d-96ab-a6e676ad3c32) successfully added.
[[email protected] ~]# nmcli con add type team-slave con-name team0-port2 ifname em2 master team0
Connection ‘team0-port2‘ (8b13a9de-d64c-44fc-a86d-ea45d2f511cf) successfully added.
[[email protected] ~]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE 
team0        64b123d5-189d-43b0-a850-77d47c0da86c  team            team0  
team0-port1  24181898-bffe-4ae1-90e8-f47da64b5512  802-3-ethernet  em1    
team0-port2  8b13a9de-d64c-44fc-a86d-ea45d2f511cf  802-3-ethernet  em2    
em1          4c8a8aee-327b-4f29-9ec4-ea2b2551420b  802-3-ethernet  --     
em2          62bde3ef-9f40-42aa-9b3a-4fe8b1aabf42  802-3-ethernet  --     
em4          761f7809-d10c-4e3b-bc06-c0906ca55d1c  802-3-ethernet  --  

7,现在查看team0的状态模式是否是roundrobin。teamdctl team0 st

[[email protected] ~]# teamdctl team0 st
setup:
  runner: roundrobin
ports:
  em1
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
  em2
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0

这样,team模式的roundrobin搭建成功。

8,测试是否正常工作
8.1,首先把em1网卡禁用,用其他的服务器ping看是否断。

[[email protected] etc]# ifdown em1
Device ‘em1‘ successfully disconnected.
[[email protected] etc]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE 
team0        64b123d5-189d-43b0-a850-77d47c0da86c  team            team0  
team0-port2  8b13a9de-d64c-44fc-a86d-ea45d2f511cf  802-3-ethernet  em2    
em1          4c8a8aee-327b-4f29-9ec4-ea2b2551420b  802-3-ethernet  --     
em2          62bde3ef-9f40-42aa-9b3a-4fe8b1aabf42  802-3-ethernet  --     
em4          761f7809-d10c-4e3b-bc06-c0906ca55d1c  802-3-ethernet  --     
team0-port1  24181898-bffe-4ae1-90e8-f47da64b5512  802-3-ethernet  -- 
[[email protected] ~]# teamdctl team0 st
setup:
  runner: roundrobin
ports:
  em2
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0

技术分享图片
技术分享图片
从上图看出实在正常的。

8.2,把网卡em1启用,em2禁用查看下下
[[email protected] etc]# ifup em1
[[email protected] etc]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE 
team0        64b123d5-189d-43b0-a850-77d47c0da86c  team            team0  
team0-port1  24181898-bffe-4ae1-90e8-f47da64b5512  802-3-ethernet  em1    
team0-port2  8b13a9de-d64c-44fc-a86d-ea45d2f511cf  802-3-ethernet  em2    
em1          4c8a8aee-327b-4f29-9ec4-ea2b2551420b  802-3-ethernet  --     
em2          62bde3ef-9f40-42aa-9b3a-4fe8b1aabf42  802-3-ethernet  --     
em4          761f7809-d10c-4e3b-bc06-c0906ca55d1c  802-3-ethernet  --     
[[email protected] etc]# ifdown em2
Device ‘em2‘ successfully disconnected.
[[email protected] etc]# nmcli con sh
NAME         UUID                                  TYPE            DEVICE 
team0        64b123d5-189d-43b0-a850-77d47c0da86c  team            team0  
team0-port1  24181898-bffe-4ae1-90e8-f47da64b5512  802-3-ethernet  em1    
em1          4c8a8aee-327b-4f29-9ec4-ea2b2551420b  802-3-ethernet  --     
em2          62bde3ef-9f40-42aa-9b3a-4fe8b1aabf42  802-3-ethernet  --     
em4          761f7809-d10c-4e3b-bc06-c0906ca55d1c  802-3-ethernet  --     
team0-port2  8b13a9de-d64c-44fc-a86d-ea45d2f511cf  802-3-ethernet  --    

网卡流量em2没变,而em1变化了。
技术分享图片

(三)双网卡绑定activebackup的实现
具体的大概步骤和上边差不多,省略如下:
(首先删除刚才绑定的网卡模式轮询:nmcli con del team0 team0-port1 team0-port2)

1, 添加逻辑网卡team0,并启用activebackup模式
[[email protected] ~]# nmcli con add type team con-name team0 ifname team0 config ‘{"runner":{"name":"activebackup"}}‘

2, 绑定ip

[[email protected] ~]# nmcli con modify team0 ipv4.address 10.128.105.157/24  ipv4.gateway 10.128.105.254
[[email protected] ~]# nmcli con modify team0 ipv4.method manual

3, 添加物理网卡到activebackup模式

[[email protected] ~]# nmcli con add type team-slave con-name team0-port1 ifname em1 master team0
Connection ‘team0-port1‘ (24181898-bffe-4ae1-90e8-f47da64b5512) successfully added.
[[email protected] ~]# nmcli con add type team-slave con-name team0-port2 ifname em2 master team0
Connection ‘team0-port2‘ (8b13a9de-d64c-44fc-a86d-ea45d2f511cf) successfully added.

4,查看状态

[[email protected] ~]# teamdctl team0 st
setup:
 runner: activebackup
ports:
 em1
  link watches:
   link summary: up
   instance[link_watch_0]:
    name: ethtool
    link: up
    down count: 0
 em2
  link watches:
   link summary: up
   instance[link_watch_0]:
    name: ethtool
    link: up
    down count: 0
runner:
 active port: em1

成功,处于活动中的网卡为 eno33554984

备注:注意 在做网卡绑定的时候,如果发现物理网卡总是无法绑定到team0,请检查该物理网卡是否处于up状态。还有就是主备模式在切换的过程中会有间隔,会丢几个包。

以上是关于Centos7双网卡绑定的主要内容,如果未能解决你的问题,请参考以下文章

centos 双网卡绑定 mode哪种好些

Centos7 / RHEL 7 双网卡绑定

centos7双网卡绑定bond0

centos7双网卡绑定bond #yyds干货盘点#

Centos7 / RHEL 7 双网卡绑定

redhat6.3操作系统怎样做双网卡绑定,谢谢