Sample IPVS Scripts

These sample scripts are to start and configure your router with EzPlanet ipvs firmware to run as a Layer 2 Load Balancer, based on LVS (kernel ip_vs modules + ipvsadm).

The Load Balancer will work on any network interface associated to your router/load balancer.

This sample is to configure the internal interface (private network) to load balance one virtual service on two real servers. With ipvsadm we can instruct the load balancer to forward only specific ports, however to maintain a simple example here I have specified port:0 which means, every incoming request on any port will be forwarded to the real server.

Before you begin:

  1. Make sure that jffs2 is enabled (must have 8MB Flash or more) Administration->JFFS2 Enable
  2. Make sure that nvram rc_startup is set to /jffs/etc/rc.local as follows
nvram set rc_startup="/jffs/etc/rc.local"
nvram commit

The following scripts are represented with their full path. Create the directories as needed.

/jffs/etc/rc.local
for i in /jffs/etc/init.d/S* ; do
        $i start
done
echo '* * * * * root /jffs/sbin/checkRealServers.sh' >> /tmp/cron
stopservice cron && startservice cron

/jffs/etc/init.d/S10ipvsadm
:
BASE_DIR=/jffs/etc/ipvsadm.d

insmod ip_vs_wlc insmod ip_vs_lc

cd $BASE_DIR for f in `ls vlan*`; do INT=10 for i in `cat $f` ; do ifconfig $f:$INT $i netmask 255.255.255.255 up iptables -I INPUT -d $i/32 -j ACCEPT INT=`expr $INT + 1` done done

for i in ${BASE_DIR}/*.vs ; do ipvsadm -R < $i done for i in ${BASE_DIR}/*.start ; do ipvsadm -R < $i done

ipvsadm --start-daemon master --mcast-interface vlan0

This is the list of virtual servers (on the internal private lan):

/jffs/etc/ipvsadm.d/vlan0
192.168.10.10

Real server one enable code:

/jffs/etc/ipvsadm.d/192.168.10.23.up
-a -t 192.168.10.10:0 -r 192.168.10.23 -g -w 1
-a -u 192.168.10.10:0 -r 192.168.10.23 -g -w 1

Real server two enable code:

/jffs/etc/ipvsadm.d/192.168.10.48.up
-a -t 192.168.10.10:0 -r 192.168.10.48 -g -w 1
-a -u 192.168.10.10:0 -r 192.168.10.48 -g -w 1

Real server one disable code:

/jffs/etc/ipvsadm.d/192.168.10.23.down
-d -t 192.168.10.10:0 -r 192.168.10.23
-d -u 192.168.10.10:0 -r 192.168.10.23

Real server two disable code:

/jffs/etc/ipvsadm.d/192.168.10.48.down
-d -t 192.168.10.10:0 -r 192.168.10.48
-d -u 192.168.10.10:0 -r 192.168.10.48

make symbolic links as follows for those real servers that you want to be brought up when the load balancer boots (this saves some lost connections if you have a stand-by backup like I do):

cd /jffs/etc/ipvsadm.d
ln -s ./192.168.10.23.up 192.168.10.23.start

This file contains the list of real servers to be probed by our simple check script:

/jffs/etc/ipvsadm.d/realServers
192.168.10.23
192.168.10.48

And finally the script that will check for real servers' availability and configure ipvsadm on the fly:

/jffs/sbin/checkRealServers.sh
#!/bin/sh
PATH=$PATH:/sbin:/usr/sbin:/jffs/bin:/jffs/sbin
BASE_DIR=/jffs/etc/ipvsadm.d
TMP_DIR=/tmp
REAL_SERVERS=realServers

for i in `cat $BASE_DIR/$REAL_SERVERS`; do ping -qc 3 $i > /dev/null RESULT=$? if [ $RESULT -gt 0 ]; then if [ ! -f $TMP_DIR/$i.down ]; then ipvsadm -R < $BASE_DIR/$i.down date > $TMP_DIR/$i.down fi else if [ -f $TMP_DIR/$i.down ]; then ipvsadm -R < $BASE_DIR/$i.up rm $TMP_DIR/$i.down fi fi done