Kubernetes在WSL2环境下如何正确挂载Windows路径

6

我有一个本地镜像,可以通过以下方式正常运行:

docker run -p 8080:8080 -v C:\Users\moritz\Downloads\1\imageService\examples1:/images -v C:\Users\moritz\entwicklung\projekte\imageCluster\logs:/logs imageservice

现在我想将其作为 Kubernetes 部署运行(使用 Docker-for-Windows v1.19.7 中内置的部署):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: image-service
spec:
  selector:
    matchLabels:
      app: image-service
  template:
    metadata:
      labels:
        app: image-service
    spec:
      containers:
      - name: image-service
        image: "imageservice"
        resources:
          limits:
            cpu: "0.9"
            memory: "1Gi"
        ports:
        - name: http
          containerPort: 8080
        volumeMounts:
          - mountPath: /images
            name: image-volume
          - mountPath: /logs
            name: log-volume  
      volumes:
        - name: image-volume
          hostPath:
            path: "c:\\Users\\moritz\\Downloads\\1\\imageService\\examples1"
            type: Directory
        - name: log-volume
          hostPath:
            path: /mnt/c/Users/moritz/entwicklung/projekte/imageCluster/logs
            type: Directory

你看到我尝试了不同的方法来设置我的Windows机器上的主机路径,但我总是遇到以下问题:

  Warning  FailedMount  0s (x4 over 4s)  kubelet            MountVolume.SetUp failed for volume "log-volume" : hostPath type check failed: /mnt/c/Users/moritz/entwicklung/projekte/imageCluster/logs is not a directory
  Warning  FailedMount  0s (x4 over 4s)  kubelet            MountVolume.SetUp failed for volume "image-volume" : hostPath type check failed: c:\Users\moritz\Downloads\1\imageService\examples1 is not a directory 

我还尝试了其他变量(两者都试过):
  • C:\Users\moritz\entwicklung\projekte\imageCluster\logs
  • C:/Users/moritz/entwicklung/projekte/imageCluster/logs
那么如何正确设置这些 Windows 主机路径。(下一步是将它们设置为环境变量。) 小更新: 去掉 type: Directory 有助于消除错误并启动 pod,但挂载不起作用。如果我在容器中“查看”/images,我看不到主机上的图像,而在容器中 /logs 包含了期望的文件,但日志挂载也无效。
同时,我还尝试了以下方法(没有效果):
  • /host_mnt/c/...
  • /C/Users/...
  • //C/Users/...

修改了我的问题 - 在Docker for Windows中使用内置的Kubernetes - dermoritz
嗨@dermoritz,你尝试过使用这个[path](https://dev59.com/rFIG5IYBdhLWcg3wzU6B#63524931)吗?从我所看到的评论中,有些人表示它已经可以正常工作了。 - Jakub
1
@Jakub 这就是了!谢谢- 把它设为答案并得到积分 :-) - dermoritz
1个回答

7

此处所述,您可以使用下面的hostPath在wsl2上使其正常工作。

// C:\someDir\volumeDir
hostPath:
  path: /run/desktop/mnt/host/c/someDir/volumeDir
  type: DirectoryOrCreate

你还可以使用一个示例。

apiVersion: v1
kind: Pod
metadata:
  name: test-localpc
spec:
  containers:
  - name: test-webserver
    image: ubuntu:latest
    command: ["/bin/sh"]
    args: ["-c", "apt-get update && apt-get install curl -y && sleep 600"]
    volumeMounts:
    - mountPath: /run/desktop/mnt/host/c/aaa
      name: mydir
    - mountPath: /run/desktop/mnt/host/c/aaa/1.txt
      name: myfile
  volumes:
  - name: mydir
    hostPath:
      # Ensure the file directory is created.
      path: /run/desktop/mnt/host/c/aaa
      type: DirectoryOrCreate
  - name: myfile
    hostPath:
      path: /run/desktop/mnt/host/c/aaa/1.txt
      type: FileOrCreate

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