Setup Kubernetes Locally – Deploy SpringBoot Application – Step by Step Tutorial

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.

  1. Overview of Kubernetes and its Components
  2. Setup Local Kubernetes Cluster (Minikube)
  3. Build the SpringBoot Image
  4. Create the Deployment
  5. Create the Service
  6. 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:

Kubernetes Minikube Dashboard
Kubernetes Minikube Dashboard
User Avatar

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

Your email address will not be published. Required fields are marked *