无法使用Windows Docker桌面上的Kubernetes连接到NodePort上的服务 [英] Cannot connect to service on NodePort using Kubernetes on Windows Docker Desktop
问题描述
首先,这是我的文件夹:
这是我的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-dis.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
当我应用2个文件时,我看到的是:
当我尝试使用以下两个选项在端口31023
上连接时:
http://localhost:31023/api/platforms
http://10.109.215.230:31023/api/platforms
它不起作用。发生这种情况:
我不知道这有什么问题.
推荐答案
linux容器的情况:
Windows Docker Desktop上的Kubernetes默认在WSL2(Linux的Windows子系统)中运行其组件,它是独立的虚拟机,具有自己的IP地址和localhost
。这就是无法在localhost
上从主机操作系统(本例中为Windows)访问服务的原因。
另一个选项是在设置中禁用using WSL2 based engine
,而将使用hyper-v
并创建虚拟机。但是在Docker Desktop中,据说最好使用WSL2来提高性能。
可用选项如何使用WSL2访问服务:
- 最快最简单(负载均衡器)
service
类型为LoadBalancer
的service
。EXTERNAL-IP
将成为本地主机,可立即解决所有问题。例如:
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
的浏览器中可用。
- 使用虚拟机的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
的浏览器中可用。
- 端口转发-用于测试
Port-forward
用于测试,但不应用于生产系统。
若要启动服务的代理,请运行以下命令:
kubectl port-forward service/nginx 8080:80 &
nginx在localhost:8080
使用Hyper-V时的假设
首先应在主机上安装hyper-v
。请注意,并非所有版本的Windows都受支持。有关启用hyper-v
here的版本和方法,请参考文档。
取消选择using WSL2 based engine
时,使用hyper-v
操作容器。它创建可在Hyper-v Manager
中找到的单独虚拟机。
nodeport
在localhost+nodeport上工作loadbalancer
不起作用,External-IP
显示localhost
也无法通过服务端口连接到localhost
。
Windows Docker桌面上的Windows容器
还可以在Windows Docker桌面上运行Windows容器。
需要更改将使用的守护进程。在托盘中,选择日期为switch to Windows containers
。Switch between linux and windows containers。
但是,kubernetes
选项将不可用,因为control plane
组件设计为在linux
主机上运行。
环境:
操作系统:Windows 10企业版,内部版本:19041.1165
Docker Desktop:4.0.0(67817)
引擎:20.10.8
Kubernetes:1.21.4
有用链接:
这篇关于无法使用Windows Docker桌面上的Kubernetes连接到NodePort上的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!