无法从 Mac 上的主机使用 NodePort 访问 minikube 服务

sim*_*bro 16 docker kubernetes minikube

我正在尝试将单个 Web 应用程序部署到 Mac 上的 Minikube,然后在浏览器中访问它。我正在尝试使用最简单的设置,但它不起作用,我只是收到“连接被拒绝”错误,我不知道为什么。

这就是我正在尝试的:

$ minikube start --insecure-registry=docker.example.com:5000
  minikube v1.12.3 on Darwin 10.14.6
?  Using the docker driver based on existing profile
  Starting control plane node minikube in cluster minikube
  Restarting existing docker container for "minikube" ...
  Preparing Kubernetes v1.18.3 on Docker 19.03.8 ...
  Verifying Kubernetes components...
  Enabled addons: default-storageclass, storage-provisioner
  Done! kubectl is now configured to use "minikube"

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

$ docker build -t web-test .
Sending build context to Docker daemon  16.66MB
Step 1/3 : FROM docker.example.com/library/openjdk:11-jdk-slim
11-jdk-slim: Pulling from library/openjdk
bf5952930446: Pull complete 
092c9b8e633f: Pull complete 
0b793152b850: Pull complete 
7900923f09cb: Pull complete 
Digest: sha256:b5d8f95b23481a9d9d7e73c108368de74abb9833c3fae80e6bdfa750663d1b97
Status: Downloaded newer image for docker.example.com/library/openjdk:11-jdk-slim
 ---> de8b1b4806af
Step 2/3 : COPY target/web-test-0.0.1-SNAPSHOT.jar app.jar
 ---> 6838e3db240a
Step 3/3 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]
 ---> Running in 550bf762bf2d
Removing intermediate container 550bf762bf2d
 ---> ce1468d1ff10
Successfully built ce1468d1ff10
Successfully tagged web-test:latest

$ kubectl apply -f web-test-service.yaml 
service/web-test unchanged

$ kubectl apply -f web-test-deployment.yaml 
deployment.apps/web-test configured

$ kubectl get po -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
web-test-6bb45ffc54-8mxbc   1/1     Running   0          16m   172.18.0.2   minikube   <none>           <none>

$ kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          16m
web-test     NodePort    10.102.19.201   <none>        8080:31317/TCP   16m

$ minikube ip
127.0.0.1

$ curl http://127.0.0.1:31317
curl: (7) Failed to connect to 127.0.0.1 port 31317: Connection refused


$ kubectl logs web-test-6bb45ffc54-8mxbc

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.3.RELEASE)

2020-08-26 14:45:32.692  INFO 1 --- [           main] com.example.web.WebTestApplication           : Starting WebTestApplication v0.0.1-SNAPSHOT on web-test-6bb45ffc54-8mxbc with PID 1 (/app.jar started by root in /)
2020-08-26 14:45:32.695  INFO 1 --- [           main] com.example.web.WebTestApplication           : No active profile set, falling back to default profiles: default
2020-08-26 14:45:34.041  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer      : Tomcat initialized with port(s): 8080 (http)
2020-08-26 14:45:34.053  INFO 1 --- [           main] o.apache.catalina.core.StandardService       : Starting service [Tomcat]
2020-08-26 14:45:34.053  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine      : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-26 14:45:34.135  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]           : Initializing Spring embedded WebApplicationContext
2020-08-26 14:45:34.135  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext     : Root WebApplicationContext: initialization completed in 1355 ms
2020-08-26 14:45:34.587  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor      : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-26 14:45:34.797  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer      : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-26 14:45:34.810  INFO 1 --- [           main] com.example.web.WebTestApplication           : Started WebTestApplication in 2.808 seconds (JVM running for 3.426)


$ minikube ssh
docker@minikube:~$ curl 10.102.19.201:8080
Up and Running
docker@minikube:~$
Run Code Online (Sandbox Code Playgroud)

如您所见,Web 应用程序已启动并正在运行,我可以通过执行 aminikube ssh从集群内部访问它,但从集群外部,它无法连接。这些是我的服务和部署清单:

web-test-service.yaml:

$ minikube start --insecure-registry=docker.example.com:5000
  minikube v1.12.3 on Darwin 10.14.6
?  Using the docker driver based on existing profile
  Starting control plane node minikube in cluster minikube
  Restarting existing docker container for "minikube" ...
  Preparing Kubernetes v1.18.3 on Docker 19.03.8 ...
  Verifying Kubernetes components...
  Enabled addons: default-storageclass, storage-provisioner
  Done! kubectl is now configured to use "minikube"

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

$ docker build -t web-test .
Sending build context to Docker daemon  16.66MB
Step 1/3 : FROM docker.example.com/library/openjdk:11-jdk-slim
11-jdk-slim: Pulling from library/openjdk
bf5952930446: Pull complete 
092c9b8e633f: Pull complete 
0b793152b850: Pull complete 
7900923f09cb: Pull complete 
Digest: sha256:b5d8f95b23481a9d9d7e73c108368de74abb9833c3fae80e6bdfa750663d1b97
Status: Downloaded newer image for docker.example.com/library/openjdk:11-jdk-slim
 ---> de8b1b4806af
Step 2/3 : COPY target/web-test-0.0.1-SNAPSHOT.jar app.jar
 ---> 6838e3db240a
Step 3/3 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]
 ---> Running in 550bf762bf2d
Removing intermediate container 550bf762bf2d
 ---> ce1468d1ff10
Successfully built ce1468d1ff10
Successfully tagged web-test:latest

$ kubectl apply -f web-test-service.yaml 
service/web-test unchanged

$ kubectl apply -f web-test-deployment.yaml 
deployment.apps/web-test configured

$ kubectl get po -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
web-test-6bb45ffc54-8mxbc   1/1     Running   0          16m   172.18.0.2   minikube   <none>           <none>

$ kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          16m
web-test     NodePort    10.102.19.201   <none>        8080:31317/TCP   16m

$ minikube ip
127.0.0.1

$ curl http://127.0.0.1:31317
curl: (7) Failed to connect to 127.0.0.1 port 31317: Connection refused


$ kubectl logs web-test-6bb45ffc54-8mxbc

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.3.RELEASE)

2020-08-26 14:45:32.692  INFO 1 --- [           main] com.example.web.WebTestApplication           : Starting WebTestApplication v0.0.1-SNAPSHOT on web-test-6bb45ffc54-8mxbc with PID 1 (/app.jar started by root in /)
2020-08-26 14:45:32.695  INFO 1 --- [           main] com.example.web.WebTestApplication           : No active profile set, falling back to default profiles: default
2020-08-26 14:45:34.041  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer      : Tomcat initialized with port(s): 8080 (http)
2020-08-26 14:45:34.053  INFO 1 --- [           main] o.apache.catalina.core.StandardService       : Starting service [Tomcat]
2020-08-26 14:45:34.053  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine      : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-26 14:45:34.135  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]           : Initializing Spring embedded WebApplicationContext
2020-08-26 14:45:34.135  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext     : Root WebApplicationContext: initialization completed in 1355 ms
2020-08-26 14:45:34.587  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor      : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-26 14:45:34.797  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer      : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-26 14:45:34.810  INFO 1 --- [           main] com.example.web.WebTestApplication           : Started WebTestApplication in 2.808 seconds (JVM running for 3.426)


$ minikube ssh
docker@minikube:~$ curl 10.102.19.201:8080
Up and Running
docker@minikube:~$
Run Code Online (Sandbox Code Playgroud)

web-test-deployment.yaml:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: web-test
  name: web-test
spec:
  type: NodePort
  ports:
  - nodePort: 31317
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: web-test
Run Code Online (Sandbox Code Playgroud)

任何人都知道我做错了什么?或者我如何尝试进一步诊断问题?我已经允许尝试部署入口,但这也不起作用。

Arg*_*dhu 15

当您使用which returns时,您主要面临这个问题。如果您使用内部 ip from而不是.minikube ip127.0.0.1kubectl get node -o wide127.0.0.1

官方参考文档中一个更简单的方法是您可以获取 url 使用minikube service web-test --url并在浏览器中使用它,或者如果您使用minikube service web-test它将直接在浏览器中打开 url。

您的部署 yamls 和其他所有内容看起来都不错,希望在部署到远程集群时不会出现任何问题。

  • 嗯,我有同样的问题,但对我来说 minikube 返回 192.168.49.2 不起作用。minikube 服务启动服务器并使用 http://127.0.0.1:53545/ 打开 Web 浏览器,该浏览器实际上可以工作,只是 53545 不是我指定的端口。我不明白这是如何工作的:) (14认同)

xse*_*rat 5

看来这与启动 minikube 时使用的默认 docker 驱动程序有关。为了避免这些问题,您可以强制使用特定的驱动程序(例如“virtualbox”)。为此,请执行以下步骤:

删除旧的 minikube:

minikube delete
Run Code Online (Sandbox Code Playgroud)

使用驱动程序启动 minikube virtualbox

minikube start --memory=4096 --driver=virtualbox
Run Code Online (Sandbox Code Playgroud)

跑步minikube ip。您将看到类似的输出192.168.99.100。然后,再次创建 Pod 和服务,它应该可以正常工作。我在此问题中找到了此信息:https://github.com/kubernetes/minikube/issues/7344#issuecomment-703225254

  • 该解决方案适用于基于英特尔的 Mac,我在我的上一台 MacBook 上使用了它。然而,新的 M1 芯片不支持 virtualbox。您知道针对新款 M1 Mac 的此解决方案还有其他替代方案吗?我看到的唯一真正的选择是使用并行,因为 vmware 也不支持,但我还没有尝试过。 (3认同)