Deploying drop wizard java application with Kubernetes and Docker

Dec 9, 2021by, Akhil C K

Technology

In this article we are going to learn how we can create a simple drop wizard application in java and dockerize the application, finally, we’ll deploy this containerized application onto Kubernetes.

Prerequisites

So, to start with, we must first ensure that we have installed the following

  • Docker — allows us to build, run and test Docker containers outside of Kubernetes on our local development machine. To install,
> $brew cask install docker
  • Minikube — It is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a VM on your laptop for users looking to try out Kubernetes or develop with it day-to-day. To install,
> $brew cask install virtualbox
> $brew install minikube
  • Docker Hub account — In order to push and store your copies of the container images that we will build.
  • Java 8 (or 9) SDK and Maven — We will be building code with the Maven build and dependency tool that uses Java 8 features.
  • I am assuming that you already developed Dropwizard Application and running it on your machine. If not, please clone this simple hello world project to start with.

We’re all set to go. Let the game begin!

Dockerizing the application

Open the app in your favorite IDE and build it using maven. I am using intelliJ IDE and to build,

$mvn clean package
....
[INFO] ------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------
[INFO] Total time: 6.023 s
[INFO] Finished at: 2018-07-10T01:41:18+05:30
[INFO] Final Memory: 30M/101M
[INFO] ------------------------------------------------------------

This will produce a runnable Java ARchive file under ./target directory.

Now, we are going to build a docker container image. Dockerfile will contain all the build steps and configuration for the docker image.

Let’s have a look,

FROM openjdk:8-jre
WORKDIR /
ADD target/hw-1.0-SNAPSHOT.jar app.jar
EXPOSE 8080 8081
ENTRYPOINT ["java", "-jar", "app.jar", "server"]

To build a docker image,

In order to push this image to the Docker hub, we must log in first.

Now, we will push it using the docker command

$docker push image_name

We can see the pushed images on logging into the docker cloud.


Deploying onto Kubernetes

Now, we’re going to deploy the docker image to Kubernetes and test it. Start minikube with $minikube start command,

To run the java application we should apply hw-service.yaml file in the root directory.

Now, to see the services running,

kubectl get pods will list all the pods that are running right now and get log kubectl logs pod_name -p can be used.

In order to see the minikube dashboard, use

This will open your browser and we can see the pods running, services running, logs of the pod, etc.

Conclusion

That’s it! In this article, We have just learned to build a docker image of a drop wizard HelloWorld java application and run it on Kubernetes.