Deploying Spring Boot Application on Kubernetes

Ashish Choudhary
Javarevisited
Published in
7 min readDec 22, 2020

--

In this article, I will explain how you can enable kubernetes, create a pod, deploy a spring boot application, monitor the single node cluster with Lens IDE on Docker Desktop.

Image Source Docker

What you will learn

  • How to Enable Kubernetes With Docker Desktop for Mac
  • How to Create a Pod
  • How to Deploy a Working Spring Boot Application on Kubernetes
  • How to Monitor and Manage Kubernetes Cluster with Lens IDE

Moving on, you may not know this, but you very well can have a functional single-node Kubernetes cluster running locally with your Docker Desktop by following the below steps. But wait, what is Docker Desktop?

What is Docker Desktop?

As per the official documentation.

Docker Desktop is an application for MacOS and Windows machines for the building and sharing of containerized applications and microservices.

You can follow the official Docker Desktop installation guide if you have not installed it already.

How to Enable Kubernetes With Docker Desktop for Mac

  • Go to Preferences in the Docker menu bar. Then click the Kubernetes tab. Click the checkbox to enable Kubernetes and switch the default orchestrator to Kubernetes. It will take some time to enable Kubernetes.
Enable Kubernetes
  • Once it is enabled you will see something like below on your Docker menu bar.
Kubernetes Running
  • Next, You should also set the Kubernetes context to docker-desktop if you have multiple clusters or environments running locally.
Context set to docker-desktop

As per official documentation

The mac Kubernetes integration provides the Kubernetes CLI command at /usr/local/bin/kubectl. This location may not be in your shell’s PATH variable, so you may need to type the full path of the command or add it to the PATH .

kubectl is a Kubernetes CLI tool for running commands against your cluster.

Now we can verify the setup using the below kubectl command. The output should be something similar to the below.

kubectl get nodesNAME             STATUS   ROLES    AGE   VERSIONdocker-desktop   Ready    master   17d   v1.18.8

How to Create a Pod on Kubernetes

A Pod in Kubernetes is the smallest possible execution unit. It can have one more container in it. However, we will test the setup by creating a simple pod with a single container image on the local Kubernetes cluster by following the below steps. This container runs an Nginx image.

  1. Create a pod using the below YAML file.
apiVersion: v1kind: Podmetadata:name: nginxlabels:app: nginxspec:containers:- name: mycontainerimage: docker.io/nginxports:- containerPort: 80

Use kubectl create and name of the YAML file to create a pod.

kubectl create -f pod.yamlpod/nginx created

2. Check the status of the pod we just created.

kubectl get podsNAME    READY   STATUS    RESTARTS   AGEnginx   1/1     Running   0          2m38s

3. To debug the pod further and it’s working we can get a shell to a running container using the kubectl exec command. Once inside the container, we will just use curl to verify the Nginx setup. Let’s do that now.

kubectl exec -it nginx -- /bin/sh# curl 10.1.0.65<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>

How to Deploy a Working Spring Boot Application on Kubernetes

Let’s deploy a working Spring Boot application on our local Kubernetes cluster. We will be using the Indian-states application for the demo. You can check out this GitHub repository to know more about this application.

In my previous article, I have already explained the service and deployment resources for this Kubernetes application.

Now we will just clone this repository and create Kubernetes manifests using the below command.

git clone https://github.com/yrashish/indian-statescd indian-states/k8skubectl create -f myservice.yamlservice/states createdkubectl create -f mydeployment.yamldeployment.apps/states createdkubectl get allNAME                          READY   STATUS    RESTARTS   AGEpod/states-6664b9dbf6-l2tgj   1/1     Running   0          8sNAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGEservice/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          55sservice/states       NodePort    10.96.78.225   <none>        8080:31238/TCP   15sNAME                     READY   UP-TO-DATE   AVAILABLE   AGEdeployment.apps/states   1/1     1            1           8sNAME                                DESIRED   CURRENT   READY   AGEreplicaset.apps/states-6664b9dbf6   1         1         1       8s

Now just do curl and access the /states REST endpoint and see what happens.

curl localhost:31238/states[{“name”:”Andra Pradesh”,”capital”:”Hyderabad”},{“name”:”Arunachal Pradesh”,”capital”:”Itangar”},{“name”:”Assam”,”capital”:”Dispur”},{“name”:”Bihar”,”capital”:”Patna”},{“name”:”Chhattisgarh”,”capital”:”Raipur”},{“name”:”Goa”,”capital”:”Panaji”},{“name”:”Gujarat”,”capital”:”Gandhinagar”},{“name”:”Haryana”,”capital”:”Chandigarh”},{“name”:”Himachal Pradesh”,”capital”:”Shimla”},{“name”:”Jharkhand”,”capital”:”Ranchi”},{“name”:”Karnataka”,”capital”:”Bangalore”},{“name”:”Kerala”,”capital”:”Thiruvananthapuram”},{“name”:”Madhya Pradesh”,”capital”:”Bhopal”},{“name”:”Maharashtra”,”capital”:”Mumbai”},{“name”:”Manipur”,”capital”:”Imphal”},{“name”:”Meghalaya”,”capital”:”Shillong”},{“name”:”Mizoram”,”capital”:”Aizawi”},{“name”:”Nagaland”,”capital”:”Kohima”},{“name”:”Orissa”,”capital”:”Bhubaneshwar”},{“name”:”Rajasthan”,”capital”:”Jaipur”},{“name”:”Sikkim”,”capital”:”Gangtok”},{“name”:”Tamil Nadu”,”capital”:”Chennai”},{“name”:”Telangana”,”capital”:”Hyderabad”},{“name”:”Tripura”,”capital”:”Agartala”},{“name”:”Uttaranchal”,”capital”:”Dehradun”},{“name”:”Uttar Pradesh”,”capital”:”Lucknow”},{“name”:”West Bengal”,”capital”:”Kolkata”},{“name”:”Punjab”,”capital”:”Chandigarh”}]

How to Monitor and Manage Kubernetes Cluster with Lens IDE

Lens IDE for Kubernetes is a desktop application for macOS, Windows, and Linux to manage your cluster. It’s completely free and open source. You can download it from here. Developers, DevOps engineers, SRE, and also people who are learning Kubernetes on their laptops can use this to view and manage their clusters. Following are some of the highlighted features that it provides.

  • Manage multiple clusters with a single IDE.
  • 360-degree view of your cluster.
  • Prometheus integration
  • Context-aware built-in terminal
  • Built-in support for managing Helm charts

After installing it you can first add a cluster. Lens connects to your Kubernetes cluster using the kubeconfig file which is typically located in your $HOME/.kube directory. You have the option to paste it as a text file also. Now select the kubeconfig file, context, and click on Add cluster.

Add a cluster to Lens IDE

After adding the cluster you can see it on the left side navigation bar. For demo purposes, I have redeployed the Spring Boot application. Under events, you can see that our application image is getting pulled.

Kubernetes Cluster Overview

We can also view the logs of the deployment we just completed. As you can see that application is deployed and running.

Spring Boot Application logs

You can scale, restart, edit and remove Kubernetes manifests from Lens IDE itself.

View Deployments

Under networks, you can also view Kubernetes services.

View Services

Lens has a feature where it allows you to install Prometheus stack in your cluster so that you can gather metrics about the cluster and its nodes. For installation, right-click on the cluster icon in the Lens UI’s top-left corner and select Settings.

Lens Settings

On the Settings page, under features, you can see a Metrics section and a button to install Prometheus. Click Install to deploy the Prometheus stack to your cluster.

Install Prometheus

Lens will start displaying metrics after a minute or so.

Prometheus metrics

We have only covered a handful of features that Lends IDE for Kubernetes provides. It’s a wrap for now.

Conclusion

In this article, we have learned how to enable Kubernetes on Docker Desktop for Mac, created a basic pod, deployed a Spring Boot application, and monitored our single-node Kubernetes cluster with the help of Lens IDE. What are you waiting for? Go Containerize and share your applications?

Support me

If you like what you just read, then you can buy me a coffee by clicking the link in the image below:

Further Reading

You can continue reading some of my previous articles.

--

--