Dblock247
Dblock247

Reputation: 6725

Kubernetes Persistent Volume Claims creates a new Persistent Volume instead of binding to the available Persistent Volume

I am running mac OSX Catalina using the docker application with the Kubernetes option turned on. I create a PersistentVolume with the following yaml and command.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-data
spec:
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 10Gi
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 192.168.1.250
    path: "/volume1/docker"

kubectl apply -f pv.yml

This create and PersistentVolume with name pv-nfs-data. Next I then create a PersistentVolumeClaim with the following yaml and command

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nfs-data
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

kubectl apply -f pvc.yml

This create a PersistentVolumeClaim with the name pvc-nfs-data however it doen't bind it to the available PersistentVolume (pv-nfs-data). Instead it creates an new one and binds it to that. How do I make the PersistentVolumeClaim bind to the available PersistentVolume

Upvotes: 2

Views: 877

Answers (1)

Matt
Matt

Reputation: 74670

The Docker for Mac default storage class is the dynamic provisioning type, like you would get on AKS/GKE, where it allocates the physical storage as well.

→ kubectl get StorageClass
NAME                 PROVISIONER          AGE
hostpath (default)   docker.io/hostpath   191d

For a PVC to use an existing PV, you can disable the storage class and specify in the PV which PVC can use it with a claimRef.

Claim Ref

The PV includes a claimRef for the PVC you will create

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-data
spec:
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 10Gi
  claimRef:
    namespace: insert-your-namespace-here
    name: pv-nfs-data-claim
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 192.168.1.250
    path: "/volume1/docker"

The PVC sets storageClassName to ''

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pv-nfs-data-claim
  namespace: insert-your-namespace-here
spec:
  storageClassName: ''
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Dynamic

You can go the dynamic route with NFS by adding an NFS dynamic provisioner, create a storage class for it and let kubernetes work the rest out. More recent version of Kubernetes (1.13+) can use the CSI NFS driver

Upvotes: 4

Related Questions