In this article, we’ll walk through setting up a cluster of Apache Tomcat servers using three Ubuntu 18.04 EC2 Instances or Virtual Machines (VMs). For demonstration purposes, EC2 instances are utilized. This guide also includes the necessary steps for installing Java, as it is a requirement for Apache Tomcat.
Cluster Details:
- Node1: Base Directory = /root/tomcat1, IP = 172.31.35.11
- Node2: Base Directory = /root/tomcat2, IP = 172.31.39.120
- Node3: Base Directory = /root/tomcat3, IP = 172.31.32.185
Prerequisites
- AWS Account (Create one here if you wish to use EC2 Instances) (Optional).
- Three EC2 Instances or three Virtual Machines running Ubuntu 18.04 LTS (Learn how to create an EC2 Instance here).
- Root access to the servers.
Overview
- Download Apache Tomcat
- Install Java 8
- Configure Apache Tomcat Cluster
- Start/Stop Apache Tomcat
Download Apache Tomcat
Switch to the “root” user to avoid permission issues, though this is not recommended in production environments.
sudo -i
Create a directory to store the Apache Tomcat package:
mkdir tomcat1 #On Node2 mkdir tomcat2, On Node3 mkdir tomcat3 cd tomcat1/ #On Node2 cd tomcat2/, On Node3 cd tomcat3/
Download and extract Apache-Tomcat-9.0.0.M17, or choose another version from here:
#On each Node wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.0.M17/bin/apache-tomcat-9.0.0.M17.tar.gz tar -zxvf apache-tomcat-9.0.0.M17.tar.gz
Install Java 8
#On each nodesudo apt-get update #Update System Package Details
sudo apt install openjdk-8-jdk #Install Java java --version #Check Java Version
Configure Apache Tomcat
Backup the default server.xml file before making changes:
#On each node
cd apache-tomcat-9.0.0.M17/ cp conf/server.xml conf/server.xml.bak
Modify the server.xml file:
#On each node
ifconfig #Get IP of the server. vim conf/server.xml
Find and replace localhost with the IP of the Node:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps
To set up the cluster, find the following code:
<!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> -->
Add the following code to each node, replacing IP-Of-Node with the IP of the node itself:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4"
port="45564" frequency="500" dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="IP-Of-Node"
port="5000" selectorTimeout="100" maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
Start/Stop Apache Tomcat
Starting Apache Tomcat
Use the startup.sh script to initiate the Apache Tomcat service. You can verify it’s running on the default port (8080) using the netstat command:
#On each node
bin/startup.sh #Start Apache Tomcat Service. netstat -tulpn #Check active ports on the system.
Check Logs
View Apache Tomcat logs in the catalina.out file with the tail command to see recent entries:
tail -100f logs/catalina.out
Shutting Down Apache Tomcat
Stop the Apache Tomcat service using the shutdown.sh script:
bin/shutdown.sh #Stop Apache Tomcat Service.
Conclusion
This guide has provided detailed steps for setting up a cluster of Apache Tomcat servers on three nodes with essential configurations.
FAQs
1. Why use Apache Tomcat for clustering?
Apache Tomcat is widely used for Java applications and offers clustering capabilities that allow for load balancing and failover, making it reliable for production environments.
2. Can I use a different version of Java?
This guide uses Java 8 due to its compatibility with many applications, but you can install any version of Java that Tomcat supports. Just make sure to change the Java version in your installation commands accordingly.
3. Is it necessary to change localhost to the node’s IP in server.xml?
Yes, this change is crucial for clustering because it allows the Tomcat server to bind to the correct network interface and communicate with other nodes.
4. What should I do if I encounter errors during installation?
Check your network settings and the configuration files, ensure all dependencies are installed, and review the system logs for error messages that might offer more details.