Home » Apache, Featured, Tomcat

Apache, Tomcat cluster, load balancing using mod_jk

14 February 2008 7,509 views 2 Comments

[print_link]

Introduction

This article discusses how to install and configure Apache 2.0.x and Tomcat (or JBoss) to communicate with each other via mod_jk. This article also discusses how to setup a Tomcat cluster using mod_jk to balance work load.

Install JDK

  1. Download JDK rpm and launch the executable by running the following commands:

    chmod a+x j2sdk-1_4_2_<version number>-linux-i586-rpm.bin
    ./j2sdk-1_4_2_<version number>-linux-i586-rpm.bin
  2. Type yes and agree to the binary license agreement.
  3. Become root by running su and entering root password
  4. Install the rpm. This will install JDK at /usr/java/j2sdk-1.4.2:

    rpm -iv jdk-1.4.2_<version number>.i386.rpm
  5. Edit PATH environment variable to include bin directory of JDK by adding the following lines to /etc/profile:

    JAVA_HOME=/usr/java/j2sdk-1.4.2_<version number>
    export JAVA_HOME
    PATH=$JAVA_HOME/bin:$PATH

Avoid Firewall and Routing issues

  1. Disable the default firewall (unless you can configure it to allow multicast packets)

    /etc/init.d/iptables stop
    /etc/init.d/ip6tables stop
  2. Add this multicast route:

    /sbin/route add -net 224.0.0.0 netmask 240.0.0.0 dev eth<id>

Install Apache

  1. Download httpd-2.0.50-i686-pc-linux-gnu.tar.gz into a temporary location and run the following commands to install Apache at /usr/local/share:

    unzip < httpd-2.0.50-i686-pc-linux-gnu.tar.gz | tar xvf -
    cd httpd-2.0.50
    ./install-bindist.sh /usr/local/share/apache-2.0.50
  2. Edit PATH environment variable to include bin directory of Apache by adding the following lines to /etc/profile:

    APACHE_HOME=/usr/local/share/apache-2.0.50
    export APACHE_HOME
    PATH=$APACHE_HOME/bin:$PATH
    export PATH

Install Tomcat

  1. Move apache-tomcat-x.y.z.zip.zip to /usr/local/share (I will refer to the installation folder by this path) and unzip it:

    unzip apache-tomcat-x.y.z.zip
  2. Edit PATH environment variable to include bin directory of Apache by adding the following lines to /etc/profile:

    TOMCAT_HOME=/usr/local/share/apache-tomcat-x.y.z
    export TOMCAT_HOME
    PATH=$TOMCAT_HOME/bin:$PATH
    export PATH

Configure mod_jk

  1. Copy the mod_jk binary to $APACHE_HOME/modules directory and rename it as mod_jk.so
  2. Add the following lines at the very bottom in $APACHE_HOME/conf/httpd.conf

    #Include mod_jk configuration file
    Include conf/mod-jk.conf
  3. Under $APACHE_HOME/conf create mod-jk.conf and write the following lines in it:

    # Load mod_jk module
    # Specify the filename of the mod_jk lib
    LoadModule jk_module modules/mod_jk.so
    # Where to find workers.properties
    JkWorkersFile conf/workers.properties
    # Where to put jk logs
    JkLogFile logs/mod_jk.log
    # Set the jk log level [debug/error/info]
    JkLogLevel info
    # Select the log format
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
    # JkOptions indicates to send SSK KEY SIZE
    JkOptions +ForwardKeySize +ForwardURICompat -
    ForwardDirectories
    # JkRequestLogFormat
    JkRequestLogFormat "%w %V %T"
    JkMount /Context/* loadbalancer



    Where Context is the web-app context name.

  4. Under $APACHE_HOME/conf create workers.properties and populate it as follows:

    # Worker list
    worker.list=loadbalancer
    # Define Node1
    worker.node1.port=8009
    worker.node1.host=node1.ip.address
    worker.node1.type=ajp13
    worker.node1.lbfactor=1
    #worker.node1.local_worker=1 (1)
    worker.node1.cachesize=10
    # Define Node2
    worker.node2.port=8009
    worker.node2.host=node2.ip.address
    worker.node2.type=ajp13
    worker.node2.lbfactor=1
    #worker.node2.local_worker=1 (1)
    worker.node2.cachesize=10
    # Load-balancer
    worker.loadbalancer.type=lb
    worker.loadbalancer.balanced_workers=node1, node2
    worker.loadbalancer.sticky_session=1
    worker.loadbalancer.local_worker_only=1

    Add one worker per node and name workers as node1, node2, node3 … nodeN. Don’t forget to define all these as load balancing workers:

    worker.loadbalancer.balanced_workers=node1, node2, node3, ... nodeN

    Replace node1.ip.address with IPAddress of Tomcat(or JBoss) instance1 and so on. The attribute sticky_session=1 tells mod_jk that SESSION ID’s should be routed back to the same Tomcat worker. The lbfactor determines the number of requests a worker receives. The higher the lbfactor the more requests a worker receives.

  5. Each node should be named according to the names specified in workers.properties. On each Tomcat instance, edit $TOMCAT_HOME/conf/server.xml. Locate the <Engine> element and add an attribute called jvmRoute:

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="node1"></Engine>

NOTE: THE jvmRoute ATTRIBUTE MUST MATCH THE NAME SPECIFIED IN workers.properties.

Start Apache and Tomcat

  1. Start Apache:

    $APACHE_HOME/bin/apachectl -k start
  2. To stop use:

    $APACHE_HOME/bin/apachectl -k stop
  3. Start Tomcat:

    $TOMCAT_HOME/bin/startup.sh
  4. Stop Tomcat:

    $TOMCAT_HOME/bin/shutdown.sh

2 Comments »

  • Ranjan Kumar said:

    Nice article. I had no idea of /etc/profile file before reading this entry. I was only aware of the .bash_profile in the user’s directory. So I had no idea how to have a single Java installation and the environment entries at a single place (/etc/profile) for all the users.

  • Dinesh Gupta said:

    Hi,
    Very good article. It is in simpler format to set up mod-jk.
    Please tell me how to integrate SSL with the Apache mod-jk & Jboss.

    I am unable to set up SSL with Jboss apache

    Please Help.

    Regards,
    Dinesh

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.