实验环境: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
记得完成后给执行权限,其配置文件如下:
- cat /etc/sysconfig/lvs.conf
- Test=.mos.html
- TestStr=MOS
- Log=Off
- Logfile=/var/log/ipvs.log
- Sec=2
- Rtmp=On
简单说一下:
Test是检查RS的网页根目录必须要有个您自己的html文件,您写上你自己的,并保证其能被正常访问例如您的是 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 并修改权限..到此完成...