结构图 1.用户通过DNS查询,得到随机的VIP1和VIP2。 2.VIP1和VIP2属于热备,负载均衡,高可用 当RIP1无法访问,主机2会检测到,将VIP1的ip加到的eth0:ha2,即在任何时候都有高可用 3.shell脚本vip和rip要对应,nginx_loadbalance_vip1.sh运行于主机1,运行命令为 #nohup ./nginx_loadbalance_vip1.sh >/dev/null & 后台运行 4.对于2台机器的监控,只需监控RIP即可,同时可以写一个监控脚本运行的crontab,防止脚本意外终止 -------------------------------------------------------------------- |
主机1的脚本如下
cat nginx_loadbalance_vip1.sh
- #!/bin/bash
- LANG=C
- date=$(date -d "today" +"%Y-%m-%d %H:%M:%S")
- BIND_VIP1=192.168.16.238
- BIND_VIP2=192.168.16.239
- WEB_IP_RIP1=192.168.16.240
- WEB_IP_RIP2=192.168.16.241
- 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
主机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
注意:本文来自于张宴的nginx书籍对双机热备的讲解,脚本也来自书中,本人对其有小小的改动,大家可以参看原书的详细内容
本文转自it你好 51CTO博客,原文链接:http://blog.51cto.com/itnihao/926211,如需转载请自行联系原作者