in

Load Balancing your JBoss App Servers with Apache

- - 3 comments
If you have your web application deployed on an application server and you need it to be online with a good uptime you should know that applications servers are not always very reliable. That's why it is a good idea to run two or more application servers and load balance them so that you can share the load, thereby reducing the dependence on one server and also handling the load better.

A single instance of the Apache web server can handle a large number of simple requests for files to be served quite well. However, a single instance of an application server such as JBoss can handle far fewer requests for applications to be run. There's a big difference between just handing someone a piece of data and having to work out something complicated to answer a question. So we'll use the Apache web server as a load balancer between two instances of JBoss, running the same application.

I'll assume that you have three servers; real server or virtual servers are fine. You will need to run your JBoss instances on two of these servers. You can alternatively run both instances of JBoss on the same server. However, make sure that they are both operating on different ports and not the same one. The default port on which JBoss run is 8080. Once both of your instances are up and running run a simple test to make sure things are running fine on both server. Launch your web browser and go to http://host:8080. I'll assume that the IP addresses of the two JBoss servers are 192.168.0.10 and 192.168.0.20.

Now we'll setup Apache. You can use the standard installation of Apache that shipped with your Linux distribution, or install it using a standard binary. We will be using a specific Apache module, mod_jk, to do the load balancing. This module does not usually ship with a copy of Apache and would need to be downloaded and setup separately. Installing mod_jk is pretty straightforward:

1. Download the module from http://apache.inetbridge.net//tomcat/tomcat-connectors/jk/binaries/.

2. Copy the .so file into /usr/lib/apache2/modules/mod_jk.so. This location may differ from distribution to distribution.
3. Now execute the following "# echo LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so" > /etc/apache2/mods-available/jk.load".
4. And "# touch /etc/apache2/mods-available/jk.conf".
5. And "# touch /etc/apache2/workers.conf".
6. "# a2enmod jk".
There you go. mod_jk should now be all setup. Just make sure you don't receive any errors while executing any of the steps shown above. I'll assume you have the web server running on 192.168.0.1:80. so our setup looks something like the following:

Apache Web Server: 192.168.0.1:80 - lb1
JBoss App Server 1: 192.168.0.10:8080 - app1
JBoss App Server 2: 192.168.0.20:8080 - app2
Next create the file /etc/apache2/workers.conf in the lb1 machine. Enter the following content into it:

# Defining the workers list:
worker.list=loadbalancer,status
# first worker properties, we use the AJB13 connection type:
worker.worker1.type=ajp13
worker.worker1.connection_pool_size=20
worker.worker1.host=app1-host
worker.worker1.port=8080
worker.worker1.lbfactor=1
# second worker properties, we use the AJB13 connection type:
worker.worker2.type=ajp13
worker.worker2.connection_pool_size=20
worker.worker2.host=app2-host
worker.worker2.port=8080
worker.worker2.lbfactor=1
# No we set the load balancing config
worker.loadbalancer.type=lb
worker.loadbalancer.sticky_session=true
worker.loadbalancer.balance_workers=worker1,worker2
worker.status.type=status
Replace 'app1' and 'app2' with the names of your app server machines. Next up edit or create the file '/etc/apache2/mods-available/jk.conf' to look like this:

<IfModule mod_jk.c>
# 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
</IfModule>
Restart your Apache web server with the command "# service httpd restart" or whatever works for your distribution. Now if you hit "192.168.0.1" or lb1 in your web browser you should be able to see the results from one or the other app server. You should put in unique pages on each server to make sure it is the case. We have chosen two app servers in this case. However, this is not a limitation. You can choose to have more servers if you like.


Sukrit Dhandhania wrote this article for TechSource

3 comments

  1. You should take a look at http://www.jboss.org/mod_cluster for doing more intelligent load balancing.

    ReplyDelete
  2. Samuel MendenhallMarch 24, 2011

    You are missing some key properties like ping_mode=A. Generate an optimized config here: http://lbconfig.appspot.com

    ReplyDelete
  3. Using the HTTP connector (on JBoss) to listen to AJP/1.3 requests will not work. You need to first enable an AJP/1.3 connector on JBoss using a different port than 8080 and configure the workers.properties to use the new port. See the links below as a reference. Other than this, nice article.

    http://docs.jboss.org/jbossweb/latest/config/http.html
    http://docs.jboss.org/jbossweb/latest/config/ajp.html

    ReplyDelete