habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Tomcat Load Balancing with Apache HTTP and ModJK

3 min read

Tomcat-Load-Balancing-with-Apache-HTTP-and-ModJK

Hi Dude, today I will write simple article about setup multiple tomcat load balancing using apache and mod JK.

Overview Simple topology configuration described by picture below.

tomcat load balancing apache

I will create a 3 node server, with the IP address configuration below:

NodeHostnameIP AddressService jvmRoute
Server1tcserver1.habibza.in192.168.197.144Tomcat + Apache + ModJK tcserver1
Server2tcserver2.habibza.in192.168.197.145Tomcat tcserver2
Server3tcserver3.habibza.in192.168.197.146Tomcat tcserver3

in my previous post, I’ve written about tomcat and apache modJK. But as of this writing, it only uses 1 tomcat node/server.

So in this post, I’m going to use multiple Servers. Ok, let’s get started.

NOTE : I will start by working on server 2 and server 3 first. Because just install tomcat only.

Install Tomcat in node 1, 2 and 3

This is my configuration in tomcat multiple setup. I prefer to setup on server 1, 2 and server 3 first. So do the same thing below on server 2 and server 3.

Set Server Hostname

hostnamectl set-hostname tcserver2.habibza.in

Set Hostname in /etc/hosts

root@tcserver1:~# cat /etc/hosts

127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts

192.168.197.144 tcserver1.habibza.in tcserver1
192.168.197.145 tcserver2.habibza.in tcserver2
192.168.197.146 tcserver3.habibza.in tcserver3

Install Tomcat using apt

root@tcserver1:~# apt update
root@tcserver1:~# apt-get install tomcat9 tomcat9-docs tomcat9-examples tomcat9-admin

After installation fisinshed, lets check java version.

root@tcserver1:~# java -version 
openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04) OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

Set Tomcat User

After that go to /etc/tomcat9/tomcat-users.xml.

<tomcat-users . . .>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>

Set Tomcat Communicate with Apache via Mod JK and Pointing Worker

Open file /etc/tomcat9/server.xml. Add this line to activate AJP 1.3 protocol and set jvmroute.

root@tcserver1:~# vim /etc/tomcat9/server.xml

...
...
<engine name="Catalina" defaulthost="localhost" jvmroute="tcserver1"></engine>
  • jvmRoute = tcserver1 on server 192.168.197.144
  • jvmRoute = tcserver2 on server 192.168.197.145
  • jvmRoute = tcserver3 on server 192.168.197.146
See also  Easy Install Tomcat9 in Ubuntu 20.04

Open browser and type this url.

http://192.168.197.144:8080

NOTE: Up to this stage, apply the same steps to server 2 and server 3.

After installation and tomcat setup finished. We can make sure the setup properly by type the IP in the browser.

http://192.168.197.146:8080
http://192.168.197.145:8080

Next, install Apache and Mod jk in server1.

It’s up to you where to install apache http and mod jk, but this time I prefer to install on server1.

root@tcserver1:~# apt install apache2 libapache2-mod-jk

Enable JK Module

Let’s check, whether the mod is loaded or not.

root@tcserver:~# systemctl restart apache2.service
root@tcserver:~# apache2ctl -M | grep jk
 jk_module (shared)

Look bold red jk_module (shared)which means Mod JK already active. And now check port .

root@tcserver1:~# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      820/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      892/sshd: /usr/sbin
tcp6       0      0 :::22                   :::*                    LISTEN      892/sshd: /usr/sbin
tcp6       0      0 ::1:<strong>8009</strong>                :::*                    LISTEN      21651/java
tcp6       0      0 :::<strong>8080</strong>                 :::*                    LISTEN      21651/java
tcp6       0      0 :::<strong>80</strong>                   :::*                    LISTEN      21582/apache2

See important port below:

  • 8009 : Port AJP Connector
  • 8080 : Port Tomcat
  • 80 : Port Apache
  • 22 : Default SSH port
See also  Install Zimbra Multi Server on Ubuntu 20.04 (Part 1)

Create workers.properties

This is important part. We need to configure the Apache HTTP Server to load and initialize the JK module.

Lets create the /etc/apache2/workers.properties file. This file defines a list of Tomcat ‘workers’ to which Apache can pass requests.

root@tcserver1:~# vim /etc/apache2/workers.properties

And paste the following into the file.

###
worker.list=jkstatus,lb_router

# Set LOADBALANCER
worker.lb_router.type=lb
worker.lb_router.method=Requests
worker.jkstatus.type=status

# HERE is where you decide on how many Tomcat Server's there are in the cluster
worker.lb_router.balance_workers=tcserver1,tcserver2,tcserver3
worker.lb_router.sticky_session=1

# Set Worker

worker.tcserver1.port=8009
worker.tcserver1.host=192.168.197.144
worker.tcserver1.type=ajp13
worker.tcserver1.lbfactor=1


worker.tcserver2.port=8009
worker.tcserver2.host=192.168.197.145
worker.tcserver2.type=ajp13
worker.tcserver2.lbfactor=1

worker.tcserver3.port=8009
worker.tcserver3.host=192.168.197.146
worker.tcserver3.type=ajp13
worker.tcserver3.lbfactor=1

Next reference this file in the primary Apache configuration file apache2.conf.

root@tcserver:~# vim /etc/apache2/apache2.conf

We add these lines at the end.

JkWorkersFile /etc/apache2/workers.properties

# Where to put jk shared memory
JkShmFile /var/log/apache2/mod_jk.shm

# Where to put jk logs
JkLogFile /var/log/apache2/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the timestamp log format
JkLogStampFormat "[ %a %b %d %H:%M:%S %Y ]"

Setting which URLs to manage with Apache

The last, we have to setting which URL manage with apache. Mount Document Root with JkMount.

root@tcserver1:~# vim /etc/apache2/sites-enabled/000-default.conf

Add the following line under the DocumentRoot entry. This makes it so that you can request JRS via the Apache web server.

JkMount /* lb_router
#JkMount /examples/* worker1

JkMount /* worker1 : Mount everything in default document root tomcat in apache with lb_router. lb-router is name of worker that defined in workers.properties.

Restart service apache and tomcat.

root@tcserver1:~# systemctl restart apache2.service
root@tcserver1:~# systemctl restart tomcat9.service

That is my article about tomcat load balancing apache. May be it’s helpful, please feel free to leave a comment if you have any questions and I’ll appreciate it.


habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss good article from us, get weekly updates in your inbox