CentOS 6双网卡绑定实现“主-备份”策略

by LauCyun Jul 20,2016 10:08:01 10,689 views

CentOS服务器安装了双网卡,为了增强服务器的网络连通性,采用“主-备份”策略来配置两个网卡:每次只有一个网卡处于活动状态,在一个网卡出现问题无法使用时可以快速的切换到另外一张网卡上去,保证网络的持续可用。当然缺点是每次只有一个网卡工作,因此硬件资源的利用率不高。

CentOS双网卡绑定实现就是使用两块网卡虚拟成为一块网卡(需要交换机支持),这个聚合起来的设备看起来是一个单独的以太网接口设备,通俗点讲就是两块网卡具有相同的IP地址而并行链接聚合成一个逻辑链路工作。

1 准备

环境:

  • 系统:CentOS 6.5
  • 网卡:

操作前需要确定NetworkManager服务是否已经停止,否则容易报错:

service NetworkManager status #显示NetworkManager 已停即可

2 创建绑定网卡

创建绑定网卡bond0:

[root@laucyun ~]# touch /etc/sysconfig/network-scripts/ifcfg-bond0
[root@laucyun ~]# vim /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.113
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
BORADCAST=192.168.0.255
DNS1=8.8.8.8

3 配置被绑定网卡

这里,我把eth0和eth1绑定为bond0。

修改eth0的配置:

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

修改eth1的配置:

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

4 配置绑定模型

修改/etc/modprobe.d/dist.conf,配置绑定模型,配置文件最后加入以下内容:

[root@localhost ~]# vim /etc/modprobe.d/dist.conf
...
# bind management network port
alias bond0 bonding
options bond0 miimon=100 mode=1
  • 选项millmon是指定隔多长时间来进行链路监测,单位是ms。
  • 选项mode是表示绑定口的工作模式,有0-7共7种模式,常用的有01模式,mode=0表示round-robin策略,两张卡同时工作在负载均衡状态。mode=1表示active-backup策略,两张卡一用一备的备份状态。

Reference form https://wiki.centos.org/TipsAndTricks/BondingInterfaces

mode=1 (active-backup)

Active-backup policy: Only one slave in the bond is active. A different slave becomes active if, and only if, the active slave fails. The bond's MAC address is externally visible on only one port (network adapter) to avoid confusing the switch. This mode provides fault tolerance. The primary option affects the behavior of this mode.

mode=2 (balance-xor)

XOR policy: Transmit based on [(source MAC address XOR'd with destination MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load balancing and fault tolerance.

mode=3 (broadcast)

Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.

mode=4 (802.3ad)

IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification.

  • Pre-requisites:
  • Ethtool support in the base drivers for retrieving the speed and duplex of each slave.
  • A switch that supports IEEE 802.3ad Dynamic link aggregation. Most switches will require some type of configuration to enable 802.3ad mode.

mode=5 (balance-tlb)

Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave.

  • Prerequisite: Ethtool support in the base drivers for retrieving the speed of each slave.

mode=6 (balance-alb)

Adaptive load balancing: includes balance-tlb plus receive load balancing (rlb) for IPV4 traffic, and does not require any special switch support. The receive load balancing is achieved by ARP negotiation. The bonding driver intercepts the ARP Replies sent by the local system on their way out and overwrites the source hardware address with the unique hardware address of one of the slaves in the bond such that different peers use different hardware addresses for the server.

5 配置开机启动绑定

修改的是/etc/rc.local,负责在系统启动时将虚拟网卡和两张物理网卡相绑定,增加以下内容:

[root@localhost ~]# vim /etc/rc.local
...
# bind management network port
ifenslave bond0 eth0 eth1

5 验证

先重启一下网络服务:

[root@localhost ~]# service network restart

使用ifconfig命令查看,如下图:

如上图所示,即说明配置成功!

Tags