实验环境:centos6.5系统
软件:ipvsadm-1.24、LVS.sh脚本
realserver.sh脚本
LVS服务器:
/opt/shell/LVS.sh
#!/bin/bash## LVS script for VS/DR#. /etc/rc.d/init.d/functions#VIP=10.124.194.10RIP1=10.124.194.242RIP2=10.124.194.241PORT1=80PORT2=443#case "$1" instart) /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev eth0: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 10.124.194.10 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:$PORT1 -s wlc /sbin/ipvsadm -A -t $VIP:$PORT2 -s wlc# Now direct packets for this VIP to# the real server IP (RIP) inside the cluster /sbin/ipvsadm -a -t $VIP:$PORT1 -r $RIP1 -g -w 1 /sbin/ipvsadm -a -t $VIP:$PORT2 -r $RIP1 -g -w 1 /sbin/ipvsadm -a -t $VIP:$PORT1 -r $RIP2 -g -w 2 /sbin/ipvsadm -a -t $VIP:$PORT2 -r $RIP2 -g -w 2 /bin/touch /var/lock/subsys/ipvsadm &> /dev/null;; stop)# Stop forwarding packets echo 0 > /proc/sys/net/ipv4/ip_forward# Reset ipvsadm /sbin/ipvsadm -C# Bring down the VIP interface /sbin/ifconfig eth0:1 down /sbin/route del $VIP /bin/rm -f /var/lock/subsys/ipvsadm 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
Realserver服务器端:
/etc/init.d/realserver.sh
#!/bin/bash# description: Config realserver lo and apply noarp SNS_VIP=10.124.194.10 #/etc/rc.d/init.d/functions case "$1" instart) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 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 sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;;stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 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 echo "RealServer Stoped" ;;*) echo "Usage: $0 {start|stop}" exit 1esac exit 0