1,Bond 的工作模式
Linux bonding 驱动提供了一个把多个网络接口设备捆绑为单个的网络接口设置来使用, 用于网络负载均衡及网络冗余.
bonding 一共有 7 种工作模式(mode):
1),bond=0,(balance-rr) Round-robin policy: (平衡轮询策略): 传输数据包顺序是依次传输, 直到最后一个传输完毕, 此模式提供负载平衡和容错能力.
2),bond=1,(active-backup) Active-backup policy:(活动备份策略): 只有一个设备处于活动状态. 一个宕掉另一个马上由备份转换为主设备. mac 地址是外部可见得. 此模式提供了容错能力.
3),bond=2,(balance-xor) XOR policy:(平衡策略): 传输根据 [(源 MAC 地址 xor 目标 MAC 地址)mod 设备数量] 的布尔值选择传输设备. 此模式提供负载平衡和容错能力.
4),bond=3,(broadcast) Broadcast policy:(广播策略): 将所有数据包传输给所有设备. 此模式提供了容错能力.
5),bond=4,(802.3ad) IEEE 802.3ad Dynamic link aggregation. IEEE 802.3ad 动态链接聚合: 创建共享相同的速度和双工设置的聚合组. 此模式提供了容错能力. 每个设备需要基于驱动的重新获取速度和全双工支持; 如果使用交换机, 交换机也需启用 802.3ad 模式.
6),bond=5,(balance-tlb) Adaptive transmit load balancing(适配器传输负载均衡): 通道绑定不需要专用的交换机支持. 发出的流量根据当前负载分给每一个设备. 由当前设备处理接收, 如果接受的设 备传不通就用另一个设备接管当前设备正在处理的 mac 地址.
7),bond=6,(balance-alb) Adaptive load balancing: (适配器负载均衡): 包括 mode5, 由 ARP 协商完成接收的负载. bonding 驱动程序截获 ARP 在本地系统发送出的请求, 用其中之一的硬件地址覆盖从属设备的原地址. 就像是在服务器上不同的人使用不同的硬件地址一样.
2, 配置步骤
以下步骤为配置 bond=6 的双网卡绑定步骤, 其他模式的方法一样;
2.1, 安装 ifenslave 软件
fenslave 为一种粘合和分离式的软件, 可以将数据包有效的分配到 bonding 驱动
sudo apt install ifenslave
2.2,/etc/modules 文件
在 / etc/modules 文件中加入以下配置
bonding mode=6 miimon=100
mode=6 表示采用模式 1;
miimon 是用来进行链路监测的. 比如 miimon=100, 表示系统每 100ms 监测一次链路连接状态, 如果有一条线路不通就切换另一条线路.
2.3, 修改 / etc/network/interfaces 文件配置
首先需要确定网卡接口的名称, 可以通过 ifconfig 命令查看, 当前是网卡接口分别是 ens33 和 ens34; 在 / etc/network/interfaces 文件增加如下配置
- # 双网卡配置
- auto ens33
iface ens33 inet manual
- bond-master bond0
- auto ens34
iface ens34 inet manual
- bond-master bond0
- auto bond0
iface bond0 inet static
- address 192.168.20.10
- netmask 255.255.255.0
- gateway 192.168.20.2
- dns-nameservers 192.168.20.1 119.29.29.29
- bond-slaves ens33 ens34
- bond-lacp-rate 1
- bond-mode 6
- bond-miimon 100
2.4, 加载 binding 模块
# 载入模块
$ sudo modprobe bonding
# 查看模块, 如果模块已经加载, 显示出来
$ sudo lsmod|grep bonding
bonding 163840 0
2.5, 查看状态
查看网络配置
$ ifconfig
bond0 Link encap: 以太网 硬件地址 00:0c:29:34:7a:4a
inet 地址: 192.168.20.10 广播: 192.168.20.255 掩码: 255.255.255.0
inet6 地址: fe80::20c:29ff:fe34:7a4a/64 Scope:Link
UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 跃点数: 1
接收数据包: 964 错误: 0 丢弃: 0 过载: 0 帧数: 0
发送数据包: 4205 错误: 0 丢弃: 0 过载: 0 载波: 0
碰撞: 0 发送队列长度: 1000
接收字节: 75022 (75.0 KB) 发送字节: 300032 (300.0 KB)
ens33 Link encap: 以太网 硬件地址 00:0c:29:34:7a:4a
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 跃点数: 1
接收数据包: 2740 错误: 0 丢弃: 0 过载: 0 帧数: 0
发送数据包: 3416 错误: 0 丢弃: 0 过载: 0 载波: 0
碰撞: 0 发送队列长度: 1000
接收字节: 3899473 (3.8 MB) 发送字节: 278409 (278.4 KB)
ens34 Link encap: 以太网 硬件地址 00:0c:29:34:7a:54
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 跃点数: 1
接收数据包: 1369 错误: 0 丢弃: 0 过载: 0 帧数: 0
发送数据包: 2125 错误: 0 丢弃: 0 过载: 0 载波: 0
碰撞: 0 发送队列长度: 1000
接收字节: 106846 (106.8 KB) 发送字节: 139061 (139.0 KB)
查看 binding 状态
$ cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
Bonding Mode: adaptive load balancing
Primary Slave: None
Currently Active Slave: ens33
- MII Status: up
- 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:34:7a:4a
- Slave queue ID: 0
- Slave Interface: ens34
- MII Status: up
- Speed: 1000 Mbps
- Duplex: full
- Link Failure Count: 0
Permanent HW addr: 00:0c:29:34:7a:54
Slave queue ID: 0
2.6, 验证测试
物理上关闭一个网卡(拔网线)
- # 关闭前, 速率是 2000Mb/s
- $ ethtool bond0
- Settings for bond0:
- Supported ports: [ ]
Supported link modes: Not reported
- Supported pause frame use: No
- Supports auto-negotiation: No
Advertised link modes: Not reported
- Advertised pause frame use: No
- Advertised auto-negotiation: No
- Speed: 2000Mb/s
- Duplex: Full
- Port: Other
- PHYAD: 0
- Transceiver: internal
- Auto-negotiation: off
Cannot get wake-on-lan settings: Operation not permitted
- Link detected: yes
- # 关闭后, 速率速率变成 1000Mb/s
- $ ethtool bond0
- Settings for bond0:
- Supported ports: [ ]
Supported link modes: Not reported
- Supported pause frame use: No
- Supports auto-negotiation: No
Advertised link modes: Not reported
- Advertised pause frame use: No
- Advertised auto-negotiation: No
- Speed: 1000Mb/s
- Duplex: Full
- Port: Other
- PHYAD: 0
- Transceiver: internal
- Auto-negotiation: off
Cannot get wake-on-lan settings: Operation not permitted
Link detected: yes
注意: 不可以使用 ifdown 关闭网卡, 会导致网络中断
3, 总结
Linux bonding 一共有 7 种工作模式(mode), 如果是想要加大网卡的吞吐量, 通常会使用 mode=6 的模式, 如果是注重网络的稳定, 通常会使用 mode=1 的模式;
来源: http://www.bubuko.com/infodetail-2655565.html