Linux 配置双网卡绑定实现负载均衡

Linux 配置双网卡绑定,实现负载均衡

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、配置步骤

以下步骤为Ubuntu系统,配置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的模式;

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 Linux 配置双网卡绑定实现负载均衡 https://www.lizi.tw/soft/10312.html

建筑工地上施工员,闲暇时弄个博客打发时间,

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Linux 配置双网卡绑定实现负载均衡-海报

分享本文封面