Load Balancing – Apache / JBoss

Enviornment
OS: Linux Redhat
Load Balancer: Apache 2 (assuming it is in /usr/local/apache2)
App Server: JBoss 4x

What to do in Apache?
You need to install mod jk module in it, if not already present.
Look inside /usr/local/apache2/modules dir, if there is any file with name similar to mod_jk*.so

If not, download from:
http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.31/x86_64/mod_jk-1.2.31-httpd-2.2.x.so

And rename it to mod_jk.so and add to:
/usr/local/apache2/modules

You will be dealing with following config files typically:

  • conf/httpd.conf – you will update it with mod jk related entries
  • conf/workers.properties – you define where load balancing workers, ports and config here.
  • conf/mod-jk.properties – you define jk properties here.
  • conf/uriworkermap.prroperties – here you map the URL to ‘loadbalancer’ worker

Add conf/mod_jk.properties:
The contents of the file typically are:
JkWorkersFile conf/workers.properties
JkMountFile conf/uriworkermap.properties

Modify conf/httpd.conf so that it contains the following line:
Include conf/mod_jk.conf
LoadModule jk_module modules/mod_jk.so

Add conf/workers.properties:
Contents of workers.properties file are:

worker.list=loadbalancer

#Worker 1
worker.worker1.type=ajp13
worker.worker1.host=IPAddressOfJBossServer1
worker.worker1.port=8109
worker.worker1.lbfactor=50
worker.worker1.cachesize=10
worker.worker1.cache_timeout=600
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300

#Worker 2
worker.worker2.type=ajp13
worker.worker2.host=IPAddressOfJBossServer2
worker.worker2.port=9109
worker.worker2.lbfactor=50
worker.worker2.cachesize=10
worker.worker2.cache_timeout=600
worker.worker2.socket_keepalive=1
worker.worker2.socket_timeout=300

#Load Balancer
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker1,worker2
worker.loadbalancer.sticky_session=1

Add conf/uriworkermap.properties:  (this maps the URI with the ‘loadbancer’ worker mentioned in workers.properties file)
/myapp/=loadbalancer
/myapp/*=loadbalancer

What to do in JBoss?
We will need at least two JBoss installations. If both are on same machine, then their ports must be different, if they are on different machines, then ports can remain same ofcourse.

Usually there is nothing to do in JBoss, since all the necessary ports are pre-configured. But you can check nevertheless.

The main thing here is to know – what the JBoss JK Port is. This is configured in bindings xml file. By default the bindings file is this one:
[JBoss_Home]/docs/examples/binding-manager/Sample-Bindings.xml
Typically, if JBoss http port is 8180, JK Port is 8109 (i.e HTTPPort-71, that’s what the calculation usually is!!)

This JKPort is what we provide in workers.properties file mentioned before. Thats it.

Restart JBoss.
Restart Apache.

All Set.

Try: http://ApacheHost/YourApp
You can see that your requests are going to both the JBoss Servers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: