實驗環境:rhel5.8
Director
DIP:192.168.88.132 VIP:192.168.88.120
RealServer
RIP1:192.168.88.129 VIP:192.168.88.120
RIP1:192.168.88.134 VIP:192.168.88.120
DR模型,調度算法為wlc
需求實現:模型正常工作,及Director實現自動檢測,當任意RS故障時,自動將其移除,當檢測到其狀態正常時,再將其添加進來,並保證其原有屬性不變。
Director的腳本放置為:/etc/init.d/director和/etc/init.d/lvs,其中director腳本可使用chkconfig -add director進行管理。 lvs僅作函數放置即可,其功能筆者已將其整合進director腳本,負責進行檢測RS的狀態並做操作的,也只有這個是筆者自己寫的,並為其提供了簡單的配置文件(/etc/sysconfig/lvs.conf) ,默認為兩秒檢測一次,如需修改,直接修改lvs.conf裡面的sec=n秒 即可。廢話不多,上代碼:
對了,為了方便各位看官,相關文件可以直接附件下載。
Director第一個腳本:
cat /etc/init.d/director #!/bin/sh # # LVS script for VS/DR # # chkconfig: 345 99 99 # description: LVS DR # # processname: director # . /etc/rc.d/init.d/functions VIP=192.168.88.120 RIP1=192.168.88.129 RIP2=192.168.88.134 PORT=80 # case "$1" in start) /sbin/ifconfig eth1:1 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev eth1:1 # Since this is the Director we must be able to forward packets echo 1 > /proc/sys/net/ipv4/ip_forward # Clear all iptables rules. /sbin/iptables -F # Reset iptables counters. /sbin/iptables -Z # Clear all ipvsadm rules/services. /sbin/ipvsadm -C # Add an IP virtual service for VIP 192.168.0.219 port 80 # In this recipe, we will use the round-robin scheduling method. # In production, however, you should use a weighted, dynamic scheduling method. /sbin/ipvsadm -A -t $VIP:80 -s wlc # Now direct packets for this VIP to # the real server IP (RIP) inside the cluster /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 9 /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 8 /bin/touch /var/lock/subsys/ipvsadm &> /dev/null ;; stop) # NUM1=`ps aux|grep lvs|grep -v grep|awk '{print $2}'` # `kill $NUM1` # Stop forwarding packets echo 0 > /proc/sys/net/ipv4/ip_forward # Reset ipvsadm /sbin/ipvsadm -C &>/dev/null # Bring down the VIP interface /sbin/ifconfig eth1:1 down &>/dev/null /sbin/route del $VIP &>/dev/null /bin/rm -f /var/lock/subsys/ipvsadm &>/dev/null echo "ipvs is stopped..." ;; status) if [ ! -e /var/lock/subsys/ipvsadm ]; then echo "ipvsadm is stopped ..." else echo "ipvs is running ..." ipvsadm -L -n fi ;; *) echo "Usage: $0 {start|stop|status}" ;; esac [ -x /etc/rc.d/init.d/lvs ] && . /etc/rc.d/init.d/lvs LVS_DIRECTOR &
完成後,記得執行 chkconfig -add director 表急著啟動哈;下面上lvs腳本代碼:
#!/bin/sh # # LVS script for VS/DR # # chkconfig: 345 95 93 # description: LVS DR Check # # processname: lvs.sh # # Author: MOS # Script name: lvs.sh # Date & Time: 2012-11-25/00:41:17 # Version: 1.0.1 # Description: # LVS_DIRECTOR(){ [ ! -x /sbin/ipvsadm ] && exit 1 [ ! -x /sbin/ipvsadm-save ] && exit 1 #[ ! -x /sbin/ipvsadm ] && echo "no such command: ipvsadm" #[ ! -x /sbin/ipvsadm-save ] && echo "no such command: ipvsadm" [ -f /etc/sysconfig/lvs.conf ] && . /etc/sysconfig/lvs.conf Test=${Test:-.mos.html} TestStr=${TestStr:-MOS} Sec=${Sec:-10} Log=${Log:-Off} [ $Log = "On" ] && Logfile=${Logfile:-/var/log/ipvs.log} VIP=`ipvsadm-save -n 2>/dev/null|awk '{print $3}'|uniq` #[ -z $VIP ] && echo "Not such VIP" && exit 1 [ -z $VIP ] && exit 1 RIP=(`ipvsadm-save -n 2>/dev/null|awk '{print $5}'|grep "^[0-9].*[0-9]$"`) #[ -z "${RIP[0]}" ] && echo "Not such RIP" && exit 2 [ -z "${RIP[0]}" ] && exit 2 Tmpfile=`mktemp /tmp/lvs.XXXXXX 2> /dev/null` `ipvsadm-save -n > $Tmpfile` Rtmp=${Rtmp:-On} [ $Rtmp = "On" ] && `find /tmp -type f -mtime +2 -name "lvs.*" -exec rm -rf {} \;` while :;do for i in ${RIP[*]};do Str=`elinks -dump "$i"/"$Test" 2>/dev/null|awk '{print $1}'` [[ "$Str" != "$TestStr" ]] && `ipvsadm -d -t $VIP -r $i 2>/dev/null` && continue # [[ "$Str" != "$TestStr" ]] && Restore=`ipvsadm-save -n|grep $i` && `ipvsadm -d -t $VIP -r $i 2>/dev/null` && continue RIP_1=`ipvsadm -Ln 2>/dev/null|grep $i` [[ -n "$RIP_1" ]] && continue if [[ "$Str" == "$TestStr" ]] ;then Restore=`grep $i $Tmpfile 2>/dev/null|uniq` ResN=`grep $i $Tmpfile 2>/dev/null|uniq|wc -l` [ $ResN -ne 1 ] && [[ "$Log" == "On" ]] && echo "Warn: Please RIP quantity." >> $Logfile `ipvsadm $Restore &>/dev/null` fi done Quit=`ipvsadm-save -n 2>/dev/null` [[ -z "$Quit" ]] && exit 0 sleep $Sec done } #LVS_DIRECTOR
記得完成後給執行權限,其配置文件如下:
 
Test=.mos.html TestStr=MOS Log=Off Logfile=/var/log/ipvs.log Sec=2 Rtmp=On
簡單說一下:
Test是檢查RS的網頁根目錄必須要有個您自己的html文件,您寫上你自己的,並保證其能被正常訪問例如您的是 test.html那麼 http://192.168.88.129/test.html必須可以訪問才行。
TestStr第二個是這個網頁的內容,腳本就是通過判斷這個測試頁內的內容,如果您的test.html裡面是test,那麼此處記得把MOS換成test即可
Log相關的只是個設想,筆者做的時候,有個測試,筆者試著寫入自定義日志記錄,後來一直沒用,不管即可
Sec是每個幾秒會重新檢測一下RS有沒有故障的,這個默認2秒,看您需求
Rtmp筆者的lvs腳本每次啟動會自動生成一個臨時文件,用以記錄當前的vip和rip,一直開著腳本還好說,測試的話,每次都會生成不同的...你懂的,這個的意思就是會自動刪除之前的,貌似筆者設置的是自動刪除兩天前的。
下面提供RealServer腳本,上代碼:
#!/bin/bash # # Script to start LVS DR real server. # chkconfig: - 95 15 # description: LVS DR real server # . /etc/rc.d/init.d/functions VIP=192.168.88.120 host=`/bin/hostname` case "$1" in start) # Start LVS-DR real server on this machine. /sbin/ifconfig lo down /sbin/ifconfig lo up echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 ;; stop) # Stop LVS-DR real server loopback device(s). /sbin/ifconfig lo:0 down echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce ;; status) # Status of LVS-DR real server. islothere=`/sbin/ifconfig lo:0 | grep $VIP` isrothere=`netstat -rn | grep "lo:0" | grep $VIP` if [ ! "$islothere" -o ! "isrothere" ];then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR real server Running." fi ;; *) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1 ;; esac
記得chkconfig --add 並修改權限..到此完成...
本文出自 “自強不息” 博客,請務必保留此出處http://mos1989.blog.51cto.com/4226977/1071245