【问题标题】:How to run a Spring Boot application in Minikube?如何在 Minikube 中运行 Spring Boot 应用程序?
【发布时间】:2021-06-09 17:01:17
【问题描述】:

我想在 Minikube 中运行 this Spring Boot application,以便可以在浏览器中访问端口 8080(一旦应用程序运行)。

为此,我执行以下步骤。

  1. 启动 Docker。
  2. 运行mvn spring-boot:build-image
  3. 使用 minikube start 启动 Minikube。
  4. 使用 kubectl create deployment example-1-engine-1 --image=example-1-engine-1 创建 Minikube 部署。
  5. 使用kubectl expose deployment example-1-engine-1 --type=NodePort --port=8080kubectl port-forward service/example-1-engine-1 8080:8080 公开端口8080。
  6. 在 Minikube 中启动应用程序。
  7. 使用 minikube service example-1-engine-1 在 Minikube 中启动应用程序。

出现以下输出:

但是,浏览器无法在端口 8080 上打开 Web 应用程序(浏览器无法连接到位于 http://127.0.0.1:50947/ 的应用程序)。

我做错了什么?如何确保相关应用程序在 Minikube 中运行并且我可以在端口 8080 访问 Web 应用程序?

更新1:这是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

更新 2: 我尝试输入 eval $(minikube -p minikube docker-env)。为此,我首先在 Docker 中打开了 Minikube 容器的 shell。

在随后出现的窗口中,我输入了eval $(minikube -p minikube docker-env),得到了回复minikube: not found

【问题讨论】:

  • 你能显示kubectl get pods的输出吗,我假设应用程序没有部署在minikube中。
  • minikube service --url <service-name> ;然后尝试输出。
  • @Thomas 查看更新 1。
  • 是的,证实了我的怀疑,请参阅 matt_j 的回答

标签: java spring-boot kubernetes minikube


【解决方案1】:

在我看来,您的案例可能存在两个问题。

1。使用本地构建的 Docker 镜像

您想在 Kubernetes 中运行本地构建的 Docker 映像,但它不能开箱即用。 通常,您有两种选择:

  1. 使用docker image push 命令将您的图像共享到Docker Hub 注册表或Docker documentation 中所述的自托管注册表。

  2. 使用eval $(minikube docker-env) 命令将您的终端指向使用minikube 中的docker 守护进程,如minikube documentation 中所述。

2。使用带有特定标签的 Docker 镜像

如我所见,您的图片有特定标签1.0-SNAPSHOT

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

您需要在kubectl create deployment 命令中指定此标记:

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

我已经部署了你的 Spring Boot application,以表明它对我来说可以正常工作。

首先,让我的终端使用我运行的 minikube 中的 docker 守护进程:

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

然后我创建了 Docker 镜像并部署了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

最后,我暴露了example-1-engine-1 Deployment,就像你的例子一样:

$ 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...

在打开的浏览器选项卡中,我看到了:

另外,我在http://127.0.0.1:8080看到了相同的页面。


很遗憾,我没有 Windows 机器(如您的示例),而是使用 Linux,因此无法准确重现您的问题。

【讨论】:

  • 感谢您的回答。如果您发现任何明显的错误,请查看更新 2 并告诉我。
  • eval $(minikube docker-env) 命令应该在主机的终端(与您正在运行的同一终端,例如 minikube startkubectl get pods)中运行,而不是在 minikube 内。
  • 我在 Windows 下找不到这个命令。但是,我设法使用了您提出的另一种方法(推送到 Docker Hub),它似乎有效。谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2016-04-27
  • 1970-01-01
  • 2021-05-12
  • 2017-01-16
  • 1970-01-01
  • 2017-06-22
  • 2020-08-24
相关资源
最近更新 更多