Linux网卡绑定
Bonding常用的mode种类:
mode=0 ,即:(balance-rr) Round-robin policy(平衡轮循环策略)数据包的传输方式轮询传输,第一个包传输给eth0 ,第二个包传输给eth1,就这样循环下去,直到传输完毕,这里需要注意的是mode0能作为负载均衡作用,不提供容错功能,如果第一张网卡出现故障网络将面临瘫痪。
mode=1,即: (active-backup) Active-backup policy(主-备份策略)只有一个处于活动状态,当主网卡出现故障时,备网卡马上由备转变成主。这个模式提供了较高的容错能力。但是资源利用率较低。
mode=3,即:broadcast(广播策略)在每个slave接口上传输每个数据包,此模式提供了容错能力。
使用bonding软件设置
1.查看系统是否支持bonding
[root@linuxprobe network-scripts]# cat /boot/config-4.18.0-80.el8.x86_64 | grep -i bonding
CONFIG_BONDING=m
2.关闭NetworkManager服务(7系列版本以下开启NetworkManager与bonding会产生冲突)
[root@linuxprobe network-scripts]# systemctl stop NetworkManager.service
[root@linuxprobe network-scripts]# chkconfig NetworkManager off //关闭开机自启动
3.创建虚拟网卡ifcfg-bondx
[root@linuxprobe network-scripts]vim /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0 //设备名
BOOTPROTO=static //IP的获取方式
ONBOOT=yes //开机自启动
IPADDR=10.129.49.240 //IP地址
NETMASK=255.255.255.0 //掩码
GATEWAY=10.129.49.1 //网关
DNS1=10.112.15.30
BONDING_OPTS="miimon=100,mode=1" //mode类型,网络链路检测频率100ms检查一次
4.修改加入bond的网卡配置文件
[root@linuxprobe network-scripts]vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
MASTER=bond0
SLAVE=yes
[root@linuxprobe network-scripts]vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth0
ONBOOT=yes
MASTER=bond0
SLAVE=yes
5.重启网络服务
[root@linuxprobe network-scripts]systemctl restart network
6.查看配置
[root@cobbler-node1 network-scripts]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: up
Speed: Unknown
Duplex: Unknown
Link Failure Count: 0
Permanent HW addr: 28:6e:d4:88:ce:5d
Slave queue ID: 0
Slave Interface: eth1
MII Status: up
Speed: Unknown
Duplex: Unknown
Link Failure Count: 0
Permanent HW addr: 28:6e:d4:88:ce:5e
Slave queue ID: 0
[root@cobbler-node1 network-scripts]# ifconfig
bond0: flags=5187
inet 10.129.49.240 netmask 255.255.255.0 broadcast 10.129.49.255
inet6 fe80::2a6e:d4ff:fe88:ce5d prefixlen 64 scopeid 0x20
ether 28:6e:d4:88:ce:5d txqueuelen 1000 (Ethernet)
RX packets 1675775 bytes 126050576 (120.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3432 bytes 496778 (485.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=6211
ether 28:6e:d4:88:ce:5d txqueuelen 1000 (Ethernet)
RX packets 837774 bytes 63015040 (60.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1718 bytes 237790 (232.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=6211
ether 28:6e:d4:88:ce:5d txqueuelen 1000 (Ethernet)
RX packets 838224 bytes 63053418 (60.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1714 bytes 258988 (252.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用NetworkManager配置
Nmcli方式(命令行)
---------------------------------------------------------------------------------------------------------------------------------------------------------
一.创建bonding
创建名称为bond1的绑定
[root@localhost ~]# nmcli connection add type bond con-name bond1 ifname bond1 mode active-backup //active-backup主备模式,balance-rr负载均衡模式
Connection 'bond1' (74659382-3e46-4a95-851d-66f90b2ece30) successfully added.
将enp7s0f0增加到bond1
[root@localhost ~]# nmcli connection add type bond-slave ifname enp7s0f0 master bond1
Connection 'bond-slave-enp7s0f0' (061f5c5c-3366-4025-8149-7c89e49ab1b3) successfully added.
将enp7s0f1增加到bond1
[root@localhost ~]# nmcli connection add type bond-slave ifname enp7s0f1 master bond1
Connection 'bond-slave-enp7s0f1' (45440593-243e-4618-ab1a-0845555e11c1) successfully added.
二.启动bonging
启动从属接口enp7s0f0
[root@localhost ~]# nmcli connection up bond-slave-enp7s0f0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
启动从属接口enp7s0f1
[root@localhost ~]# nmcli connection up bond-slave-enp7s0f1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
启动绑定
[root@localhost ~]# nmcli connection up bond1
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/11)
[root@localhost ~]# ifconfig
bond1: flags=5123 mtu 1500
ether a0:36:9f:dc:5c:48 txqueuelen 0 (Ethernet)
RX packets 34 bytes 6596 (6.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12 bytes 1992 (1.9 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.创建出一个bond网卡
[root@linuxprobe ~]# nmcli connection add type bond con-name bond0 ifname bond0 bond.options "mode=balance-rr"
Connection 'bond0' (b37b720d-c5fa-43f8-8578-820d19811f32) successfully added.
2.向bond0添加从属网卡
[root@linuxprobe ~]# nmcli connection add type ethernet slave-type bond con-name bond0-port1 ifname ens160 master bond0 Connection 'bond0-port1' (8a2f77ee-cc92-4c11-9292-d577ccf8753d) successfully added. [root@linuxprobe ~]# nmcli connection add type ethernet slave-type bond con-name bond0-port2 ifname ens192 master bond0 Connection 'bond0-port2' (b1ca9c47-3051-480a-9623-fbe4bf731a89) successfully added.
3.配置bond0设备的网卡信息
可以使用nmcli命令依次配置网络的IP地址及子网掩码、网关、DNS、搜索域和手动配置等参数,也可以直接编辑网卡配置文件,或使用nmtui命令完成下面的操作。
[root@linuxprobe ~]# nmcli connection modify bond0 ipv4.addresses 192.168.10.10/24 [root@linuxprobe ~]# nmcli connection modify bond0 ipv4.gateway 192.168.10.1 [root@linuxprobe ~]# nmcli connection modify bond0 ipv4.dns 192.168.10.1 [root@linuxprobe ~]# nmcli connection modify bond0 ipv4.dns-search abc.com [root@linuxprobe ~]# nmcli connection modify bond0 ipv4.method manual
4.启动bond网卡
[root@linuxprobe ~]# nmcli connection up bond0 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/22) [root@linuxprobe ~]# nmcli device status DEVICE TYPE STATE CONNECTION bond0 bond connected bond0 ens160 ethernet connected ens160 virbr0 bridge connected virbr0 ens192 ethernet connected bond0-port2 lo loopback unmanaged -- virbr0-nic tun unmanaged --
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Nmtui方式(图形化)
#nmtui
1.在开始菜单中选择 编辑连接。选择 添加 时会打开 编辑连接 页面。
NetworkManager 文本用户界面的添加绑定连接菜单
2.选择 绑定,然后选择 创建 打开绑定的 编辑连接 页面。
NetworkManager 文本用户界面的添加绑定连接菜单
3.NetworkManager 此时需要在绑定中添加从属接口。要添加从属接口,请选择 添加 打开 新建连接 页面。选择连接类型后,请点击 创建 按钮。
NetworkManager 文本用户界面的配置绑定从属连接菜单
4.显示从属连接 编辑连接 页面。在 Device 字段输入所需从属设备名称或 MAC 地址。如有必要,选择 以太网 标签右侧的 显示,输入作为绑定的 MAC 地址的克隆 MAC 地址。选择 确定 按钮保存辅设备。
注意:如果没有为该设备指定 MAC 地址,则会在重新载入 编辑连接 窗口时自动填写 Device 部分,但只能在成功找到该设备时方可有此效果。
NetworkManager 文本用户界面的配置绑定从属连接菜单
5.从属连接 部分显示绑定从属连接名称。重复上述步骤添加其他从属连接。
6.点击 确定 按钮前请检查并确定设置。
NetworkManager 文本用户界面中完成的绑定。