Search code examples
kubernetesdocker-desktopkubernetes-pvc

Mounting Windows local folder into pod


I'm running a Ubuntu container with SQL Server in my local Kubernetes environment with Docker Desktop on a Windows laptop. Now I'm trying to mount a local folder (C:\data\sql) that contains database files into the pod. For this, I configured a persistent volume and persistent volume claim in Kubernetes, but it doesn't seem to mount correctly. I don't see errors or anything, but when I go into the container using docker exec -it and inspect the data folder, it's empty. I expect the files from the local folder to appear in the mounted folder 'data', but that's not the case.

Is something wrongly configured in the PV, PVC or pod?

Here are my yaml files:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: dev-customer-db-pv
  labels:
    type: local
    app: customer-db
    chart: customer-db-0.1.0
    release: dev
    heritage: Helm
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /C/data/sql
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dev-customer-db-pvc
  labels:
    app: customer-db
    chart: customer-db-0.1.0
    release: dev
    heritage: Helm
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dev-customer-db
  labels:
    ufo: dev-customer-db-config
    app: customer-db
    chart: customer-db-0.1.0
    release: dev
    heritage: Helm
spec:
  selector:
    matchLabels:
      app: customer-db
      release: dev
  replicas: 1
  template:
    metadata:
      labels:
        app: customer-db
        release: dev
    spec:
      volumes:
        - name: dev-customer-db-pv
          persistentVolumeClaim:
            claimName: dev-customer-db-pvc
      containers:
      - name: customer-db
        image: "mcr.microsoft.com/mssql/server:2019-latest"
        imagePullPolicy: IfNotPresent
        volumeMounts:
          - name: dev-customer-db-pv
            mountPath: /data
        envFrom:
          - configMapRef:
              name: dev-customer-db-config
          - secretRef:
              name: dev-customer-db-secrets

At first, I was trying to define a volume in the pod without PV and PVC, but then I got access denied errors when I tried to read files from the mounted data folder.

spec:
      volumes:
        - name: dev-customer-db-data
          hostPath:
            path: C/data/sql
      containers:
        ...        
        volumeMounts:
          - name: dev-customer-db-data
            mountPath: data

I've also tried to Helm install with --set volumePermissions.enabled=true but this didn't solve the access denied errors.


Solution

  • Based on this info from GitHub for Docker there is no support hostpath volumes in WSL 2.

    Thus, next workaround can be used.

    We need just to append /run/desktop/mnt/host to the initial path on the host /c/data/sql. No need for PersistentVolume and PersistentVolumeClaim in this case - just remove them.

    I changed spec.volumes for Deployment according to information about hostPath configuration on Kubernetes site:

    volumes:
    - name: dev-customer-db-pv
      hostPath:
        path: /run/desktop/mnt/host/c/data/sql
        type: Directory
    

    After applying these changes, the files can be found in data folder in the pod, since mountPath: /data