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

时间:2018-06-23 10:32:57   收藏:0   阅读:1098
Ubuntu 配置双网卡绑定,实现负载均衡

1、Bond的工作模式

Linux bonding驱动提供了一个把多个网络接口设备捆绑为单个的网络接口设置来使用,用于网络负载均衡及网络冗余。

bonding一共有7种工作模式(mode):

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)
$ 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
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!