TA的每日心情 | 无聊 4 天前 |
---|
签到天数: 1644 天 [LV.Master]伴坛终老
|
环境:
centos 6.7 32位
主机1:192.168.3.2 (内网)、192.168.3.3(外网)-调度器
主机2:192.168.3.7(内网)-目标主机1
主机3:192.168.3.8(内网)-目标主机2
工作原理:
1、当目标主机中的任意一台挂掉了,直接剔除掉
2、当挂掉的目标主机恢复之后,添加该主机
3、当两台目标主机都挂掉,全部剔除,添加备用主机,这里的备用主机是调度器自身(也可以是是其它)
4、当目标主机有一台恢复了,添加该目标主机,剔除备用主机
5、检测目标主机是否可以访问,连续检测3次,以防个别情况
6、发生剔除目标主机和备用主机,以及添加目标主机和备用主机,相应的信息输出到本地日志当中
注:
VIP:请求主机地址
CPORT:请求主机地址的端口(服务)
FAIL_BACK:备用主机(这里是调度器自身)
RS:数组,包含两个目标主机
RSTATUS:判断目标主机的状态,0:主机存活,1:主机挂掉
RW:数组,给目标主机赋予的权重
RPORT:请求目标主机的端口(服务)
CHKLOOP:检测目标主机的次数
TYPE:lvs类型
LOG:相关信息保存的日志
脚本如下:
#!/bin/bash
#
VIP=192.168.3.3
CPORT=80
FAIL_BACK=127.0.0.1
RS=("192.168.3.7" "192.168.3.8")
export RSTATUS=("0" "0")
RW=("2" "1")
RPORT=80
CHKLOOP=3
TYPE=g
LOG=/var/log/ipvsmonitor.log
[ ! -f $LOG ] && touch $LOG
addrs() {
ipvsadm -a -t $VIP:$CPORT -r $1:$RPORT -$TYPE -w $2
[ $? -eq 0 ] && printf "\e[1;31mreturn 0\e[0m\n" || printf "\e[1;31mreturn 1\e[0m\n"
echo "$I has been added..."
echo -e "`date +'%F %H:%M:%S'`,$I has been added...\n" >> $LOG
RSTATUS[$COUNT]=0
# echo ${RSTATUS[$COUNT]}
}
addback() {
ipvsadm -a -t $VIP:$CPORT -r $FAIL_BACK:$RPORT
[ $? -eq 0 ] && printf "\e[1;31mreturn 0\e[0m\n" || printf "\e[1;31mreturn 1\e[0m\n"
echo "backup machine has added..."
echo -e "`date +'%F %H:%M:%S'`,$FAIL_BACK has been added...\n" >> $LOG
}
delback() {
ipvsadm -d -t $VIP:$CPORT -r $1:$RPORT
[ $? -eq 0 ] && printf "\e[1;31mreturn 0\e[0m\n" || printf "\e[1;31mreturn 1\e[0m\n"
echo "backup machine has removed..."
echo -e "`date +'%F %H:%M:%S'`,$FAIL_BACK has been removed...\n" >> $LOG
}
delrs() {
ipvsadm -d -t $VIP:$CPORT -r $1:$RPORT
[ $? -eq 0 ] && printf "\e[1;31mreturn 0\e[0m\n" || printf "\e[1;31mreturn 1\e[0m\n"
RSTATUS[$COUNT]=1
echo -e "`date +'%F %H:%M:%S'`,$I has been removed...\n" >> $LOG
# echo ${RSTATUS[$COUNT]}
}
checkbak() {
if [ "${RSTATUS[0]}" != "0" -a "${RSTATUS[1]}" != "0" ] && [ "$LOCAL" == "" ];then
addback
elif [ "$LOCAL" == "127.0.0.1" ] && [ "$REALSERVER1" == "" -a "$REALSERVER2" == "" ];then
echo -e "\nbackup machine has added..."
elif [ "$LOCAL" == "127.0.0.1" ] && [ "$REALSERVER1" != "" -o "$REALSERVER2" != "" ];then
delback $LOCAL
else
return 0
fi
}
checkrs() {
local J=1
while [ $J -le $CHKLOOP ]
do
if /usr/bin/curl --connect-timeout 1 http://$I &> /dev/null;then
return 0
fi
let J++
done
return 1
}
while :; do
let COUNT=0
for I in ${RS}
do
checkrs
# echo $?
STATUS=`echo $?`
REALSERVER=`ipvsadm -L -n | grep $I |awk '{print $2}' | cut -d : -f 1`
REALSERVER1=`ipvsadm -L -n | grep ${RS[0]} |awk '{print $2}' | cut -d : -f 1`
REALSERVER2=`ipvsadm -L -n | grep ${RS[1]} |awk '{print $2}' | cut -d : -f 1`
LOCAL=`ipvsadm -L -n | grep 127.0.0.1 |awk '{print $2}' | cut -d : -f 1`
[ $STATUS -eq 0 -a "$REALSERVER" == "" ] && addrs $I ${RW[$COUNT]}
# echo ${RSTATUS[$COUNT]}
[ $STATUS -eq 0 -a "$REALSERVER" != "" ] && echo "$I running normally..."
[ $STATUS -ne 0 -a "$REALSERVER" != "" ] && delrs $I
[ $STATUS -ne 0 -a "$REALSERVER" == "" ] && echo "$I has been removed..."
let COUNT++
done
checkbak
echo "----------------------------------------------------------------"
# echo ${RSTATUS[0]}
# echo ${RSTATUS[1]}
# echo $REALSERVER1
# echo $REALSERVER2
# echo $LOCAL
sleep 5
done
|
|