Linux下通过bonding技术实现网络负载均衡

有时候一些重要的文件服务器除了在应用层实现高可用和负载均衡外,还需要在硬件(网卡)、物理线路上来保证网络的的高可用及负载均衡。

Linux bonding 驱动提供了一个把多个网络接口设备捆绑为单个的网络接口设置来使用,用于网络负载均衡及网络冗余,bonding在Linux内核2.4.12以后就已经默认支持,你可以通过如下方法查看内核是否支持bonding,否则你需要将它编译到内核。

[root@localhost ~]# modprobe -l | grep bonding
/lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/bonding/bonding.ko

如果有返回结果,就说明内核支持,下面就简单介绍下实施步骤。



实施步骤如下:

1、创建网卡bond0文件
[root@localhost network-scripts]# more ifcfg-bond0
# Broadcom Corporation NetXtreme II BCM5708 Gigabit Ethernet
DEVICE=bond0
BOOTPROTO=none
ONBOOT=yes
NETMASK=255.255.255.0
IPADDR=192.168.100.11
GATEWAY=192.168.100.1
TYPE=Ethernet


2、修改ifcfg-eth0文件
[root@localhost network-scripts]# more ifcfg-eth0
# Broadcom Corporation NetXtreme II BCM5708 Gigabit Ethernet
DEVICE=eth0
USERCTL=no
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
TYPE=Ethernet


3、修改ifcfg-eth0文件
[root@localhost network-scripts]# more ifcfg-eth1
# Broadcom Corporation NetXtreme II BCM5708 Gigabit Ethernet
DEVICE=eth1
USERCTL=no
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
TYPE=Ethernet


4、修改/etc/modprobe.conf配置文件
目的是将新添加的bond0设备加入modprobe.conf中,以便kernel识别。

[root@localhost network-scripts]# more /etc/modprobe.conf
alias bond0 bonding
options bond0 miimon=100 mode=1

注:
miimon:是用来进行链路监测的 ,例如miimon=100,单位是毫秒,那么系统每100ms监测一次链路连接状态,如果有一条线路不通就转入另一条线路。
mode:为工作模式,可设置为高可用还是负载均衡,0为负载均衡(默认值),需根据交换机提供的工作模式选择,1为主备模式(也就是说只有一块网卡工作,另一块做备份)。

如果你要更改mode,记得一定要重新启动机器,切记切记!!!


5、将bonding生效(或完成上述步骤后重新启动电脑)
[root@localhost ~]# ldconfig
[root@localhost ~]# /etc/init.d/network restart


6、查看bonding状态
[root@localhost ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.4.0 (October 7, 2008)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:1e:c9:b6:ac:cf

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:1e:c9:b6:ac:d1

你也可以down掉 eth0 借口,再看看情况:

[root@localhost ~]# ifdown eth0
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.4.0 (October 7, 2008)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:1e:c9:b6:ac:d1



在测试中分别测试了高可用环境和负载均衡环境,网络故障的切换速度很快,对数据包的传输没有任何影响,在负载均衡下,整个机器的网路吞吐量比单网卡翻了一倍,你可以使用cacti来监控网络流量状况。

官方说明文档:http://www.kernel.org/pub/linux/kernel/people/marcelo/linux-2.4/Documentation/networking/bonding.txt

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老帽爬新坡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值