【05-4】 网卡绑定bond0的实现

环境

[root@centos8 ~]# cat /etc/redhat-release 
CentOS Linux release 8.4.2105
[root@centos8 ~]# uname -r
4.18.0-305.3.1.el8.x86_64

网卡信息

[root@centos8 ~]# nmcli connection 
NAME UUID TYPE DEVICE
eth0 6ace3223-0c8c-4e95-a41f-7520a377b492 ethernet eth0
有线连接 1 2a2fe90a-7117-3938-afad-f332e5dc29da ethernet eth1
有线连接 2 f3759019-f734-37a3-8e8e-1bec847c6261 ethernet eth2

 一、Bongding聚合链路工作模式

bond聚合链路模式共7种:0-6Mode

  • bond 0 负载均衡 轮询方式往每条链路发送报文,增加带宽和容错能力。容易出现数据包无序到达的问题,数据包重发后吞吐量下降,性能增长不理想
  • bond 1 主备策略 主备策略为两个网卡中只有一个在工作就行,另一个完全待机。
  • bond 2 平衡策略 通过算法聚和端口,增加带宽,提高容错率,当一条有问题会由另一条接管。适合本地网络配置,如果通过单个路由器或只有一个网关算法将无意义
  • bond 3 广播策略 该策略bond端口会复制报文往下端口分别传输。该方案由较好的容错机制,适合金融行业,不容易出问题
  • bond 4 动态链路聚合 该协议需要上线链路支持LACP(链路汇聚控制协议)协议,端口状态通过该协议自动维护。可以增加带宽,由容错能力,链路出问题会切换到正常的端口,但是增加了LACP协议 所以该协议会发送LACPDU报文来维护链路聚合状态,保证链路质量
  • bond 5 适配器传输负载均衡 balance-tlb 在每个物理接口上根据当前的负载(根据速度计算)分配外出流量。如果正在接收数据的物理接口出故障了,另一个物理接口接管该故障物理口的MAC地址
  • bond 6 适配器适应性负载均衡 balance-alb 支持负载均衡,也不需要交换机支持,负载均衡通过ARP协商实现。bonding驱动截获本机发送的ARP应答,并把源硬件地址改为bond中某个物理接口的唯一硬件,从而是的不同的对端使用不同的硬件地址进行通讯。 6和0的区别是 0是平均分配,6是一个满了换下一个

常用的模式为0,1,3,6

mode 1、5、6 不需要交换设置

mode 0、2、3、4需要交换机设置

二、配置bond0

· 使用nmcli配置

1.创建bond0的主属

[root@centos8 ~]# nmcli connection add con-name bond0 type bond ifname bond0 mode active-backup 
连接 "bond0" (ef1d24dc-5dc5-4405-a935-f1f0fe7e6d4b) 已成功添加。
[root@centos8 ~]# nmcli connection
NAME UUID TYPE DEVICE
bond0 ef1d24dc-5dc5-4405-a935-f1f0fe7e6d4b bond bond0
eth0 6ace3223-0c8c-4e95-a41f-7520a377b492 ethernet eth0
有线连接 1 2a2fe90a-7117-3938-afad-f332e5dc29da ethernet eth1
有线连接 2 f3759019-f734-37a3-8e8e-1bec847c6261 ethernet eth2

2.把现有网卡配置的master配置为bond0网卡上并且修改名字

[root@centos8 ~]# nmcli connection modify 有线连接\ 1 con-name eth1 master bond0 
[root@centos8 ~]# nmcli connection modify 有线连接\ 2 con-name eth2 master bond0
#如果不用修改名字
[root@centos8 ~]# nmcli connection modify eth1 master bond0
[root@centos8 ~]# nmcli connection modify eth2 master bond0

 3.配置bond0网卡

[root@centos8 ~]# nmcli connection modify bond0 ipv4.addresses 10.0.0.102/24 ipv4.gateway 10.0.0.2 ipv4.dns '10.0.0.2,114.114.114.114'
[root@centos8 ~]# nmcli device reapply bond0

我的实验室在ESXi上进行的,关闭一个网卡后仍能使用 

· 使用文件配置

 1.创建个虚拟网卡并配置

[root@centos1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-bond0
-bash: vim: command not found
[root@centos1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-bond0
[root@centos1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=202.207.146.31
GATEWAY=255.255.255.0
NETMASK=202.207.146.1
DNS1=202.207.0.06

 

2.配置物理网卡并绑定bond0

[root@centos1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

[root@centos1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE=eth2
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

 

 3.修改modprobe相关设定文件,并加载bonding模块

#1.配置相关文件

# miimon为链路检查时间设定为200s
# mode=1 模式为1
[root@centos1 ~]# vi /etc/modprobe.d/bonding.conf
alias bond0 bonding
options bonding mode=0 miimon=200
# 2.加载模块

[root@centos1 ~]# modprobe bonding

# 3.确认模块是否加载成功:

[root@centos1 ~]# lsmod |grep bonding
bonding 133013 0

 

4.重启网卡并查看情况

[root@centos1 ~]# /etc/init.d/network restart
正在关闭接口 bond0: [确定]
正在关闭接口 eth0: [确定]
关闭环回接口: [确定]
弹出环回接口: [确定]
……
[root@centos1 ~]# 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: eth1
MII Status: up
MII Polling Interval (ms): 200
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth1
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:ea:06:d7
Slave queue ID: 0

Slave Interface: eth2
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:ea:06:e1
Slave queue ID: 0
[root@centos1 ~]# ifconfig | grep HW
bond0 Link encap:Ethernet HWaddr 00:0C:29:EA:06:D7
eth0 Link encap:Ethernet HWaddr 00:0C:29:EA:06:CD
eth1 Link encap:Ethernet HWaddr 00:0C:29:EA:06:D7
eth2 Link encap:Ethernet HWaddr 00:0C:29:EA:06:D7

 

4.测试网卡

ping 百度时候在ESXi中关闭一个网卡 依然是能ping通的,说明链路没有问题