Tomcat Clustering + Multicast + Firewall
How do you build a Tomcat 7 or 8 cluster, with an example, and make it work with the firewall that is on by default?
It's simple:
- Install, configure and start Tomcat (say, two instances)
1.1) Add a Cluster element under Host in server.xml.
- Create two pages in the Context you need:
2.1) index.jsp
<%= session.getAttribute("cluster_attr") %>
2.2) set.jsp
<% session.setAttribute("cluster_attr", "blablabla"); %>
Set up a shared Context (if you didn't put the pages into the ROOT context)
Start Tomcat:
tomcat/bin/startup.bat
Open "/" on both in a browser: you get null
Open /set.jsp on one of them
Repeat step 5: you get "blablabla" on both
...
Not working? Check the firewall + multicast settings
Add a firewall rule and reload:
10.1) RHEL / CentOS 6
10.1.1) Add to /etc/sysconfig/iptables:
-A INPUT -m pkttype -j ACCEPT --pkt-type multicast
10.1.2) Run
# service iptables restart
10.2) RHEL / CentOS 7: run
# firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -m pkttype --pkt-type multicast -j ACCEPT
success
# firewall-cmd --reload
success
- Check (step 5) that everything works without restarting Tomcat.
Originally published in Russian on LiveJournal.