使用方式

# 主节点执行
sh keepalived.sh master '主节点eth1 备节点eth1 eth1的VIP' '主节点eth2 备节点eth2 eth2的VIP'

# 备节点执行
sh keepalived.sh slave '主节点eth1 备节点eth1 eth1的VIP' '主节点eth2 备节点eth2 eth2的VIP'

脚本内容

#!/bin/bash

#自动获取与ip地址相匹配的网卡
#两个节点,一个主一个备
#传参格式(主): master '主节点eth1 备节点eth1 eth1的VIP' '主节点eth2 备节点eth2 eth2的VIP'
#传参格式(备): slave '主节点eth1 备节点eth1 eth1的VIP' '主节点eth2 备节点eth2 eth2的VIP'

#判断是否具备参数
echo "$@" | grep  -E "master|slave"  &>/dev/null
if [ $? -ne 0 ]; then
        echo -e "\e[32m 传参格式(主): master '主节点eth1 备节点eth1 eth1的VIP' '主节点eth2 备节点eth2 eth2的VIP' \e[0m"
        echo -e "\e[32m 传参格式(备): slave  '主节点eth1 备节点eth1 eth1的VIP' '主节点eth2 备节点eth2 eth2的VIP' \e[0m"
        exit 1
fi


#定义传参
updateconfig(){
master=$1  #主节点
slave=$2   #备节点
vip=$3     #VIP
}

#安装模块
MASTER_Install(){

#检测keepalived服务
if systemctl  status keepalived | grep -i "running" &>/dev/null ;then
        echo -e "\033[42;37m keepalived服务正常,清除自带的配置文件中...... \033[0m"
        rm -rf /etc/keepalived/keepalived.conf
        echo -e "\033[42;37m 配置文件清除完成...... \033[0m"
else
#安装keepalived
        yum -y install keepalived  &>/dev/null
        if [ $? -ne 0 ]; then
                echo -e "\033[31m keepalived {yum} 安装失败! \033[0m"
                exit 0
        else
                echo -e "\033[42;37m keepalived服务安装成功,清除自带的配置文件中...... \033[0m"
                rm -rf /etc/keepalived/keepalived.conf
                echo -e "\033[42;37m 配置文件清除完成...... \033[0m"
        fi
fi

#加入开机自启动
systemctl  status keepalived | grep -E "enabled" &>/dev/null
if [ $? -ne 0 ];then
        systemctl enable keepalived &>/dev/null
        if [ $? -ne 0 ];then
            echo -e "\033[31m keepalived 加入开机自启失败! \033[0m"
        fi
fi

}
MASTER_Install


#编写配置文件
cat >>  /etc/keepalived/keepalived.conf   << EOF
vrrp_instance VI_VRIDSHU {
    # 发送心跳包的源IP,可使用绑定的网卡IP,也可以使用本服务器上的其他IP
    interface  NETWORK

    # 抢占模式,指定vrrp_instance的初始状态,master还是backup
    state node MASTERIDA   #抢占模式

    #非抢占模式
    #state BACKUP


    # 本机的优先级,VRID相同的机器中,优先级最高的会被选举为MASTER
    priority PRIORITY
    # 相当于VRID,用于在一个网内区分组播,需要组播域内内唯一
    virtual_router_id 233

    # 定义主从的验证方式以及密码,一般使用PASS
    authentication {
        auth_type PASS
        auth_pass 9999    # 设置密码
    }
    #设置单播模式
    unicast_src_ip SELFIP
    unicast_peer {
    SLAVEIP
    }

    # 虚拟IP
    virtual_ipaddress {
    }
}

EOF

#(主)配置文件模块
MASTER_Cnfig(){

#获取ip地址所在的网卡配置文件
eth=$(find /etc/sysconfig/network-scripts/ -type f -exec grep -l "$master" {} +)

device=`cat $eth |grep DEVICE |awk -F"=" '{print $2}'`

#获取配置文件中的详细网卡信息
net=$(cat $eth | grep  -i DEVICE | awk -F '=' '{print $2}'| sed 's/\"//g')

#掩码信息
mask=$(cat $eth | grep -i PREFIX | awk -F '=' '{print $2}'| sed 's/\"//g')

#用于在一个网内区分组播,需要组播域内内唯一
VRID=$(echo $(( $RANDOM % 100 )))

sed -i "s/PRIORITY/150/g" /etc/keepalived/keepalived.conf
sed -i "s/NETWORK/$device/g" /etc/keepalived/keepalived.conf
sed -i "s/VRIDSHU/$VRID/g" /etc/keepalived/keepalived.conf
sed -i "s/SELFIP/$master/g" /etc/keepalived/keepalived.conf
sed -i "s/SLAVEIP/$slave/g" /etc/keepalived/keepalived.conf
sed -i "s/MASTERIDA/MASTER/g" /etc/keepalived/keepalived.conf
sed -i "/.*virtual_ipaddress.*{/a${vip}/${mask} dev ${device}"  /etc/keepalived/keepalived.conf

}



#(备)配置文件模块
SLAVE_Cnfig(){

#获取ip地址所在的网卡配置文件
eth=$(find /etc/sysconfig/network-scripts/ -type f -exec grep -l "$slave" {} +)

device=`cat $eth |grep DEVICE |awk -F"=" '{print $2}'`

#获取配置文件中的详细网卡信息
net=$(cat $eth | grep -i DEVICE | awk -F '=' '{print $2}'| sed 's/\"//g')

#掩码信息
mask=$(cat $eth | grep -i PREFIX | awk -F '=' '{print $2}'| sed 's/\"//g')

#用于在一个网内区分组播,需要组播域内内唯一
VRID=$(echo $(( $RANDOM % 100 )))

sed -i "s/PRIORITY/100/g" /etc/keepalived/keepalived.conf
sed -i "s/NETWORK/$device/g" /etc/keepalived/keepalived.conf
sed -i "s/VRIDSHU/$VRID/g" /etc/keepalived/keepalived.conf
sed -i "s/SELFIP/$slave/g" /etc/keepalived/keepalived.conf
sed -i "s/SLAVEIP/$master/g" /etc/keepalived/keepalived.conf
sed -i "s/MASTERIDA/SLAVE/g" /etc/keepalived/keepalived.conf

sed -i "/.*virtual_ipaddress.*{/a${vip}/${mask} dev ${device}"  /etc/keepalived/keepalived.conf

}

#服务启动模块
Start(){

#重启服务
systemctl restart keepalived &>/dev/null
if [ $? -ne 0 ] ;then
        echo -e "\033[31m keepalived 重启失败! \033[0m"
        exit 0
else
        echo -e "\033[42;37m keepalived 重启成功! \033[0m"
fi

#等待启动中
sleep 3

#判断VIP是否生效
ping $vip -c 1 -W 5 &>/dev/null
if [ $? -ne 0 ] ;then
        echo -e "\033[31m {$vip} vip配置失败! \033[0m"
else
        echo -e "\033[42;37m {$vip} vip配置成功! \033[0m"
fi
}

#定义传参updateconfig
for iloop in "$@"
do
        data=`echo $iloop`
        if [ 'master' == "$data" ];then
                continue  #
        else
                if [ 'slave' == "$data" ];then
                        continue  #
                fi
        fi
        updateconfig $data
        #判断主
        if [ $1 == "master" ];then
                MASTER_Cnfig    #配置文件模块
                Start    #启动服务
        fi
        #判断备
        if [ $1 == "slave" ];then
                SLAVE_Cnfig    #配置文件模块
                Start    #启动服务
        fi
done