簡體   English   中英

Kubernetes 如何在 wsl2 支持的環境中正確安裝 windows 路徑

[英]Kubernetes how to correctly mount windows path in wsl2 backed environment

I have a local image that runs fine this way: 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/用戶/...
  • //C/用戶/...

如此所述,您可以使用下面的 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM