Nginx+Keepalived实现站点高可用(负载均衡)
时间:2016-12-22 15:46 来源:linux.it.net.cn 作者:IT
该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix,如果启动失败则停止keepalived,准备让其它机器接管。
PS:该脚本应赋予相应的执行权限等,否则Keepalived将无法调用从而无法进行切换
/etc/keepalived/check_nginx.sh :
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l )
if [ " ${counter} " = "0" ]; then
/usr/local/bin/nginx -c /opt/nginx/conf/nginx.conf
sleep 2
counter=$(ps -C nginx --no-heading|wc -l )
if [ " ${counter} " = "0" ]; then
/etc/init.d/keepalived stop
fi
fi
也可以根据自己的业务需求,总结出在什么情形下关闭keepalived,如 curl 主页连续2个5s没有响应则切换:
#!/bin/bash
# curl -IL http://localhost/member/login.htm
# curl --data "memberName=fengkan&password=22" http://localhost/member/login.htm
count = 0
for (( k= 0 ; k< 2 ; k++ ))
do
check_code=$( curl --connect-timeout 3 -s L -w "%{http_code}\\n" http://localhost/login.html -o /dev/null )
if [ " $check_code " != "200" ]; then
count = count + 1
continue
else
count = 0
break
fi
done
if [ " $count " != "0" ]; then
# /etc/init.d/keepalived stop
exit 1
else
exit 0
fi
(责任编辑:IT)
该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix,如果启动失败则停止keepalived,准备让其它机器接管。 PS:该脚本应赋予相应的执行权限等,否则Keepalived将无法调用从而无法进行切换 /etc/keepalived/check_nginx.sh : #!/bin/bash counter=$(ps -C nginx --no-heading|wc -l ) if [ " ${counter} " = "0" ]; then /usr/local/bin/nginx -c /opt/nginx/conf/nginx.conf sleep 2 counter=$(ps -C nginx --no-heading|wc -l ) if [ " ${counter} " = "0" ]; then /etc/init.d/keepalived stop fi fi 也可以根据自己的业务需求,总结出在什么情形下关闭keepalived,如 curl 主页连续2个5s没有响应则切换: #!/bin/bash # curl -IL http://localhost/member/login.htm # curl --data "memberName=fengkan&password=22" http://localhost/member/login.htm count = 0 for (( k= 0 ; k< 2 ; k++ )) do check_code=$( curl --connect-timeout 3 -s L -w "%{http_code}\\n" http://localhost/login.html -o /dev/null ) if [ " $check_code " != "200" ]; then count = count + 1 continue else count = 0 break fi done if [ " $count " != "0" ]; then # /etc/init.d/keepalived stop exit 1 else exit 0 fi (责任编辑:IT) |