Image of Load balancing tomcat with Apache

ADVERTISEMENT

Table of Contents

Introduction

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..port: Defines the ajp port which tomcat instance runs onto.
  • worker..host: The ip address or host name of the tomcat machine.
  • worker..type: The type of worker, in this example we use 2 types of workers
  1. ajp13: This worker communicates directly with a tomcat instance.
  2. lb: a load balancer worker which manage and load balance several ajp13 workers.
  • worker..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.

Summary

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.

Next Steps

If you're interested in learning more about the basics of Java, coding, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.

Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to jacob@initialcommit.io.

Final Notes