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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM