How to run a Spring Boot application in Minikube?

3/11/2021

I want to run this Spring Boot application inside Minikube so that I can reach port 8080 in my browser (once the application is running).

To do so, I do following steps.

  1. Start Docker.
  2. Run mvn spring-boot:build-image.
  3. Start Minikube using minikube start.
  4. Create a Minikube deployment using kubectl create deployment example-1-engine-1 --image=example-1-engine-1.
  5. Expose port 8080 using kubectl expose deployment example-1-engine-1 --type=NodePort --port=8080 and kubectl port-forward service/example-1-engine-1 8080:8080.
  6. Start the appliation in Minikube.
  7. Start the application in Minikube using minikube service example-1-engine-1.

Following output appears:

Output of Minikube in the command line

However, the browser cannot open the web application on port 8080 (browser cannot connect to the application at http://127.0.0.1:50947/).

What am I doing wrong and how can I make sure that the application in question runs inside Minikube and I can access the web application at port 8080?

Update 1: Here is the output of kubectl get pods:

C:\Users\XXXXXXX>kubectl get pods
NAME                                 READY   STATUS             RESTARTS   AGE
example-1-engine-1-d9fc48785-zdvkf   0/1     ImagePullBackOff   0          2d18h
hello-minikube-6ddfcc9757-wj6c2      1/1     Running            1          5d

Update 2: I tried to enter eval $(minikube -p minikube docker-env). For this purpose, I first opened the shell of Minikube container in Docker.

Screenshot of the button for opening the shell in Minikube

In the window that appeared thereafter, I entered eval $(minikube -p minikube docker-env) and got the response minikube: not found.

Screenshot of the error message "minikube: not found"

-- PDP
java
kubernetes
minikube
spring-boot

1 Answer

3/12/2021

In my opinion, there are two possible issues with your case.

1. Using a locally built Docker image

You want to run locally built Docker image in Kubernetes, but it doesn't work out-of-the-box. Generally, you have two options:

1) Use docker image push command to share your image to the Docker Hub registry or to a self-hosted one as described in the Docker documentation.

2) Use eval $(minikube docker-env) command to point your terminal to use the docker daemon inside minikube as described in the minikube documentation.

2. Using a Docker image with a specific tag

As I can see, your image has specific tag 1.0-SNAPSHOT:

$ docker images
REPOSITORY                                TAG                     
example-1-engine-1                        1.0-SNAPSHOT            

You need to specify this tag in your kubectl create deployment command:

$ kubectl create deployment example-1-engine-1 --image=example-1-engine-1:1.0-SNAPSHOT

I've deployed your Spring Boot application, to show that it works as expected for me.

First, to point my terminal to use the docker daemon inside minikube I ran:

$ eval $(minikube -p minikube docker-env)

Then I created Docker image and deployed example-1-engine-1 app:

$ mvn spring-boot:build-image
...
[INFO] Successfully built image 'docker.io/library/example-1-engine-1:1.0-SNAPSHOT'
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

$ kubectl create deployment example-1-engine-1 --image=example-1-engine-1:1.0-SNAPSHOT
deployment.apps/example-1-engine-1 created

$ kubectl get pod
NAME                                 READY   STATUS    RESTARTS   AGE
example-1-engine-1-c98f8c569-xq2cv   1/1     Running   0          13s

Finally, I exposed the example-1-engine-1 Deployment as in your example:

$ kubectl expose deployment example-1-engine-1 --type=NodePort --port=8080
service/example-1-engine-1 exposed

$ kubectl port-forward service/example-1-engine-1 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080

$ minikube service example-1-engine-1
|-----------|--------------------|-------------|---------------------------|
| NAMESPACE |        NAME        | TARGET PORT |            URL            |
|-----------|--------------------|-------------|---------------------------|
| default   | example-1-engine-1 |        8080 | http://192.168.49.2:32766 |
|-----------|--------------------|-------------|---------------------------|
Opening service default/example-1-engine-1 in default browser...

In the open browser tab, I saw:

enter image description here

Additionally, I saw the same page at http://127.0.0.1:8080.


Unfortunately, I don't have Windows machine (as in your example) and I used Linux instead, so I wasn't able to exactly reproduce your issue.

-- matt_j
Source: StackOverflow