Load Balance a Tomcat Application

45 minutes
  • 5 Learning Objectives

About this Hands-on Lab

Welcome to this hands-on lab, where you will be setting up an Apache web server as a load balancer front end to Apache Tomcat. This will also provide fail over capabilities which will be tested.

Apache’s Tomcat is a Java-based application platform built around deploying and managing Java Web Applications. The Apache web server is an application that powers many of the world’s web sites.

This hands-on lab focuses on installing and configuring the Apache web server as a load balancer/failover cluster with sticky sessions to two pre-installed instances of Tomcat.

The servers provided are running Red Hat Enterprise 8. Tomcat 9 is pre-installed on two of the servers, and available on port **8080**. You will be tasked with making Tomcat available on port **80** on the load balancer, with failover working via the Apache httpd server.

Once you have completed the task, you will need to ensure you can connect via port **80** and access the Tomcat GUI. You should check which Tomcat instance is being shown, and make sure the failover works by shutting down that instance and testing again to see that you are viewing the second instance.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Test to Ensure You Can Access the Preconfigured Tomcat Instances on Port 8080

You will have three servers. The second and third are the Tomcat servers. It may take from 3 to 10 minutes after the hands-on lab has started before you can access them. When enough time has elapsed, visit those two servers’ public IP addresses on port 8080 in a web browser.

To access them, copy the external IP addresses and test in your browser by using the IP address and appending :8080 to it.

For example, if an IP address shown was, then you would put the following into your web browser:

You should see a Tomcat GUI at each server’s public IP.

Install the Apache Web Server onto the First Provided Lab Server and Configure It as a Load Balancer to the Two Tomcat Instances

Log into the load balancer (via SSH) and install the Apache web server:

sudo dnf install httpd

sudo systemctl enable httpd

sudo systemctl start httpd

Go to a browser and test that the Apache web server is working by connecting to its IP address. You should see the default Apache web page. Now you need to configure the load balancer.

Edit the Apache web server’s configuration file. This can be found at /etc/httpd/conf/httpd.conf. If you used Vim, the command would be as follows.

sudo vim /etc/httpd/conf/httpd.conf

Jump to the end of the file, and edit above where it has the following entry:

IncludeOptional conf.d/*.conf

You can use the following as an example of what needs to be inside this configuration file (Note: Replace the TOMCAT1-INTERNAL-IP and TOMCAT2-INTERNAL-IP with the respective internal IP address of each Tomcat server):

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so

<IfModule proxy_module>
ProxyRequests on
ProxyPass /examples balancer://mycluster stickysession=JSESSIONID
ProxyPassReverse /examples balancer://mycluster stickysession=JSESSIONID

<Proxy balancer://mycluster>
BalancerMember http://TOMCAT1-INTERNAL-IP:8080/examples route=tomcat1
BalancerMember http://TOMCAT2-INTERNAL-IP:8080/examples route=tomcat2

Make Changes to the Tomcat Instances as Required, by Editing Configuration in Each’s /usr/local/tomcat9 Directory

Log into one Tomcat server with SSH, not the Tomcat GUI. Edit server.xml to make the required changes:

sudo vim /usr/local/tomcat9/conf/server.xml

Change this line:

<Engine name="Catalina" defaultHost="localhost" >

Make it read:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

Save and exit, then restart Tomcat.

systemctl restart tomcat

Now log into the second Tomcat server, again with SSH (not the Tomcat GUI). Edit server.xml to make the required changes:

sudo vim /usr/local/tomcat9/conf/server.xml

Change this line:

<Engine name="Catalina" defaultHost="localhost" >

Make it read:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

Save and exit, then restart Tomcat:

systemctl restart tomcat
Either Disable or Change SELinux so That the Load Balancing Will Work

SELinux is enabled on the hands-on lab server. You can choose to disable it, or adjust it so it allows the Apache web server to act as a load balancer. Both ways are shown here:

Disabling SELinux

To Disable SELinux you would do the following:

sudo setenforce permissive 

Then edit the SELinux config file to change the server to be permissive.

sudo vim /etc/selinux/config 

Once in the editor, edit this line:


Change enforcing to permissive, so that it looks like this:


Save the file, then use the setenforce command to change the running SELinux to be permissive.

sudo setenforce permissive

Editing SELinux

If you wish to keep SELinux enabled, and allow it to permit Apache web server to work as a proxy, you would run the following commands:

sudo setsebool -P httpd_can_network_connect 1

sudo setsebool -P httpd_can_network_relay 1

sudo setsebool -P httpd_graceful_shutdown 1

sudo setsebool -P nis_enabled 1  
Restart the Apache Web Server and Confirm You Can Access the Test Application, Then Test to Ensure the Failover Works

Restart the Apache web server and check for errors. If there are errors and Apache does not start, then troubleshoot those errors:

sudo systemctl restart httpd

You can test to see if the load balancer is working by going to the web page of the Apache web server. Then go to the URL that will be serving the load balanced application. That URL will be:


If your Apache server’s external IP address was, for example, you would go to this URL:

Change the above IP address to be your own.

You should be able to see a web page that shows something like the following. It will be slightly different in your own example:

Sessions Example
Session ID: 71DA102F3D1AF0818230595F801A93B7.tomcat1

If you see something like this then it works.

The previous task displayed a Session ID: that showed which Tomcat server you were connected to. Confirm the failover works by shutting down that Tomcat server (in the terminal you already have a session open in):

sudo systemctl stop tomcat

Then refresh your load balancer web page. You should now get connected to the second Tomcat server, and the application should show the other Tomcat server’s Session ID.

If this did not happen, but you still see the page, check to make sure you shutdown the correct Tomcat server. If you see an error page or not found, there is a problem and you will need to troubleshoot it.

Additional Resources

Your manager is interested in using a load balancer with failover abilities, to connect to multiple Tomcat instances.

They would like you to test whether this is possible by installing an Apache web server as a load-balanced front end to multiple Tomcat instances.

The Tomcat servers and their GUIs are being delivered on port 8080. You are tasked with installing the Apache web server to act as the front end, to deliver the Tomcat GUI on port 80 instead. The web server will be on a single server, and the Tomcat instances will be two other separate servers.

To get all of this running, you will need to perform the tasks in this lab.

After a successful hands-on lab, you will be able to see the /examples/servlets/servlet/SessionExample application being served, on port 80, on the front end Apache web server. You should be able to see the application running and tell which Tomcat server is providing the application. If you shut that server down, the application should failover to the other server.

Logging In

Use the credentials provided on the hands-on lab overview page, and log into the load balancing server, using SSH, as cloud_user. Note that this is for connecting to the server, not the Tomcat GUI.

Note: It may take 3 to 10 minutes after the hands-on lab has started before you are able to log into the server. This is fine and will not affect the performance of the tasks for the hands-on lab.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?