Home > Software design >  How to mount a host volume in Kubernetes running on Docker Desktop (Windows 10) backed by WSL2?
How to mount a host volume in Kubernetes running on Docker Desktop (Windows 10) backed by WSL2?

Time:02-02

I've figured out the syntax to mount a volume (Kubernetes YAML):

apiVersion: v1
kind: Pod
metadata:
  ...
spec:
  containers:
    - name: php
      volumeMounts:
        - mountPath: /app/db_backups
          name: db-backups
          readOnly: true
  volumes:
    - hostPath:
        path: /mnt/c/Users/Mark/PhpstormProjects/proj/db_backups
        type: DirectoryOrCreate
      name: db-backups

And the volume does show when I drop into a shell:

kubectl --context docker-desktop exec --stdin --tty deploy/app-deployment-development -cphp -nmyns -- /bin/bash

But the db_backups directory is empty, so I guess the volume is backed by nothing -- it's not finding the volume on my Windows host machine.

I've tried setting the host path like C:\Users\Mark\PhpstormProjects\proj\db_backups but if I do that then my Deployment fails with a CreateContainerError:

Error: Error response from daemon: invalid volume specification: 'C:\Users\Mark\PhpstormProjects\proj\db_backups:/app/db_backups:ro'

So I guess it doesn't like the Windows-style filepath.

So what then? If neither style of path works, how do I get it to mount?

CodePudding user response:

From here it is clear that, for WSL2 we need to mention the specific path before we are actually giving the path we desired in the host machine.

In your file you are giving like path: /mnt/c/Users/Mark/PhpstormProjects/proj/db_backups but you need to mention the path like this path: /run/desktop/mnt/host/path_of_directory_in_local_machine. The key is we need to mention /run/desktop/mnt/host/ before we are going to give the actual path to the directory.

You gave the type: DirectoryOrCreate in the above file, so that is creating an empty directory in the path you mentioned. Because it is not actually going to your desired path.

So try with this

apiVersion: v1
kind: Pod
metadata:
  ...
spec:
  containers:
    - name: php
      volumeMounts:
        - mountPath: /app/db_backups
          name: db-backups
          readOnly: true
  volumes:
    - hostPath:
        path: /run/desktop/mnt/host/c/Users/Mark/PhpstormProjects/proj/db_backups
#In my case tested with path: /run/desktop/mnt/host/d/K8-files/voldir
        type: DirectoryOrCreate
      name: db-backups

It worked in our case, we created a directory in 'd' drive so we used this path: /run/desktop/mnt/host/d/K8-files/voldir. So try giving /run/desktop/mnt/host/ before the actual path.

For more information refer this Link

  • Related