[英]Trying to expose an endpoint from a kubernetes pod to the internet/ browser/ API
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.这对于解决我的开发环境至关重要。
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 进行了测试。
Here are the commands that I executed to create the service.这是我为创建服务而执行的命令。
~ 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
~ 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
~ 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/
~ 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.