Thursday, August 7, 2014

Apache Load Balancer Module with Tomcat

Load Balancing is one of the important Aspect when running production servers. Load balancing provides many additional benefits in the production environment. In this article we will see how we can configure Apache Http Server to load balance requests that are going to the back end Tomcat server. For the demo purpose I will have multiple tomcat instances with the same application running on the context and configure apache to load balance requests for the application from Apache server.

1. Make sure to load module ion the httpd.conf file like
LoadModule proxy_module modules/
LoadModule proxy_balancer_module modules/
LoadModule proxy_ftp_module modules/
LoadModule proxy_http_module modules/
LoadModule proxy_ajp_module modules/

2. Now Configure the Balancer for the Apache Http server like,

<IfModule proxy_module>
ProxyRequests Off
ProxyPass /myApp balancer://mycluster stickysession=JSESSIONID
ProxyPassReverse /myApp balancer://mycluster stickysession=JSESSIONID

<Proxy balancer://mycluster>
BalancerMember http://localhost:8080/myApp route=jvm1
BalancerMember http://localhost:18080/myApp route=jvm2


In the above configuration we have defined our web heads and declaring how they will be balanced. The BalancerMember is how we declare our web heads,

All of this will be wrapped in <Proxy> tags which is how apache knows to send it to mod_proxy, the "balancer://mycluster" identifier is only an identifier, you could technically call it what you want as long as you put the "balancer://" prefix

3. Configure the Back end Tomcat instances. Since we have to test the load balancing, I have configured 2 tomcat instances with port: 8080 and 18080. We also need to change the JVM route to jvm1 and jvm2 respectively.

Deploy the application on both tomcat instances. In the above case I deployed a sample application with the context “myApp”.

Once the configuration is done, restart Apache Http server and the backend Tomcat instances.

We can access the application using https://localhost/myApp

Access the application multiple times and check the access logs in the tomcat instances  and we can see

Tomcat 1
0:0:0:0:0:0:0:1 - - [07/Aug/2014:17:45:46 +0530] "GET /myApp/ HTTP/1.1" 200 415

Tomcat 2
0:0:0:0:0:0:0:1 - - [07/Aug/2014:17:45:04 +0530] "GET /myApp/ HTTP/1.1" 200 41

We can see from the above access logs ,that the request is being processed by both tomcat instances

Optional Step

This is a tool packaged with the mod_proxy_balancer tool, and allows you to make configurations from a gui tool through the web browser. Viewable at "", keep in mind that these changes die after you restart apache. I won't go over how to use this tool, but it is available to you.For this to work we need to add the below statements to http.conf file

<Location /balancer-manager>
   SetHandler balancer-manager

More to Come ,Happy Learning

No comments :

Post a Comment