【问题标题】:Trying to expose an endpoint from a kubernetes pod to the internet/ browser/ API尝试将端点从 kubernetes pod 公开到互联网/浏览器/API
【发布时间】:2022-01-17 23:09:57
【问题描述】:

我想做什么

在启用了 WSL 2 并使用 Powershell、Windows 上的 Docker、kubectl 和 minikube 的 Windows 11 平台上,尝试从 kubernetes pod 向 Internet/浏览器/API 公开端点。这对于解决我的开发环境至关重要。

会发生什么

根据我在文档和在线中可以找到的任何内容,我认为 Loadbalancer 是用于 的选项。隧道似乎从未发生过。我使用浏览器和 curl 进行了测试。

环境信息

  • Windows:Windows 11 专业版
  • Windows 上的 Docker:Docker Desktop 4.3.2 (72729)
  • Kubernetes:v1.22.3
  • Minikube:minikube 版本:v1.24.0

命令 - 执行

这是我为创建服务而执行的命令。

1。创建部署

~ kubectl create deployment hello-world3 --image=nginx:mainline-alpine
deployment.apps/hello-world3 created

~ kubectl get deployment
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
hello-world3   1/1     1            1           19s

2。公开出站

~ kubectl expose deployment hello-world3 --type=LoadBalancer --port=8080
service/hello-world3 exposed

~ kubectl get svc
NAME           TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
hello-world3   LoadBalancer   10.103.203.156   127.0.0.1     8080:30300/TCP   14s
kubernetes     ClusterIP      10.96.0.1        <none>        443/TCP          6d8h

3。创建隧道服务

~ minikube service hello-world3
|-----------|--------------|-------------|---------------------------|
| NAMESPACE |     NAME     | TARGET PORT |            URL            |
|-----------|--------------|-------------|---------------------------|
| default   | hello-world3 |        8080 | http://192.168.49.2:30300 |
|-----------|--------------|-------------|---------------------------|
* Starting tunnel for service hello-world3.
|-----------|--------------|-------------|------------------------|
| NAMESPACE |     NAME     | TARGET PORT |          URL           |
|-----------|--------------|-------------|------------------------|
| default   | hello-world3 |             | http://127.0.0.1:62864 |
|-----------|--------------|-------------|------------------------|
* Opening service default/hello-world3 in default browser...
! Because you are using a Docker driver on windows, the terminal needs to be open to run it.

当我连接到http://127.0.0.1:8080时,我希望得到“Nginx 欢迎”页面

原来如此

无法访问此站点。连接已重置。
试试看:
检查连接
检查代理和防火墙
运行 Windows 网络诊断程序
ERR_CONNECTION_RESET

同样的情况发生在: http://127.0.0.1:62864/

使用 curl 时的输出

~ curl http://127.0.0.1:8080/* -v
VERBOSE: GET with 0-byte payload
curl : The underlying connection was closed: An unexpected error occurred on a receive.
At line:1 char:1
+ curl http://127.0.0.1:8080/ -v
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
~ curl http://127.0.0.1:62864/ -v
VERBOSE: GET with 0-byte payload
curl : The underlying connection was closed: An unexpected error occurred on a receive.
At line:1 char:1
+ curl http://127.0.0.1:62864/ -v
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

【问题讨论】:

  • 很难确定,你做了什么,什么只是 cmets。你能改进你的问题吗?看看stackoverflow.com/editing-help
  • 我已经尝试澄清我的问题并改进了编辑。希望读得更好
  • 我编辑了你的问题。下次尝试使用代码和引用块。 :)
  • 看起来您的 Web 服务 (nginx) 正在侦听端口 80(在 kubernetes 中,因为这是默认设置)。然而,您的端口映射似乎是 8080:30300。

标签: docker kubernetes minikube


【解决方案1】:
kubectl expose deployment hello-world3 --type=LoadBalancer --port=8080

您可以使用kubectl get svc hello-world3 -o yaml 来检查使用上述命令时发生的情况并查看端口部分:

ports:
- nodePort: 30514
  port:8080
  protocol: TCP
  targetPort:8080

如您所见,targetPort 已设置为与port 相同的端口。你可以阅读更多here

注意:服务可以将任何传入端口映射到目标端口。默认情况下,为方便起见,targetPort 设置为与端口字段相同的值。

您看不到 nginx 页面,因为 targetPort 应设置为 80,默认情况下由 pod 侦听,而不是 8080

要解决您的问题,您可以将 targetPort 设置为端口 80 ,方法是将 --target-port=80 添加到您的命令中,如下所示:

kubectl expose deployment hello-world3 --type=LoadBalancer --port=8080 --target-port=80

在Windows机器上使用Kubernetes更方便的选项是在Settings&gt;Kubernetes的Docker Desktop中设置Enable Kubernetes选项。集群将自动创建,您将可以在几分钟内在终端或 powershell 中使用kubectl 命令。如果出现问题,您可以通过单击Reset Kubernetes Cluster 按钮轻松重置集群,该按钮与您在 Docker Desktop 中启用 Kubernetes 时位于同一位置。

【讨论】:

  • 非常感谢。有用。非常感谢您耐心的解释:)。将阅读您提供的链接。
  • 它运行得非常好,非常感谢 - 节省了很多时间。 kubernetes 文档很棒 - 但在这种情况下,如果他们指出显式使用不同的 targetPort - 侦听端口在某些情况下有助于解决端口冲突问题,可能会很有用。
猜你喜欢
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
相关资源
最近更新 更多