Deploy Tomcat App on Kubernetes

Always learning
4 min readMay 13, 2024

Apache Tomcat, also known as Tomcat Server, proves to be a popular choice for web developers building and maintaining dynamic websites and applications based on the Java software platform.

It’s reportedly called ‘Tomcat’ web server because the founder saw it as an animal that could take care of and fend for itself.

buymeacoffee ☕ 👈 Click the link

The Apache server is an HTTP web server, while the Apache Tomcat server is mainly a Java application server.

Kubernetes automates operational tasks of container management and includes built-in commands for deploying applications, rolling out changes to your applications.

Scaling your applications up and down to fit changing needs, monitoring your applications, and more making it easier to manage applications.

Deploying Tomcat on a Kubernetes cluster using a dedicated namespace, NodePort service, and a single deployment.

A Kubernetes namespace provides the scope for Pods, Services, and Deployments in the cluster. Users interacting with one namespace do not see the content in another namespace.

Create a Namespace for Tomcat

This creates a namespace named tomcat-namespace-ibbus to isolate Tomcat resources from other applications in our cluster.

kubectl create namespace tomcat-namespace-ibbus

Create a NodePort Service

The NodePort service serves as the external entry point for incoming requests for your app. The assigned NodePort is publicly exposed in the kubeproxy settings of each worker node in the cluster.

Every worker node starts listening on the assigned NodePort for incoming requests for the service.

We will use the imperative approach, and then save it to a manifest file for some changes.

kubectl create service nodeport  tomcat-service-ibbus  -n tomcat-namespace-ibbus \
--tcp 8080:8080 --dry-run=client -o yaml > tomcat-service.yaml

Service object

A Kubernetes service is a logical abstraction for a deployed group of pods in a cluster (which all perform the same function).

Since pods are ephemeral, a service enables a group of pods, which provide specific functions (web services, image processing, etc.) to be assigned a name and unique IP address (clusterIP).

apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: tomcat-service-ibbus
name: tomcat-service-ibbus
namespace: tomcat-namespace-ibbus
spec:
ports:
- name: 8080-8080
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: tomcat-service-ibbus
type: NodePort
status:
loadBalancer: {}

Selector: Labels to select pods for exposing the service. Here, pods with the label app: tomcat will be included.

Create a Tomcat Deployment

Tomcat includes a web application, deployed by default on context path /manager , that allows you to deploy and un deploy applications on a running Tomcat server without restarting it.

Like with the service, we will use the imperative approach and save it to a file.

kubectl create deployment tomcat-deployment-ibbus -n tomcat-namespace-ibbus \
--replicas=1 --image=gcr.io/kodekloud/centos-ssh-enabled:tomcat \
--port 8080 --dry-run=client -o yaml > tomcat-deployment.yaml

Deployment object

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: tomcat-deployment-ibbus
name: tomcat-deployment-ibbus
namespace: tomcat-namespace-ibbus
spec:
replicas: 1
selector:
matchLabels:
app: tomcat-deployment-ibbus
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: tomcat-deployment-ibbus
spec:
containers:
- image: gcr.io/kodekloud/centos-ssh-enabled:tomcat
name: centos-ssh-enabled
ports:
- containerPort: 8080
resources: {}
status: {}

Selector: Labels to identify pods managed by our deployment

Labels: Labels for the pod template (app: tomcat)

Pod Running

NodePort

Service

Deployment

ReplicaSet

If you are accessing the tomacat server localhost:8080

Thank you 🙏 for taking the time to read our blog.

--

--

Always learning

கற்றுக் கொள்ளும் மாணவன்...