码迷,mamicode.com
首页 > Web开发 > 详细

编写检测web节点是否正常,自动加入或踢出LVS

时间:2018-03-11 23:11:34      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:#!/bin/bash VIP1=192

#!/bin/bash
VIP1=192.168.1.200:80
VIP=192.168.1.200
web1=192.168.1.10
web2=192.168.1.20
Port=80

. /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

配置VIP

     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

编写检测web节点是否正常,自动加入或踢出LVS

标签:#!/bin/bash VIP1=192

原文地址:http://blog.51cto.com/qingfeng00/2085321

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!