无法使用Windows Docker Desktop上的Kubernetes连接NodePort服务

7

首先,这是我的文件夹:

进入图像描述

这是我的 Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "PlatformService.dll"]

平台部署配置文件 platforms-depl.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: platforms-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: platformservice
  template:
    metadata:
      labels:
        app: platformservice
    spec:
      containers:
        - name: platformservice
          image: hao14102000/platformservice:latest

platforms-np-srv.yaml(NodePort服务文件)

apiVersion: v1
kind: Service
metadata:
  name: platformnpservice-srv
spec:
  type: NodePort
  selector:
    app: platformservice
  ports:
    - name: platformservice
      protocol: TCP
      port: 80
      targetPort: 80

当我应用这两个文件时,我看到的是:

enter image description here

当我尝试使用以下两个选项之一连接端口31023时:

http://localhost:31023/api/platforms
http://10.109.215.230:31023/api/platforms

它不起作用。 出现了以下情况:

输入图像描述

输入图像描述

我不知道这里有什么问题......


请检查您的端点是否正常? - Miffa Young
Kubernetes集群在哪里以及如何部署?在某些情况下,本地主机可能无法正常工作。 - moonkotte
@MiffaYoung 我的端点很好,本地主机和 Docker 环境下都能运行良好,只有在 k8s 环境下它才不能工作... - Hao
@moonkotte 我在我的笔记本电脑上使用Docker桌面部署了Kubernetes...数据在内存中使用,我在本地环境和Docker环境中测试成功。 - Hao
不需要道歉,当你能回复时就回复,这很好 :) 我怀疑这与使用“Docker桌面版”实现Kubernetes有关。请尝试端口转发直接在您的主机上公开服务。命令将是kubectl port-forward service/platformnpservice-srv 8080:80 &,然后在localhost:8080上访问应用程序。 - moonkotte
显示剩余5条评论
1个回答

6

Linux容器的运行机制:

在Windows Docker Desktop上,Kubernetes默认在WSL2(Windows子系统Linux)中运行其组件,这是一个单独的虚拟机,拥有自己的IP地址和localhost。这就是为什么服务无法从主机操作系统(本例为Windows)访问localhost的原因。

另一种选择是在设置中禁用使用WSL2引擎,而改用hyper-v,这将创建虚拟机。但是在Docker Desktop中,建议使用WSL2以获得更好的性能优势。

使用WSL2访问服务的可用选项:

  1. 最快、最简单的方法(负载均衡器)

使用LoadBalancer类型设置一个serviceEXTERNAL-IP将为localhost,从而立即解决所有问题。例如:

kubectl get svc
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP          11m
nginx        LoadBalancer   10.110.15.53   localhost     8080:30654/TCP   4m10s

Nginx 可以在浏览器中通过 localhost:8080 访问。

  1. 使用虚拟机的IP地址和节点端口号

另一种选择是找到 WSL 虚拟机,然后通过该虚拟机的 IP 地址和 nodeport 访问服务。

要查找 WSL VM 地址,需要运行 wsl 命令连接到此 VM,然后查找其 IP 地址:

wsl

# ip a | grep eth0

6: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.19.xxx.yyy/20 brd 172.19.xxx.yyy scope global eth0

Nginx可以在浏览器中通过172.19.xxx.yyy:30654访问。

  1. 端口转发 - 用于测试目的

端口转发对于测试目的非常有用,但不应在生产系统上使用。

要启动服务代理,请运行以下命令:

kubectl port-forward service/nginx 8080:80 &

Nginx可以在浏览器中通过localhost:8080访问。

使用Hyper-V时的假设条件

首先,在主机上安装hyper-v。请注意,并非所有版本的Windows都受支持。请参阅有关哪些版本以及如何启用hyper-v的文档here

当取消选中使用基于WSL2的引擎时,将使用hyper-v来处理容器。它创建了一个独立的虚拟机,可以在Hyper-v Manager中找到。

  • nodeport在localhost + nodeport上工作。
  • loadbalancer不工作,即使External-IP显示为localhost,您也无法连接到具有服务端口的localhost

Windows Docker桌面上的Windows容器

也可以在Windows Docker桌面上运行Windows容器。

需要更改要使用的守护程序。在托盘中选择switch to Windows containersSwitch between linux and windows containers

但是,kubernetes选项将变为不可用状态,因为control plane组件是设计在linux主机上运行的。

环境:

操作系统:Windows 10 Enterprise,版本:19041.1165

Docker桌面:4.0.0(67817)

引擎:20.10.8

Kubernetes:1.21.4

有用的链接:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接