Deploying a containerized Docker web app solution to Kubernetes

Godfrey Menezes
4 min readOct 11, 2020

In my earlier post we talked about creating a Kubernetes cluster, deploying an NGINX deployment and then exposing as a service. Lets take a step further by actually deploying a customized solution. For that here’s what we are going to do -

  1. Create a simple Java Web application that runs in Tomcat.
  2. Deploy this application to a Tomcat Container, Save and Tag container to Container Repository.
  3. Deploy this container using Kubernetes yaml as a Deployment/Service.

Java Application for Tomcat

Here is the sample application-

When deployed the application should run as follows -

Deploy the application to Tomcat container

First up create the war file that will deploy to Tomcat, the simple command for that is -

jar -cvf HelloWorldWeb.war *

Create a Dockerfile that will pull the tomcat container, tag it locally and add the war file to the container. Here are the contents of the Dockerfile -

FROM tomcat:9.0-alpine

LABEL maintainer=”gmenezes”

ADD HelloWorldWeb.war /usr/local/tomcat/webapps/

EXPOSE 8080

CMD [“catalina.sh”, “run”]

For this I’m using the free container registry provided by Docker Hub. How to create your own container registry?

Execute the command in the directory where you have the Dockerfile and it should create the image.

docker build -t gmenezes/gpmrepod/helloworldweb.v1 .

Push the image to your container registry -

docker push gmenezes/gpmrepod/helloworldweb.v1

If all’s well the image should show up as follows -

On your local machine it should show up as follows —

Before we move forward with Kubernetes, check if the container functions as expected with the code. To do that execute the following command -

docker run -d -p 80:8080 helloworldweb.v1:latest

Check for the logs of the container -

docker logs <container id>

If the tomcat container has started it should print something like this -

05-Oct-2020 14:19:51.318 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [“http-nio-8080”]
05-Oct-2020 14:19:51.327 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [“ajp-nio-8009”]
05-Oct-2020 14:19:51.332 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 540 ms

The complete output is here -

Check if the application functions -

There you have it, your container with the application code is up and running!

Deploy the container using Kubernetes

Now that we have our Docker image created, the next step would be to have it managed and orchestrated using Kubernetes. My earlier post details about using minikube doing that. The only change in this case would be the change in the container image. We will be using this Tomcat application instead of the nginx image.

So the deployment yaml -HelloWorldWeb.yaml- is changed to reflect the image from container repository—

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: hww-deployment
spec:
selector:
matchLabels:
app: helloworldweb
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: helloworldweb
spec:
containers:
- name: helloworldweb
image: gmenezes/gpmrepod:helloworldweb.v1
ports:
- containerPort: 8080

Run the deployment -

kubectl apply -f HelloWorldWeb.yaml

Make sure that the PODs are running -

godfrey@thinkpad:~/Learning/K8$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hww-deployment-94fc69fbc-k7wfp 1/1 Running 1 12m
hww-deployment-94fc69fbc-s6864 1/1 Running 1 12m

Expose the deployment as a Service -

godfrey@thinkpad:~/Learning/K8$ kubectl expose deployment hww-deployment --type=LoadBalancer name=hww-service
service/hww-deployment exposed

Check the service to get the LB IP address -

godfrey@thinkpad:~/Learning/K8$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hww-deployment LoadBalancer 10.xxx.xx.228 10.xxx.xx.228 8080:31924/TCP 3m20s

Check if the application is being served via the IP address -

The application is up and running! One final check would be to check if the requests are being routed to both the PODs, so check these PODs individually -

kubectl exec --stdin --tty <pod name> -- /bin/bash

And the second one!

Final thoughts: we created a simple web application, deployed it to a base container, tagged and saved that container to a container registry and then finally deployed it to a kubernetes cluster.

References —

--

--