Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Load Balance a Tomcat Application

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 45m
Published
Clock icon May 08, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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 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.

  2. Challenge

    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
    </Proxy>
    
    </IfModule>
    
  3. Challenge

    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
    
  4. Challenge

    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:

    SELINUX=enforcing
    

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

    SELINUX=permissive
    

    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  
    
  5. Challenge

    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:

    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

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans