一般来讲由于LVS的配置文件里在调用脚本时只有一个输入参数占位符%h
send_program = "/etc/sysconfig/ha/lvs.sh %h" ........... server app1 { address = 10.92.21.18 active = 1 port = 8080 weight = 1 } server app2 { address = 10.92.21.20 active = 1 port = 8080 weight = 1 }
%h 自动匹配server里的address,但是如果这些server的address一样(一台机上有多个负载实例的情况下),LVS就会启动不了。
为此我做些特殊处理
我使用的机器原本的ip为10.92.21.18,在这机器上部署两个应用实例,端口分别为8080和8082,另外我通过
设置了一个虚拟ip,对应关系如下:
10.92.21.18----》8080
10.92.21.200---》8082
send_program = "/etc/sysconfig/ha/lvs_ips.sh %h" 。。。。。。。 server app1 { address = 10.92.21.18 active = 1 port = 8080 weight = 1 } server app2 { address = 10.92.21.200 active = 1 port = 8082 weight = 1 }
lvs_ips.sh
#!/bin/sh tmpUriName=$1 keyword="SUCCESS" timeout=10 errorTimes="1" case "$tmpUriName" in "10.92.21.18") uri="http://${tmpUriName}:8080/XXXX/lvs.Monitor" ;; "10.92.21.200") uri="http://${tmpUriName}:8082/XXXX/lvs.Monitor" ;; esac writeError(){ if [ -f /tmp/.errorcount_${tmpUriName} ]; then errorCount="$(cat /tmp/.errorcount_${tmpUriName})" else errorCount="0" fi if [ $errorCount == ${errorTimes} ]; then echo "NOSUCCESS" #echo "LOG ${tmpUriName} FAIL">>/tmp/.errorcount else echo ${errorTimes} >/tmp/.errorcount_${tmpUriName} echo "OK" #echo "LOG ${tmpUriName} FAIL OK">>/tmp/.errorcount fi } testConn(){ if [ `GET -t ${timeout} ${uri} |grep ${keyword} |wc -l` -gt 0 ]; then echo "0" > /tmp/.errorcount_${tmpUriName} echo "OK" #echo "LOG ${tmpUriName} OK">>/tmp/.errorcount else writeError fi } testConn
搞掂,有图为证~