I’m seeing many posts achieving Tomcat session replication in docker swarm using traefik. But I just don’t want to add another component. I use httpd as frontend for tomcat. Tomcat will be deployed as a service with 4 replicas and will be scaled when required. httpd is running as docker service and both are in same network. My question is, is there any way to achieve Tomcat session replication in docker swarm in this case. TIA.
2
Answers
As long as all Tomcat Docker containers are reachable you can achieve this by below Cluster setup.
The below setup is for tomcat 7 but should work in the same way for other versions also.
In case of Apache Tomcat 7 uncomment
<Cluster className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”/>
tag and update cluster detail something like below.Also, we need to add
workers.properties
atapache/conf/
path.Please note this file has to be placed in all the Tomcat servers which will run in a cluster.
You can read more in detail here.
You can do this without a front end, BUT you’re going to have issues because if the ingress routes to a different node, that node has to request the session from another node.
The plain tomcat image is not sufficient to do clustering, it wouild need some customizations to
/usr/local/tomcat/conf/server.xml
to the cluster configuration to use theDNSMembershipProvider
DNSMembershipProvider
uses the environment variableDNS_MEMBERSHIP_SERVICE_NAME
to look up the server so it should match the service name.The following
docker-compose.yml
shows how to deploy it without any other component.Proper way
As I stated earlier, using the above approach will satisfy the OP’s requirements of not adding an additional component, but will have significant performance issues. Instead you need a proxy that can route to the servers with the following features:
Traefik handles all the requirements quite nicely even if itself does not support clustering due to their ACME storage not allowing concurrent access without going to their EE version.
The
docker-compose.yml
would be as follows.One property of note is
endpoint_mode: dnsrr
is not available when exposing ports earlier. This enables a better replication of session data within the Tomcat cluster as each node will have individual IDs.An example implementation of this is in my github repo https://github.com/trajano/tomcat-docker-swarm