Tuesday, March 18, 2008

Automate Apache Tomcat Clusters

The following script is a handy tool which I use to install an apache instance with mod_jk module and several tomcat instances which are used as a loadbalancing setup. After executing this script all you have to do is start the individual servers and everything should work.

The code


#!/bin/bash
################################################################################
# This script is to automate a typical apache tomcat loadbalanced setup
# Author: Tharun Kumar Allu
# Email : tharun_DOT_allu_AT_gmail.com
################################################################################
# Setup all the variables properly
# Change the source to your choice before executing the script
# The only requirement in this script is they have to be tar.gz
# if you have other formats then change the basename directive below to reflect
# that
APACHE_SOURCE="http://apache.seekmeup.com/httpd/httpd-2.2.8.tar.gz"
TOMCAT_BINARY="http://www.uniontransit.com/apache/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.tar.gz"
MODJK_SOURCE="http://www.apache.org/dist/tomcat/tomcat-connectors/jk/source/jk-1.2.26/tomcat-connectors-1.2.26-src.tar.gz"
# Number of Tomcat clusters that you want to be installed
NUM_CLUSTERS=2
# Apache parameters to ./configure script enable more modules you might require
# here
APACHE_CONFIG=" --enable-so --enable-jk"

# Path where all the applications are installed if this path is not accessable
# by the user executing this script then prefix su - to 'make install' for
# apache and mod_jk
INSTALL_PREFIX="/path/to/your/install/directory"

# Set the prefix of the ports that are used by Tomcat here for example port 8080
# is changed to 12080 for cluster 0 and 13080 and so on for the other clusters

TC_PORT_PREFIX_ORIG=12

################################################################################
OLD_PWD=`pwd`

mkdir temp$$

cd temp$$

wget $APACHE_SOURCE
wget $TOMCAT_BINARY
wget $MODJK_SOURCE

tar zxvf `basename $APACHE_SOURCE` >/dev/null
cd `basename $APACHE_SOURCE .tar.gz`

./configure --prefix=$INSTALL_PREFIX/apache/ $APACHE_CONFIG
make
# prefix su - to the following command if current user does not have write
# access to $INSTALL_PREFIX
make install
cd ..
pwd

# Installing tomcat clusters and changing server.xml

TC_PORT_PREFIX=$TC_PORT_PREFIX_ORIG

for ((i=0;i<$NUM_CLUSTERS;i++));do tar zxvf `basename $TOMCAT_BINARY` >/dev/null
mv `basename $TOMCAT_BINARY .tar.gz` $INSTALL_PREFIX/tomcat-cluster-$i
mv $INSTALL_PREFIX/tomcat-cluster-$i/conf/server.xml $INSTALL_PREFIX/tomcat-cluster-$i/conf/server.xml.orig
cat $INSTALL_PREFIX/tomcat-cluster-$i/conf/server.xml.orig | sed s/"Engine name=\"Catalina\""/"Engine name=\"Catalina\" jvmRoute=\"cluster-"$i"\""/ | sed s/8005/"$TC_PORT_PREFIX"005/ | sed s/8080/"$TC_PORT_PREFIX"080/ | sed s/8009/"$TC_PORT_PREFIX"009/ >$INSTALL_PREFIX/tomcat-cluster-$i/conf/server.xml
TC_PORT_PREFIX=`expr $TC_PORT_PREFIX + 1`;
done

# Compiling and installing mod_jk.so
tar zxvf `basename $MODJK_SOURCE`
cd `basename $MODJK_SOURCE .tar.gz`
cd native
./configure --with-apxs=$INSTALL_PREFIX/apache/bin/apxs
make
# prefix su - to the following command if current user does not have write
# access to $INSTALL_PREFIX
make install

cd $OLD_PWD

rm -rf temp$$

touch $INSTALL_PREFIX/apache/conf/workers.properties

# Adding JK related entries to apache httpd.conf
echo "JkWorkersFile \"conf/workers.properties\"" >> $INSTALL_PREFIX/apache/conf/httpd.conf
echo "JkLogFile \"logs/mod_jk.log\"" >>$INSTALL_PREFIX/apache/conf/httpd.conf

echo "JkLogLevel info" >>$INSTALL_PREFIX/apache/conf/httpd.conf

echo "JkMount /jkstatus* jkstatus" >> $INSTALL_PREFIX/apache/conf/httpd.conf

#Building the loadbalancer worker.properties file

echo "worker.list=loadbalancer,jkstatus" >> $INSTALL_PREFIX/apache/conf/workers.properties

TC_PORT_PREFIX=$TC_PORT_PREFIX_ORIG

for ((i=0;i<$NUM_CLUSTERS;i++));do echo "worker.type=ajp13" >> $INSTALL_PREFIX/apache/conf/workers.properties
TEMP_X=`hostname`;
HOST=`host $TEMP_X | awk '{print $4}'`
echo "worker.cluster-$i.host=$HOST" >> $INSTALL_PREFIX/apache/conf/workers.properties
echo "worker.cluster-$i.port=$TC_PORT_PREFIX"009 >> $INSTALL_PREFIX/apache/conf/workers.properties
echo "worker.cluster-$i.lbfactor=1" >> $INSTALL_PREFIX/apache/conf/workers.properties
BALANCED_WORKERS=$BALANCED_WORKERS,cluster-$i
TC_PORT_PREFIX=`expr $TC_PORT_PREFIX + 1`;
done

echo "worker.loadbalancer.balance_workers=$BALANCED_WORKERS" >> $INSTALL_PREFIX/apache/conf/workers.properties
echo "worker.jkstatus.type=status" >> $INSTALL_PREFIX/apache/conf/workers.properties

mv $INSTALL_PREFIX/apache/conf/httpd.conf $INSTALL_PREFIX/apache/conf/httpd.conf.orig

sed s/"Listen 80"/"Listen 80\n\n\nLoadModule modules\/mod_jk.so\n"/ $INSTALL_PREFIX/apache/conf/httpd.conf.orig > $INSTALL_PREFIX/apache/conf/httpd.conf

No comments: