主机2的脚本如下
cat nginx_loadbalance_vip2.sh
#!/bin/bash
LANG=C
date=$(date -d "today" +"%Y-%m-%d %H:%M:%S")
BIND_VIP1=192.168.16.239
BIND_VIP2=192.168.16.238
WEB_IP_RIP1=192.168.16.241
WEB_IP_RIP2=192.168.16.240
NETMASK=255.255.255.0
GATEWAY=192.168.16.1
BROADCAST=219.232.254.255
function_bind_vip1()
{
/sbin/ifconfig eth0:ha1 ${BIND_VIP1} broadcast ${BROADCAST} netmask ${NETMASK} up
/sbin/route add -host ${GATEWAY} dev eth0:ha1
}
function_bind_vip2()
{
/sbin/ifconfig eth0:ha2 ${BIND_VIP2} broadcast ${BROADCAST} netmask ${NETMASK} up
/sbin/route add -host ${GATEWAY} dev eth0:ha2
}
function_restart_nginx()
{
kill -USR1 `cat /var/log/nginx/nginx.pid`
}
function_remove_vip1()
{
/sbin/ifconfig eth0:ha1 ${BIND_VIP1} broadcast ${BROADCAST} netmask ${NETMASK} down
}
function_remove_vip2()
{
/sbin/ifconfig eth0:ha2 ${BIND_VIP2} broadcast ${BROADCAST} netmask ${NETMASK} down
}
function_vip_arping1()
{
/sbin/arping -I eth0 -c 3 -s ${BIND_VIP1} ${GATEWAY} > /dev/null 2>&1
}
function_vip_arping2()
{
/sbin/arping -I eth0 -c 3 -s ${BIND_VIP2} ${GATEWAY} > /dev/null 2>&1
}
bind_time_vip1="N";
bind_time_vip2="N";
while true
do
httpcode_rip1=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://${WEB_IP_RIP1}/status.htm`
httpcode_rip2=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://${WEB_IP_RIP2}/status.htm`
#vip1
if [ x$httpcode_rip1 == "x200" ];
then
if [ $bind_time_vip1="N" ];
then
function_bind_vip1
function_vip_arping1
function_restart_nginx
bind_time_vip1="Y"
fi
function_vip_arping1
else
if [ $bind_time_vip1 == "Y" ];
then
function_remove_vip1
bind_time_vip1="N"
fi
fi
#vip2
if [ x$httpcode_rip2 == "x200" ];
then
if [ $bind_time_vip2="Y" ];
then
function_remove_vip2
bind_time_vip2="N"
fi
else
if [ $bind_time_vip2 == "N" ];
then
function_bind_vip2
function_vip_arping2
function_restart_nginx
bind_time_vip2="Y"
fi
function_vip_arping2
fi
sleep 5
done
原文地址:http://sageliu.blog.51cto.com/8969608/1546178