Learn how to setup Kubernetes locally with Minikube, build a SpringBoot application image, create a deployment and service, and access the Kubernetes dashboard.
TL;DR
-
Learn the basic Kubernetes components, including clusters, nodes, pods, and Minikube.
-
Install Docker and Minikube to create a local Kubernetes cluster.
-
Build a Docker image for a SpringBoot application.
-
Deploy the SpringBoot application to Kubernetes using
kubectl. -
Expose the application with a NodePort service.
-
Access the application and Kubernetes dashboard using Minikube commands.
In this tutorial, you will learn how to easily setup local Kubernetes cluster. Then we would create a simple Spring Boot application and deploy to Kubernetes. I would be explaining each step as I go.
In the next tutorial, we would learn how to deploy both database and application to Kubernetes cluster. Finally, we’ll learn how to automate the deployment using a Helm Chart.
- Overview of Kubernetes and its Components
- Setup Local Kubernetes Cluster (Minikube)
- Build the SpringBoot Image
- Create the Deployment
- Create the Service
- Access the Minikube Dashboard
1. Overview of Kubernetes and its Components
Kubernetes is a container orchestration platform that helps you create and deploy images of of your application. To understand how it works, you need to know about these components.
Cluster – A cluster in Kubernetes has the same meaning as in other contexts. It is a collection of nodes (or Virtual Machines)
Minikube – Minikube is simply a local Kubernetes. In this way you don’t need to a cloud environment to learn and develop Kubernetes.
Node – A node is a single virtual machine. Think of it as a single computer.
Pod – A pod is the smallest unit of deployment in Kubernetes. A node can contain one or more pod. So your application actually runs in containers called pods.
2. Setup Local Kubernetes Cluster
We would go through how to setup and test Kubernetes cluster on Mac. And it would be just a few simple steps.
Step 1 – Download and install Docker (it’s quite simple but you can find steps here)
Step 2 – Install Minikube. For Mac, the easiest way is the use Homebrew via the command
brew install minikube
Other ways to install here – https://minikube.sigs.k8s.io/docs/start/
You can also see learn about Docker, Containers and Kubernetes here
Step 3 – Start Kubernetes using the following command:
minikube start
This command starts the local kubernetes cluster.
3. Build the Image of Your SpringBoot Application
You need to have an existing Spring Boot application. Now we would first create a Dockerfile, then we use the Dockerfile to build an image of our SpringBoot application.
Step 1 – Create a Dockerfile. The content is shown below:
FROM adoptopenjdk/openjdk11:alpine-jre ADD target/fleetmsv2-0.0.1-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]
Step 2 – Run the application to create the jar file.
Step 3 – Run this command to make Kubernetes work with your local Docker installation
eval $(minikube docker-env)
Step 4 – Build the docker image using the command below:
docker build -t springboot-kubernetes:1.0 .
4. Create the Deployment
First we need to create a deployment. We can do this either using a command or using a yaml file. In this tutorial, we would use a command. In subsequent tutorial, we would use a yaml file. Same goes for the service.
Step 1 – Run this command to create the deployment
kubectl create deployment springboot-kubernetes --image=springboot-kubernetes:1.0 --port=8080
This command creates a deployment which starts your SpringBoot application inside a Kubernetes cluster.
Step 2 – Use the kubectl get pods command to check that the pod is up and running. This command also gives you the name of the pod
Step 3 – Use the kubectl logs <pod_name> to view the logs. You’ll see that the application is started at pot 8080.
5. Create the Service and Access the Application
We would now create a service. A service would help us to connect to the application running in a node inside the Kubernetes cluster. So we need to expose the deployment
Step 1 – Get the name of the deployment using the command kubectl get deployments
Step 2 – Create a service by exposing the deployment using the command below:
kubectl expose deployment springboot-kubernetes --type=NodePort
You can get the services name using the kubectl get services command
Step 3 – Get the service url using the command
minikube service springboot-kubernetes
This command would display the services url for container running inside the pod. It would also open the default browser and redirect to the url.
6. Access the Kubernetes Dashboard
The minikube dashboard gives you a user interface showing the Kubernetes status as well as the services, workloads, pods, jobs etc.
To access the Kubernetes dashboard, enter the command:
minikube dashboard
This command would start the Minikube dashboard and display the corresponding url. Click on the link and it would launch the dashboard as shown below:
FAQs
What is Minikube?
Minikube is a local Kubernetes environment that allows you to create and use a Kubernetes cluster on your computer without requiring a cloud environment.
How do I setup Kubernetes locally?
Install Docker and Minikube, then start the local Kubernetes cluster using the minikube start command.
How do I deploy a SpringBoot application to Minikube?
Build a Docker image of your SpringBoot application, configure Minikube to use the local Docker environment, and create a Kubernetes deployment using the application image.
How do I expose a SpringBoot application running in Minikube?
You can expose the deployment using a NodePort service with kubectl expose deployment, then obtain the service URL using minikube service.
How do I access the Minikube dashboard?
Run the minikube dashboard command. Minikube will start the dashboard and provide a URL that you can open in your browser.
Final Thoughts
Setting up Kubernetes locally with Minikube provides a practical way to learn Kubernetes without needing a cloud environment. In this tutorial, you build a Docker image of a SpringBoot application, deploy it to the local cluster, expose it through a service, and monitor it through the Minikube dashboard.
This setup provides the foundation for the next stages of the tutorial series, including deploying both the SpringBoot application and database to Kubernetes and eventually automating deployments with Helm Charts.