Deploy a Spring Boot Application into minikube with Command

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
2
NAME                                    READY   STATUS    RESTARTS   AGE
springboot-k8s-basic-6786f5cd68-6w5s5 1/1 Running 0 20h

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
2
NAME                                    READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
springboot-k8s-basic-6786f5cd68-6w5s5 1/1 Running 0 20h 172.17.0.6 minikube <none> <none>

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
2
3
4
5
6
                         _             _
_ _ ( ) ( )
___ ___ (_) ___ (_)| |/') _ _ | |_ __
/' _ ` _ `\| |/' _ `\| || , < ( ) ( )| '_`\ /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )( ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

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
2
kubernetes             ClusterIP   10.96.0.1      <none>        443/TCP          19d
springboot-k8s-basic NodePort 10.98.59.165 <none> 8080:30891/TCP 95s

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

本文标题:Deploy a Spring Boot Application into minikube with Command

文章作者:Morning Star

发布时间:2022年01月06日 - 11:01

最后更新:2022年01月07日 - 09:01

原始链接:https://www.mls-tech.info/microservice/k8s/minikube-deploy-springboot-app-with-cmd-en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。