Skip to main content

Tomcat 10 Cluster with individual Node Server

Siem Reap, Cambodia

Setting up Minion and Master Nodes with Docker-Compose

docker-compose.yml

version: "3.8"
services:

tomcatOne:
container_name: tomcatOne
image: tomcat:10-jdk11-corretto
networks:
- gateway
restart: unless-stopped
networks:
gateway:
ipv4_address: 172.25.0.10

volumes:
- /opt/tomcat/docker/cluster/tomcat/conf/server.xml:/usr/local/tomcat/conf/server.xml
- /opt/tomcat/docker/cluster/tomcat/conf/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml
- /opt/tomcat/docker/cluster/tomcat/webapps/ROOT_01:/usr/local/tomcat/webapps/ROOT

tomcatTwo:
container_name: tomcatTwo
image: tomcat:10-jdk11-corretto
networks:
- gateway
restart: unless-stopped
networks:
gateway:
ipv4_address: 172.25.0.20
volumes:
- /opt/tomcat/docker/cluster/tomcat/conf/server.xml:/usr/local/tomcat/conf/server.xml
- /opt/tomcat/docker/cluster/tomcat/conf/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml
- /opt/tomcat/docker/cluster/tomcat/webapps/ROOT_02:/usr/local/tomcat/webapps/ROOT

tomcatThree:
container_name: tomcatThree
image: tomcat:10-jdk11-corretto
networks:
- gateway
restart: unless-stopped
networks:
gateway:
ipv4_address: 172.25.0.30
volumes:
- /opt/tomcat/docker/cluster/tomcat/conf/server.xml:/usr/local/tomcat/conf/server.xml
- /opt/tomcat/docker/cluster/tomcat/conf/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml
- /opt/tomcat/docker/cluster/tomcat/webapps/ROOT_03:/usr/local/tomcat/webapps/ROOT

ingress:
image: nginx:stable-alpine
container_name: ingress
networks:
gateway:
ipv4_address: 172.25.0.5
ports:
- "192.168.2.111:443:443"
restart: unless-stopped
volumes:
- /opt/tomcat/docker/cluster/nginx/conf.d:/etc/nginx/conf.d
- /opt/tomcat/docker/cluster/nginx/ssl:/etc/nginx/ssl
- /opt/tomcat/docker/cluster/nginx/nginx.conf:/etc/nginx/nginx.conf
depends_on:
- tomcatOne
- tomcatTwo
- tomcatThree

networks:
gateway:
ipam:
config:
- subnet: 172.25.0.0/24

NGINX Configuration

default.conf

Master / Minion1

##########################################################################
########################## Cluster Ingress ###############################
##########################################################################


server {
listen 80;
listen [::]:80;

server_name virtual-host1.com;

return 301 https://$server_name$request_uri;
}


upstream cluster_nodes {
server virtual-host2.com:443;
server virtual-host3.com:443;
}

server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2;
include conf.d/self-signed.conf;
include conf.d/ssl-params.conf;
include conf.d/header.conf;

server_name virtual-host1.com;

location = / {
proxy_pass http://cluster_nodes;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}


##########################################################################
########################## Tomcat Servlet 1 ##############################
##########################################################################


server {
listen 80;

server_name virtual-host2.com;

return 301 https://$server_name$request_uri;
}


upstream tc_servlets {
server tomcatOne:8080;
server tomcatTwo:8080;
server tomcatThree:8080;
}

server {
listen 443 ssl http2;
include conf.d/self-signed.conf;
include conf.d/ssl-params.conf;
include conf.d/header.conf;

server_name virtual-host2.com;

location = / {
proxy_pass http://tc_servlets;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Minion2

##########################################################################
########################## Tomcat Servlet 2 ##############################
##########################################################################


server {
listen 80;

server_name virtual-host3.com;

return 301 https://$server_name$request_uri;
}


upstream tc_servlets {
server tomcatOne:8080;
server tomcatTwo:8080;
server tomcatThree:8080;
}

server {
listen 443 ssl http2;
include conf.d/self-signed.conf;
include conf.d/ssl-params.conf;
include conf.d/header.conf;

server_name virtual-host3.com;

location = / {
proxy_pass http://tc_servlets;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

nginx.conf

user  nginx;
worker_processes auto;
worker_rlimit_nofile 15000;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;


events {
worker_connections 2048;
multi_accept on;
use epoll;
}


http {
default_type application/octet-stream;
# access_log /var/log/nginx/access.log;
# activate the server access log only when needed
access_log off;
error_log /var/log/nginx/error.log;
# don't display server version on error pages
server_tokens off;
server_names_hash_bucket_size 64;
include /etc/nginx/mime.types;
sendfile on;
tcp_nopush on;
tcp_nodelay on;

charset utf-8;
source_charset utf-8;
charset_types text/xml text/plain text/vnd.wap.wml application/javascript application/rss+xml;

include /etc/nginx/conf.d/default.conf;
include /etc/nginx/conf.d/buffers.conf;
include /etc/nginx/conf.d/timeouts.conf;
# Only activate caching in production
# include /etc/nginx/conf.d/cache.conf;
include /etc/nginx/conf.d/gzip.conf;
}

I forwarded the Tomcat service on port 443 so I can now access the sample app under:

https://<My-Server>:443/sample/

Tomcat 10 Docker Cluster

Repeat these steps on all your minion servers.