当前位置 博文首页 > 江晓龙的博客:centos7系统双网卡bound绑定配置

    江晓龙的博客:centos7系统双网卡bound绑定配置

    作者:[db:作者] 时间:2021-07-14 18:41

    centos7双网卡绑定bond

    1.bond简介

    生产环境必须提供 7×24 小时的网络传输服务。借助于网卡绑定技术,不仅 可以提高网络传输速度,更重要的是,还可以确保在其中一块网卡出现故障时,依然可以正 常提供网络服务。假设我们对两块网卡实施了绑定技术,这样在正常工作中它们会共同传输 数据,使得网络传输的速度变得更快;而且即使有一块网卡突然出现了故障,另外一块网卡 便会立即自动顶替上去,保证数据传输不会中断。

    Linux 内核网卡绑定驱动模式:

    mode0(平衡负载模式):平时两块网卡均工作,且自动备援,但需要在与服务器本地 网卡相连的交换机设备上进行端口聚合来支持绑定技术。

    mode1(自动备援模式):平时只有一块网卡工作,在它故障后自动替换为另外的网卡。

    mode6(平衡负载模式):平时两块网卡均工作,且自动备援,无须交换机设备提供辅 助支持。

    2.配置bond

    以双网卡为例

    2.1.配置两块网卡配置信息

    [root@k8s-master ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
    TYPE=Ethernet
    BOOTPROTO=none
    ONBOOT=yes
    DEVICE=ens33
    MASTER=bond0
    SLAVE=yes
    
    [root@k8s-master ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens37
    TYPE=Ethernet
    BOOTPROTO=none
    ONBOOT=yes
    DEVICE=ens37
    MASTER=bond0
    SLAVE=yes
    
    

    2.2.配置bond网卡信息

    [root@k8s-master ~]# vim /etc/sysconfig/network-scripts/ifcfg-bond0 
    TYPE=Ethernet
    BOOTPROTO=none
    ONBOOT=yes
    DEVICE=bond0
    IPADDR=192.168.81.210
    NETMASK=255.255.255.0
    GATEWAY=192.168.81.2
    DNS=114.114.114.114
    

    2.3.配置内核网卡驱动模式

    [root@k8s-master ~]# vim /etc/modprobe.d/bond.conf 
    alias bond0 bonding
    options bonding mode=6 miimon=200
    

    2.4.重启网卡

    [root@k8s-master ~]# systemctl restart network
    

    2.5.查看bond是否生效

    [root@k8s-master ~]# ifconfig 
    bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
            inet 192.168.81.210  netmask 255.255.255.0  broadcast 192.168.81.255
            inet6 fe80::20c:29ff:fed5:a566  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:d5:a5:66  txqueuelen 1000  (Ethernet)
            RX packets 811  bytes 65115 (63.5 KiB)
            RX errors 0  dropped 187  overruns 0  frame 0
            TX packets 1574  bytes 118007 (115.2 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    ens33: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
            ether 00:0c:29:d5:a5:66  txqueuelen 1000  (Ethernet)
            RX packets 1851  bytes 209289 (204.3 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 1857  bytes 234505 (229.0 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    ens37: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
            ether 00:0c:29:d5:a5:70  txqueuelen 1000  (Ethernet)
            RX packets 457  bytes 39789 (38.8 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 753  bytes 52110 (50.8 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    

    3.自动化配置bond脚本

    使用方式:sh boun.sh 网卡模式 IP地址

    [root@elk-1 ~/soft]# cat bound.sh
    #!/bin/bash
    #设置多网卡bond,实现网卡的高可用,提升网卡带宽
    #此脚本需要传入两个参数 $1为bond模式  $2为boundIP地址
    
    if [  $# = 2 ] ; then			#判断传入的参数是否为2个
    	#定义主机上网卡的名称变量
    	NAME1=eno1
    	NAME2=eno2
    
    	#定义要写入网卡配置文件的网络参数
    	IPADDR=$2
    	NETMASK=255.255.255.0
    	GATEWAY=192.168.1.1
    	DNS=114.114.114.114
    else
    	echo "脚本使用错误!!!!
            	格式为:$PWD/$0 [ 0-6 ] ipaddr
    	例如:
            	$PWD/$0 网卡模式 192.168.1.100"
    	exit 1;
    fi
    
    #生成bond配置文件
    echo "alias bond0 bonding
    options bonding mode=$1 miimon=200 " > /etc/modprobe.d/bonding.conf
    
    #判断第一个网卡是否存在,如果存在就生成第一个网卡的配置文件
    if [ ! -z $NAME1 ] ;then
    	echo "DEVICE=$NAME1
    	BOOTPROTO=none
    	MASTER=bond0
    	SLAVE=yes" > /etc/sysconfig/network-scripts/ifcfg-$NAME1
    fi
    
    #判断第二个网卡是否存在,如果存在就生成第二个网卡的配置文件
    if [ ! -z $NAME2 ] ;then
    	echo "DEVICE=$NAME2
    	BOOTPROTO=none
    	MASTER=bond0
    	SLAVE=yes" > /etc/sysconfig/network-scripts/ifcfg-$NAME2
    fi
    
    #生成bond配置文件
    echo "DEVICE=bond0
    BOOTPROTO=none
    ONBOOT=yes
    IPADDR=$IPADDR
    NETMASK=$NETMASK
    GATEWAY=$GATEWAY
    DNS1=$DNS " >/etc/sysconfig/network-scripts/ifcfg-bond0
    
    #重启网络服务
    systemctl  disable NetworkManager
    systemctl  stop NetworkManager
    modprobe -r bonding
    modprobe  bonding
    service network restart
    
    #打印网络信息
    echo "IPADDR=$IPADDR
    NETMASK=$NETMASK
    GATEWAY=$GATEWAY
    DNS1=$DNS "
    
    
    cs