Deploying Spring Boot Application on Kubernetes
In this article, I will explain how you can create a pod, deploy a spring boot application, and manage the single node cluster with Lens IDE on Docker Desktop.
Join the DZone community and get the full member experience.Join For Free
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 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 MacGo 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.
Once it is enabled you will see something like below on your Docker menu bar.
Next, you should also set the Kubernetes context to docker-desktop if you have multiple clusters or environments running locally.
As per the 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
kubectl is a Kubernetes CLI tool for running commands against your cluster. Learn more about
kubectl here. Now we can verify the setup using the below
kubectl command. The output should be something similar to the one below:
How To Create a Pod
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.Create a pod using the below YAML file.
kubectl createand the name of the YAML file to create a pod.
Check the status of the pod we just created.
To debug the pod further and ensure it's working, we can get a shell to a running container using the
kubectl execcommand. Once inside the container, we will just use curl to verify the Nginx setup. Let's do that now.
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 just do curl and access the /states REST endpoint and see what happens.
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 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, 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.
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.
We can also view the logs of the deployment we just completed. As you can see that application is deployed and running.
You can scale, restart, edit, and remove Kubernetes manifests from Lens IDE itself.
Under networks, you can also view Kubernetes services.
Lens has a feature where it allows you to install a 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.
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.
Lens will start displaying metrics after a minute or so.
We have only covered a handful of features that Lends IDE for Kubernetes provides. It's a wrap for now.
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!
If you like what you just read, then you can buy me a coffee by clicking the link in the image below:
You can also read some of my previous articles here.
Published at DZone with permission of Ashish Choudhary. See the original article here.
Opinions expressed by DZone contributors are their own.