Windows WSL 2 挂载 k8s local PV的解决方案

本地使用docker desktop启动k8s, 发现pod挂在local pv提示错误:

Warning  FailedMount  12s   kubelet            MountVolume.NewMounter 
initialization failed for volume "kms-logs-pv" : path "mnt/d/data/kms/kms-logs" 
does not exist

这是k8s 在windows WSL 2 环境下找不到相应的路径。在docker的问题群里有一个workround, 使用/run/desktop/mnt/host/d/data/kms/kms-logs代替原来的路径。以下是对应的PV:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: kms-logs-pv
spec:
  capacity:
    storage: 2Gi
  # volumeMode field requires BlockVolume Alpha feature gate to be enabled.
  volumeMode: Filesystem
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Delete
  local:
    path: /run/desktop/mnt/host/d/data/kms/kms-logs
  storageClassName: logs-storage
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop

PV 对应的 PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: kmslogsclaim
spec:
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 2Gi
  storageClassName: logs-storage

对应的deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kms
  labels:
    app: kms-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kms-app
  template:
    metadata:
      labels:
        app: kms-app
        name: openvidu-kms
    spec:
      terminationGracePeriodSeconds: 10    #当删除Pod时,等待时间
      containers:
        - name: openvidu-kms
          image: kurento/kurento-media-server:6.15.0
          volumeMounts:
            - mountPath: /opt/openvidu/kms-crashes
              name: crashesstore
            - mountPath: /opt/openvidu/recordings
              name: recordingstore
            - mountPath: /opt/openvidu/kurento-logs
              name: logsstore
      dnsPolicy: "ClusterFirst"
      volumes:
      - name: crashesstore
        persistentVolumeClaim:
          claimName: kmscrashesclaim
      - name: recordingstore
        persistentVolumeClaim:
          claimName: kmsrecordingclaim
      - name: logsstore
        persistentVolumeClaim:
          claimName: kmslogsclaim

参考连接:
docker issues1
# Persistent volumes get lost in Kubernetes on Docker Desktop for Windows/WSL2#7023

推荐阅读更多精彩内容