1 什么是bond
网卡bond是通过多张网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡,在生产场景中是一种常用的技术。Kernels 2.4.12及以后的版本均供bonding模块,以前的版本可以通过patch实现。可以通过以下命令确定内核是否支持 bonding:
[root@hexin ~]#cat /boot/config-2.6.32-573.el6.x86_64 |grep -i bonding
CONFIG_BONDING=m
2 bond的模式
bond的模式常用的有两种:
mode=0(balance-rr)
表示负载分担round-robin,并且是轮询的方式比如第一个包走eth0,第二个包走eth1,直到数据包发送完毕。
优点:流量提高一倍
缺点:需要接入交换机做端口聚合,否则可能无法使用
mode=1(active-backup)
表示主备模式,即同时只有1块网卡在工作。
优点:冗余性高
缺点:链路利用率低,两块网卡只有1块在工作
3 配置bond
3.1 测试环境:
[root@hexin ~]# cat/etc/redhat-release
CentOS release 6.7 (Final)
[root@hexin ~]# uname -r
2.6.32-573.el6.x86_64
3.2 配置物理网卡
[root@hexin network-scripts]#cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes //可以没有此字段,就需要开机执行ifenslave bond0 eth0 eth1命令了。
[root@hexin network-scripts]#cat ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
3.3 配置逻辑网卡bond0
#由于没有这个配置文件我们可以使用拷贝一个ifcfg-eth1来用:
cp ifcfg-{eth0,bond0}
[root@hexin network-scripts]#cat ifcfg-bond0 //需要我们手工创建
DEVICE=bond0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.10
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=10.0.0.2
DNS2=4.4.4.4
[root@hexin network-scripts]#
3.4 加载模块,让系统支持bonding
#配置bond0的链路检查时间为100ms,模式为0。
[root@hexin ~]# cat /etc/modprobe.conf //不存在的话,手动创建(也可以放在modprobe.d下面)
alias bond0 bonding
options bond0 miimon=100 mode=0
[root@hexin ~]#
注:
<1>:millmon表示链路监测时间间隔,单位为ms,millmon=100表示每100ms监测一次链路连接状态,如果有一条不通,就转入另一条。这个值建议为100, 设成其它值可能导致不稳定
<2>:mode表示两张网卡的运行方式,0 表示load blance,1 表示热备(建议使用热备)
4.检测、验证配置
首先执行命令装载bonding模块:modprobe bonding
重启网络服务,并确认bond0正确启动:service network restart
确认设备已经正确加载: cat /proc/net/bonding/bond0
确认模块是否加载成功: lsmod | grep bonding
列出所有网口:ifconfig -all
--------------------------------------------------------------------------------------------------------------------
命令模式添加bond
1.为主机添加网卡,保证机器有两块可用的闲置网卡
2.添加 bond
通过执行以下指令:
[root@localhost ~]# nmcli connection add con-name bond0 ifname bond0 type bond mode active-backup ip4 172.25.151.50/24
# 添加 bond 并指定 IP
add :为NetworkManager添加一个连接
con-name :连接名称
ifname :连接到的接口
type :连接类型
mode :bonding 模式(默认为: balance-rr )
ip4 :IPv4 类的地址
# 注:这时的 bond 是不可用的,需要为其添加真实的网卡设备
为 bond0 添加两张可用网卡:
[root@localhost ~]# nmcli connection add con-name eth0 ifname eth0 type bond-slave master bond0
# eht0 为添加的网卡名称,bond-slave 表示为 bond 的从属设备,master bond0 表示为 bond0 服务
[root@localhost ~]# nmcli connection add con-name eth1 ifname eth1 type bond-slave master bond0
[root@localhost ~]# watch -n 1 cat /proc/net/bonding/bond0
# 可以通过此监控命令
建立完后就可以使用 bond0 了
3.若要删除建立的 bond0 可执行:
先删除 bond0 的两个从属设备
[root@localhost ~]# nmcli connection delete eth0
[root@localhost ~]# nmcli connection delete eth1
删除 bond0
[root@localhost ~]# nmcli connection delete bond0