. /etc/init.d/functions
#检测web1是否正常,如果正常则加入LVS ,不正常则踢出LVS
add1(){
web1ip=$(ipvsadm -ln|grep $web1:$Port|awk ‘{print $2}‘)
Curl1=$(curl -s -I $web1 |grep 200|awk ‘{print $2}‘)
if [ "$Curl1" = "200" ]
then
if [ "$web1ip" != "$web1:$Port" ]
then
ipvsadm -a -t "$VIP1" -r "$web1" -g -w 1 &>/dev/null
action "add $web1" /bin/true
fi
return 0
else
if [ "$web1ip" = "$web1:$Port" ]
then
ipvsadm -d -t "$VIP1" -r "$web1" &>/dev/null
action "del $web1" /bin/true
fi
return 1
fi
}
#检测web2是否正常,如果正常则加入LVS ,不正常则踢出LVS
add2(){
web2ip=$(ipvsadm -ln|grep $web2:$Port|awk ‘{print $2}‘)
Curl2=$(curl -s -I $web2 |grep 200|awk ‘{print $2}‘)
if [ "$Curl2" != "200" ]
then
if [ "$web2ip" = "$web2:$Port" ]
then
ipvsadm -d -t "$VIP1" -r "$web2" &>/dev/null
action "del $web2" /bin/true
fi
return 1
else
if [ "$web2ip" != "$web2:$Port" ]
then
ipvsadm -a -t "$VIP1" -r "$web2" -g -w 1 &>/dev/null
action "add $web2" /bin/true
fi
return 0
fi
}
#启动LVS
/etc/init.d/ipvsadm start
ifconfig eth0:0 $VIP/24
ipvsadm -A -t "$VIP1" -s rr
#死循环
while true
do
add1
add2
/etc/init.d/ipvsadm save &>/dev/null
/etc/init.d/ipvsadm reload &>/dev/null
sleep 5
done
原文地址:http://blog.51cto.com/qingfeng00/2085321