Deploying a Spring Boot App on Kubernetes


In this post, I will be explaining how to deploy a simple Spring Boot Application onto Kubernetes. I will be using MicroK8s for the Kubernetes environment. For a quick guide on how setup MicroK8s refer here.

Spring Boot App Docker Image:

Let us create a simple Spring Boot application and dockerize it. The source could be downloaded from github repo here.
Build the jar file and then dockerize it. I will be using the docker image from local, we can also push/pull this image from a Docker Repository.
Docker command to dockerize the app is:

docker build -t basic-microservice:local .

Run the command 'docker images' and you should be able to see the docker image 'basic-microservice:local'. I have tagged the image as 'local' and not as latest. This is because, since we are using local images for Kubernetes, MicroK8s has a workaround for using local images.

Follow the instructions here, to upload docker image to MicroK8s cache.

Create Deployment in Kubernetes:

Let us create a deployment.yaml file which is an abstraction which specifies how many instances of the app should be there, which docker image to be used etc.

Run the following kubectl command to create the deployment:
microk8s.kubectl  apply -f basic-ms-deployment.yml

If you observe the deployment yml, we can observe that we have mentioned the docker image name created above and also the 'imagePullPolicy' is set to 'Never', which means the docker image would not be pulled from any repository. We have also configured the 'replicas' to 1, which means only one instance will be created. 

Create Service in Kubernetes:

Let us create a service.yaml file which is an abstraction which specifies how should we access the application deployed in terms of what IP/Port should be used.

Run the following kubectl command to create the service:
microk8s.kubectl  apply -f basic-ms-service.yml

If you observe the service yml, we can observe that it has a selector node with apps tag, which points to 'basic-ms'. This should match the name we gave to our deployment.  We have also provided type as 'NodePort', which means, the application is accessible from outside the cluster using a Node IP and a static port. We have provided the port as 8086, which is the port accessible from outside.

Run the following commands to observe the Pods/Deployment/Service which are created:
microk8s.kubectl  get pods

microk8s.kubectl  get deployments

microk8s.kubectl  get services

Testing the Application:

Finally, let us test our application. When we run 'get services' command, we can see that the port is 31869 which is the port assigned by Kubernetes and 8086 is the port assigned by us.

Let us try to do a curl to our application:

We can see the REST API providing a response: "From Spring Boot!"

This concludes a very simple application being deployed to Kubernetes with relative ease. Let us look at more features in upcoming posts.


Popular posts from this blog

Pivotal Cloud Foundry (PCF) Integration with Elastic Cloud Storage (ECS)

Restful code example using Spring MVC

Spring Integration - Bulk processing Example