This article simply demonstrates how to deploy a springboot application to minikube with kubectl command.
Create and publish images
Typically, the way to publish a SpringBoot app to a minikube or k8s cluster is to use it as a Docker image. For more information on how to make a Docker image for a SpringBoot application, you can refer to: Deploy Spring Boot App into Docker Container。
After making the docker image locally, push it into the docker hub or your private image library.
Later in this article, you’ll use an application that’s been pushed into the docker hub directly: jini/springboot-k8s-basic, which is a simple WEB application implemented using Spring Boot that simply returns the IP address of the server when the customer visits.
Deploy app into minikube
For a simple case, you can use the command line to directly establish a “deploy deployment” to deploy the app in minikube, executing the following command:
1 | kubectl create deployment springboot-k8s-basic --image=jini/springboot-k8s-basic:latest |
The system displays:
1 | deployment.apps/springboot-k8s-basic created |
The command builds a pod using the default settings and downloads the image from the docker hub for deployment. Depends on the speed of the network, which can be executed after a while:
After the deployment is complete, you can view it through the “get pods” command, execute:
1 | kubectl get pods |
If there are no errors, you can see information similar to the following:
1 | NAME READY STATUS RESTARTS AGE |
If status is Running, the deployment was successful.
Access the app inside minikube
After completing the steps above, you can access the services you just deployed inside minikube.
First execute the following command to locate the IP address of the POD:
1 | kubectl get pods -o wide |
The system displays:
1 | NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES |
As you can see, the application’s POD is assigned an IP address of 172.17.0.6. Now, execute:
1 | minikube ssh |
Log in to minikube internally, the system displays:
1 | _ _ |
Next, we visit the service we just deployed,execute:
1 | curl http://172.17.0.6:8080 |
As you can see, the following result is returned:
1 | My IP is: 172.17.0.6 |
Use NodePort to expose the app
In the above operation, we can already access the application from inside the minikube, but in reality, we must also need to access the application from the outside, in the minikube, there are a variety of technologies to achieve this goal, here first introduce the use of NodePort.
Execute:
1 | kubectl expose deployment springboot-k8s-basic --type=NodePort --port=8080 |
The system display:
1 | service/springboot-k8s-basic exposed |
Next, execute:
1 | kubectl get svc |
The system display:
1 | kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19d |
As you can see, we’ve built a new service that maps port 8080 of the on-premises (application) springboot-k8s-basic to port 30891 of the native machine.
Now,execute:
1 | minikube service springboot-k8s-basic --url |
This command returns the URL address of the application deployed in minikube that can be accessed,In my environment, return:
1 | http://192.168.1.54:30891 |
Open your browser, enter this url, and you can see the feedback:
1 | My IP is: 172.17.0.6 |