Skip to main content

Load Balance a Tomcat Application

Hands-On Lab

 

Photo of

Training Architect

Length

00:45:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Load Balance a Tomcat Application

Introduction

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.

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.

The Scenario

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

It may be helpful to have an SSH session open in each server. The credentials for all three are on the hands-on lab overview page.

It may also be helpful to have three web browser tabs open: one to look at the load balancer, and one each for the Tomcat servers. There is more information about connecting to them in the first and second tasks.

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.

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

The second and third of the provisioned servers are running Tomcat. When enough time has elapsed (the 3-10 minutes mentioned earlier), visit those two servers' public IP addresses on port 8080 in a web browser.

To access each one, copy its external IP address and paste it into a web browser's address bar, then append :8080 to it.

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

http://3.92.152.3:8080

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:

[cloud_user@loadbalancer ]$ sudo dnf install httpd

[cloud_user@loadbalancer ]$ sudo systemctl enable httpd

[cloud_user@loadbalancer ]$ sudo systemctl start httpd

In a browser tab, 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.

[cloud_user@loadbalancer ]$ 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
</Proxy>

</IfModule>

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

In a terminal open to the first Tomcat server, edit server.xml to make the required changes. First get root privileges:

[cloud_user@tomcat1 ]$ sudo su -

Now get into Tomcat's configuration directory:

[root@tomcat1 ]# cd /usr/local/tomcat9/

Finally, we can edit the file:

[root@tomcat1 ]# sudo vim 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.

[root@tomcat1 ]# systemctl restart tomcat

Now in the terminal open to the second Tomcat server, do the same kind of thing. Become root:

[cloud_user@tomcat2 ]$ sudo su -

Get into Tomcat's configuration directory:

[root@tomcat2 ]# cd /usr/local/tomcat9/

Edit the server.xml file:

[root@tomcat2 ]# sudo vim 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:

[root@tomcat2 ]# systemctl restart tomcat

Change SELinux so That the Load Balancing Will Work

SELinux is enabled on the hands-on lab server. We need to edit settings so it allows the Apache web server to act as a load balancer. These need to be run on the load balancer:

[cloud_user@loadbalancer ]$ sudo setsebool -P httpd_can_network_connect 1

[cloud_user@loadbalancer ]$ sudo setsebool -P httpd_can_network_relay 1

[cloud_user@loadbalancer ]$ sudo setsebool -P httpd_graceful_shutdown 1

[cloud_user@loadbalancer ]$ 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:

[cloud_user@loadbalancer ]$ 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:

http://LOAD-BALANCER-IP/examples/servlets/servlet/SessionExample

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

http://18.144.72.70/examples/servlets/servlet/SessionExample

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:

[root@tomcat1 ]# 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.

Conclusion

We have correctly set up an Apache web server to act as a load balancer and failover for two Tomcat instances running behind it. Congratulations on completing this hands-on lab!