Deploy A Spring Boot Microservice container on a Kubernetes Cluster : A Step-by-Step Guide
By: Vishal Zadoo
Tags: Deployment, Spring Boot, Kubernetes, Container Orchestration, Docker

This article gives a step by step guidance for deploying a simple Spring Boot microservice as a container on a Kubernetes cluster.

Step 1: Lets start from the mundane stuff. Building a Spring Boot Application.

Those who are well versed with Spring Boot , can create a Spring Boot application or use an existing one and jump to Step 2.

For Spring Boot newbies, here goes :

You can create a simple web application by using the Spring Initilizr

Click Spring Initilizr

Add Web dependency and click “Generate”

Unzip the downloaded zip file in a folder of your choice, usually your eclipse workspace. I unzipped the “demo.zip” in my folder TestWorkSpace

Open Eclipse, then choose Import existing Maven projects

In the project, I created a controller and a service class — doesnt do much , returns a message

Do a Maven Clean Install:

Verify that your jar is created in the target folder

Step 2: Create a Docker image from your jar

Make sure you have Docker installed on your machine.

https://docs.docker.com/get-docker/

Create a file called Dockerfile with the below contents. Note that the JAR_FILE should point to the path of your jar file. I am running my Dockerfile from the target folder of jar file therefore *.jar works for me. You might have to modify based on where your jar is located:

After creating Dockerfile, run the below command to build the image from the same folder as your Dockerfile:

docker build -t veezee/vz-docker-example .

Run the docker ls command to verify if the image is created

docker image ls

Now push the image to the docker repository , either using the docker push command or manually from docker hub .

Step 3: Create a Kubernetes Cluster

There are multiple ways to build Kubernetes clusters, I used the Google Cloud console to create a cluster .

Google provides an easy way to create a cluster of Nodes — Create a GCP account and you get 300$ of credit free. This enables you to create a Kubernetes cluster using the Google Cloud console, without paying for it. THANK YOU Google :-)

Go to https://cloud.google.com/ to sign up for a Google Cloud account . If you already have an account, sign in, otherwise click ‘Get Started for Free’ and create a new account.

Click the drop down in the header toolbar and create a new project by clicking the ‘NEW PROJECT’ button.

Go to the main navigation menu drop down and select Kubernetes Engine > Clusters, then +Create Cluster

Add relevant configurations to your cluster. Make sure you have selected enough CPU and memory in your Nodes for your pods/deployments to function in a seamless manner. Click on Nodes> Machine Type to change Node configuration for CPU/mem.

Choose a zone closest to you. I choose the default Zone ‘us-east4-a’, which is in the Region ‘us’

Click the ‘Create’ button, after a few minutes your new Google Kubernetes Engine (GKE) cluster will be ready.

This is what it should look like

Click on Connect button and then “Run In Cloud Shell” for connecting to your cluster- Exciting times begin :)

This is how the Cloud shell should look. Run the below command to make sure kubectl is installed

kubectl version

Step 4 : Create a Deployment on the cluster

Finally , time for some exciting stuff. Explore your cluster using the following command.

kubectl get nodes

I have 3 worker nodes in my cluster — the output of the above command is like below:

Now, we need to deploy our docker image that we created in Step 2 on this cluster.

In Kubernetes, you cannot deploy containers directly on a cluster. The smallest unit of replication in Kubernetes is:

A Pod.

A pod consists of 1 (or more) containers which can be deployed on a cluster of Nodes using :

A Deployment.

Use the image you created earlier in Step 2 to create a deployment on your cluster with the create deployment command below. If you wish to create more replicas than 1 , you can specify the — replicas option. For now I am creating a deployment with only 1 pod

kubectl create deployment mydeployment — image=veezee/vz-docker-example

There — This deployment enables our container to be running inside a pod which is deployed on the cluster using the deployment.

Magical!

Pardon my drawing skills below — The below graphic enabled me to learn this concept pretty quickly . The box labelled C is the container inside the POD(Red), which gets latched onto the cluster of Nodes(Grey/Black) using the Deployment(blue)

If we need more pods in our deployment, we can scale the deployment using the scale command . For now we will skip the scaling — maybe I will explain that in detail in my next article.

Check your deployment using

kubectl get deployment

Check the pods and the container of this deployment

kubectl describe deployment mydeployment

Now — I need to expose my deployment so that other deployments can access it. We can do it using :

A Service.

kubectl expose deployment mydeployment — port=80 — target-port=8080

A service called mydeployment gets created which exposes our deployment . This service runs on port 80 and targets the pod running on port 8080. This is a service of type ClusterIP, which is used for communication within the cluster. Note: There are other service types called NodePort and LoadBalancer, which enables access from outside of the cluster. We will not be exploring them today- more on them in future articles.

Creating a clusterIP service enabled my container to be accessed from any other deployment within the cluster by referring the service name (instead of ip address).

Now lets test this theory. Lets create a pod with the image nginx and try to create our API service running on the container .

I created a pod called nginxtest with the image nginx on this cluster

Using exec I logged into the container nginxtest.

I tried to access OUR container from the nginxtest pod by calling

curl http://mydeployment:80/welcome

and voila! I get the result from our service

“Hello , Welcome to my world..”

This completes a single end to end flow of

Writing a Spring Boot microservice

Building an image for the microservice using Docker

Deploy the image container in a kubernetes cluster as a pod in a deployment

Creating a service for accessing the deployment

Accessing the service and voila- we have the desired result!

Mind you- we just scratched the surface on kubernetes . There are many more exciting functionalities that Kubernetes allows us — I plan to publish more articles describing some cool Kubernetes stuff in the coming days.

Watch out this space for more!