前言

本实验使用VMware workstation14 pro环境,给虚拟机CentOS7新添加两张网卡来进行链路捆绑。

命令方法(nmcli)

添加网卡

我们用新添加的网卡来做链路捆绑,查看新添加的两张网卡的信息

[root@nat21 network-scripts]# ifconfig
…………省略…………

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        …………省略…………
# 获取VMnet1分配的IP地址
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.81.131  netmask 255.255.255.0  broadcast 192.168.81.255
        inet6 fe80::9df4:419e:bcfc:35fd  prefixlen 64  scopeid 0x20<link>
         …………省略…………
# 获取VMnet2分配的IP地址
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.131  netmask 255.255.255.0  broadcast 192.168.91.255
        inet6 fe80::3dc:4fd0:967b:2603  prefixlen 64  scopeid 0x20<link>
         …………省略…………
…………省略…………

查找模板

接下来配置链路捆绑,因为命令又长又复杂,我们来参照man文档的模板

[root@nat21 ~]# man nmcli-examples
# 输入"/Example 6"查看配置例6
 …………省略…………
 		# 添加一个bonding网卡
       Example 6. Adding a bonding master and two slave connection profiles
			# 添加bonding网卡,模式为active-backup
           $ nmcli con add type bond ifname mybond0 mode active-backup
           # 为bonding网卡添加两个slave
           $ nmcli con add type ethernet ifname eth1 master mybond0
           $ nmcli con add type ethernet ifname eth2 master mybond0

 …………省略…………

配置捆绑接口

# 添加类型为bond的网卡,名为Bond0,模式为active-backup
[root@nat21 network-scripts]#  nmcli con add type bond ifname Bond0 mode active-backup ip4 192.168.10.100/24
Connection 'bond-Bond0' (3a9f0f36-8cf9-4409-bf20-80a9fa1e3805) successfully added.

配置物理接口聚合

# 分别把ens36和ens37配置为Bond0的slave
[root@nat21 network-scripts]# nmcli con add type ethernet ifname ens36 master Bond0 
Connection 'bond-slave-ens36' (a54b4c12-f3ec-43f7-adc0-602f9b4c66fb) successfully added.
[root@nat21 network-scripts]# nmcli con add type ethernet ifname ens37 master Bond0 
Connection 'bond-slave-ens37' (29a10484-8129-468b-a358-41f75c4252df) successfully added.

查看接口

[root@nat21 network-scripts]# systemctl restart network
[root@nat21 network-scripts]# ifconfig
Bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 192.168.10.100  netmask 255.255.255.0  broadcast 192.168.10.255
        …………省略…………

ens36: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0c:29:24:c8:7d  txqueuelen 1000  (Ethernet)
       …………省略…………

ens37: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0c:29:24:c8:7d  txqueuelen 1000  (Ethernet)
        …………省略…………
# ping测试
[root@nat21 network-scripts]# ping 192.168.10.100
PING 192.168.10.100 (192.168.10.100) 56(84) bytes of data.
64 bytes from 192.168.10.100: icmp_seq=1 ttl=64 time=0.055 ms
64 bytes from 192.168.10.100: icmp_seq=2 ttl=64 time=0.072 ms
64 bytes from 192.168.10.100: icmp_seq=3 ttl=64 time=0.042 ms
^C
--- 192.168.10.100 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.042/0.056/0.072/0.013 ms

测试

[root@nat21 network-scripts]# ifconfig ens36 down		# bond配置为主备模式,down掉ens36
[root@nat21 network-scripts]# ping 192.168.10.100		# 正常ping通
PING 192.168.10.100 (192.168.10.100) 56(84) bytes of data.
64 bytes from 192.168.10.100: icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from 192.168.10.100: icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from 192.168.10.100: icmp_seq=3 ttl=64 time=0.044 ms
^C
--- 192.168.10.100 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.043/0.043/0.044/0.005 ms
# 开启ens36,down掉ens37
[root@nat21 network-scripts]# ifconfig ens36 up
[root@nat21 network-scripts]# ifconfig ens37 down
[root@nat21 network-scripts]# ping 192.168.10.100		# 正常ping通
PING 192.168.10.100 (192.168.10.100) 56(84) bytes of data.
64 bytes from 192.168.10.100: icmp_seq=1 ttl=64 time=0.053 ms
64 bytes from 192.168.10.100: icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from 192.168.10.100: icmp_seq=3 ttl=64 time=0.037 ms
^C
--- 192.168.10.100 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.037/0.044/0.053/0.008 ms

查看网卡文件内容

# 查看Bond0
[root@nat21 network-scripts]# vim ifcfg-bond-Bond0 
BONDING_OPTS=mode=active-backup	# 主备模式
TYPE=Bond
BONDING_MASTER=yes
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.10.100		# 手动指定IP地址
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=bond-Bond0
UUID=3a9f0f36-8cf9-4409-bf20-80a9fa1e3805
DEVICE=Bond0
ONBOOT=yes

# 查看ens36,ens37类似
[root@nat21 network-scripts]# vim ifcfg-bond-slave-ens36
TYPE=Ethernet
NAME=bond-slave-ens36
UUID=a54b4c12-f3ec-43f7-adc0-602f9b4c66fb
DEVICE=ens36
ONBOOT=yes
MASTER=Bond0
SLAVE=yes

编写网卡文件方法

查看网卡

[root@nat21 ~]# nmcli connection
NAME                UUID                                  TYPE      DEVICE  
docker0             408175a7-165a-496f-84a7-4ce0cff798cf  bridge    docker0 
ens33               472946ef-89e8-4f48-b28a-ad88581ae7b3  ethernet  ens33   
virbr0              90e8ef1f-aa62-4040-91ed-52887551603e  bridge    virbr0  
Wired connection 1  459a4e1d-1950-3008-be8e-4e12f22bb8ef  ethernet  ens36   
Wired connection 2  f7de1547-d1c6-3bef-a365-11389bba5377  ethernet  ens37  

编辑网卡文件

[root@nat21 network-scripts]# cp ifcfg-ens33 ifcfg-ens36	# 以ens33的网卡文件为模板进行修改
[root@nat21 network-scripts]# cp ifcfg-ens33 ifcfg-ens37
[root@nat21 network-scripts]# vim ifcfg-ens36
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none		# static/dhcp改为none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens36				# name改为ens36
UUID=459a4e1d-1950-3008-be8e-4e12f22bb8ef	 # UUID通过nmcli connection获取,改为ens36的UUID
DEVICE=ens36			# 设备名改为ens36
ONBOOT=yes
#IPADDR=192.168.218.21		# ens33的IP地址和掩码等信息,注释掉
#NETMASK=255.255.255.0
#GATEWAY=192.168.218.2
#DNS1=192.168.218.2
#ZONE=public
# 添加以下三行
USERCTL=no		# 不允许除root用户外的用户控制该设备
MASTER=bond0	# 指定master
SLAVE=yes		# 状态为slave


# 用同样的方法编辑ens37的网卡文件,注意UUID、NAME和DEVICE要对应ens37

新建网卡文件Bond0

[root@nat21 network-scripts]# vim ifcfg-Bond0
# 将下列内容写入文件中
ONBOOT=yes
DEVICE=bond0
BONDING_MASTER=yes
IPADDR=192.168.81.131	# IP address
PREFIX=24				# 前缀
GATEWAY=192.168.81.1	# 网关
BOOTROTO=static
USERCTL=no
BONDING=OPTS="mode=4 miimon=100"

测试&查看

[root@nat21 network-scripts]# systemctl restart network
[root@nat21 network-scripts]# ifconfig
…………省略…………
# 手动配置的IP生效
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 192.168.81.131  netmask 255.255.255.0  broadcast 192.168.81.255
        inet6 fe80::20c:29ff:fe24:c87d  prefixlen 64  scopeid 0x20<link>
         …………省略…………

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        …………省略…………

ens36: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0c:29:24:c8:7d  txqueuelen 1000  (Ethernet)
         …………省略…………

ens37: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0c:29:24:c8:7d  txqueuelen 1000  (Ethernet)
         …………省略…………
…………省略…………
# ping测试,pingVMnet1的网关,能通
[root@nat21 network-scripts]# ping 192.168.81.1
PING 192.168.81.1 (192.168.81.1) 56(84) bytes of data.
64 bytes from 192.168.81.1: icmp_seq=2 ttl=64 time=3.36 ms
^C
--- 192.168.81.1 ping statistics ---
3 packets transmitted, 1 received, 66% packet loss, time 2002ms
rtt min/avg/max/mdev = 3.365/3.365/3.365/0.000 ms

补充总结

在方法一中,bond模式默认支持捆绑两块网卡,同时支持更多模式。

mode模式
0(balance-rr)轮询方式负载分担
1(active-backup)主备模式
2(balance-xor)XOR hash负载分担
3(broadcast)广播策略
4(IEEE802.3ad)动态链接聚合
5(balance-tlb)适配器传输负载均衡
6(balance-alb)适配器适应性负载均衡

如需捆绑更多网卡,可以尝试绑成team接口,参照nmcli-examples里的Example 7。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐