Deploying Spring apps to k8s cluster using Minikube

In this article we will learn how to run a Spring Boot application on a local Kubernetes cluster using Minikube and Kubectl.

Pre-requisite installations

  1. Docker
  2. Minikube
  3. Kubectl

Verify Minikube installation

Run the command

minikube version

on terminal to see the version and commit.

Start the minikube on docker

minikube start --driver=docker

Check the status of your instance by running

minikube status

Run the following command to see the url of your Kubernetes control plane and KubeDNS

kubectl cluster-info

Check node status

To see the status of the node in your cluster, run the command

kubectl get node

Lets create a String Boot App

  1. Go to start.spring.io
  2. Add the Spring Web dependency
  3. Generate the project
  4. Open the maven project in any IDE

Note : The artifact id used for this article is k8s-test

Add an endpoint

Add a controller class and add an endpoint to verify after deployment

K8sController.java

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class K8sController 
{
    @GetMapping("/apitest")
    public String displayMessage()
    {
        return "If you are seeing this then your app is successfully deployed.";
    }
}

Add a dockerfile

Create a dockerfile in the root directory of the project to generate the docker image

Dockerfile

FROM openjdk:8
EXPOSE 8080
ADD target/k8s-test.jar k8s-test.jar
ENTRYPOINT ["java","-jar","/k8s-test.jar"]

Allow Kubernetes to read your docker image

minikube docker-env
@FOR /f "tokens=*" %i IN ('minikube -p minikube docker-env --shell cmd') DO @%i

Build the docker image for the Spring application

Go to the project directory and run the following command

docker build -t k8s-test:1.0

Verify if the image was successfully created by running

docker images

Create a deployment

Using the docker image we just created, we will create a deployment object using terminal.

kubectl create deployment k8s-spring-app --image=k8s-test:1.0 --port=8080

Verify your deployment by running

kubectl get deployment

Get the details of the deployment by running

kubectl describe deployment k8s-spring-app

Check the status of the pods by running

kubectl get pods

You can check the logs of your app by running

kubectl logs <pod-name>

Create a service object

We need to expose our deployment so that our app can be accessed from outside of the cluster

kubectl expose deployment k8s-spring-app --type=NodePort

Verify the status of your service by running

kubectl get service

Get the service/proxy URL

Our service will redirect our request from outside of the cluster to the respective pods. To get the service or proxy URL, run

minikube service k8s-spring-app --url

Now use this url and hit the endpoint

<proxy-url>/apitest

in browser or any API testing tool.

If you see the success message being returned by the API, then our Spring Boot application is successfully deployed.

Monitor in the minikube dashboard

You can monitor your cluster, deployment , service and pods in the minikube dashboard by running

minikube dashboard

Terminate the deployment, service and minikube instance.

Terminate service

kubectl delete service k8s-spring-app

Terminating deployment

kubectl delete deployment k8s-spring-app

Terminate minikube

minikube stop

I hope you found the article useful.

Lets connect :

Happy Coding :) .