一、基本原理
Linux bonding驱动提供了一种方法,用以把多个网络接口(network interface)组合成一个逻辑的"bonded"接口。Bonded接口的工作方式取决于驱动的模式:不同的模式提供了热备份或负载均衡等不同的服 务。此外,驱动还会监控链路的可用性。linux操作系统下双网卡绑定有七种模式。0. round-robin,1.active-backup,2.load balancing (xor), 3.fault-tolerance (broadcast), 4.lacp, 5.transmit load balancing, 6.adaptive load balancing。
二、案例分析
1:[root@uodekoaix ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 //编辑eth0设备
[root@uodekoaix ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1 //编辑eth1设备
2:[root@uodekoaix ~]# cd /etc/sysconfig/network-scripts/
[root@uodekoaix network-scripts]# cp ifcfg-eth0 ifcfg-bond0 //生成一个bond0的虚拟网卡
[root@uodekoaix network-scripts]# vim ifcfg-bond0 //编辑该网卡
3:[root@uodekoaix network-scripts]# vim /etc/modprobe.conf //编辑该配置
注:(1)、miimon 是链路监测的时间间隔单位是毫秒,miimon=100的意思就是,每100毫秒检测网卡和交换机之间是否连通,如不通则使用另外的链路。
(2)、mode=0 表示负载均衡方式,两块网卡都工作,需要交换机作支持。
mode=1 表示冗余方式,网卡只有一个工作,一个出问题启用另外的。
mode=6 表示负载均衡方式,两块网卡都工作,不需要交换机作支持。
4:[root@uodekoaix network-scripts]# vim /etc/rc.local //编辑该开机脚本,将eth0和eth1绑定
1 #!/bin/sh
2 #
3 # This script will be executed *after* all the other init scripts.
4 # You can put your own initialization stuff in here if you don't
5 # want to do the full Sys V style init stuff.
6
7 touch /var/lock/subsys/local
8 ifenslave bond0 eth0 eth1 //将网卡eth0和eth1绑定成bond0虚拟网卡
5:[root@uodekoaix network-scripts]# init 6 //重启,bond0启动成功
[root@uodekoaix ~]# ifconfig //查看网卡信息,在此处三块网卡的mac地址是一样的
bond0 Link encap:Ethernet HWaddr 00:0C:29:70:78:13
inet addr:192.168.145.22 Bcast:192.168.145.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe70:7813/64 Scope:Link
UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
RX packets:247 errors:0 dropped:0 overruns:0 frame:0
TX packets:80 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:19308 (18.8 KiB) TX bytes:14248 (13.9 KiB)
eth0 Link encap:Ethernet HWaddr 00:0C:29:70:78:13
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:73 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4937 (4.8 KiB) TX bytes:0 (0.0 b)
Interrupt:19 Base address:0x2000
eth1 Link encap:Ethernet HWaddr 00:0C:29:70:78:13
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:177 errors:0 dropped:0 overruns:0 frame:0
TX packets:85 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14551 (14.2 KiB) TX bytes:15082 (14.7 KiB)
Interrupt:19 Base address:0x2080
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1543 errors:0 dropped:0 overruns:0 frame:0
TX packets:1543 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2435276 (2.3 MiB) TX bytes:2435276 (2.3 MiB)
[root@uodekoaix ~]# vim /proc/net/bonding/bond0 //查看模式及网卡信息,实际mac地址是不一样的
1 Ethernet Channel Bonding Driver: v3.2.5 (March 21, 2008)
2
3 Bonding Mode: fault-tolerance (active-backup) // 主备模式
4 Primary Slave: None
5 Currently Active Slave: eth1 //正在使用的网卡
6 MII Status: up
7 MII Polling Interval (ms): 100
8 Up Delay (ms): 0
9 Down Delay (ms): 0
10
11 Slave Interface: eth0
12 MII Status: up
13 Link Failure Count: 0
14 Permanent HW addr: 00:0c:29:70:78:13
15
16 Slave Interface: eth1
17 MII Status: up
18 Link Failure Count: 0
19 Permanent HW addr: 00:0c:29:70:78:1d
测试
ping 192.168.145.22 –t //测试网络的连通性
断开eth1网络后
//丢失包之后,然后又启用备份的网卡后,又通了
1 Ethernet Channel Bonding Driver: v3.2.5 (March 21, 2008)
2
3 Bonding Mode: fault-tolerance (active-backup)
4 Primary Slave: None
5 Currently Active Slave: eth0 //此时使用eth0网卡
6 MII Status: up
7 MII Polling Interval (ms): 100
8 Up Delay (ms): 0
9 Down Delay (ms): 0
10
11 Slave Interface: eth0
12 MII Status: up
13 Link Failure Count: 0
14 Permanent HW addr: 00:0c:29:70:78:13
15
16 Slave Interface: eth1
17 MII Status: down //eth1处于down状态
18 Link Failure Count: 1
19 Permanent HW addr: 00:0c:29:70:78:1d
[root@uodekoaix ~]# vim /etc/modprobe.conf
[root@uodekoaix ~]# vim /proc/net/bonding/bond0
测试
Xshell:\> ping 192.168.145.22 –t //测试网络连通性
断开eth1后网络依然通
[root@uodekoaix ~]# vim /proc/net/bonding/bond0