Tomcat load balancing with mod_proxy and mod_proxy_balancer

Published on November 1, 2018 by Daniel Lanza

 opensuse  httpd

This tutorial will explain how to load balancing two Tomcat instances with Apache HTTPD server with mod_proxy and mod_proxy_balancer modules.

The environment will be simulated with three virtual machines and VirtualBox:

#Server FQDNIP
1apache-server.dalanzg.com192.168.56.101Apache HTTPD - Port 80
2tomcat-server1.dalanzg.com192.168.56.102Apache Tomcat - Port 8080
3tomcat-server2.dalanzg.com192.168.56.103Apache Tomcat - Port 8080

Check the following post to configure openSUSE with Internet and statick IP address by using YaST on VirtualBox

Steps

Install Apache HTTPD

Install Apache HTTP in server #1

dlanza@apache-server:~> sudo zypper install apache2 apache2-example-pages
dlanza@apache-server:~> sudo systemctl enable apache2
Created symlink /etc/systemd/system/httpd.service → /usr/lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/apache.service → /usr/lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /usr/lib/systemd/system/apache2.service.
dlanza@apache-server:~> sudo systemctl start apache2

Check Apache HTTPD works in server #1 from your host machine.

Apache HTTPD works
Apache HTTPD works

Install Apache Tomcat

Steps for servers #2 and #3:

And create a static html file.

dlanza@tomcat-server1:/opt> mkdir /opt/apache-tomcat-8.5.34/webapps/dalanzg
dlanza@tomcat-server1:/opt> vim /opt/apache-tomcat-8.5.34/webapps/dalanzg/index.html
<html>
  <body>
    <h3>Tomcat server #1</h3>
  </body>
</html>

Check you can reach the index page from servers #2 and #3:

Tomcat 1
Tomcat 1
Tomcat 2
Tomcat 2

Load apache modules

First, load the following modules by editing loadmodule.conf file:

Add the modules at the end:

LoadModule proxy_module                   /usr/lib64/apache2-prefork/mod_proxy.so
LoadModule proxy_http_module              /usr/lib64/apache2-prefork/mod_proxy_http.so
LoadModule lbmethod_byrequests_module     /usr/lib64/apache2-prefork/mod_lbmethod_byrequests.so
LoadModule proxy_balancer_module          /usr/lib64/apache2-prefork/mod_proxy_balancer.so
LoadModule slotmem_shm_module             /usr/lib64/apache2-prefork/mod_slotmem_shm.so

Restart and check if Apache modules are loaded.

dlanza@apache-server:/etc/apache2> sudo service apache2 restart
dlanza@apache-server:/etc/apache2> sudo httpd -M
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_prefork_module (static)
 unixd_module (static)
 systemd_module (static)
 actions_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_file_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cgi_module (shared)
 dir_module (shared)
 env_module (shared)
 expires_module (shared)
 include_module (shared)
 log_config_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 setenvif_module (shared)
 ssl_module (shared)
 socache_shmcb_module (shared)
 userdir_module (shared)
 reqtimeout_module (shared)
 authn_core_module (shared)
 authz_core_module (shared)
 proxy_module (shared)
 proxy_http_module (shared)
 lbmethod_byrequests_module (shared)
 proxy_balancer_module (shared)
 slotmem_shm_module (shared)

Example 1 - Reverse Proxying a Single Backend Server

All the Apache requests http://192.168.56.101/dalanzg will be redirected to server #2 (http://192.168.56.102:8080/dalanzg)

First, enable the modules mod_proxy and mod_proxy_http with a2enmod, and restart apache.

dlanza@apache-server:/etc/apache2> sudo a2enmod proxy
dlanza@apache-server:/etc/apache2> sudo a2enmod proxy_http
dlanza@apache-server:/etc/apache2> sudo service apache2 restart

Create the a configuration file in /etc/apache2/vhosts.d

dlanza@apache-server:/etc/apache2/vhosts.d> sudo vim singlebackend.conf
<VirtualHost *:80>
        ProxyPreserveHost On

        ProxyPass /dalanzg http://192.168.56.102:8080/dalanzg
        ProxyPassReverse /dalanzg http://192.168.56.102:8080/dalanzg
</VirtualHost>

Let’s check the following URL -> http://192.168.56.101/dalanzg

Reverse Proxying a Single Backend Server
Reverse Proxying a Single Backend Server

Example 2 - Load Balancing Across Multiple Backend Servers

All the Apache request will be balanced from http://192.168.56.101/dalanzg to:

Rename the Example 1 configuration file to singlebackend.conf.bak to disable the rule and restart apache.

dlanza@apache-server:/etc/apache2/vhosts.d> sudo mv singlebackend.conf singlebackend.conf.bak
dlanza@apache-server:/etc/apache2/vhosts.d> sudo service apache2 restart

Activate the 5 modules and restart apache.

dlanza@apache-server:/etc/apache2/vhosts.d> sudo a2enmod proxy
"proxy" already present
dlanza@apache-server:/etc/apache2/vhosts.d> sudo a2enmod proxy_http
"proxy_http" already present
dlanza@apache-server:/etc/apache2/vhosts.d> sudo a2enmod lbmethod_byrequests
dlanza@apache-server:/etc/apache2/vhosts.d> sudo a2enmod proxy_balancer
dlanza@apache-server:/etc/apache2/vhosts.d> sudo a2enmod slotmem_shm
dlanza@apache-server:/etc/apache2/vhosts.d> sudo service apache2 restart

Create a new conf file.

dlanza@apache-server:/etc/apache2/vhosts.d> sudo vim balancer.conf
<VirtualHost *:80>
        ProxyRequests off
        ProxyPreserveHost On

        <Proxy balancer://mycluster>
                BalancerMember http://192.168.56.102:8080 route=worker1
                BalancerMember http://192.168.56.103:8080 route=worker2
                ProxySet lbmethod=byrequests
        </Proxy>

        ProxyPass /dalanzg balancer://mycluster/dalanzg
        ProxyPassReverse /dalanzg balancer://mycluster/dalanzg

        <Location /balancer-manager>
                SetHandler balancer-manager
        </Location>

        ProxyPass /balancer-manager !
</VirtualHost>

And check how the page http://192.168.56.101/dalanzg is balanced from server #2 and #3.

Balanced to Tomcat 1
Balanced to Tomcat 1
Balanced to Tomcat 2
Balanced to Tomcat 2

Check the balancer-manager status -> http://192.168.56.101/balancer-manager

Balancer manager
Balancer manager

If Tomcat 2 (server #3) were down, the URL http://192.168.56.101/dalanzg would be still working, since the requests would go always to Tomcat 1 (server #2). Let’s try it.

Shutdown Tomcat 2 in server #3.

dlanza@tomcat-server2:~> /opt/apache-tomcat-8.5.34/bin/shutdown.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.34
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.34
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.34/temp
Using JRE_HOME:        /usr/lib64/jvm/jre
Using CLASSPATH:       /opt/apache-tomcat-8.5.34/bin/bootstrap.jar:/opt/apache-tomcat-8.5.34/bin/tomcat-juli.jar

Go several times to the URL http://192.168.56.101/dalanzg, and you will only see Tomcat 1 page.

Balanced to Tomcat 1
Balanced to Tomcat 1

Errors to balance Worker URL 2 will be gotten.

Balancer manager. Errors in worker2
Balancer manager. Errors in worker2
comments powered by Disqus

See also

How to install Apache HTTPD in openSUSE

This tutorial will explain how to install Apache HTTPD in openSUSE 15. The original documentation is found in The Apache HTTP Server Project. And the official documentation for openSUSE is in The Apache HTTP Server. Requirements You will need the following: openSUSE Leap 15 Steps Set hostname and domain Install Apache HTTPD Start and stop Apache Start Apache automatically at boot time Set hostname and domain Files /etc/hostname and /etc/hosts will be modified to resolve the following:

Read more

openSUSE with Internet and static IP address by using YaST on VirtualBox

An openSUSE virtual machine will be configured to have a static IP address and Internet connection on VirtualBox. This scenario is really useful if you want to install a specific application in guest and you need to communicate from your host. Therefore, the virtual machine will have two network adapters: NAT -> Internet connection Host-only-adapter -> Static IP address (192.168.56.200) and FQDN server1.dalanzg.com Steps Settings for VirtualBox Virtual machine network settings Settings for network devices Settings for VirtualBox First, create a Host Network Manager in VirtualBox.

Read more

How to install Oracle 12c R2 in openSUSE

This tutorial will explain how to install Oracle Database 12c R2 in openSUSE Leap 42.3. Requirements You will need the following: openSUSE Leap 42.3 -> Install openSUSE virtual machine with VirtualBox Oracle 12c R2 database installation file -> linuxx64_12201_database.zip Steps Install libraries Set hostname and domain Create groups and users Grant oracle user access to X server Configuring Kernel Parameters and Resource Limits Create directory for Oracle Database Install Oracle Database Configuration of Oracle Listener Configuration of Oracle Database Create user for database Create table for database Connect to Oracle database with SQL Developer Start database when server is restarted Install libraries According to Supported SUSE Linux Enterprise Server 12 in Oracle documentation, install the following libraries by using YaST or zypper command:

Read more