keepalived 预防脑裂检测脚本

1 检查vip

[root@mysql2 keepalived]# cat /etc/keepalived/check_brain_keepalived.sh

#!/bin/bash
# 检查脑裂的脚本,在备节点上进行部署
LB01_VIP=*
LB01_IP=*
LB02_IP=*
while true
do
  ping -c 2 -W 3 $LB01_VIP &>/dev/null
    if [ $? -eq 0 -a `ip add|grep "$LB01_VIP"|wc -l` -eq 1 ];then
        echo "ha is brain."
    else
        echo "ha is ok"
    fi
    sleep 5
done

运行脚本,在备节点(keepalived的备),不是mysql的备节点

[root@mysql2 keepalived]# ./check_brain_keepalived.sh
ha is ok
ha is ok

由于在M-M+Keepalived环境中,脑裂是一个始终存在的问题,因为vip的存在,这里提供一个脚本进行检查,检查vip漂移到备节点

发现backup有vip就发警告,此脚本部署在backup机器上

2 定时ping网关的方法

[root@mysql1 keepalived]# /sbin/arping -I ens192 -c 5 -s * *

#!/bin/bash
host=127.0.0.1
CHECK_TIME=3
VIP=*
GATEWAY=*
eth=ens192
#mysql  is working MYSQL_IS_OK is 1 , mysql down MYSQL_IS_OK is 0
keepalived_IS_OK=1
function check_keepalived_status (){
    /sbin/arping -I $eth -c 5 -s $VIP $GATEWAY >/dev/null 2>&1
    if [ $? = 0 ] ;then
    keepalived_IS_OK=1
    else
    keepalived_IS_OK=0
    fi
    return $keepalived_IS_OK 
}
while [ $CHECK_TIME -ne 0 ]
do
    let "CHECK_TIME -= 1"
    
    check_keepalived_status
if [ $keepalived_IS_OK = 1 ] ; then
    CHECK_TIME=0
   #/bin/systemctl stop keepalived
    echo 1
    exit 0
fi

if [ $keepalived_IS_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]
then
    /bin/systemctl stop keepalived
    echo 0
    exit 1
fi
sleep 3
done

 

转载于:https://www.cnblogs.com/yhq1314/p/9921671.html

你可能感兴趣的:(keepalived 预防脑裂检测脚本)