Centos7/RHEL7双网卡绑定的方法

1. 简要

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名雅安服务器托管、营销软件、网站建设、伊州网站维护、网站推广。

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

2. 原理

这里介绍两种最常见的双网卡绑定模式:

        (1) roundrobin - 轮询模式

            所有链路处于负载均衡状态,这种模式的特点增加了带宽,同时支持容错能力。

        (2) activebackup - 主备模式

            一个网卡处于活动状态,另一个处于备份状态,所有流量都在主链路上处理,当活动网卡down掉时,启用备份网卡。

3. 基础环境

系统:centos7

在centos7中,关闭虚拟机添加网卡,再开启虚拟机,系统是不会自动生成网卡的配置文件:

Centos7/RHEL7双网卡绑定的方法

在centos7中要生成网卡配置文件的方法很简单,使用如下命令:

  # 查看物理网卡信息
[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模式必须要在交换机上做以太通道,不然会出现网络无法连通。

实际操作:

  # 使用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问题排查

第一次查看出现的是这样的结果,检查下配置:

  # 通过直接查看网卡发现team0并没有启动
[root@localhost ~]# ip a | grep team0
5: team0:  mtu 1500 qdisc noqueue state DOWN

  # 使用ifup 启动team0并没有成功,继续排查
[root@localhost ~]# ifup team0
[root@localhost ~]# ip a | grep team0
5: team0:  mtu 1500 qdisc noqueue state DOWN

直接sysetmctl restart network 也没有成功。

  # 发现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具有容错的效果,为了测试是否有容错的效果,虚拟机做了下更改来验证:

Centos7/RHEL7双网卡绑定的方法

这一次网卡全部改成桥接模式,为了更好的测试是否具有容错性。

Centos7/RHEL7双网卡绑定的方法

roundrobin已经配置完毕。接下来我们down掉一张网卡检查是否具有容错性。

Centos7/RHEL7双网卡绑定的方法 

通过虚拟机测试,容错性是存在的。

 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掉活动网卡测试网络是否仍然处于连通状态

 Centos7/RHEL7双网卡绑定的方法

测试成功。

注意: 在做网卡绑定的时候,如果发现物理网卡总是无法绑定到team0,请检查该物理网卡是否处于up状态。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


本文名称:Centos7/RHEL7双网卡绑定的方法
本文网址:http://hbruida.cn/article/jhsicd.html