Pages

Wednesday, August 3, 2011

Load Balancing Using Apache mod_jk and JBoss

Share it Please

For a heavily loaded application deployed in an application server or a web server and maintaining multiple nodes of server, there is a need for load balancing the requests coming to the nodes. This allows balancing the requests and provides a faster response time.

In This article we will see how we can configure load balance using Apache Web Server and JBoss application servers. We will write a sample web application for the testing purpose. 
Load balancing involves using a basic load distribution algorithm or more advanced ones that distribute load to the servers, keeping track of the available nodes and much other functionality. 
For this article we will use mod_jk.so ,Apache Http Server 2.0 and JBoss 5.
In Configuring a Load Balancer, the following steps are followed.
  1. Configure the Apache Http server configuration file
  2. Configure workers.properties file
  3. Configure JBoss web Server.xml file
  4. Develop a Simple Web application

1.    Configure the Apache Http server configuration file
The First step is to configure the apache http configuration file, go to the location /etc/httpd/conf/.Open the file httpd.conf.
Add the following lines to the httpd.conf file below the LoadModule directives,

#
# Mod_Jk Configuration
#

LoadModule jk_module modules/mod_jk.so

<IfModule mod_jk.c>
    JkWorkersFile conf/workers.properties
    JkLogFile logs/mod_jk.log
    JkLogLevel error
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
    JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
    JkRequestLogFormat "%w %V %T"

    JkMount /*.jsp loadbalancer
    JkMount /servlet/* loadbalancer

</IfModule>

#
# Done
#

Let see one by one

LoadModule jk_module modules/mod_jk.so
This directive says to apache to load the mod_jk.so module from the modules dicrectory.

JkWorkersFile conf/workers.properties
we must inform the mod_jk where the worker.properties files are located by using the JKWorkersFile directive.

JkLogFile logs/mod_jk.log
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
   
The above 3 directives says about the log level, location to place the log and time stamp format in the log level.

JKMount option allows us to assign specific url to the instances like,

JkMount /*.jsp loadbalancer

# send all requests ending /servlet to loadbalancer
JkMount /servlet/* loadbalancer

# send all requests jsp requests to files located in /otherworker will go worker2
JkMount /otherworker/*.jsp worker2

2. Configure workers.properties file
The next step is to configure the worker.properties file.create a worker.properties file in /conf directory like,
#
#Worker.Properties
#
worker.list=web1, web2, loadbalancer
#
#First Server Details
#
worker.web1.port=11009
worker.web1.host= 183.83.15.120
worker.web1.type=ajp13
#Specify the size of connection Cache
#worker.web1.cachesize
#specifies the Load Balance Factor When Used with loadbalance worker
worker.web1.lbfactor=100
#
#Second Server Details
#
worker.web1.port=12009
worker.web1.host= 183.83.15.150
worker.web1.type=ajp13
#Specify the size of connection Cache
#worker.web1.cachesize
#specifies the Load Balance Factor When Used with load balance worker
worker.web1.lbfactor=100
#
#LoadBalancer Worker
#
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=web1, web2
#
#End Of the Properties File
#
The directives are pretty much explanatory. I have given the comments and they are easy to understand.

3. Configure the JBoss server.xml file
The next step is to configure the server.xml file in the JBoss directory.Go to the location /server/deploy/jbossweb.sar/ and modify the 2 lines in the server.xml file.The lines to modify are 

<Connector port="11009" protocol="AJP/1.3" redirectPort="8443" /> 

The port number is changed according to the port that we specified in the worker.porperties file (11009)

<Engine name="Catalina" defaultHost="localhost" jvmRoute="web1">

The second line to change is the above one, we need to change the defaultHost to 183.83.15.120 and the jvmRoute to “web1” as we defined in the worker.properties.

DO the same changes in the second server as specified in the worker.properties file (12009 as ajp port, defaulthost to183.83.15.150 and jvmRoute to web2)

Once the changes are done, make sure you restart the servers.

4. Write a sample application

For testing purpose, we write a sample web application with a index.jsp file in it.the contents of the jsp file looks like this

For the first server, we have this bgcolor=red

<body bgcolor="red">
          <center>
                   <%= request.getSession().getId() %>
                   <h1>Web 1</h1>
</body>

For the Second server, we have the bgcolor=blue. In this way we can make sure that if apache is load balancing the requests, we see different colors in different browsers like this,



















 More articles to come.Happy Coding

1 comment :

  1. I blog often and I really appreciate your content.
    The article has truly peaked my interest. I am going to take a note of your site and keep checking for new information about once a week.
    I subscribed to your RSS feed as well.

    Feel free to surf to my site; Animas Online Hack

    ReplyDelete