Problem

I have had a hard time figuring out how to make a persistent volume work in minikube running on Mac OS.

Solution

I could get this to work only if I used the xhyve driver when you create minikube. How do you do that?

Step 1: Install xhyve driver

https://github.com/kubernetes/minikube/blob/master/DRIVERS.md#xhyve-driver

$ brew install docker-machine-driver-xhyve

# docker-machine-driver-xhyve need root owner and uid
$ sudo chown root:wheel $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
$ sudo chmod u+s $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve

Step 2: Create and start minikube using xhyve driver

minikube start --vm-driver=xhyve

Step 3: Verify that your /Users folder from Mac is auto-mounted in the minikube vm

minikube runs a VM. The /Users folder from your Mac gets mounted on this VM at the same location /Users by default. You can verify this by going into the minikube vm.

$ minikube ssh
$ cd /Users
$ ls

Step 4: Create a Persistent Volume in kubernetes using hostPath

When you create a persistent volume in kubernetes using a hostPath, the hostPath can use any location under /Users. Here is a sample YAML file for creating a persistent volume in kubernetes:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: microxs-commerce-jenkins-volume-1
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 5Gi
  hostPath:
    path: /Users/lijojacob/bitbucket/microxs-commerce/jenkins-data

Step 5: Use the persistent volume in a persistent volume claim

Pods can use persistent volume claims. The persistent volume claim can in-turn use the persistent volume we created above. Here is the YAML file for a persistent volume claim:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: microxs-commerce-jenkins-volume-1-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  volumeName: microxs-commerce-jenkins-volume-1

Step 6: Go ahead and use your persistent volume claim in your pod/deployment

Here is a YAML snippet of using the persistent volume claim in your pod

volumes:
  - name: microxs-commerce-jenkins-volume-1
    persistentVolumeClaim:
      claimName: microxs-commerce-jenkins-volume-1-claim
Advertisements