Connecting to persistent volume in Kubernetes?

5/4/2018

I'm in the process of converting a stack to k8s. The database requires persistent storage.

I have used kubectl create -f pv.yaml

pv.yaml (with edits based on @whites11's answer):

kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/nfs"
  claimRef:
    kind: PersistentVolumeClaim
    namespace: default
    name: mongo-persisted-storage

I then create an example mongo replica set.

When I look at my k8s dashboard I see the error:

PersistentVolumeClaim is not bound: "mongo-persistent-storage-mongo-0" (repeated 2 times)

enter image description here

In the persistent volume tab I see the volume which looks ok:

enter image description here

I'm having trouble figuring out the next step to make the volume claim happen successfully.

Edit #2

I went into the PVC page on the GUI and added a volume to the claim manually (based on feedback from @whites11). I can see that the PVC has been updated with the volume but it is still pending.

enter image description here

Edit #3

Realizing that since making the change suggested by @whites11, the original error message in the pod has changed. It is now "persistentvolume "pvvolume" not found (repeated 2 times)", I think I just need to figure out where I wrote pvvolume, instead of pv-volume. (or it could be the - was auto-parsed out somewhere?

enter image description here

-- Philip Kirkbride
kubernetes
kubernetes-pvc

1 Answer

5/4/2018

You need to manually bind your PV to your PVC, by adding the appropriate claimRef section to the PV spec.

In practice, edit your PV with the method you prefer, and add a section similar to this:

claimRef:
  name: mongo-persisted-storag
  namespace: <your PVC namespace>

Than, you need to edit your PVC to bind the correct volume, by adding the following in its spec section:

volumeName: "<your volume name>"

Here an explanation on how this process works: https://docs.openshift.org/latest/dev_guide/persistent_volumes.html#persistent-volumes-volumes-and-claim-prebinding

-- whites11
Source: StackOverflow