LVS负载均衡之DR模型实例

LVS负载均衡中的DR模型是现实生产环境中应用最广泛的一种模型,只要能理解DR模型的原理,实际操作起来是非常简单的。另外LVS是基于四层负载均衡,nginx负载基于七层负载,在应用方向上会有一些区别。
好了,不多说,下面是LVS负载均衡DR模型中Director主机和RealServer机器上配置脚本,不详细解释每一项说明,相信大家都能看懂,另外,因为是实验环境,所以只有一台Director和2台RealServer,如果是生产环境,肯定要考虑Director冗余的问题。
Director主机上面脚本如下:

#!/bin/sh
#chkconfig:2345 08 92
#description:ipvsadm
#

VIP=192.168.16.200
RealServer1=192.168.10.253
RealServer2=192.168.10.254

. /etc/rc.d/init.d/functions

retval=0
start()
{
# Since this is the Director we must be able to forward packets
echo 1 > /proc/sys/net/ipv4/ip_forward

# Add ip address and route
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:0

# Clear all iptables rules and Reset iptables counters
/sbin/iptables -F
/sbin/iptables -Z

# Clear all ipvsadm rules
/sbin/ipvsadm -C

# Add ipvsadm rules
/sbin/ipvsadm -A -t $VIP:80 -s wlc -p 120
/sbin/ipvsadm -a -t $VIP:80 -r $RealServer1:80 -g -w 1
/sbin/ipvsadm -a -t $VIP:80 -r $RealServer2:80 -g -w 1

touch /var/lock/subsys/ipvsadm > /dev/null 2 >&1
echo "ipvsadm started [ok]"
}

stop()
{
echo 0 > /proc/sys/net/ipv4/ip_forward

/sbin/ipvsadm -C

ifconfig eth0:0 down
route del $VIP

rm -rf /var/lock/subsys/ipvsadm > /dev/null 2 >&1
echo "ipvsadm stoped [ok]"
}

status()
{
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm is stoped ..."
exit 1
else
echo "ipvsadm is running ..."
/sbin/ipvsadm -L -n
fi
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
retval=1
esac
exit $retval
</ol>
RealServer主机上面脚本如下:
<ol>
#!/bin/bash
VIP=192.168.16.200
. /etc/rc.d/init.d/functions

case "$1" in
start)
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0

echo 1 &gt;/proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 &gt;/proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 &gt;/proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 &gt;/proc/sys/net/ipv4/conf/all/arp_announce

echo "RealServer Start Ok"
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $VIP &gt;/dev/null 2 &gt;&amp;1

echo 0 &gt;/proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 &gt;/proc/sys/net/ipv4/conf/lo/arp_announce
echo 0 &gt;/proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 &gt;/proc/sys/net/ipv4/conf/all/arp_announce

echo "RealServer Stoped Ok"
;;
status)
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ ! -z "$islothere" -o ! -z "$isrothere" ];then
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
标签: 无
返回文章列表 文章二维码
本页链接的二维码
打赏二维码