Friday, September 21, 2012

Web server Load Balance Pen



Install Pen to configure Load Balance server. Pen is a light weight simple load balancer. This example shows to configure on the environment like follows.
        (1)  gw.horoppa.net         [10.0.0.50]  -  Pen Server
        (2)  www01.horoppa.net   [10.0.0.51]  -  Web Server#1
        (3)  www02.horoppa.net   [10.0.0.52]  -  Web Server#2
[1] Install and Configure Pen
[root@gw ~]# 
yum --enablerepo=epel -y install pen 
  
# install from EPEL
[root@gw ~]# 
vi /etc/pen.conf
# create new

# log file

LOGFILE=/var/log/pen.log
# output file of status

WEBFILE=/var/www/pen/webstats.html
# control port

CONTROL=127.0.0.1:10080
# max connections

MAX_CONNECTIONS=500
# listen port

PORT=80
# number of backend servers

BACKEND=2
# IP address of a backend

SERVER1=10.0.0.51:80
# IP address of a backend

SERVER2=10.0.0.52:80
[root@gw ~]# 
vi /etc/rc.d/init.d/pend
# create init script

# this is an example

#!/bin/bash

# pend: Start/Stop Pend
# chkconfig: - 90 10
# description: Pen is a light weight simple load balancer.
# pidfile: /var/run/pen.pid

. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
. /etc/pen.conf

LOCKFILE="/var/lock/subsys/pen"
PID=/var/run/pen.pid
PROG=/usr/bin/pen

RETVAL=0
start() {
echo -n $"Starting Pend: "
SERVER=`grep "^SERVER" /etc/pen.conf | cut -d= -f2`
daemon $PROG -w $WEBFILE -x $MAX_CONNECTIONS -p $PID -l $LOGFILE -C $CONTROL -S $BACKEND -r $PORT $SERVER
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
echo -n $"Stopping Pend: "
killproc $PROG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $PID $LOCKFILE
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status pend
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit $?

[root@gw ~]# 
vi /etc/logrotate.d/pen
# this is an example

/var/log/pen.log {
daily
copytruncate
compress
notifempty
missingok
postrotate
/etc/rc.d/init.d/pend restart 2>&1 > /dev/null || true
endscript
}

[root@gw ~]# 
chmod 755 /etc/rc.d/init.d/pend 

[root@gw ~]# 
/etc/rc.d/init.d/pend start 

Starting Pend: [ OK ]
[root@gw ~]# 
chkconfig --add pend 

[root@gw ~]# 
chkconfig pend on 
[2]Access to the URL you set and make sure backend server answers normally like follows.
[3]Shutdown other httpd manually and make sure another httpd answers normally like follows.
[4]Configure the tool that it's possible to watch Pen's status.
[root@gw ~]# 
cp /usr/share/doc/pen-*/penstats /var/www/pen 

[root@gw ~]# 
vi /var/www/pen/penstats
# line 4: change

PIDFILE=
/var/run/pen.pid
# line 5: change

WEBFILE=
/var/www/pen/webstats.html
[root@gw ~]# 
vi /etc/httpd/conf.d/pen.conf
# change

Alias
 /pen/ /var/www/pen/
<Directory /var/www/pen/>
   DirectoryIndex penctl.cgi
   Options ExecCGI
   order deny,allow
   deny from all
   allow from 127.0.0.1 
10.0.0.0/24
   
# IP address you permit

</Directory>
[root@gw ~]# 
/etc/rc.d/init.d/httpd restart 

Stopping httpd: 
[ OK ]

Starting httpd: 
[ OK ]

[root@gw ~]# 
chmod 755 /var/www/pen/penstats 

[root@gw ~]# 
/var/www/pen/penstats > /dev/null 
  
# run

[root@gw ~]# 
# update by 5 minutes

*/5 * * * * /var/www/pen/penstats > /dev/null
[5]Access to "http://(pen's hostname or IP address):(httpd listen port)/pen/webstats.html" and make sure following site is shown normally.

0 comments: