Mounting Windows local folder into pod
Asked Answered
V

1

8

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.

Vexatious answered 29/10, 2021 at 6:54 Comment(10)
Are you able to create persistent volume? What does kubectl get pv dev-customer-db-pv and kubectl get pvc dev-customer-db-pvc show?Overbid
kubectl describe pv: Name: dev-customer-db-pv Labels: <removed for brevity> Annotations: <removed for brevity> Finalizers: [kubernetes.io/pv-protection] StorageClass: Status: Available Claim: Reclaim Policy: Retain Access Modes: RWO VolumeMode: Filesystem Capacity: 1Gi Node Affinity: <none> Message: Source: Type: HostPath (bare host directory volume) Path: /C/data/sql HostPathType: Events: <none>Vexatious
kubectl describe pvc: ... Capacity: 100Mi Access Modes: RWO VolumeMode: Filesystem Events: Type Reason Age From Message ... Normal Provisioning 13m docker.io/hostpath_storage-provisioner_... External provisioner is provisioning volume for claim "default/dev-customer-db-pvc" Normal ProvisioningSucceeded 13m docker.io/hostpath_storage-provisioner_7da78fbd-f930-4d1d-b9ad-66b6ddbac442 Successfully provisioned volume pvc-d017adcc-462e-4d10-aead-cf75d83000dbVexatious
@Vexatious What are you using for Docker Desktop - WSL 2 or Hyper-V?Aphotic
I'm using WSL2. I'm on the latest version of Docker Desktop (4.1.1, 69879)Vexatious
Based on this info from GitHub for Docker there is no support hostpath volumes in WSL 2. So, below workaround that I tested. I changed spec.volumes for Deployment: volumes: - name: dev-customer-db-pv hostPath: path: /run/desktop/mnt/host/c/data/sql type: Directory No need for PV and PVC in that case (removed them previously). About configuring hostPath on Kubernetes siteAphotic
I do see a run folder in the pod, but it doesn't have a subfolder desktop. /run/mount is empty. Should I do anything to get the desktop folder in the pod?Vexatious
In my initial question, I also stated I tried something like that without PV/PVC. Then the files show up but I get access denied errors. SQL is also not able to attach the database in that case.Vexatious
You can see your files in data folder in pod, since your mountPath: /data And in addition for my previous comment - we need just to add /run/desktop/mnt/host to your initial path on your host /c/data/sql. That is why path:/run/desktop/mnt/host/c/data/sqlAphotic
Yes, it works with the last reply of Andrew. Using the hostpath /run/desktop/mnt/host/c/data/sql, the database files can be used inside the SQL container.Vexatious
A
9

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

Aphotic answered 12/11, 2021 at 13:52 Comment(1)
Worked for me after I adjusted volumeMounts: /data/db and subPath: dataCohligan

© 2022 - 2024 — McMap. All rights reserved.