快捷搜索:   nginx

linux bonding实现冗余和负载均衡

linux,bonding

说明:
绑定多块网卡为一个虚拟ip,类似csico的etherchannel,实现冗余或负载均衡和增加带宽的功能。
内核需要bonding的支持,察看是否挂在bonding,lsmod命令。默认2.6内核中bonding已经被编译为M的选项,不需重新编译内核。

其实Redhat关于bond,在kernel-doc里有一篇文档,讲述得非常详细,可以先看看/usr/share/doc/kernel-doc- 2.6.18/Documentation/networking/bonding.txt

一:不需重起的配置方法。
1 modprobe bonding miimon=100
2 ifconfig bond0 192.168.1.1 netmask 255.255.255.0
3 ifenslave bond0 eth0 eth1


二:重起仍然生效的配置方法一。
1关闭要绑定的物理网卡
修改ifcfg-eth0和ifcfg-eth1的启动项

BOOTPROTO=none
ONBOOT=no

2建立虚拟网卡

在/etc/sysconfig/network-scripts/ 目录下建立 ifcfg-bond0,并修改 /etc/modprobe.conf文件实现开机自动挂载。

/etc/sysconfig/network-scripts/ifcfg-bond0 配置如下:

DEVICE=bond0
IPADDR=192.168.0.193
NETMASK=255.255.255.0
BOOTPROTO=static
ONBOOT=yes
GATEWAY=192.168.0.3

/etc/modprobe.conf 配置如下:
alias eth0 bnx2
alias eth1 bnx2
alias bond0 bonding
options bonding miimon=100 mode=1(miimon是用来进行链路监测的。
比如:miimon=100,那么系统每100ms监测一次链路连接状态,如果有一条线路不通就转入另一条线路。模式1为主备模式,模式0为负载均衡与增加带宽的模式)
注:以上为只做一组bonding的方式,如果做多组的话可以更改为以下的方式:
alias eth0 bnx2
alias eth1 bnx2
alias eth2 e1000
alias eth3 e1000
install bond0 /sbin/modprobe -a eth0 eth1 && /sbin/modprobe bonding
alias bond0 bonding
install bond1 /sbin/modprobe -a eth2 eth3 && /sbin/modprobe bonding
alias bond1 bonding
options bonding mode=1 miimon=100 max_bonds=2

最后执行测试, REBOOT确认bond0是否启动,如果启动,配置成功。


查看bonding状态
cat /proc/net/bonding/bond0


三:重起仍然生效的配置方法二
1 vi /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
IPADDR=192.168.0.193
NETMASK=255.255.255.0
BOOTPROTO=static
ONBOOT=yes
GATEWAY=192.168.0.3


2 vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes

3 vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes

4 vi /etc/modprobe.conf

alias bond0 bonding
options bonding miimon=100 mode=1

5 reboot




四: Linux 的 BONDING 模式

bonding 的应用分为合并网卡提高带宽与冗余两种功能。

在 linux kernel bonding 的 kernel module 內,可以依据传入 mode=X 的方式来决定运行模式,其中数值可能結果依据官方文件说明如下:

mode=0 (balance-rr)

Round-robin policy: Transmit packets in sequential order from the first available
slave through the last. This mode provides load balancing and fault tolerance.

mode=1 (active-backup)

Active-backup policy: Only one slave in the bond is active. A different slave
becomes active if, and only if, the active slave fails. The bond's MAC address is externally visible on only one port

(network adapter) to avoid confusing the switch. This mode provides fault tolerance. The primary option affects the behavior

of this mode.

mode=2 (balance-xor)

XOR policy: Transmit based on [(source MAC address XOR'd with destination
MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load

balancing and fault tolerance.

所以可以依据实际需求決定要使用哪种模式来提供 bonding 功能。一般所谓合并与平衡负载功能部份,选择 mode=0,而若是要达成 active-backup 架构的話,則选择使用 mode=1 即可,注意,网卡需要支持mii-tool

设定配置文件, /etc/modprobe.conf 放置如下內容即可决定类型:

alias bond0 bonding
options bond0 miimon=100 mode=1

说明:miimon 是用来进行链路监测的。如果miimon=100,那么系统每100ms 监测一次链路连接状态,如果有一条线路不通就转入另一条线路;mode 的值表 示工作模式,它共有0,1,2,3四种模式,常用的为0,1两种。
mode=0 表示load balancing (round-robin)为负载均衡方式,两块网卡都工作。
mode=1 表示fault tolerance (active-backup)提供冗余功能,工作方式是主备的工作方式,也就是说默认情况下只有一块网卡工作,另一块做备份。
max_bonds=2 表示最大的网卡绑定数量为2。
顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论