Apache Tomcat

How to Set up Load Balancing for Apache Tomcat

Load balancing refers to distributing tasks to a set of resources to reduce the heavy workload on a single resource. Think of a load balancer as a traffic cop that manages the traffic from the clients and distributes it across multiple servers.

Load balancing enhances performance and ensures the servers do not go down due to heavy workload. It can also help manage the traffic if one of the resources is down.
This tutorial will show you how to set up a load balancer on Apache Tomcat using the Apache HTTP server.

NOTE: Before we proceed, ensure you have Apache Tomcat and Apache HTTPD installed and working correctly. Check out our tutorial on the topics to learn more.

Step 1 – Download mod_jk

In this tutorial, we will use the mod_jkk module to implement load balancing for the Tomcat server. The mod_jk is an apache module used to provide a cluster for load balancing and proxy features.

You will need to install the module separately because it is not packaged in the Apache HTTP.
Open your browser and navigate to:

https://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/

Before we install the mod_jk module, ensure both Apache Tomcat and HTTPD are running correctly.

Step 2 – Installing mod_jk

Once you have the mod_jk module downloaded, the next step is to install it on the Apache server.

Start by extracting the archive. Next, select the mod_jk.so file and copy to the modules directory in the HTTPD root directory.

Step 3 – Add Module to HTTP configuration

Once we have added the mod_jk module to the modules directory of the Apache HTTPD, we need to load it by editing the httpd.conf file.

In the conf directory, edit the httpd.conf file using your favorite text editor:

Next, add the following entry in the httpd.conf file to load the mod_jk module.

In the next step, we need to configure the mod_jk module in the httpd.conf file. Consider the entries shown below:

# Path to the worker config file
JkWorkersFile C:\httpd\conf\workers.properties
# Logging and Memory
JkShmFile     C:\httpd\log\mod_jk.shm
JkLogFile     C:\httpd\log\mod_jk.log
JkLogLevel    info
# Monitoring
JkMount  /stat/*  stat
JkMount  /*  balancer

In the JkWorkersFile, we define the path to the Tomcat workers:

  • JkShmFile – defines the path to the mod_jk shared memory file.
  • JkLogFile – the mod_jk log file.
  • JkLogLevel – sets the mod_jk log level.
  • JkMount – maps worker to the status worker
  • JkMount – maps tomcat load balancer.

Step 4 – Set up Cluster Workers

In this step, we need to configure the workers. Workers refer to the Tomcat servers used to process requests.

Open the Apache HTTPD installation directory and navigate to the conf directory. Create a worker.properties file.

NOTE: Ensure the path of the workers.properties file is similar to one set in the httpd.conf directory.

Inside the workers.properties file, enter the entries shown below:

worker.list=stat
worker.jk-status.type=status
worker.jk-status.read_only=true

worker.tomcat_1.type=ajp13
worker.tomcat_1.port=9001
worker.tomcat_1.host=127.0.0.1

worker.tomcat_2.type=ajp13
worker.tomcat_2.port=9002
worker.tomcat_2.host=127.0.0.1

worker.tomcat_3.type=ajp13
worker.tomcat_3.port=9003
worker.tomcat_3.host=1270.0.0.1

worker.list=balancer
worker.balancer.type=lb
worker.balancer.balance_workers=tomcat_1,tomcat_2,tomcat_3

The following are the properties in the worker’s file and their respective purpose:

  1. worker.list=stat – specifies the status worker to the list of workers.
  2. worker.jk_status.type – defines worker using status.
  3. worker.jk_status.read_only – sets status read-only.
  4. worker.tomcat_1.type – defines connector for the tomcat instance. This is similar in all the workers 1, 2, 3, and n.
  5. worker.tomcat_1.port – sets the port for each worker.
  6. worker.tomcat1.host – defines host for each worker.
  7. worker.list=balancer – defines load balance worker which to distribute.
  8. worker.balancer.balance_workers – adds all the defined workers to the load balancer “balancer.”

Finally, save the files and restart the servers.

Conclusion

This guide covered how to set up load balancing for the Apache Tomcat and HTTPD servers. To learn more, check out the load balancing documentation.
https://tomcat.apache.org/connectors-doc/common_howto/loadbalancers.html
Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list