一、bond的工作原理
所谓的bond就是将两块或两块以上网卡绑定同一个IP地址对外提供服务,从而实现负载均衡高可用。当然,直接给两块网卡设置同一IP地址 是不可能的。通过bonding,虚拟一块网卡对外提供连接, 物理网卡的被修改为相同的MAC地址。
二、bond工作模式分类
1)Mode 0 (balance-rr) 轮转(Round-robin)策略:从头到尾顺序的在每一个slave 接口上面发送数据包。本模式提供负载均衡和容错的能力
2)Mode 1 (active-backup) 活动-备份(主备)策略:在绑定中,只有一个slave被激活。 当且仅当活动的slave接口失败时才会激活其他slave。为了 避免交换机发生混乱此时绑定的MAC地址只有一个外部端口上可见
3)Mode 3 (broadcast) 广播策略:在所有的slave接口上传送所有的报文。本模式提供 无容错能力。
三、实现步骤
1、为每个设备生成独立的接口配置文件,关闭NetworkManager服务。
chkconfig --level 3 NetworkManager off
2、添加网卡,配置静态联网(bond0)
[[email protected] network-scripts]# cat ifcfg-bond0 DEVICE=bond0 IPADDR=10.1.249.34 PRFIX=16 GATEWAY=10.1.249.254 DNS1=8.8.8.8 BONDING_OPTS="miimon=101 mode=0" #如果mode=0则为轮转模式,为1则为主备模式,为3则为广播策略 [[email protected] network-scripts]# cat ifcfg-eth0 DEVICE=eth0 SLAVE=yes MASTER=bond0 [[email protected] network-scripts]# cat ifcfg-eth1 DEVICE=eth1 SLAVE=yes MASTER=bond0 [[email protected] network-scripts]#
3、随意断开一网卡测试,并查看结果
[[email protected] network-scripts]# !cat cat /proc/net/bonding/bond0 Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011) Bonding Mode: fault-tolerance (active-backup) Primary Slave: None Currently Active Slave: eth0 MII Status: up MII Polling Interval (ms): 102 Up Delay (ms): 0 Down Delay (ms): 0 Slave Interface: eth0 MII Status: up Speed: 1000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 00:0c:29:7c:55:8d Slave queue ID: 0 Slave Interface: eth1 MII Status: up Speed: 1000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 00:0c:29:7c:55:97 Slave queue ID: 0 [[email protected] network-scripts]# [[email protected] ~]# cat /proc/net/bonding/bond0 Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011) 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 Speed: 1000 Mbps Duplex: full Link Failure Count: 3 Permanent HW addr: 00:0c:29:7c:55:8d Slave queue ID: 0 Slave Interface: eth1 MII Status: down Speed: Unknown Duplex: Unknown Link Failure Count: 3 Permanent HW addr: 00:0c:29:7c:55:97 Slave queue ID: 0 [[email protected] ~]# cat /proc/net/bonding/bond0 Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011) Bonding Mode: fault-tolerance (broadcast) MII Status: up MII Polling Interval (ms): 101 Up Delay (ms): 0 Down Delay (ms): 0 Slave Interface: eth0 MII Status: up Speed: 1000 Mbps Duplex: full Link Failure Count: 2 Permanent HW addr: 00:0c:29:7c:55:8d Slave queue ID: 0 Slave Interface: eth1 MII Status: up Speed: 1000 Mbps Duplex: full Link Failure Count: 1 Permanent HW addr: 00:0c:29:7c:55:97 Slave queue ID: 0 [[email protected] ~]#
[[email protected] network-scripts]# ifconfig bond0 Link encap:Ethernet HWaddr 00:0C:29:7C:55:8D #三的MAC地址信息是一样的,代表绑定为一块网卡 inet addr:10.1.249.34 Bcast:10.255.255.255 Mask:255.0.0.0 inet6 addr: fe80::20c:29ff:fe7c:558d/64 Scope:Link UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1 RX packets:62258 errors:0 dropped:0 overruns:0 frame:0 TX packets:7493 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:5599522 (5.3 MiB) TX bytes:716163 (699.3 KiB) eth0 Link encap:Ethernet HWaddr 00:0C:29:7C:55:8D #三的MAC地址信息是一样的,代表绑定为一块网卡 UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 RX packets:32698 errors:0 dropped:0 overruns:0 frame:0 TX packets:4300 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2915521 (2.7 MiB) TX bytes:393343 (384.1 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:7C:55:8D #三的MAC地址信息是一样的,代表绑定为一块网卡 UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 RX packets:33832 errors:0 dropped:0 overruns:0 frame:0 TX packets:3359 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3059478 (2.9 MiB) TX bytes:339826 (331.8 KiB) 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:65536 Metric:1 RX packets:425 errors:0 dropped:0 overruns:0 frame:0 TX packets:425 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:42694 (41.6 KiB) TX bytes:42694 (41.6 KiB) [[email protected] network-scripts]#
截图演示:
1、关闭NetworkManager服务
2、准备所有配置文件
3、重启网络服务
4、断开一网卡在物理机上ping本ip地址,测试主备功能
结论:
同理可使用不同的bond工作模式进行测试,结果为,当处于mode0时为轮转模式有容错能力可坏一块网卡,当处于mode1时为主备模式有容错能力可坏一块网卡,当处于mode3时为广播策略模式,亲测,只要工作的网卡不坏则正常工作,当开始不是工作的网卡切换为工作网卡时则不能工作。
时间: 2024-10-13 11:31:03