首页 > 代码库 > 网络服务高可用——双网卡绑定同一IP
网络服务高可用——双网卡绑定同一IP
很多时候,企业里面的一些关键型网络服务,不仅数据吞量相当大,而且还不允许随便离线的,所以就要求我们的网络服务一定要具有高可用性。数据吞吐量大,很多人就说了,这个简单,在我们的关键业务服务器上多装几张网卡,均衡流量负载也就可以了。但如果多网卡,多IP,不仅浪费了IP资源,更麻烦的事在客户访问的过程中如果出现了某张网卡离线的情况时,还需要重新连接另一IP的网卡才能继续会话,这是一件很头疼的事,有没有一个两全其美的办法了。有,其实操作起来也很简单,那么今天就给大家分享一下,如何实现双网卡绑定同一IP,实现网络服务高可用。
一、环境需求
硬件需要:两张网卡
操作操作:Centos6.8
服务需求:停用NetworkManager服务
二、Bonding技术
将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或者负载均衡。当然,直接给两块网卡设置同一IP地址是不可能的。通过bonding技术,我们可以将两块网卡的MAC地址修改为一样的,这样就可以使用同一IP连续外提供服务了。
三、Bonding的工作模式
Mode 0 (Round-robin)轮转模式:从头到尾顺序的在每一个slave接口上面发送数据包,本模式提供负载均衡
Mode 1 (active-backup)主备模式:只有一个slave被激活,仅当活动的slave接口失败时才会激活其他slave,本模式提供高可用性
Mode 3 (broadcast)组播模式:在所有的slave接口上传送所有的报文,本模式提供高冗余能力
四、创建实例
我们以创建主备模式为例,演示创建过程,其它模式只需要把mode={0|1|3}修改一下,再重启网络服务就可以
第一步:建立双网卡绑定文件
[root@Centos6 network-scripts]# vim ifcfg-bond0
[root@Centos6 network-scripts]# cat ifcfg-bond0
DEVCIE=bond0
BOOTPROTO=none
BONDING_OPTS="miimon=100 mode=1"
IPADDR=10.1.253.253
PREFIX=16
[root@Centos6 network-scripts]#
第二步:修改两张网卡的配置文件
[root@Centos6 network-scripts]# vim ifcfg-eth0
[root@Centos6 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
[root@Centos6 network-scripts]#
[root@Centos6 network-scripts]# vim ifcfg-eth1
[root@Centos6 network-scripts]# cat ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
[root@Centos6 network-scripts]#
文件配置基本工作完成,可以进行下一步了
第三步:重启网络服务(确保NetworkManager服务是处于停止状态的)
[root@Centos6 network-scripts]# service network restart
Shutting down interface bond0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface bond0: Determining if ip address 10.1.253.253 is already in use for device bond0...
[ OK ]
[root@Centos6 network-scripts]# ifconfig
bond0 Link encap:Ethernet HWaddr 00:0C:29:C8:72:26
inet addr:10.1.253.253 Bcast:10.1.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fec8:7226/64 Scope:Link
UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
RX packets:13897 errors:0 dropped:0 overruns:0 frame:0
TX packets:869 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1295315 (1.2 MiB) TX bytes:84869 (82.8 KiB)
eth0 Link encap:Ethernet HWaddr 00:0C:29:C8:72:26
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:12376 errors:0 dropped:0 overruns:0 frame:0
TX packets:789 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1145956 (1.0 MiB) TX bytes:77363 (75.5 KiB)
eth1 Link encap:Ethernet HWaddr 00:0C:29:C8:72:26
UP BROADCAST SLAVE MULTICAST MTU:1500 Metric:1
RX packets:1528 errors:0 dropped:0 overruns:0 frame:0
TX packets:80 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:149913 (146.3 KiB) TX bytes:7506 (7.3 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:344 errors:0 dropped:0 overruns:0 frame:0
TX packets:344 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:24240 (23.6 KiB) TX bytes:24240 (23.6 KiB)
[root@Centos6 network-scripts]#
使用cat /proc/net/bonding/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
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:c8:72:26
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:c8:72:30
Slave queue ID: 0
[root@Centos6 ~]#
第四步:在Centos7上去ping刚才配置好双网卡绑定的Centos6,随便停用其中的任意一张网卡,观察ping的过程会不会丢包
现在我们在Centos7开启ping Centos6,一切正常,然后停用Centos6的网卡2后查看ping的过程有没有丢包情况
[root@Centos6 network-scripts]# 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): 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:c8:72:26
Slave queue ID: 0
Slave Interface: eth1
MII Status: down
Speed: Unknown
Duplex: Unknown
Link Failure Count: 0
Permanent HW addr: 00:0c:29:c8:72:30
Slave queue ID: 0
[root@Centos6 network-scripts]#
停用网卡2 ,网络正常连通,性能差一点机器的话,最多丢两三个包而矣
再启动网卡2,停用网卡1,继续观察ping的过程
[root@Centos6 network-scripts]# 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: eth1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: down
Speed: Unknown
Duplex: Unknown
Link Failure Count: 1
Permanent HW addr: 00:0c:29:c8:72:26
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:c8:72:30
Slave queue ID: 0
[root@Centos6 network-scripts]#
启用网卡2 、停用网卡1 后,网络依然正常连通,即使有丢的,也就丢两三个,对外服务不会有什么大影响,我这里停用任意一张网卡,都没有丢包的情况。
所以说mode1模式随便那张网卡故障,都可以正常提供服务,也就是说具有有高可用性
五、性能对比
1、轮转模式(mode0)只能允许网卡2故障,网络可以正常连通,也就是说轮转模式提供负载均衡,是没有高可用性的
[root@Centos6 network-scripts]# 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: 0
Permanent HW addr: 00:0c:29:c8:72:26
Slave queue ID: 0
Slave Interface: eth1
MII Status: down
Speed: Unknown
Duplex: Unknown
Link Failure Count: 1
Permanent HW addr: 00:0c:29:c8:72:30
Slave queue ID: 0
[root@Centos6 network-scripts]#
2、组播模式(mode3)只能允许网卡2故障,网络可以正常连通,也就是说组播模式是没有容错能力的
[root@Centos6 network-scripts]# 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): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 1
Permanent HW addr: 00:0c:29:c8:72:26
Slave queue ID: 0
Slave Interface: eth1
MII Status: down
Speed: Unknown
Duplex: Unknown
Link Failure Count: 2
Permanent HW addr: 00:0c:29:c8:72:30
Slave queue ID: 0
[root@Centos6 network-scripts]#
所以在这里需要提醒各位的是:轮转模式(mode0)和组播模式的(mode3)只能允许用网卡2 故障,网卡1故障,网络就不能正常对外提供服务了,也就是说想拥有负载均衡能力就选择轮转模式(mode0);想拥有高可用性就选择主备模式(mode1);想拥有高冗余能力就选择组播模式(mode3)
本文出自 “爱情防火墙” 博客,请务必保留此出处http://183530300.blog.51cto.com/894387/1845545
网络服务高可用——双网卡绑定同一IP