Prerequisites
Before configuring the Tomcat cluster on Debian, ensure the following prerequisites are met:
sudo apt install default-jdk).tomcat) is created for running Tomcat instances to enhance security (sudo groupadd tomcat && sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat).Step 1: Install Tomcat on Each Node
sudo apt update && sudo apt install tomcat9 tomcat9-admin
http://<node-ip>:8080 in a browser. You should see the Tomcat welcome page.Step 2: Configure Multiple Tomcat Instances
To run multiple Tomcat instances on a single node (or across multiple nodes), duplicate the Tomcat directory and customize ports for each instance:
tomcat1, tomcat2):sudo mkdir -p /opt/tomcat1/{conf,webapps,temp,work} && sudo cp -R /usr/share/tomcat9/* /opt/tomcat1/
sudo mkdir -p /opt/tomcat2/{conf,webapps,temp,work} && sudo cp -R /usr/share/tomcat9/* /opt/tomcat2/
conf/server.xml to avoid conflicts:
<Connector port="8005" to unique values (e.g., 8005 for tomcat1, 8006 for tomcat2).<Connector port="8080" to unique values (e.g., 8081 for tomcat2).<Connector port="8009" to unique values (e.g., 8010 for tomcat2).tomcat user:sudo chown -R tomcat:tomcat /opt/tomcat1 /opt/tomcat2
Step 3: Enable Clustering in server.xml
Edit the conf/server.xml file for each Tomcat instance to enable clustering. The key configurations include:
org.apache.catalina.ha.tcp.SimpleTcpCluster for basic TCP clustering.jvmRoute attribute to the <Engine> element (e.g., jvmRoute="node1" for tomcat1, jvmRoute="node2" for tomcat2).Example configuration for one node:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="node1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster">
<!-- Membership: Multicast for cluster discovery -->
<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: TCP listener for this node -->
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"/>
<!-- Sender: Replication transmitter -->
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<!-- Interceptors: Failure detection and message dispatch -->
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
</Channel>
<!-- Replication Valve: Filters non-session resources -->
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.css;.*\.txt"/>
<!-- Session Listener: Tracks session changes -->
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
</Engine>
Repeat this step for all nodes, ensuring the jvmRoute and Receiver port are unique per instance.
Step 4: Configure Session Replication
For session consistency across the cluster, add the <distributable/> element to the web.xml file of each web application:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<distributable/>
</web-app>
This tells Tomcat to replicate sessions across the cluster.
Step 5: Set Up a Load Balancer (Nginx)
Use Nginx as a reverse proxy to distribute requests across the Tomcat cluster. Install Nginx and configure it as follows:
sudo apt install nginx
/etc/nginx/sites-available/default):upstream tomcat_cluster {
server 192.168.1.101:8080; # Replace with IP/port of tomcat1
server 192.168.1.102:8081; # Replace with IP/port of tomcat2
}
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://tomcat_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo systemctl restart nginx
Step 6: Start and Validate the Cluster
tomcat user:sudo systemctl start tomcat9@tomcat1 # For instance 1
sudo systemctl start tomcat9@tomcat2 # For instance 2
http://<node-ip>:8080/manager/html) and navigating to the “Cluster” section. You should see all nodes listed as “running.”Optional: Use Redis for Centralized Session Storage
For more robust session management (e.g., across multiple data centers), configure Tomcat to use Redis as a session store:
sudo apt install redis-server
WEB-INF/lib directory (e.g., tomcat-redis-session-manager-x.x.x.jar and commons-pool2-x.x.x.jar).context.xml file of your web application:<Context>
<Manager className="org.apache.catalina.session.PersistentManager"
storeClassName="org.apache.catalina.session.RedisStore"
connectionURL="redis://localhost:6379/0"
driverName="org.apache.tomcat.session.RedisStore"/>
</Context>
This setup ensures sessions are stored in Redis, eliminating the need for multicast communication.