试图将kubernetes pod中的端点暴露给internet/浏览器/API



我想做什么

在启用WSL2并使用Powershell、Windows上的Docker、kubectl和minikube的Windows 11平台上,尝试将kubernetes pod中的端点暴露给互联网/浏览器/API。这对于解决我的开发环境至关重要。

发生了什么

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

环境信息

  • Windows:Windows 11 Pro
  • 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.

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

但那是

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

以下情况相同: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

您可以使用kubectl get svc hello-world3 -o yaml检查使用上面的命令时发生了什么,并查看端口部分:

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

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

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

您看不到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>Kubernetes的Docker Desktop中设置Enable Kubernetes选项。集群将自动创建,您将能够在几分钟内在终端或powershell中使用kubectl命令。如果出现问题,您可以通过单击Reset Kubernetes Cluster按钮轻松重置集群,该按钮与您在Docker Desktop中启用Kubernetes时位于同一位置。

最新更新