DEV Community

Cover image for 8.Deploy Tomcat App on Kubernetes
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

8.Deploy Tomcat App on Kubernetes

Lab Information

A new java-based application is ready to be deployed on a Kubernetes cluster. The development team had a meeting with the DevOps team to share the 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-xfusion.

Create a deployment for tomcat app which should be named as tomcat-deployment-xfusion under the same namespace you created. Replica count should be 1, the container should be named as tomcat-container-xfusion, 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-xfusion under the same namespace you created. Service type should be NodePort and nodePort should be 32227.
Enter fullscreen mode Exit fullscreen mode

Before clicking on Check 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.

Lab Solutions

Step 1: Create the Namespace

First, create the required namespace:

kubectl create namespace tomcat-namespace-xfusion
Enter fullscreen mode Exit fullscreen mode

Verify the namespace was created:

kubectl get namespaces | grep tomcat-namespace-xfusion
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Tomcat Deployment YAML file

Create the deployment configuration file:

cat > tomcat-deployment.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment-xfusion
  namespace: tomcat-namespace-xfusion
  labels:
    app: tomcat-app
    team: xfusion
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat-app
      team: xfusion
  template:
    metadata:
      labels:
        app: tomcat-app
        team: xfusion
    spec:
      containers:
      - name: tomcat-container-xfusion
        image: gcr.io/kodekloud/centos-ssh-enabled:tomcat
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
EOF
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Tomcat Service YAML file

Create the service configuration file:

cat > tomcat-service.yaml << EOF
apiVersion: v1
kind: Service
metadata:
  name: tomcat-service-xfusion
  namespace: tomcat-namespace-xfusion
  labels:
    app: tomcat-app
    team: xfusion
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 32227
  selector:
    app: tomcat-app
    team: xfusion
EOF
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy the Tomcat Deployment

Apply the deployment to your Kubernetes cluster:

kubectl apply -f tomcat-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Step 5: Deploy the Tomcat Service

Apply the service configuration:

kubectl apply -f tomcat-service.yaml
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify the Deployment (Namespace-specific)

Check if the deployment was created successfully in the correct namespace:

kubectl get deployments -n tomcat-namespace-xfusion
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify the Service (Namespace-specific)

Check if the service is running in the correct namespace:

kubectl get services -n tomcat-namespace-xfusion
Enter fullscreen mode Exit fullscreen mode

Step 8: Check the Pod Status (Namespace-specific)

Verify that the Tomcat pod is running:

kubectl get pods -n tomcat-namespace-xfusion -l app=tomcat-app
Enter fullscreen mode Exit fullscreen mode

Step 9: Verify Application is Running

Check the pod logs to ensure Tomcat is running properly:

kubectl logs -n tomcat-namespace-xfusion -l app=tomcat-app --tail=10
Enter fullscreen mode Exit fullscreen mode

Step 10: Test Service Accessibility


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)