Ubuntu 14.04
Sponsored Link

Pen : HTTP Load Balancing2016/01/11

 
Install pen which is lightweight simple Load Balancing software.
It is TCP protocol based, so it's possible to balance not only HTTP but SMTP, FTP, LDAP and so on.
This example is based on the environment like follows.
        |
--------+--------------------------------------------------------------------
        |
        +-------------------+--------------------+--------------------+
        |10.0.0.30          |10.0.0.51           |10.0.0.52           |10.0.0.53
 +------+-----+     +-------+------+     +-------+------+     +-------+------+
 |  Frontend  |     |   Backend#1  |     |   Backend#2  |     |   Backend#3  |
 | Pen Server |     |  Web Server  |     |  Web Server  |     |  Web Server  |
 +------------+     +--------------+     +--------------+     +--------------+

 
Configure Pen to load balance to Backend#1, Backend#2, Backend#3 web servers.
[1] Install Pen.
root@dlp:~#
apt-get -y install pen
[2] Configure Pen.
root@dlp:~#
vi /etc/pen.conf
# create new

# log file

LOGFILE=/var/log/pen.log
# statics report file

WEBFILE=/var/www/html/pen/webstats.html
# max connections

MAX_CONNECTIONS=256
# send X-Forwarded-For header

XFORWARDEDFOR=true
# Round-Robin mode

ROUNDROBIN=true
# listening port

PORT=80
# number of backends

BACKEND=3
# define backend servers

SERVER1=10.0.0.51:80
SERVER2=10.0.0.52:80
SERVER2=10.0.0.53:80
# create an init script

root@dlp:~#
vi /etc/init.d/pend
#! /bin/sh
### BEGIN INIT INFO
# Provides:          pen
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: light weight simple load balancer
# Description:       light weight simple load balancer
#
### END INIT INFO
#
# Pen - light weight simple load balancer

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/pen
DESC="light weight simple load balancer"
NAME=pen
PIDFILE=/var/run/pen.pid

# Exit if the daemon does not exist (anymore)
test -f $DAEMON || exit 0

. /lib/lsb/init-functions
. /etc/pen.conf

SERVER=`grep "^SERVER" /etc/pen.conf | cut -d= -f2`
[ $XFORWARDEDFOR = "true" ] && SERVER="-H $SERVER"
[ $ROUNDROBIN = "true" ] && SERVER="-r $SERVER"
[ $SSLCERTS ] && SERVER="-E $SSLCERTS $SERVER"

# The real work of an init script
case "$1" in
    start)
        log_daemon_msg "Starting $DESC" "$NAME"
        start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE \
        --startas $DAEMON -- $PORT -w $WEBFILE -x $MAX_CONNECTIONS -p $PIDFILE -l $LOGFILE -S $BACKEND $SERVER
        log_end_msg $?
        ;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        log_end_msg $?
        ;;
  restart|force-reload)
        $0 stop && sleep 2 && $0 start
        ;;
  status)
        status_of_proc $DAEMON "Pend"
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|force-reload|status}"
        exit 1
        ;;
esac

# Fallthrough if work done.
exit 0

root@dlp:~#
chmod 755 /etc/init.d/pend

root@dlp:~#
/etc/init.d/pend start

[3] Change Apache2 settings on Backend Web servers to record logs of X-Forwarded-For.
root@node01:~#
a2enmod remoteip

Enabling module remoteip.
To activate the new configuration, you need to run:
  service apache2 restart

root@node01:~#
vi /etc/apache2/apache2.conf
# line 206-209: change like follows

# specify Pen server's IP address for RemoteIPInternalProxy

RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 10.0.0.30

LogFormat "%v:%p
%a
%l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "
%a
%l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
root@node01:~#
/etc/init.d/apache2 restart

 * Restarting web server apache2
   ...done.
[4] Make sure it works fine to access to the frontend server from a Client with HTTP like follows.
Matched Content