path for PersistentVolume in Kubernetes .yaml in Windows

3/20/2020

I'm at the first steps with Kubernetes, and i'm stuck with a problem of Windows paths. I defined a .yaml where for a PersistentVolume i have (file not complete, only the part for the problem)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage
  local:
    path: /c/temp/data/db
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop

I'm using the latest minikube (1.8.2) with updated kubectl on Windows 10 Pro updated, Docker Community latest version.

I searched a lot because of every sample for Kubernetes is referring to paths for Unix/Macos. I found (don't remember where...) that a valid path for Windows should be the one in the sample path: /c/temp/data/db

But it does not work: Docker is switched on Linux containers, c: shared, Kubernetes in Docker activated, with "describe pod " I get

"didn't find available persistent volumes to bind"

Obviously I tried another disk (shared in Docker), tried "/c/temp/data/db", that is surrounding with ", tried to give all perms to Everyone on this path, /c/Users...nothing

-- Roberto Alessi
kubernetes
persistence
windows
yaml

1 Answer

3/23/2020

I searched a lot because of every sample for Kubernetes is referring to paths for Unix/Macos. I found (don't remember where...) that a valid path for Windows should be the one in the sample path: /c/temp/data/db

With minikube, you can't mount your local directory into a PersistentVolume as you are trying.

Minikube creates a virtual machine with Linux and your cluster is running inside this Linux VM. That's why it can't see your files in your windows machine.

To be able to access your local directory into your minikube cluster you need to mount it into your minikube:

You have few options to achieve what you need. The best and easiest way is to start your minikube with the option --mount. This option will mount C:/Users/ by default.

Example:

PS C:\WINDOWS\system32> minikube delete; minikube.exe start --vm-driver=virtualbox --mount  

If you ssh into minikube Linux VM:

PS C:\WINDOWS\system32> minikube ssh
                         _             _
            _         _ ( )           ( )
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

$
$ df -h  
Filesystem Size Used Avail Use% Mounted on  
tmpfs 1.9G 489M 1.5G 26% /  
devtmpfs 987M 0 987M 0% /dev  
tmpfs 1.1G 0 1.1G 0% /dev/shm  
tmpfs 1.1G 17M 1.1G 2% /run  
tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup  
tmpfs 1.1G 4.0K 1.1G 1% /tmp  
/dev/sda1 17G 1.3G 15G 8% /mnt/sda1  
/c/Users 181G 106G 76G 59% /c/Users  
$ cd /c/Users/  
$ pwd  
/c/Users  

If you want\need to mount any other directory than C:/Users, take a look at minikube mount and/or --mount-string. You may face some issues with these option depending on your vm-driver.

After mounting your directory you can use it in you PersistentVolume refering to the Linux path that based on my examples it can be /c/Users/myname/myapp/db.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage
  local:
    path: /c/Users/myname/myapp/db
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop

Please, let me know if my answer helped you to solve your problem.

-- mWatney
Source: StackOverflow