Load balancing tomcat with Apache

In some circumstances, it is mandatory to deploy your application on multiple tomcat instances instead of a single one, this usually happens when working with a heavy application which consumes a big amount of memory storage and has a big number of active users. The main advantage of using multiple instances is to distribute the requests and the load on multiple nodes, to provide high availability and to speed up serving requests.

Load balancing is the process of distributing client requests to multiple available nodes , normally the application is exposed to the clients through a web server which receives the requests and distributes them to the configured application servers based on the availability and strength of each one. In this article, we describe how to configure a simple tomcat load balancer through an apache web server.

Apache Load Balancer

1. Installing tomcat instances

Install 2 tomcat instances on 2 different windows machines using this tutorial. Normally it’s okay to configure multiple tomcat instances on the same machine, but it is always recommended to install each instance on a separate machine so that you distribute the load to 2 physical memories instead of 1.

2. Download and install Apache

Download the latest stable Apache version from here, you can set up Apache on one of tomcat machines or dedicate for it a separate machine.

Download Apache

Unzip the downloaded file into D: drive and edit httpd.conf under conf folder through setting SRVROOT variable as the following:

Define SRVROOT "D:\Apache24"

To install Apache, run cmd as administrator and change your location to D:\Apache24\bin and execute the following command:

httpd -k install

Now that Apache web server is successfully installed on your machine.

To start Apache, run as administrator the following command under D:\Apache24\bin:

sc start Apache2.4

By default apache runs on port 80, to check if it starts successfully, try localhost on any browser and you’ll get the following screen:

Apache runs successfully

 3. Download mod_jk module

Download mod_jk module from here, this plugin is responsible for the communication between Apache and tomcat.

Download mod_jk module

Extract mod_jk zip file and copy mod_jk.so into D:Apache24\modules.

4. Configure Apache load balancer

Edit D:Apahe24\conf\http.conf as below:

  • add the following at the end of LoadModule directives section:
    LoadModule jk_module modules/mod_jk.so
  • add the following just before the end of the file:
    JkWorkersFile conf/workers.properties
    JkLogFile logs/mod_jk.log
    JkLogLevel info
    # JkRequestLogFormat
    JkRequestLogFormat "%w %R %U %T"
    
    JkMount /* loadbalancer
    JkMount / loadbalancer

workers.properties

Create workers.properties file under conf directory, this file defines the different tomcat instances to be load balanced, in addition to a load balance worker which manage the load balancing process. Below is the content of worker.properties:

# Define the name of the load balancer worker, here it's name is loadbalancer
worker.list=loadbalancer

# Define the properties of first tomcat worker named tomcat1
worker.tomcat1.port=8008
worker.tomcat1.host=<ip>
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1

# Define the properties of second tomcat worker named tomcat2  
worker.tomcat2.port=8009
worker.tomcat2.host=<ip>
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1

# Defines the properties of load balancer worker, and the different workers which manage.
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcat1, tomcat2
  • worker.<workerName>.port: Defines the ajp port which tomcat instance runs onto.
  • worker.<workerName>.host: The ip address or host name of the tomcat machine.
  • worker.<workerName>.type: The type of worker, in this example we use 2 types of workers
    • ajp13: This worker communicates directly with a tomcat instance.
    • lb: a load balancer worker which manage and load balance several ajp13 workers.
  • worker.<workerName>.lbfactor: Define the strength of the tomcat node, the load balancer worker will take this value into consideration when forwarding requests to tomcat nodes.
  • worker.loadbalancer.balance_workers: Define the name of ajp13 workers to be managed by the load balancer.

5. Configure tomcat instances

Modify the startup configuration of each tomcat instance in order to match the corresponding worker properties in Apache.

Edit server.xml under TOMCAT_HOME/conf for tomcat1 as the following:

<!-- Define the http port of tomcat -->
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" 
redirectPort="8443" URIEncoding = "utf-8"/>

<!-- Define the ajp port of tomcat, this port should match the one 
in workers.properties -->
<Connector port="8008" protocol="AJP/1.3" redirectPort="8443"   
URIEncoding = "utf-8"/>

<!-- Define the jvm routing of tomcat, this routes to tomcat1 -->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
	

Edit server.xml under TOMCAT_HOME/conf for tomcat2 as the following:

<!-- Define the http port of tomcat -->
<Connector port="9090" protocol="HTTP/1.1" connectionTimeout="20000" 
redirectPort="8143" URIEncoding = "utf-8"/>

<!-- Define the ajp port of tomcat, this port should match the one 
in workers.properties -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8143"   
URIEncoding = "utf-8"/>

<!-- Define the jvm routing of tomcat, this routes to tomcat2 -->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

After restarting tomcat nodes and Apache server, the load balance is set up successfully and Apache is ready to consume requests and distribute them on tomcat1 and tomcat2.

P.S The application is now accessed through Apache host name and port.

 

0 0 votes
Article Rating

Hussein Terek

Owner of programmergate.com, I have a passion for software engineering and everything related to Java environment.

guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Paras Chawla
Paras Chawla
3 years ago

crisp and informative !! Thanks so much .
Now i got to know actual usage of AJP protocol.

Bryce Matheson
Bryce Matheson
3 years ago

In the tomcat2 server.xml file, does redirectPort actually need to be set to “8143” in both places, as opposed to “8443” like in server.xml for tomcat1? Or is this a typo? I’m wondering why it’s not set to 8443 for both servers.

Please explain. Thanks.

igorweiss
igorweiss
2 years ago

Hello,

is it possible to configure tomcat for multiple apache httpd as well? (n http to m tomcat instances)?