03: Kubernetes (i.e. Minikube) – deploy a spring boot microservice app

This extends Kubernetes (i.e. Minikube) – deploy a node js service.

Docker containers & Kubernetes for deploying Spring Boot microservices. As mentioned earlier in Kubernetes, Docker & Microservices interview Questions & Answers.

Docker gives you application isolation, and solves the dependency hell issues. For example, if you have two microservices servers, and both of which use Spring-boot. But, each of them uses a slightly different version of flask and other dependencies. Running these API servers under different containers provides an easy way to avoid the “dependency hell” issue.

Get the Spring-boot app going first

Step 1: Go to https://start.spring.io/ to create a skeleton spring-boot application. Click on “generate” to download the “minikube-springboot.zip” file.

minikube-springboot app

minikube-springboot app

Step 2: Unzip “minikube-springboot” inside the “projects folder” and open this folder with your IDE of your choice. I am opening the with “Visual Studio Code” IDE.

Minikube springboot structure

Minikube springboot structure

Step 3: Create “HelloController.java” inside the package “com.myapp.minikubespringboot.controller;” under “src/main/java”. “controller” is a new folder.

Step 4: The “pom.xml” looks like

Step 5: Open a terminal windo and run:

This builds the jar file “minikube-springboot-0.0.1-SNAPSHOT.jar” in the “target” folder. Lets’s run the app.

Step 6: Open a browser and execute: “http://localhost:8080/hello”

Which outputs:

“Hello from Spring boot on Minikube”.

Dockerfile

Step 7: Create the “Dockerfile” in the base directory.

Step 8: The pom.xml file needs to be updated as shown below with additional plugins.

Step 9: Run the “./mvnw” command to build the image.

Now check the image that was built from the “Dockerfile”

If you have a repository access you can configure it in your pom.xml and push the artefacts using

Step 10: Run the image on the container.

The application is running inside the Docker container. You can open a browser now and type the URL “http://localhost:8080/hello“.

Step 10: Stop the container.

to get the running container id and then stop the container as shown

Kubernetes – minikube

Step 11: Start the minikube

Step 12: Run the following command to make minikube use local docker registry. Otherwise, minikue will seek image from other public Docker registry.

Step 13: Check if the cluster is up.

The –image-pull-policy Never means Minikube doesn’t try to pull the image from a registry, but takes it from the local Docker host instead.

Step 14: Get the deployments

You can also view it via “$ minikube dashboard”

Step 15: Expose the deployment

View the services:

Run the services:

Which outputs: http://192.168.64.8:31873

You can run “http://192.168.64.8:31873/hello” on a browser.

Delete the service & deployment

Project structure

Kubernetes-springboot-ptoject

Kubernetes-springboot-ptoject

Learn more on Spring boot

Spring Boot tutorials


Select by a category

300+ Java Interview Questions - FAQs

Tutorials on Java & Big Data