docker for windows 上的 Kubernetes,带有 hostPath 的持久卷给出了“不允许操作”

Try*_*gen 10 windows volumes docker kubernetes

我正在尝试将 Windows 中的文件夹连接到容器文件夹。这适用于需要读取文件夹中文件的 .NET 应用程序。在普通的 docker 容器中,使用 docker-compose,该应用程序可以正常运行,但由于这只是我们必须监视的几个不同应用程序之一,因此我们正在尝试让 kubernetes 参与其中。这也是我们失败的地方。作为 kubernetes 的初学者,我使用 kompose.exe 将 compose 文件转换为 kuberetes 风格。但是,无论我使用 hostPath 还是 permanentVolumeClaim 作为标志,我都无法让事情“开箱即用”。使用 hostPath 时,路径非常不正确,而使用 permanentVolumeClaim 时,我收到一条警告,指出不支持在主机上安装卷。因此,我尝试自己完成这一部分,但既不能使用持久卷,也不能直接在部署文件中输入安装数据。我最接近的是我可以进入该文件夹,并且可以更改为其中的子文件夹,但是一旦我尝试运行任何其他命令,无论是“ls”还是“cat”,我都会收到“不允许操作” 。这是我的 docker compose 文件,它按预期工作

version: "3.8"
services:
    test-create-hw-file:
        container_name: "testcreatehwfile"
        image: test-create-hw-file:trygg
        network_mode: "host"
        volumes:
            - /c/temp/testfiles:/app/files
Run Code Online (Sandbox Code Playgroud)

在该文件上运行 konvert compose:

PS C:\temp> .\kompose.exe convert -f .\docker-compose-tchwf.yml --volumes hostPath -v
DEBU Checking validation of provider: kubernetes
DEBU Checking validation of controller:
DEBU Docker Compose version: 3.8
WARN Service "test-create-hw-file" won't be created because 'ports' is not specified
DEBU Compose file dir: C:\temp
DEBU Target Dir: .
INFO Kubernetes file "test-create-hw-file-deployment.yaml" created
Run Code Online (Sandbox Code Playgroud)
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: C:\temp\kompose.exe convert -f .\docker-compose-tchwf.yml --volumes hostPath -v
    kompose.version: 1.26.1 (a9d05d509)
  creationTimestamp: null
  labels:
    io.kompose.service: test-create-hw-file
  name: test-create-hw-file
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: test-create-hw-file
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: C:\temp\kompose.exe convert -f .\docker-compose-tchwf.yml --volumes hostPath -v
        kompose.version: 1.26.1 (a9d05d509)
      creationTimestamp: null
      labels:
        io.kompose.service: test-create-hw-file
    spec:
      containers:
        - image: test-create-hw-file:trygg
          name: testcreatehwfile
          resources: {}
          volumeMounts:
            - mountPath: /app/files
              name: test-create-hw-file-hostpath0
      restartPolicy: Always
      volumes:
        - hostPath:
            path: C:\temp\c\temp\testfiles
          name: test-create-hw-file-hostpath0
status: {}
Run Code Online (Sandbox Code Playgroud)

在该文件上运行 kubectl apply 只会给出臭名昭著的错误“错误:守护程序的错误响应:无效模式:/app/files”,这意味着,据我所知,并不是“/app/files”是错误的,但所谓连接的文件夹的格式不正确。这是相当奇怪的C:\temp\c\temp\testfiles一排。经过谷歌搜索和阅读大量内容后,我有两种方法可以将其更改为/c/temp/testfiles/host_mnt/c/temp/testfiles。两者最终都会出现相同的“不允许操作”。我通过进入 docker 桌面容器上的 CLI 来检查这一点。

测试中的图像只是一个应用程序,除了等待五分钟不退出才能检查文件夹之外,它现在什么也不做。我以 root 身份登录到 shell,并且在执行“ls -lA”时,我有这一行文件夹:

drwxrwxrwx 1 root root      0 Feb  7 12:04 files
Run Code Online (Sandbox Code Playgroud)

此外,还docker-user具有对该c:\temp\testfiles文件夹的完全访问权限。

部分版本数据:

Docker version 20.10.12, build e91ed57

Kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.5", GitCommit:"5c99e2ac2ff9a3c549d9ca665e7bc05a3e18f07e", GitTreeState:"clean", BuildDate:"2021-12-16T08:38:33Z", GoVersion:"go1.16.12", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.5", GitCommit:"5c99e2ac2ff9a3c549d9ca665e7bc05a3e18f07e", GitTreeState:"clean", BuildDate:"2021-12-16T08:32:32Z", GoVersion:"go1.16.12", Compiler:"gc", Platform:"linux/amd64"}

Kompose version
1.26.1 (a9d05d509)

Host OS: Windows 10, 21H2
Run Code Online (Sandbox Code Playgroud)

//特里格

Rad*_*ekW 20

很高兴我最初的评论解决了您的问题。我想以官方答案的形式稍微扩展一下我的想法。

要在 Windows 版 Docker Desktop 上使用 Kubernetes 挂载卷,路径为:

/run/desktop/mnt/host/c/PATH/TO/FILE
Run Code Online (Sandbox Code Playgroud)

不幸的是没有官方文档,但这里有一个很好的评论,并解释了这与 Docker Daemon 相关:

/mnt/wsl 实际上是跨发行版挂载 tmpfs 的挂载点
Docker Daemon 将其挂载在其 /run/desktop/mnt/host/wsl 目录中