通过linux自带的bond技术实现linux双网卡绑定和负载均衡
绑定的前提条件:芯片组型号相同,而且网卡应该具备自己独立的BIOS芯片
一、建立虚拟网络接口ifcfg-bond0文件
# cd /etc/sysconfig/network-scripts/ # cp ifcfg-eth0 ifcfg-bond0
修改ifcfg-bond0文件其内容为:
# cat ifcfg-bond0 DEVICE=bond0 BROADCAST=10.0.0.255 IPADDR=10.0.0.3 NETMASK=255.255.255.0 NETWORK=10.0.0.0 MTU=1500 GATEWAY=10.0.0.1
二、编辑原有网卡eth0和eth1信息文件
修改内容为:
# more ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none ONBOOT=yes TYPE=Ethernet MASTER=bond0 slave=yes #more ifcfg-eth1 DEVICE=eth1 BOOTPROTO=none ONBOOT=yes TYPE=Ethernet MASTER=bond0 slave=yes
三、编辑/etc/modprobe.conf加入下面两行
# vi /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)提供冗余功能,工作方式是主备的工作方式,也就是说默认情况下只有一块网卡工作,另一块做备份.
bonding只能提供链路监测,即从主机到交换机的链路是否接通。如果只是交换机对外的链路down掉了,而交换机本身并没有故障,那么bonding会认为链路没有问题而继续使用
四、编辑/etc/rc.d/rc.local文件
echo “ifenslave bond0 eth0 eth1” >>/etc/rc.d/rc.local
重新启动后,负载均衡就能正常工作了,可以用ifconfig查看具体信息
# ifconfig bond0 Link encap:Ethernet HWaddr 00:1A:64:6A:55:98 inet addr:10.0.0.3 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::21a:64ff:fe6a:5598/64 Scope:Link UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1 RX packets:985369 errors:0 dropped:0 overruns:0 frame:0 TX packets:804306 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:148943796 (142.0 MiB) TX bytes:2097755332 (1.9 GiB) eth0 Link encap:Ethernet HWaddr 00:1A:64:6A:55:98 inet6 addr: fe80::21a:64ff:fe6a:5598/64 Scope:Link UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 RX packets:984133 errors:0 dropped:0 overruns:0 frame:0 TX packets:804292 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:148805154 (141.9 MiB) TX bytes:2097751381 (1.9 GiB) eth1 Link encap:Ethernet HWaddr 00:1A:64:6A:55:98 inet6 addr: fe80::21a:64ff:fe6a:5598/64 Scope:Link UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 RX packets:1236 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:138642 (135.3 KiB) TX bytes:4275 (4.1 KiB) Interrupt:16 Memory:ca000000-ca011100
通过查看bond0的工作状态查询能详细的掌握bonding的工作状态
# cat /proc/net/bonding/bond0 bonding.c:v2.4.1 (September 15, 2003) Bonding Mode: load balancing (round-robin) MII Status: up MII Polling Interval (ms): 0 Up Delay (ms): 0 Down Delay (ms): 0 Multicast Mode: all slaves Slave Interface: eth1 MII Status: up Link Failure Count: 0 Permanent HW addr: 00:0e:7f:25:d9:8a Slave Interface: eth0 MII Status: up Link Failure Count: 0 Permanent HW addr: 00:0e:7f:25:d9:8b