How to Deploy Tomcat on Kubernetes Step by Step

Overview

In this post, we are going to see how to deploy tomcat on Kubernetes.  How to Deploy Tomcat based web application into Kubernetes. We are taking Google Cloud - Kubernetes Engine (GKE) for our example and creating our Kubernetes Cluster with 3 nodes

You can do all these steps and test this with your Free 12 month Google Cloud account itself but make sure you shut down the resources once your setup is completed to avoid charges.

We are going to use our Tomcat image we have created in the earlier post. which can be pulled directly from this DockerHub using the following command

docker pull saravak/tomcat8

Tomcat Kubernetes

 

Steps to Deploy Tomcat in Kubernetes on Google Cloud

  1. Creating a GKE Cluster with 3 nodes
  2. Check the Context is set to your newly created cluster.
  3. List the nodes of the cluster
  4. Create a K8s Deployment to Deploy Tomcat on Kubernetes
  5. List all the deployments
  6. List the pods created by the deployment
  7. ScaleUP the deployment
  8. Validate if new Pods are created after Scaling up
  9. Expose it as a Service with Load balancing
  10. Access your application using the Service External IP and Test

 

Step1: Creating a GKE Cluster with 3 nodes

in my local machine, I had to install the gcloud CLI and setup my access with google cloud once this is done. I have used the gcloud cli command to create a Kubernetes cluster with 3 nodes.

This is the easiest way to set up Kubernetes.

➜  gcloud container clusters create mwik8scluster – num-nodes 3

This will enable the autorepair feature for nodes. Please see 

https://cloud.google.com/kubernetes-engine/docs/node-auto-repair

for more information on node autorepairs. 
Creating cluster mwik8scluster in asia-south1-a... 
Cluster is being health-checked (master is healthy)...done. 
Created [https://container.googleapis.com/v1/projects/kubernetestest-270009/zones/asia-south1-a/clusters/mwik8scluster]. 
To inspect the contents of your cluster, go to: 
https://console.cloud.google.com/kubernetes/workload_/gcloud/asia-south1-a/mwik8scluster?project=kubernetestest-270009
kubeconfig entry generated for mwik8scluster.

NAME           LOCATION       MASTER_VERSION  MASTER_IP     MACHINE_TYPE   NODE_VERSION    NUM_NODES  STATUS
mwik8scluster  asia-south1-a  1.14.10-gke.27  35.244.39.79  n1-standard-1  1.14.10-gke.27  3          RUNNING

 

Step2: Check the Context is set to your newly created cluster.

Kubectl is a command-line interface and administration tool for managing your Kubernetes cluster, Wheater you are using minikube, EKS or GKE the kubectl remains your command-line interface for Your Kubernetes Cluster

So make sure you are switching to the right Kubernetes cluster by changing the context to the Google Kubernetes Engine and to the cluster you have created in the previous step.

# To Change the Context
➜  kubectl config use-context gke_kubernetestest-270009_asia-south1-a_mwik8scluster
Switched to context "gke_kubernetestest-270009_asia-south1-a_mwik8scluster" 

# To make sure context is changed
➜  kubectl config current-context
gke_kubernetestest-270009_asia-south1-a_mwik8scluster

Step3: list the nodes of the cluster

In the first step we have created a Kubernetes cluster with gcloud command line with 3 nodes (or) worker nodes.  Let us list the nodes and make sure they are available.

kubectl get nodes command would show the list of nodes available in our cluster

➜  kubectl get nodes
NAME                                           STATUS   ROLES    AGE     VERSION
gke-mwik8scluster-default-pool-04115284-3tsb   Ready    <none>   4m16s   v1.14.10-gke.27
gke-mwik8scluster-default-pool-04115284-kv9b   Ready    <none>   4m16s   v1.14.10-gke.27
gke-mwik8scluster-default-pool-04115284-sz9l   Ready    <none>   4m16s   v1.14.10-gke.27

 

 

Step4: Create a K8s Deployment to Deploy Tomcat on Kubernetes

Deployment is a Declarative approach to create and manage pods, when you are creating deployments, replica sets ( new gen replication controller) would be created by default and pods would be managed through this replicaset.

ReplicaSet is responsible to maintain the pod count and make sure the desired count of pods running for any deployment. By default, the replicaset would create a single pod when you are creating a deployment which can be scaled up or down later.

Besides that, Kubernetes Deployments would make your deployment and rolling update and recreation tasks easy and efficient. Deploying a container directly as a pod is not a production approach and deployment is the standard way of deploying any service/app into Kubernetes.

To Create deployments, you can either use yaml file or use the kubectl create deployment command.

Note*:  We are not defining any namespace here so the default namespace would be taken

Using CLI

➜  kubectl create deployment tomcatinfra – image=saravak/tomcat8
deployment.apps/tomcatinfra created

Using YAML file

Save this file as tomcatinfra.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcatinfra
spec:
  replicas: 1
  template:
    metadata:
      name: tomcatinfra
      labels:
        app: tomcatinfra
    spec:
      containers:
      - image: saravak/tomcat8
        name: tomcatapp

Execute the kubectl apply command

➜  kubectl apply -f tomcatinfra.yml
deployment.apps/tomcatinfra created

Now we have successfully created the deployment, this would also create a replica set automatically behind the screen

 

Step5: List all the deployments

Once the deployment has been created using CLI or YAML file.  Let us validate if the deployments are created properly and ready

➜  kubectl get deployments
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
tomcatinfra   1/1     1            1           24s

You can see here the deployment we have created in the last step is present with the age of 24seconds (created recently) and in a READY state.

If you are wondering what is 1/1 - It represents the number of pods created by the replicaset as explained earlier the default number of pods created by replica set is one. it can be scaled up and down

 

Step6: List the pods created by the deployment

Make sure that your pods are running, By default, a replica set would create only one pod and it can be scalable at any time using the scale command.

If you have increased the replicas value during the Deployment task. You would see a different number of pods than mine.

➜   kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
tomcatinfra-7dddd9bcf4-ksbtm   1/1     Running   0          30s

Step7: ScaleUP the deployment

Now the production traffic is increasing and we want to quickly scale up the number of pods on this deployment.  We can easily do that with kubectl scale command

➜  kubectl scale – replicas=3 deployment tomcatinfra
deployment.extensions/tomcatinfra scaled

In the preceding command snippet, you can see that we are increasing the replica value from 1 to 3 and it will create two more new pods to match the desired count of 3.

you can also scale down by decreasing the number of replicas at any point of time in future.

If you describe the deployment using kubectl describe deployments command, you can see the NewReplicaSet value is changed to 3

➜  kubectl describe deployments tomcatinfra

Name:                   tomcatinfra
Namespace:              default
CreationTimestamp:      Sat, 18 Apr 2020 12:35:18 +0530
Labels:                 app=tomcatinfra
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=tomcatinfra
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
Labels:  app=tomcatinfra
Containers:
tomcat8:
Image:        saravak/tomcat8
Port:         <none>
Host Port:    <none>
Environment:  <none>
Mounts:       <none>
Volumes:        <none>
Conditions:
Type           Status  Reason
----           – ----  – ----
Progressing    True    NewReplicaSetAvailable
Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   tomcatinfra-7dddd9bcf4 (3/3 replicas created)
Events:          <none>

You can also validate this using the kubectl get deployments command

➜  kubectl get deploy tomcatinfra
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
tomcatinfra   3/3     3            3           5h13m

 

Step8: Validate if new Pods are created after Scaling up

Since we have scaled from 1 to 3 in the last step. we should list the pods with the tag app=tomcatinfra which is defined during the creation of deployment.

You can see that the pod count has been increased from one to three.

➜  kubectl get pods -l app=tomcatinfra
NAME                           READY   STATUS    RESTARTS   AGE
tomcatinfra-7dddd9bcf4-gcrp2   1/1     Running   0          5h51m
tomcatinfra-7dddd9bcf4-gpqth   1/1     Running   0          5h51m
tomcatinfra-7dddd9bcf4-ksbtm   1/1     Running   0          5h55m

Step9: Expose it as a Service with Load balancing

Now our deployment is ready with three pods. We need to access this from the public and make sure all three are handling the load when a request comes in.

To achieve this we are exposing our deployment as a service and setting the type as Load Balancer to make the pods handle the load.

$ kubectl expose deployment tomcatinfra – port=80 – target-port=8080 -type LoadBalancer

service/tomcatinfra exposed

Here

--port represents the service listening port for requests to come in

--target-port represents the port where our container is listening. It is defined with in the Dockerfile itself and this is a default port of tomcat

Now let us know more information about the service we have created using the kubectl describe service command

$ kubectl describe svc tomcatinfra
Name:                     tomcatinfra
Namespace:                default
Labels:                   app=tomcatinfra
Annotations:              <none>
Selector:                 app=tomcatinfra
Type:                     LoadBalancer
IP:                       10.47.255.230
Port:                     <unset>  80/TCP
TargetPort:               8080/TCP
NodepPort:                 <unset>  31710/TCP
Endpoints:                10.44.1.5:8080,10.44.1.6:8080,10.44.2.5:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
Type    Reason                Age   From                Message
----    – ----                – --  – --                – -----
Normal  EnsuringLoadBalancer  14s   service-controller  Ensuring load balancer

The preceding snippet shows more details about the service we have just created including the Ports and Endpoints etc.

 

Step10: Access your application using the Service External IP

Now it is time to test the application. To know the public IP address of the service you have created. use the kubectl get svc command and look for the service we have just created and grab the IP shown in the External-IP column.

In my case, it is 35.200.217.28.

You can use this IP behind any DNS name or you can manage it with Ingress Controller like nginx with the Domain name like www.example.com

For now, you can access the application with IP http://35.200.217.28/SampleWebApp/

$ kubectl get svc

NAME          TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
kubernetes    ClusterIP      10.47.240.1     <none>          443/TCP        24m
tomcatinfra   LoadBalancer   10.47.255.230   35.200.217.28   80:31710/TCP   106s

to know more about the Kubernetes ingress and how it works.refer the following article

Kubernetes Ingress Example on Google Cloud

 

Conclusion

Hope this article serves as a quick reference to create and deploy tomcat on Kubernetes. You can use this as a Kubernetes tomcat example setup.

Though this article is created for GoogleCloud, Except the Cluster creation part with gcloud all other instructions are applicable for minikube and Amazon EKS too.

If you have any questions or feedback let me know comments

 

Cheers
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading