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:
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:
# Logging and Memory
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:
The following are the properties in the worker’s file and their respective purpose:
- worker.list=stat – specifies the status worker to the list of workers.
- worker.jk_status.type – defines worker using status.
- worker.jk_status.read_only – sets status read-only.
- worker.tomcat_1.type – defines connector for the tomcat instance. This is similar in all the workers 1, 2, 3, and n.
- worker.tomcat_1.port – sets the port for each worker.
- worker.tomcat1.host – defines host for each worker.
- worker.list=balancer – defines load balance worker which to distribute.
- worker.balancer.balance_workers – adds all the defined workers to the load balancer “balancer.”
Finally, save the files and restart the servers.
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.
Thank you for reading!