Bonding
将多块网卡绑定于同一IP地址来对外提供服务,可以实现高可用或者负载均
衡功能。因为直接给两块网卡设置同一IP地址是不可以的,所以可以通过bon-
ding,虚拟出一块网卡提供对外连接,物理网卡则被修改成相同的MAC地址。
Bonding工作模式
Mode 0 (balance-rr)
轮转(Round-robin)策略:从头到尾顺序的在每一个slave接口上面发送数
据包。本模式提供负载均衡和容错的能力
Mode 1 (active-backup)
活动-备份(主备)策略:只有一个slave被激活,当且仅当活动的slave接口
失败时才会激活其他slave.为了避免交换机发生混乱此时绑定的MAC地址只
有一个外部端口上可见
Mode 3 (broadcast)
广播策略:在所有的slave接口上传送所有的报文,提供容错能力
active-backup、balance-tlb 和 balance-alb 模式不需要交换机的任何特
殊配置。其他绑定模式需要配置交换机以便整合链接。如:Cisco 交换机需
要在模式 0、2 和 3 中使用 EtherChannel,但在模式4中需要 LACP和
EtherChannel
Bonding的实现
1.将主机的两块网卡全部设置为仅主机模式
2.用vim /etc/sysconfig/network-scripts/ifcfg-bond0命令创建ifcfg-bond0文件,
在里面写入
DEVICE=bond0
IPADDR=192.168.30.6
PREFIX=24
BONDING_OPTS='miimon=100 mode=1'
## 每间隔100ms检查一次,工作模式为模式1
3.设置eth0和eth1
DEVICE=eth0 ## 或者 eth1
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no
```bash
4.查看bond0的状态
[root@centos6 ~]#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 ##网卡eth0在使用
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: 0
Permanent HW addr: 00:0c:29:63:47:1c
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:63:47:26
Slave queue ID: 0
5.用ip a命令查看网卡信息(确定mac地址是否都相同)
[root@centos6 ~]#ifconfig
bond0 Link encap:Ethernet HWaddr 00:0C:29:63:47:1C
inet addr:192.168.12.6 Bcast:192.168.12.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe63:471c/64 Scope:Link
UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
RX packets:36 errors:0 dropped:0 overruns:0 frame:0
TX packets:52 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4121 (4.0 KiB) TX bytes:5881 (5.7 KiB)
eth0 Link encap:Ethernet HWaddr 00:0C:29:63:47:1C
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:23 errors:0 dropped:0 overruns:0 frame:0
TX packets:50 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3341 (3.2 KiB) TX bytes:5735 (5.6 KiB)
eth1 Link encap:Ethernet HWaddr 00:0C:29:63:47:1C
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:780 (780.0 b) TX bytes:146 (146.0 b)
6.是否可以ping通
[09:02:24 root@centos7 ~]#ping 192.168.12.6
PING 192.168.12.6 (192.168.12.6) 56(84) bytes of data.
64 bytes from 192.168.12.6: icmp_seq=1 ttl=64 time=2.83 ms
64 bytes from 192.168.12.6: icmp_seq=2 ttl=64 time=0.255 ms
64 bytes from 192.168.12.6: icmp_seq=3 ttl=64 time=0.237 ms
...
--- 192.168.12.6 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2006ms
rtt min/avg/max/mdev = 0.237/1.107/2.831/1.219 ms
7.可以ping通,试验成功
8.卸载
ifconfig bond0 down ##停止bond0
rm -f /etc/sysconfig/network-scripts/ifcfg-bond0 ##删除文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0 ##改回网卡配置
vim /etc/sysconfig/network-scripts/ifcfg-eth1
rmmod bonding ##卸载bonding模块
service network restart ##重启服务