Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How To Set Up a Tomcat, Apache and mod_jk Cluster

DZone's Guide to

How To Set Up a Tomcat, Apache and mod_jk Cluster

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

In this article I will go through a common set-up for a small production environment. A single tier, load balanced application server cluster.


Overview

A high level overview of what we will be doing.

  1. Downloading and installing Apache HTTP server and mod_jk
  2. Downloading Tomcat
  3. Downloading Java
  4. Configuring two local Tomcat servers
  5. Clustering the two Tomcat servers
  6. Configuring Apache to use mod_jk to forward request to Tomcat
  7. Deploying application to Tomcat server that tests our set-up

Introduction


What is Apache?

Apache is an HTTP server.


What is mod_jk?

It is an Apache module that allows AJP communication between Apache and a back end application server like Tomcat.I am running this on Ubuntu 14.04LTS installed on a dual boot PC with Windows 7.


 Application Server Cluster 


Download Apache2

We are going to use Ubuntu's APT package maintenance system to obtain and install Apache2.


sudo apt-get install apache2


This will install in /etc/apache2


Download and install mod_jk

The mod_jk module is not included in the Apache2 download so must be obtained and installed separately. The installation requires that the mod_jk module is visible to Apache and configured to ensure that Apache knows where to look for it and what to do with the requests you want to proxy.


sudo apt-get install libapache2-mod-jk


This will install in /etc/libapache2-mod-jk also two files have been added to the /etc/apache2/mods-available folder.


Downloading and installing Tomcat 8

At the time of writing this Tomcat 8 does not have a package in APT so you must download the binaries from the tomcat website.http://tomcat.apache.org/download-80.cgi select the appropriate binary distribution and extract it as follows.


tar xvzf apache-tomcat-8.0.5.tar.gz


We need two copies of the Tomcat server to be load balanced. I created two directories in the /opt/ location: /opt/tomcat-server1/ and /opt/tomcat-server2/ and copied tomcat into each one.


Download and install Java

Download Java from APT as follows:


apt-get install openjdk-7-jdk

and set JAVA_HOME in .bashrc


vim ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64


Configure two local Tomcat servers

We will edit only the server.xml of the server2 installation of tomcat. We need to change port numbers to avoid conflicts.We change the following:


<Server port="9005" shutdown="SHUTDOWN">
<Connector port="9009" protocol="AJP/1.3" redirectPort="9443"/>


and comment out the HTTP Connector as we only want the web application to be accessible through the load balancer.Here is my server2 Tomcat server.xml configuration.


Configure mod_jk

Load balancing is configured in the workers.properties file, located /etc/libapache2-mod-jk/ where workers represent actual or virtual workers.We will define two actual workers and two virtual workers which map to the Tomcat servers. In the worker.list property I have defined two virtual workers: status and loadbalancer, I will refer to these later in the Apache configuration.Workers for each server have been defined using values for the server.xml configuration files. I used the port values for the AJP connectors and I have included an lbfactor that sets the preference that the load balancer will show for that server.Finally we define the virtual workers. The loadbalancer worker is set to type lb and set the workers that represent the Tomcat servers in the balancer_workers properties. The status only needs to be set to type status.


worker.list=loadbalancer,status  worker.server1.port=8009worker.server1.host=localhostworker.server1.type=ajp13  worker.server2.port=9009worker.server2.host=localhostworker.server2.type=ajp13  worker.server1.lbfactor=1worker.server2.lbfactor=1  worker.loadbalancer.type=lbworker.loadbalancer.balance_workers=server1,server2  worker.status.type=status


Ensure that you remove any other worker configuration that are not being used.


Configure Apache Web Server to forward requests

You will need to add the following to the Apache configurations located in etc/apache2/sites-enabled/000-default.conf


JkMount /status status
JkMount /* loadbalancer


Verify the installation

To test that all has been configured correctly we need to deploy an application. A sample application that has been used for years to test such configurations is called the ClusterJSP sample application. You can find it by googling in or from the JBoss site.Now deploy the war to the webapps folder on both servers and start each server using the start-up script /opt/tomcat-server1/bin/startup.sh.Go to http://localhost/clusterjsp/HaJsp.jsp and you should see the page show HttpSession information.


Cluster - HA JSP Sample


Now lets look at the mod_jk status page: http://localhost/status. You will see that this page shows information about the load balancer workers and the workers it is balancing.


 Workers


If everything is working you will see the worker error state show OK or OK/IDLE if they are not currently balancing load.


Things to try out

Enable sticky sessions: Configure jvmRoute in the server.xml configuration.


Further reading

Loadbalancing with mod_jk and ApacheWorking with mod_jk

Connecting Apache's Web Server to Multiple Instances of Tomcat

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:
mod_jk ,tomcat 8 ,tomczt ,java

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}