简体   繁体   English

试图将端点从 kubernetes pod 暴露到互联网/浏览器/API

[英]Trying to expose an endpoint from a kubernetes pod to the internet/ browser/ API

What am I trying to do我想做什么

Trying to expose an endpoint from a kubernetes pod to the internet/ browser/ API on a Windows 11 platform with WSL 2 enabled and using Powershell, Docker on Windows, kubectl and minikube. Trying to expose an endpoint from a kubernetes pod to the internet/ browser/ API on a Windows 11 platform with WSL 2 enabled and using Powershell, Docker on Windows, kubectl and minikube. This is essential for resolving my dev environment.这对于解决我的开发环境至关重要。

What happens怎么了

Based on whatever I could find in the docs and online, I saw Loadbalancer as the option used for <>.根据我在文档和在线上可以找到的任何内容,我将 Loadbalancer 视为用于 <> 的选项。 The tunneling never seemed to happen.隧道似乎从未发生过。 I tested using the browser and using curl.我使用浏览器和 curl 进行了测试。

Environment Information环境信息

  • Windows: Windows 11 Pro Windows:Windows 11 Pro
  • Docker on Windows: Docker Desktop 4.3.2 (72729) Docker 上 Windows:Docker 桌面 4.3.2 (72729)
  • Kubernetes: v1.22.3 Kubernetes:v1.22.3
  • Minikube: minikube version: v1.24.0 Minikube:minikube 版本:v1.24.0

Commands - executed命令 - 执行

Here are the commands that I executed to create the service.这是我为创建服务而执行的命令。

1. Create the deployment 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. Expose outbound 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. Create tunnel service 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.

I expected to get the “Nginx welcome” page when I connect to http://127.0.0.1:8080当我连接到http://127.0.0.1:8080时,我希望得到“Nginx 欢迎”页面

But it was但它是

This site can't be reached.无法访问此站点。 The connection was reset.连接被重置。
Try:尝试:
Checking the connection检查连接
Checking the proxy and the firewall检查代理和防火墙
Running Windows Network Diagnostics运行 Windows 网络诊断
ERR_CONNECTION_RESET ERR_CONNECTION_RESET

Same occurs with: http://127.0.0.1:62864/同样发生在: http://127.0.0.1:62864/

Output when I use curl Output 当我使用 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
kubectl expose deployment hello-world3 --type=LoadBalancer --port=8080

You can check what happened when you used command above using kubectl get svc hello-world3 -o yaml and look on ports section:您可以使用kubectl get svc hello-world3 -o yaml检查使用上述命令时发生的情况并查看端口部分:

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

As you can see targetPort has been set to the same port as port .如您所见, targetPort已设置为与 port 相同的port You can read more here你可以在这里阅读更多

Note: A Service can map any incoming port to a targetPort.注意:一个服务可以 map 任何传入端口到一个目标端口。 By default and for convenience, the targetPort is set to the same value as the port field.默认情况下,为方便起见,targetPort 设置为与端口字段相同的值。

You couldn't see nginx page because targetPort should be set to 80 which is listening by pods by default instead of 8080 .您看不到 nginx 页面,因为targetPort应设置为80 ,默认情况下由 pod 侦听而不是8080

To solve your issue you can set a targetPort to port 80 , by adding --target-port=80 to your command as below:要解决您的问题,您可以将targetPort设置为端口80 ,方法是将--target-port=80添加到您的命令中,如下所示:

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

More convenient option using Kubernetes on Windows machine is set up Enable Kubernetes option in Docker Desktop in Settings>Kubernetes .在 Windows 机器上使用 Kubernetes 更方便的选项已设置 在Settings>Kubernetes桌面中的 ZC5FD214CDD0D2B3B42Z2E 中Enable Kubernetes选项Cluster will be created automatically and you will be able use kubectl commands in few minutes in terminal or powershell.集群将自动创建,您将能够在几分钟内在终端或 powershell 中使用kubectl命令。 If something goes wrong, you will be able easily reset your cluster by clicking Reset Kubernetes Cluster button which is in the same place when you enabled Kubernetes in Docker Desktop.如果出现问题,您可以通过单击Reset Kubernetes Cluster按钮轻松重置集群,当您在 Docker 桌面中启用 Kubernetes 时,该按钮位于同一位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的pod公开到互联网并从浏览器访问它? - How to expose my pod to the internet and get to it from the browser? 如何使用Azure容器服务将Kubernetes pod公开到公共Internet - How to expose the Kubernetes pod to the public internet using Azure container service 将容器中的文件夹公开为 Kubernetes Pod 中的卷 - Expose folder from a container as a volume in a Kubernetes Pod Kube.netes.networking inte.net-pod - Kubernetes networking internet-pod 如何将Kubernetes服务公开给互联网? - How do I expose Kubernetes service to the internet? 尝试从 kubernetes pod 访问服务时连接被拒绝 - Getting Connection refused while trying to access service from kubernetes pod 在 kubernetes pod 中访问 spring boot 控制器端点 - Accessing spring boot controller endpoint in kubernetes pod Kubernetes 的 pod 内的 Internet 连接不工作 - Internet connectivity inside the pod of Kubernetes is not Working 无法从 pod 容器内访问 kubernetes api - Not able to access kubernetes api from inside a pod container 如何基于每个pod从Kubernetes将日志发送到GELF UDP端点 - How do I send logs to GELF UDP endpoint from Kubernetes on a per-pod basis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM