小梁先森

导航

CentOS7和RHEL7配置主备模式端口聚合实现冗余目的

一、实施步骤

1、备份网卡目录# cp -r /etc/sysconfig/network-scripts/    /etc/sysconfig/network-scripts.bak

2、查看需要聚合的端口是否为“UP”状态:ethtool eth0 |grep "Link detected: yes"   如未激活则使用命令 # ifup eth0

3、创建脚本文件

4、增加可执行权限 chmox +x test.sh

5、运行脚本文件 ./test.sh

6、由于NetWorkManager服务和network服务两者有冲突,需关闭NetWorkManager服务,并永久关闭,再重启network服务。

7、脚本执行完毕查看聚合信息是否成功cat /proc/net/bonding/bond0  

二、回退方案

1、查看状态聚合状态

# cat /proc/net/bonding/bond0 

2、删除聚合端口

# rmmod bond0

3、把之前备份网卡目录覆盖掉当前的网卡目录

# cp -r  /etc/sysconfig/network-scripts.bak /etc/sysconfig/network-scripts

# /etc/init.d/network restart

三、聚合脚本

1、主备模式脚本

为容错设定 active-backup 策略。数据传输将会通过第一个可用的 slave 接口接收和发送。只有在当前使用的绑定 slave 接口失败时才会使用另一个绑定 slave 接口。

#!/bin/bash
ethtool enp7s0f0 |grep "Link detected: yes"> /dev/null
if [ $? -ne 0 ] ;then
    echo Can not detect the link of enp7s0f0
    exit 1
fi

ethtool enp7s0f1 |grep "Link detected: yes"> /dev/null
if [ $? -ne 0 ] ;then
    echo Can not detect the link of enp7s0f1
    exit 1
fi

set_rhel7_bond_config ()
{
unset OPTIND
while getopts 'b:m:i:n:g:s:t:' opt; do
    case $opt in
        b) bond_name=$OPTARG;;
        m) bond_mode=$OPTARG;;
        i) ip=$OPTARG;;
        n) mask=$OPTARG;;
        g) gateway=$OPTARG;;
        s) bond_opts=$OPTARG;;
        t) network_type=$OPTARG;;
    esac
done
bond_config_file="/etc/sysconfig/network-scripts/ifcfg-$bond_name"
echo $bond_config_file
if [ -f $bond_config_file ]; then
    echo "Backup original $bond_config_file to bondhelper.$bond_name"
    mv $bond_config_file /etc/sysconfig/network-scripts/bondhelper.$bond_name -f
fi

if [ "static" == $network_type ]; then 
    if [ ! -n "$gateway" ]; then
        ip_setting="IPADDR=$ip
NETMASK=$mask
USERCTL=no"
    else
        ip_setting="IPADDR=$ip
NETMASK=$mask
GATEWAY=$gateway
USERCTL=no"
    fi
else
    ip_setting="USERCTL=no"
fi
cat << EOF > $bond_config_file
DEVICE=$bond_name
ONBOOT=yes
BOOTPROTO=$network_type
$ip_setting
BONDING_OPTS="mode=$bond_mode $bond_opts"
NM_CONTROLLED=yes
EOF
}
set_rhel7_bond_config -b bond0 -m 1 -i 192.168.43.51 -n 255.255.255.128 -g 192.168.43.2 -t static -s "miimon=100 primary=enp7s0f0"
set_rhel7_ethx_config()  {
    bond_name=$1
    eth_name=$2

    eth_config_file="/etc/sysconfig/network-scripts/ifcfg-$eth_name"
    if [ -f $eth_config_file ]; then
        echo "Backup original $eth_config_file to bondhelper.$eth_name"
        mv $eth_config_file /etc/sysconfig/network-scripts/bondhelper.$eth_name -f
    fi

    cat << EOF  > $eth_config_file
DEVICE=$eth_name
BOOTPROTO=none
ONBOOT=yes
MASTER=$bond_name
SLAVE=yes
USERCTL=no
NM_CONTROLLED=yes
EOF
}

set_rhel7_ethx_config bond0 enp7s0f0
set_rhel7_ethx_config bond0 enp7s0f1

echo "Network service will be restarted."
service network restart
cat /proc/net/bonding/bond0

 

2、负载均衡脚本

为容错和负载均衡设定 round-robin 策略。数据传输将会从第一个可用的 slave 接口开始,顺序地向每个绑定的接口发送和接收。

#!/ bin/bash

ethtool ens1f0 |grep "Link detected: yes"> /dev/null

if [ $? -ne 0 ] ;then

echo Can not detect the link of ens1f0

exit 1

fi


ethtool ens2f0 |grep "Link detected: yes"> /dev/null

if [ $? -ne 0 ] ;then

echo Can not detect the link of ens2f0

exit 1

fi


set_rhel7_bond_config ()

{

unset OPTIND

while getopts 'b:m:i:n:g:s:t:' opt; do

case $opt in

b) bond_name =$OPTARG;;

m) bond_mode =$OPTARG;;

i) ip =$OPTARG;;

n) mask =$OPTARG;;

g) gateway =$OPTARG;;

s) bond_opts =$OPTARG;;

t) network_type =$OPTARG;;

esac

done

bond_config_file="/etc/sysconfig/network-scripts/ifcfg-$bond_name"

echo $bond_config_file

if [ -f $bond_config_file ]; then

echo "Backup original $bond_config_file to bondhelper.$bond_name"

mv $bond_config_file /etc/sysconfig/network-scripts/bondhelper.$bond_name -f

fi


if [ "static" == $network_type ]; then

if [! -n "$gateway"]; then

ip_setting="IPADDR=$ip

NETMASK=$mask

USERCTL=no"

else

ip_setting="IPADDR=$ip

NETMASK=$mask

GATEWAY=$gateway

USERCTL=no"

fi

else

ip_setting="USERCTL=no"

fi

cat << EOF > $bond_config_file

DEVICE=$bond_name

ONBOOT=yes

BOOTPROTO=$network_type

$ip_setting

BONDING_OPTS="mode=$bond_mode $bond_opts"

NM_CONTROLLED=yes

EOF

}

set_rhel7_bond_config -b bond0 -m 0 -i 192.168.43.51 -n 255.255.255.0 -g 192.168.43.254 -t static -s "miimon=100"

set_rhel7_ethx_ config( ) {

bond_name=$1

eth_name=$2


eth_config_file="/etc/sysconfig/network-scripts/ifcfg-$eth_name"

if [ -f $eth_config_file ]; then

echo "Backup original $eth_config_file to bondhelper.$eth_name"

mv $eth_config_file /etc/sysconfig/network-scripts/bondhelper.$eth_name -f

fi


cat << EOF > $eth_config_file

DEVICE=$eth_name

BOOTPROTO=none

ONBOOT=yes

MASTER=$bond_name

SLAVE=yes

USERCTL=no

NM_CONTROLLED=yes

EOF

}


set_rhel7_ethx_config bond0 ens1f0

set_rhel7_ethx_config bond0 ens2f0


echo "Network service will be restarted."

service network restart

cat /proc/net/bonding/ bond0

 

posted on 2019-10-17 12:11  小梁先森  阅读(1174)  评论(0编辑  收藏  举报