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.

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


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 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):


Real server one enable code:

-a -t -r -g -w 1
-a -u -r -g -w 1

Real server two enable code:

-a -t -r -g -w 1
-a -u -r -g -w 1

Real server one disable code:

-d -t -r
-d -u -r

Real server two disable code:

-d -t -r
-d -u -r

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 ./

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


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


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