Cover V11, I09

Article
Figure 1
Listing 1
Listing 2
Listing 3

sep2002.tar

Listing 1 Director control script

#!/bin/sh
#
# Read in the LVS configuration file.
. /etc/lvs.conf



reset_lvs() {
# Reset the LVS virtual server tables.
ipvsadm --clear
}


configure_networking() {
# Turn off all relevant ICMP redirects. $LVS_IF is the network
# interface used for LVS traffic on the director.
echo "0" > /proc/sys/net/ipv4/conf/all/send_redirects
echo "0" > /proc/sys/net/ipv4/conf/default/send_redirects
echo "0" > /proc/sys/net/ipv4/conf/$LVS_IF/send_redirects

# Turn on the IP forwarding flag
echo "1" >/proc/sys/net/ipv4/ip_forward

# Add interface and routing for the VIP interface.
/sbin/ifconfig $VIP_IF $VIP
/sbin/route add -host $VIP dev $VIP_IF
}


remove_interface() {
# Remove the VIP network interface.
[ -n "$(ifconfig | grep ^$VIP_IF)" ] && /sbin/ifconfig $VIP_IF down
}


add_services() {
# For each TCP service listed in the configuration, add the service
# and add the Real-servers that provide the service.
for TPORT in $TPORTS; do

# -A = Add service; -t = Service uses TCP
# $VIP:$TPORTS = Virtual IP Address of new service, and the port
# number of the service being presented on this interface.
# -s = scheduling method (rr = Round Robin)
ipvsadm -A -t $VIP:$TPORT -s rr

# Add real-servers into the LVS cluster for the above service.
#
# -a = Add real-server;  -t = Service uses TCP
# 156.141.59.63:telnet = Virtual IP Address of new service,
# and the port number of the service being presented on this interface.
# -r : = hostname/IP Address of the real-server and port
# number of the service on the real-server.
# -m = use Masquerading (aka Network Address Translation)
# -w = Weight of the real-server in the pool. Can be used to direct traffic
# to more powerful servers in the pool.
for RIP in $RIPS; do
ipvsadm -a -t $VIP:$TPORT -r $RIP:$TPORT -m -w 1
done
done

# For each UDP service listed in the configuration, add the service
# and add the Real-servers that provide the service.
for UPORT in $UPORTS; do

# -A = Add service; -u = Service uses UDP
# $VIP:$UPORTS = Virtual IP Address of new service, and the port
# number of the service being presented on this interface.
# -s = scheduling method (rr = Round Robin)
ipvsadm -A -u $VIP:$UPORT -s rr

# Add real-servers into the LVS cluster for the above service.
#
# -a = Add real-server;  -t = Service uses TCP
# 156.141.59.63:telnet = Virtual IP Address of new service,
# and the port number of the service being presented on this interface.
# -r : = hostname/IP Address of the real-server and port
# number of the service on the real-server.
# -m = use Masquerading (aka Network Address Translation)
# -w = Weight of the real-server in the pool. Can be used to direct traffic
# to more powerful servers in the pool.
for RIP in $RIPS; do
ipvsadm -a -u $VIP:$UPORT -r $RIP:$UPORT -m -w 1
done
done
}


poll_servers() {
# Ping each host in the cluster to verify whether or not they are
# alive. Systems which do not respond should be removed from the
# ipvs table.
for RIP in $RIPS; do
# In order to be reduce dependencies, assume that fping is
# unavailable, and use the less efficient system ping command. In
# order to accommodate network latency, systems that do not
# respond will be polled again, up to a maximum of $SEEK
# iterations.
SEEK=4
while [ $SEEK -gt 0 ]; do
[ -z "`ping -c 1 $RIP | grep 100%`" ] && SEEK=-1 || SEEK=$(($SEEK-1))
done

# If SEEK is "-1" then a positive result was obtained and the host
# is available to the network. Now check to see if the host is in
# the LVS table -- if it is not, then add the host to the
# configuration.
if [ "$SEEK" = "-1" ]; then
# Check to see if the host is already in the configuration. If
# it is not, then add it.
if [ -z "$(ipvsadm -l | grep $RIP)" ]; then
for TPORT in $TPORTS; do
ipvsadm -a -t $VIP:$TPORT -r $RIP:$TPORT -m -w 1
done
for UPORT in $UPORTS; do
ipvsadm -a -u $VIP:$UPORT -r $RIP:$UPORT -m -w 1
done
fi
else
# If the value of SEEK is not -1, then the host was not found on
# the network. If this is the case, the host is removed from the
# active LVS configuration.
if [ ! -z "$(ipvsadm -l | grep $RIP)" ]; then
for TPORT in $TPORTS; do
ipvsadm -d -t $VIP:$TPORT -r $RIP:$TPORT
done
for UPORT in $UPORTS; do
ipvsadm -d -u $VIP:$UPORT -r $RIP:$UPORT
done
fi
fi
done
}


lock_file() {
case "$1" in
add)
touch /var/lock/subsys/lvs-director
;;
remove)
rm -f /var/lock/subsys/lvs-director
;;
esac
}


usage() {
echo $"Usage: $0 {start|stop|reload|status|update}"
}


#
# Start of the main iteration
#

case "$1" in
start)
if [ -f /var/lock/subsys/lvs-director ]; then
echo "LVS Already running"
exit 1
fi
reset_lvs
configure_networking
add_services
lock_file add
;;
stop)
reset_lvs
remove_interface
lock_file remove
;;
reload)
reset_lvs
add_services
;;
status)
ipvsadm -L
;;
update)
if [ ! -f /var/lock/subsys/lvs-director ]; then
echo "LVS is not running. Cannot update service."
exit 1
fi
poll_servers
;;
*)
usage
;;
esac

Title