Search This Blog

Friday 5 March 2021

Deploy tomcat app on Kubernetes

Task: Deploy Tomcat App on Kubernetes 

Task Details

A new java-based application is ready to be deployed on a Kubernetes cluster. The development team had a meeting with the DevOps team share requirements and application scope. The team is ready to setup an application stack for it under their existing cluster. Below you can find the details for this:

Create a namespace named tomcat-namespace-nautilus.

Create a deployment for tomcat app which should be named tomcat-deployment-nautilus under the same namespace you created. Replicas count should be 1, the container should be named as tomcat-container-nautilus, its image should be gcr.io/kodekloud/centos-ssh-enabled:tomcat and its container port should be 8080.

Create a service for tomcat app which should be named as tomcat-service-nautilus under the same namespace you created. Service type should be NodePort. Port's protocol should be TCP, port should be 80, targetPort should be 8080 and nodePort should be 32227.

Before clicking on Finish button please make sure the application is up and running.

You can use any labels as per your choice.

Note: The kubectl on jump_host has been configured to work with the kubernetes cluster.


1. Run the following commands on the jump server to list namespaces and services

thor@jump_host /$ kubectl get namespace
NAME              STATUS   AGE
default           Active   12m
kube-node-lease   Active   12m
kube-public       Active   12m
kube-system       Active   12m

thor@jump_host /$ kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   12m

2. Create namespace as per the task request.

thor@jump_host /$ kubectl create namespace tomcat-namespace-nautilus
namespace/tomcat-namespace-nautilus created

thor@jump_host /$ kubectl get namespace
NAME                        STATUS   AGE
default                     Active   13m
kube-node-lease             Active   13m
kube-public                 Active   13m
kube-system                 Active   13m
tomcat-namespace-nautilus   Active   9s

3. Create deploy.yaml file using --dry-run command and modify later as per your requirements.

thor@jump_host ~$ kubectl create deploy tomcat-namespace-nautilus --image=gcr.io/kodekloud/centos-ssh-enabled:tomcat --dry-run=client -o yaml > deploy.yaml

deploy.yml file would look like below after modification

thor@jump_host ~$ vi deploy.yaml 
thor@jump_host ~$ cat deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment-nautilus
  namespace: tomcat-namespace-nautilus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat-deployment-nautilus
  template:
    metadata:
      labels:
        app: tomcat-deployment-nautilus
    spec:
      containers:
      - image: gcr.io/kodekloud/centos-ssh-enabled:tomcat
        name: tomcat-container-nautilus
        ports:
        - containerPort: 8080


Create a deployment 

thor@jump_host ~$ kubectl apply -f deploy.yaml
deployment.apps/tomcat-deployment-nautilus created

thor@jump_host ~$ kubectl get deploy --all-namespaces
NAMESPACE                   NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
kube-system                 coredns                      2/2     2            2           19m
tomcat-namespace-nautilus   tomcat-deployment-nautilus   1/1     1            1           106s

thor@jump_host ~$ kubectl get pods --all-namespaces
NAMESPACE                   NAME                                          READY   STATUS    RESTARTS   AGE
kube-system                 coredns-f9fd979d6-4t9x6                       1/1     Running   0          19m
kube-system                 coredns-f9fd979d6-pwfp2                       1/1     Running   0          19m
kube-system                 etcd-controlplane                             1/1     Running   0          19m
kube-system                 kube-apiserver-controlplane                   1/1     Running   0          19m
kube-system                 kube-controller-manager-controlplane          1/1     Running   0          19m
kube-system                 kube-flannel-ds-amd64-hx7nz                   1/1     Running   0          19m
kube-system                 kube-flannel-ds-amd64-rb26q                   1/1     Running   0          19m
kube-system                 kube-proxy-4qwmv                              1/1     Running   0          19m
kube-system                 kube-proxy-dc7zt                              1/1     Running   0          19m
kube-system                 kube-scheduler-controlplane                   1/1     Running   0          19m
tomcat-namespace-nautilus   tomcat-deployment-nautilus-59c4c6c6bd-657pd   1/1     Running   0          2m17s

4. Create service.yaml file using --dry-run command and modify file service.yaml as per your requirements.

thor@jump_host ~$ kubectl expose deploy tomcat-deployment-nautilus --namespace=tomcat-namespace-nautilus --name=tomcat-service-nautilus --type=NodePort --port=80 --target-port=8080 --dry-run=client -o yaml > service.yaml

service.yaml file would look like below after modification

thor@jump_host ~$ vi service.yaml 
thor@jump_host ~$ cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: tomcat-service-nautilus
  namespace: tomcat-namespace-nautilus
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 32227
  selector:
    app: tomcat-deployment-nautilus
  type: NodePort

Create a service

thor@jump_host ~$ kubectl apply -f service.yaml
service/tomcat-service-nautilus unchanged

thor@jump_host ~$ kubectl get service --all-namespaces
NAMESPACE                   NAME                      TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
default                     kubernetes                ClusterIP   10.96.0.1        <none>        443/TCP                  29m
kube-system                 kube-dns                  ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP   29m
tomcat-namespace-nautilus   tomcat-service-nautilus   NodePort    10.100.134.191   <none>        80:32227/TCP             46s

Validations:

thor@jump_host ~$ kubectl get no -o wide
NAME           STATUS   ROLES    AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
controlplane   Ready    master   30m   v1.19.0   172.17.0.11   <none>        Ubuntu 18.04.5 LTS   4.15.0-122-generic   docker://19.3.13
node01         Ready    <none>   29m   v1.19.0   172.17.0.14   <none>        Ubuntu 18.04.5 LTS   4.15.0-122-generic   docker://19.3.13

thor@jump_host ~$ curl 172.17.0.14:32227
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>SampleWebApp</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h2>Welcome to xFusionCorp Industries!</h2>
        <br>
    
    </body>
</html>

No comments: