Home > General > Load balancing JBoss and Apache2 / mod_jk

Load balancing JBoss and Apache2 / mod_jk

February 9, 2011


Having more and more needs to deploy high load and critical applications, clustering JBoss in a load balancing mode is becoming a priority.

I will discuss a good old way to use mod_jk on Apache2 to balance the load on multiple JBoss servers, there’s also different newish approaches using mod_proxy or mod_cluster from JBoss that i will discuss in future posts.

So let’s start installing JBoss AS, you have to download it from the official site here, i’ve taken the last 6.x release (6.0.0.M5) to the date i’m writing this post, anyway you can even take a 5.x release as it has no difference for what we’re going to make later.

After downloading, put the file into your /opt folder and unpack it, you’ll get a /opt/jboss-, we’ll try to run the JBoss AS server and check if it runs out of the box:

# cd /opt/jboss-
# sudo ./run.sh

Wait some seconds, you shouldn’t have errors till you get the last line looking like this: Started in 51s:29ms.

Everything seems okay, now we’ll start the second JBoss server, all we have to do is to copy the jboss- to another server (i use virtual nodes over Xen hypervisor) and start it.

You can go with one server and install two instances of JBoss AS on it, but dont forget to change the listening ports of the second instance to avoid this kind of errors: java.lang.Exception: Port 8080 already in use.

Anyway, whatever configuration you’ve chosen, we should end with an architecture like this:

Before going to put the load balancer on top of these two nodes (tux1 & tux2), make sure your JBoss AS is working by pointing your browser to the JMX console link on the http://host:8080 page.

Everything is okay ? let’s go to Apache:

I’ll take the latest stable version of Apache2, i recommend you to install it from your package manager (Ubuntu’s Synaptic in my cas) directly rather than compiling it from the source, life is too short.

In order to install the mod_jk module, you have to download the binary version that fits to your system architecture and copy the mod_jk*.so file to /usr/lib/apache2/modules/mod_jk.so and run this commands:

echo "LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so" > /etc/apache2/mods-available/jk.load
touch /etc/apache2/mods-available/jk.conf
touch /etc/apache2/workers.conf
a2enmod jk

You should have no errors when running the above commands, that means the mod_jk is now enabled and we’ll proceed to configuring it to balance the load between our two JBoss servers.

By the way, I’ve installed Apache2 for load balancing purpose and i’ve chosen to run it on tux1 node, this is not a wise decision since we created a SPOF node: if tux1 goes down, the whole cluster will go down.
This can be resolved by putting Apache2 in a fail over mode so when tux1 goes down, tux2 will take the job and run the load balancer flawlessly making the solution highly available, at Tritux we use many high availability solutions including Red Hat Cluster Suite which has proven its enterprise class grade service level with many of our deployments.
I will not cover this part assuming we want only to bring life to a simple load balancing Apache2 configuration, you can contact us for commercial support on how to design a high availability solution with JBoss or any other web server.
Below is what it should look like when going with a high availability solution:

Let’s return to the configuration part, first we’ll edit the /etc/apache2/workers.conf file this way

# Defining the workers list:
# first worker properties, we use the AJB13 connection type:
# second worker properties, we use the AJB13 connection type:
# No we set the load balancing config

And we’ll edit the /etc/apache2/mods-available/jk.conf file this way

    # The Jk shared mem location
    JkShmFile /var/log/apache2/mod_jk.shm

    # Jk logs
    JkLogFile /var/log/apache2/mod_jk.log
    # Jk loglevel
    JkLogLevel info
    # Jk logformat
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

    # Our workers config
    JkWorkersFile /etc/apache2/workers.conf

    # The most important:
    # We will send eveything (/*) to our loadbalancer (set in JkWorkersFile)
    JkMount /* loadbalancer

Restart your Apache2, everything is okay ? excellent ! you have a load balancer working !

Open your browser at http://tux1-host:80 (80 is Apache2’s port) and you’ll get the JBoss page in place of the Apache2 page, what happened ? your request is sent to one of the JBoss servers we configured !

Now you can deploy your application on both JBoss servers and test how the load is balanced between the two nodes, you can test how the balancer will detect failure on one node and keep servicing your application with no interruption …

More to do:
You can setup as many workers as you have to get N nodes, the failover can be set for a preferred active node or even to a standby node.
The configuration of Apache2 and JBoss servers is not tuned enough for going to production, there’s a lot to do before deciding to put a cluster like this in a production environment.

Taken from ::http://www.tritux.com/blog/2010/10/23/load-balancing-jboss-and-apache2-mod_jk/8/1

Categories: General
%d bloggers like this: