文章目录
- 1、bonding简述
- 1.1 bonding原理
- 1.2 Bonding模式(bonding mode)
- 2、网卡配置文件的配置
- 2.1 配置环境
- 2.2 需要添加或修改的配置文件有5个(mode=1)
- 2.2.1 /etc/sysconfig/network-scripts/ifcfg-{eth0,eth1,bonding0}修改或添加
- 2.2.2 修改/etc/modprobe.d/dist.conf文件
- 2.2.3 修改配置文件/etc/rc.local
- 2.2.4 重启网络(systemctl restart network),并查看三个接口的mac地址
- 3、验证网络的连通性
1、bonding简述
双网卡配置设置虚拟为一个网卡实现网卡的冗余,其中一个网卡坏掉后网络通信仍可正常使用,实现网卡层面的负载均衡和高可用性。
现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时又能做相应的冗余,可以说是好处多多。而一般企业都会使用linux操作系统下自带的网卡绑定模式,当然现在网卡产商也会出一些针对windows操作系统网卡管理软件来做网卡绑定(windows操作系统没有网卡绑定功能 需要第三方支持)。
1.1 bonding原理
网卡工作在混杂(promisc)模式,接收到达网卡的所有数据包,tcpdump工作用的也是混杂模式(promisc),将两块网卡的MAC地址修改为相同接收特定MAC的数据帧,然后把相应的数据帧传送给bond驱动程序进行处理。
1.2 Bonding模式(bonding mode)
- 轮询策略(round robin),mode=0,按照设备顺序依次传输数据包,提供负载均衡和容错能力
- 主备策略(active-backup),mode=1,只有主网卡处于工作状态,备网卡处于备用状态,主网卡坏掉后备网卡开始工作,提供容错能力
- 异或策略(load balancing (xor)),mode=2,根据源MAC地址和目的MAC地址进行异或计算的结果来选择传输设备,提供负载均衡和容错能力
- 广播策略(fault-tolerance (broadcast)),mode=3,将所有数据包传输给所有接口通过全部设备来传输所有数据,一个报文会复制两份通过bond下的两个网卡分别发送出去,提供高容错能力
- 动态链接聚合(lacp),mode=4,按照802.3ad协议的聚合自动配置来共享相同的传输速度,网卡带宽最高可以翻倍,链路聚合控制协议(LACP)自动通知交换机聚合哪些端口,需要交换机支持 802.3ad协议,提供容错能力
- 输出负载均衡模式(transmit load balancing),mode=5,输出负载均衡模式,只有输出实现负载均衡,输入数据时则只选定其中一块网卡接收,需要网卡和驱动支持ethtool命令
- 输入/输出负载均衡模式(adaptive load balancing),mode=6,输入和输出都实现负载均衡,需要网卡和驱动支持ethtool命令
2、网卡配置文件的配置
2.1 配置环境
环境:系统CentOS 7 ,至少两块物理网卡(VMware上添加eth0,eth1)
2.2 需要添加或修改的配置文件有5个(mode=1)
这5个配置文件是:
/etc/sysconfig/network-scripts/ifcfg-ens3{3,7}
/etc/sysconfig/network-scripts/ifcfg-bond0 #需要手动创建
/etc/modprobe.d/dist.conf #需要手动创建
/etc/rc.local
2.2.1 /etc/sysconfig/network-scripts/ifcfg-{eth0,eth1,bonding0}修改或添加
提示:先备份好ens33和ens37,再修改这几个文件
以下是修改好的三个网卡配置文件的参数
[root@myos ~]# systemctl stop NetworkManager
[root@myos ~]# systemctl disable NetworkManager
[root@ant network-scripts]# vimdiff ifcfg-ens33 ifcfg-ens37 ifcfg-bond0
2.2.2 修改/etc/modprobe.d/dist.conf文件
在此文件中添加以下内容:
[root@myos ~]# cat /etc/modprobe.d/dist.conf
alias bond0 bonding
options bond0 miimon=100 mode=1 primary=ens33
alias bond0 bonding,表示系统在启动时加载bonding模块,对外虚拟网络接口设备为 bond0
miimon=100,表示系统每100ms监测一次链路连接状态,如果有一条线路不通就转入另一条线
mode=1,表示绑定模式为1
primary=eth0,系统首先eth0作为bond0接口与外界信息的传输接口
2.2.3 修改配置文件/etc/rc.local
在此文件中添加以下内容:
[root@myos ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
#bondind ens33 &ens37 to bond0
modprobe bonding miimon=100 mode=1
2.2.4 重启网络(systemctl restart network),并查看三个接口的mac地址
使用ifconfig命令显示,bond0,ens33,ens37物理地址相同,提示三个网卡均通过一个ip主机端口与外界通信
[root@myos ~]# ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 1500
inet 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe2d:5855 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2d:58:55 txqueuelen 1000 (Ethernet)
RX packets 69200 bytes 4792417 (4.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 135252 bytes 34283816 (32.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether 00:0c:29:2d:58:55 txqueuelen 1000 (Ethernet)
RX packets 69761 bytes 4846113 (4.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 135543 bytes 34368632 (32.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether 00:0c:29:2d:58:55 txqueuelen 1000 (Ethernet)
RX packets 2237 bytes 182416 (178.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1174 bytes 177860 (173.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
但是,我们可以看到,在mode=1的情况下,当前bond0采用eth0通信,实际的物理网卡地址
[root@myos ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
Bonding Mode: fault-tolerance (active-backup)
Primary Slave: ens33 (primary_reselect always)
Currently Active Slave: ens33
MII Status: upd
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: ens33
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:2d:58:55
Slave queue ID: 0
Slave Interface: ens37
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:2d:58:5f
Slave queue ID: 0
3、验证网络的连通性
[root@myos ~]# ping www.baidu.com -c 6
PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=128 time=29.8 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=128 time=44.6 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=3 ttl=128 time=33.1 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=128 time=24.9 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=5 ttl=128 time=34.1 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=6 ttl=128 time=28.8 ms
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5019ms
rtt min/avg/max/mdev = 24.915/32.587/44.604/6.162 ms
没有丢包,网络连通性可。
[root@myos ~]# tcpdump port 22 -Stn -c 10
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on bond0, link-type EN10MB (Ethernet), capture size 262144 bytes
IP 192.168.1.11.ssh > 192.168.1.1.65219: Flags [P.], seq 2174852580:2174852768, ack 1393506032, win 261, length 188
IP 192.168.1.11.ssh > 192.168.1.1.65219: Flags [P.], seq 2174852768:2174852924, ack 1393506032, win 261, length 156
IP 192.168.1.1.65219 > 192.168.1.11.ssh: Flags [.], ack 2174852924, win 4103, length 0
IP 192.168.1.11.ssh > 192.168.1.1.65219: Flags [P.], seq 2174852924:2174853168, ack 1393506032, win 261, length 244
IP 192.168.1.11.ssh > 192.168.1.1.65219: Flags [P.], seq 2174853168:2174853324, ack 1393506032, win 261, length 156
IP 192.168.1.1.65219 > 192.168.1.11.ssh: Flags [.], ack 2174853324, win 4102, length 0
IP 192.168.1.11.ssh > 192.168.1.1.65219: Flags [P.], seq 2174853324:2174853568, ack 1393506032, win 261, length 244
IP 192.168.1.11.ssh > 192.168.1.1.65219: Flags [P.], seq 2174853568:2174853724, ack 1393506032, win 261, length 156
IP 192.168.1.1.65219 > 192.168.1.11.ssh: Flags [.], ack 2174853724, win 4106, length 0
IP 192.168.1.11.ssh > 192.168.1.1.65219: Flags [P.], seq 2174853724:2174853968, ack 1393506032, win 261, length 244
10 packets captured
10 packets received by filter
0 packets dropped by kernel
参考:
https://www.linuxidc.com/Linux/2017-11/148410.htm?utm_source=tuicool&utm_medium=referral