Linux网卡绑定

CentOS配置网卡绑定(bonding)

1. bonding 简介

将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或者负载均衡。直接给两块网卡设置同一IP地址是不可以的。通过bonding,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的MAC地址

2. bonding 工作模式

常见的工作模式

  • Mode 0 (balance-rr) : 轮转(Round-robin)策略,从头到尾顺序的在每一个slave 接口上面发送数据包。本模式提供负载均衡和容错的能力
  • Mode 1 (active-backup) :活动-备份(主备)策略,只有一个slave被激活,当且仅当活动的 slave 接口失败时才会激活其他 slave 。为了避免交换机发生混乱此时绑定的MAC地址只有一个外部端口上可见
  • Mode 3 (broadcast) :在所有的slave接口上传送所有的报文,提供容错能力

注意:active-backup、balance-tlb 和 balance-alb 模式不需要交换机的任何特殊配置。其他绑定模式需要配置交换机以便整合链接。如:Cisco 交换机需要在模式 0、2 和 3 中使用 EtherChannel,但在模式4中需要 LACP和 EtherChannel

3. CentOS 6上实现 bonding

准备两块网卡实现bonding(这两块网卡必须在同一物理网络中,这里使用vmware workstation虚拟机的仅主机模式),工作模式选择mode1

(1) 关闭 NetworkManager

[root@centos6 ~]# chkconfig NetworkManager off
[root@centos6 ~]# service NetworkManager stop

(2) 创建 bonding 设备和物理设备的配置文件

[root@centos6 ~]# cd /etc/sysconfig/network-scripts/
[root@centos6 network-scripts]# cat ifcfg-bond0   
DEVICE=bond0                       // 设备名称
BOOTPROTO=none
BONDING_OPTS='miimon=100 mode=1'   // miimon=100表示每100ms 监测一次链路连接状态,如果有一条线路不通就转入另一条线路,mode用来指定bonding的工作模式
IPADDR=192.168.249.100
PREFIX=24

[root@centos6 network-scripts]# cat ifcfg-eth0
DEVICE=eth0             
BOOTPROTO=none
MASTER=bond0                       // 指定主设备为bond0
SLAVE=yes                         // 将当前设备设置为从设备

[root@centos6 network-scripts]# cat ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

(3) 重启网络服务使配置生效

[root@centos6 network-scripts]# service network restart
[root@centos6 network-scripts]# ip a
......
2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000
   link/ether 00:0c:29:4d:68:f8 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000
   link/ether 00:0c:29:4d:68:f8 brd ff:ff:ff:ff:ff:ff
4: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
   link/ether 00:0c:29:4d:68:f8 brd ff:ff:ff:ff:ff:ff
   inet 192.168.249.100/24 brd 192.168.249.255 scope global bond0
   inet6 fe80::20c:29ff:fe4d:68f8/64 scope link tentative dadfailed 
      valid_lft forever preferred_lft forever

可以看出只有 bonding 设备有IP地址,物理网卡没有 IP 地址,两块物理网卡的被修改为和 binding 设备相同的MAC地址

(4) 查看 bonding 信息

通过查看 /proc/net/bonding/bond0 能够查看网卡的的状态,在主备模式下那块网卡处于激活状态

[root@centos6 network-scripts]# 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:4d:68:f8
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:4d:68:02
Slave queue ID: 0

这里eth0正处于active状态,如果断开eth0,eth1就会active,eth0的状态为down

(5) 删除 bonding

[root@centos6 network-scripts]# ifconfig bond0 down      // 停止bind0
[root@centos6 network-scripts]# rmmod bonding            // 卸载bongding模块
[root@centos6 network-scripts]# rm -rf ifcfg-bond0       // 删除bond0配置文件

然后把eth0,eth1的配置文件改回原样,重启网络服务即可

4. CentOS 7上实现 bonding

CentOS 7也可以像上面那样通过修改配置文件来实现bonding,这里演示用nmcli命令来实现bonding

(1) 添加 bonding 接口

[root@centos7 ~]# nmcli con add type bond con-name bond0 ifname bond0 mode active-backup    // 创建bonding接口,模式为主从模式
[root@centos7 ~]# nmcli con show
NAME   UUID                                  TYPE      DEVICE 
bon0   396e7d08-7824-4d1c-bd61-c34455caf6a3  bond      bond0  
......

在/etc/sysconfig/network-scripts目录下生成了ifcfg-bond0文件

(2) 为 bonding 接口添加从属接口

[root@centos7 ~]# nmcli con add type bond-slave ifname ens33 master bond0    // 将ens33接口添加到bond0的从属接口
[root@centos7 ~]# nmcli con add type bond-slave ifname ens34 master bond0    // 将ens34接口添加到bond0的从属接口

在/etc/sysconfig/network-scripts目录下生成了ifcfg-bond-slave-ens33
和ifcfg-bond-slave-ens34文件

(3) 启动绑定,先要启动从属接口

[root@centos7 ~]# nmcli con up bond-slave-ens33    // 启动绑定从属接口ens33
[root@centos7 ~]# nmcli con up bond-slave-ens34    // 启动绑定从属接口ens34
[root@centos7 ~]# nmcli con up bond0               // 启动绑定

(4) 查看绑定状态信息

可以用通过查看/proc/net/bonding/bond0文件来查看网卡状态信息,这里不再赘述

(5) 删除绑定

[root@centos7 ~]# nmcli con down bond0              // 停止 bond0,停掉后物理网络会自动开启 
[root@centos7 ~]# nmcli con del bond0               // 删除绑定配置文件
[root@centos7 ~]# nmcli con del bond-slave-ens33    // 删除绑定从属接口ens33配置文件
[root@centos7 ~]# nmcli con del bond-slave-ens34    // 删除绑定从属接口ens34配置文件

用nmcli命令删除这些配置文件后,网络会恢复到初始状态

CentOS 7下通过网络组 Network Teaming 绑定

1. Network Teaming 简介

  • 网络组:是将多个网卡聚合在一起方法,从而实现冗错和提高吞吐量
  • 网络组不同于旧版中bonding技术,提供更好的性能和扩展性
  • 网络组由内核驱动和teamd守护进程实现
  • 多种方式 runner :broadcast,roundrobin,activebackup,loadbalance,lacp

2. 创建网络组

(1) 创建网络组接口

使用如下命令创建网络组接口

nmcli con add type team con-name CNAME ifname INAME [config JSON]

其中 CNAME 是连接名,INAME 是接口名,JSON 指定 runner 方式,格式如下

'{"runner": {"name": "METHOD"}}'

METHOD 可以是 broadcast,roundrobin,activebackup,loadbalance,lacp

下面创建一个 team0 接口

[root@centos7 ~]# nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}'

在 /etc/sysconfig/network-scripts/ 目录下生成了 ifcfg-team0 文件

修改 team0 接口的地址

[root@centos7 ~]# nmcli con modify team0 ipv4.method manual ipv4.addresses 192.168.249.200/24

网关和 DNS 也可以根据自行需求配置修改,这里就不配置了

(2) 创建 port 接口

使用如下命令创建 port 接口

nmcli con add type team-slave con-name CNAME ifname INAME master TEAM

其中 CNAME 是连接名,INAME 是网络接口名,TEAM 是网络组接口名,连接名若不指定,默认为 team-slave-IFACE

下面将 ens33 和 ens34 添加到网络组

[root@centos7 ~]# nmcli con add type team-slave con-name team0-ens33 ifname ens33 master team0
[root@centos7 ~]# nmcli con add type team-slave con-name team0-ens34 ifname ens34 master team0

在 /etc/sysconfig/network-scripts/ 目录下生成了 ifcfg-team0-ens33 和 ifcfg-team0-ens34 文件

[root@centos7 ~]# nmcli con show
NAME         UUID                                  TYPE      DEVICE
......
team0        7f9c5773-47a6-47d4-8009-74a40fe2bc87  team      team0  
team0-ens33  ba16e000-3fa7-4db2-b7c5-9a7ef248cb98  ethernet  --     
team0-ens34  b5c77978-ff7f-4b3a-abc3-9df2eebb1ddb  ethernet  -- 

(3) 启动网络组

  • 启动网络组接口不会自动启动网络组中的 port 接口
  • 启动网络组接口中的 port 接口总会自动启动网络组接口
  • 禁用网络组接口会自动禁用网络组中的 port 接口
  • 没有port接口的网络组接口可以启动静态 IP 连接
  • 启用DHCP连接时,没有 port 接口的网络组会等待 port 接口的加入
[root@centos7 ~]# nmcli con up team0    // 启动网络组,完毕后就能 ping 通
[root@centos7 ~]# nmcli con up team0-ens33
[root@centos7 ~]# nmcli con up team0-ens34

(4) 查看网络组状态信息

[root@centos7 ~]# teamdctl team0 state
setup:
  runner: activebackup
ports:
  ens33
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
  ens34
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
runner:
  active port: ens33

(5) 删除网络组

[root@centos7 ~]# nmcli con down team0    // 停掉 team0,停止后物理网络会自动开启
[root@centos7 ~]# nmcli con del team0
[root@centos7 ~]# nmcli con del team0-ens33
[root@centos7 ~]# nmcli con del team0-ens34
posted @ 2018-08-28 11:31  独孤柯灵  阅读(912)  评论(0)    收藏  举报