Load balance websockify proxy

 

Introduction to load balancing websockify proxy

You can run multple websockify proxies to spread load and achieve high availability of the remote console viewers. First get several proxies running. For the sake of simplicity this document refers to 2 backend servers running the websockify proxy and one load balancer using HAProxy.

If you use a load balancer for your websockify proxies, configure the UI client to point to the IP and port of the load balancer.

  • Abiquo 3.8+ in client-config-custom.json
  • Prior to Abiquo 3.8, in tightvnc.html

 

Installing HAProxy

You will need the gcc compiler and make to compile HAProxy.

# yum -y install gcc gcc-c++ make

Next download and uncompress the HAProxy tarball:

# wget http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev18.tar.gz
# tar xzf haproxy-1.5-dev18.tar.gz

Change to the extracted directory, then compile and install HAProxy.

# cd haproxy-1.5-dev18
# make TARGET=linux26
# make install

Configuration file

Once haproxy is installed, create a directory for haproxy under /etc:

# mkdir /etc/haproxy

And create its config file, /etc/haproxy/haproxy.conf with the following contents:

global
 log 127.0.0.1 local0

frontend public
 bind *:41338
 timeout client 3600s
 default_backend ws


backend ws
 balance source
 timeout queue 3600s
 timeout server 3600s
 timeout connect 3600s
 server websockify1 192.168.2.218:41337 weight 1 maxconn 1024 check
 server websockify2 192.168.2.219:41337 weight 1 maxconn 1024 check


listen stats
 bind *:80
 mode http
 stats enable
 stats uri /admin?stats
 stats refresh 5s
 stats auth admin:xabiquo
 timeout client 3600s
 timeout server 3600s
 timeout connect 3600s

Make sure you change IP addresses in each server line to match the IP addresses of your websockify proxies. To activate logging, you need rsyslog package installed:

# yum -y install rsyslog

Then create /etc/rsyslog.d/20-haproxy.conf file with the following content:

local0.*         /var/log/haproxy.log

And reload the rsyslog daemon:

# service rsyslog reload

Note that the previous configuration file also enables the HAProxy stats page on http://<your_balancer_address>/admin?stats with authentication credentials  user: admin and password: xabiquo.

Starting haproxy

Run haproxy from the command line by typing:

# haproxy -f /etc/haproxy.cfg

To run haproxy as a daemon, create an haproxy script under /etc/init.d/ directory.

# vi /etc/init.d/haproxy

And add the following content:

#!/bin/sh
#
# custom haproxy init.d script, by Mattias Geniar <mattias@nucleus.be>
#
# haproxy         starting and stopping the haproxy load balancer
#
# chkconfig: 345 55 45
# description: haproxy is a TCP loadbalancer
# probe: true
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -f /usr/local/sbin/haproxy ] || exit 0
[ -f /etc/haproxy/haproxy.conf ] || exit 0
# Define our actions
checkconfig() {
	# Check the config file for errors
	/usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
	if [ $? -ne 0 ]; then
    		echo "Errors found in configuration file."
    		return 1
  	fi
	# We're OK!
	return 0
}
start() {
	# Check config
	/usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi
	echo -n "Starting HAProxy: "
	daemon /usr/local/sbin/haproxy -D -f /etc/haproxy/haproxy.conf -p /var/run/haproxy.pid
	RETVAL=$?
	echo
  	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
  	return $RETVAL
}
stop() {
	echo -n "Shutting down HAProxy: "
  	killproc haproxy -USR1
  	RETVAL=$?
  	echo
  	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy
  	[ $RETVAL -eq 0 ] && rm -f /var/run/haproxy.pid
  	return $RETVAL
}
restart() {
	/usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi
	stop
  	start
}
check() {
  	/usr/local/sbin/haproxy -c -q -V -f /etc/haproxy/haproxy.conf
}
rhstatus() {
  	status haproxy
}
reload() {
	/usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
        if [ $? -ne 0 ]; then
                echo "Errors found in configuration file."
                return 1
        fi
	echo -n "Reloading HAProxy config: "
	/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.conf -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)
	success $"Reloading HAProxy config: "
	echo
}

# Possible parameters
case "$1" in
  start)
        start
	;;
  stop)
	stop
        ;;
  status)
        rhstatus
        ;;
  restart)
	restart
        ;;
  reload)
        reload
        ;;
  checkconfig)
	check
	;;
  *)
        echo "Usage: haproxy {start|stop|status|restart|reload|checkconfig}"
        exit 1
esac
exit 0

Give the script execution permissions:

# chmod u+x /etc/init.d/haproxy

And you will be able to perform the following commands:

# service haproxy {start|stop|restart|reload|condrestart|status|check}

Set it up to start at boot with:

# chkconfig haproxy on
Unable to render {include} The included page could not be found.