Let's Discuss
Enquire NowIn 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,
$ docker build -t akhilck/dw-hw:1.0 .
Sending build context to Docker daemon 18.16MB
Step 1/5 : FROM openjdk:8-jre
---> 447ab1e8e627
Step 2/5 : WORKDIR /
---> Using cache
---> 8c1fe829031e
Step 3/5 : ADD target/hw-1.0-SNAPSHOT.jar app.jar
---> b26b7a448d70
Step 4/5 : EXPOSE 8080 8081
---> Running in 6153da2ff348
Removing intermediate container 6153da2ff348
---> 3232f2add50f
Step 5/5 : ENTRYPOINT ["java", "-jar", "app.jar", "server"]
---> Running in 2e29c1f29f5e
Removing intermediate container 2e29c1f29f5e
---> c3963ea5fc6f
Successfully built c3963ea5fc6f
Successfully tagged akhilck/dw-hw:1.0
In order to push this image to the Docker hub, we must log in first.
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (akhilck): akhilck
Password:
Login Succeeded
Now, we will push it using the docker command
$docker push image_name
$ docker push akhilck/dw-hw:1.0
The push refers to repository [docker.io/akhilck/dw-hw]
3436bfaff97e: Pushed
1.0: digest: sha256:793284c6ca6c14e0e2933c671dfecf0365b82666b397c3d280e043da45c2be8e size: 2211
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,
$ minikube start
Starting local Kubernetes v1.10.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.
To run the java application we should apply hw-service.yaml
file in the root directory.
$ kubectl apply -f hw-service.yaml
service/dw-hw created
replicationcontroller/dw-hw created
Now, to see the services running,
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dw-hw NodePort 10.103.150.37 <none> 8080:30697/TCP 2m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6m
kubectl get pods
will list all the pods that are running right now and get log kubectl logs pod_name -p
can be used.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
dw-hw-s48ft 1/1 Running 0 4m$ kubectl logs dw-hw-s48ft -p
Jul 10, 2018 6:07:04 PM com.dex.hw.application.Application main
INFO: Hello World
INFO [2018-07-10 18:07:05,767] org.eclipse.jetty.util.log: Logging initialized @1641ms to org.eclipse.jetty.util.log.Slf4jLog
INFO [2018-07-10 18:07:05,847] io.dropwizard.server.DefaultServerFactory: Registering jersey handler with root path prefix: /
INFO [2018-07-10 18:07:05,848] io.dropwizard.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO [2018-07-10 18:07:05,853] io.dropwizard.server.ServerFactory: Starting Application
INFO [2018-07-10 18:07:06,037] org.eclipse.jetty.setuid.SetUIDListener: Opened application@53a9fcfd{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
INFO [2018-07-10 18:07:06,038] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@21f459fc{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
INFO [2018-07-10 18:07:06,039] org.eclipse.jetty.server.Server: jetty-9.4.z-SNAPSHOT
INFO [2018-07-10 18:07:06,524] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:GET /hw/{name} (com.dex.hw.resource.HwResource)
In order to see the minikube dashboard, use
$minikube dashboard
Opening kubernetes dashboard in default browser...
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.
Have a project in mind that includes complex tech stacks? We can be just what you’re looking for! Connect with us here.
Disclaimer: The opinions expressed in this article are those of the author(s) and do not necessarily reflect the positions of Dexlock.