上周五,代理商的一位同事问及,如何在linux下实现双网绑定同一个IP,对外提供服务。由此便写了一篇文档,如何在linux下网卡绑定实现负载均衡。
这里以CentOS4.8为例(公司的产品就架在CentOS4.8上的,有点老,但配置与5并没区别)
配置很简单:
第一步:编辑/etc/sysconfig/network-scripts/ifcfg-bond0文件
DEVICE=bond0<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

BOOTPROTO=static

IPADDR=192.168.5.128

NETMASK=255.255.255.0

GATEWAY=192.168.5.1

ONBOOT=yes

TYPE=Ethernet
copy  /etc/sysconfig/network-scripts/ifcfg-eth0然后修改如上
第二步:编辑/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0

BOOTPROTO=static

MASTER=bond0

SLAVE=yes

ONBOOT=yes

TYPE=Ethernet

 

第三步:编辑/etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1

BOOTPROTO=static

MASTER=bond0

SLAVE=yes

ONBOOT=yes

TYPE=Ethernet

 

第四步:修改/etc/modprobe.conf文件,末行添加
alias bond0 binding

options bond0 miimon=100 mode=0
mode=0表示负载均衡模式(load balancing),即两块网卡同时工作;mode=1表示容灾模式(fault tolerance),即一块网卡工作另一块网卡为备份(注意,如果使用容灾模式,请将/etc/sysconfig/network-scripts/ifcfg-eth*配置文件中的MASTERSLAVE项注释掉,注释方法:在行首插入”#”。如:
DEVICE=eth1

BOOTPROTO=static

#MASTER=bond0

#SLAVE=yes

ONBOOT=yes

TYPE=Ethernet
修改完/etc/modprobe.conf 文件,使用如下命令重新加载模块
[root@localhost ~]# mkinitrd -f /boot/initrd-$(uname -r).img $(uname -r)
执行命令完毕,重新启动系统
重启系统后,可使用cat /proc/net/bonding/bond0命令查看负载均衡的工作状态
[root@localhost ~]# cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver: v2.6.3-rh (June 8, 2005)

 

Bonding Mode: load balancing (round-robin)

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: 1

Permanent HW addr: 00:25:90:02:70:7c

 

Slave Interface: eth1

MII Status: up

Link Failure Count: 1

Permanent HW addr: 00:25:90:02:70:7d

[root@localhost ~]#
出现如上所示即说明网卡绑定负载均衡已实现。