Persistent Volume Claim

Persistent Volume claim is a request made for persistent volume. It consumes the storage provided by persistent volume. It can request specific storage size and access modes (such as ReadWriteOnce, ReadOnlyMany etc).

Kubernetes find the best persistent volume available matching the claim requirements such as requested storage, access modes etc.

Tip

Claim can also have label matchers to select particular volumes.

spec:
   selector:
     matchLabels:
       volume: frontend-pv

A simple pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: mypvc
    namespace: learning
spec:
    accessModes:
        - ReadWriteOnce
    resources:
        requests:
            - storage: 500Mi

Using PVC in Pod

Persistent volume claim can be used inside as follows.

apiVersion: v1
kind: Pod
metadata:
  name: nginx-with-pvc
  labels:
    type: router
  namespace: learning
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: mypvc
          mountPath: /shared
  volumes:
    - name: mypvc
      persistentVolumeClaim:
        claimName: mypvc

Some observations

Assumption is if persistent volume is implemented using hostPath.

  1. Pods using same pvc, get same location mounted on node if they run on same node. For above pvc, /shared will be mounted to some path such as /tmp/hostpath-provisioner/learning/mypvc where hostpath-provisioner is the provisioner, learning is namespace and mypvc is pvc name.
  2. Pods using same pvc but on different nodes, they get same mounted path but on their respective nodes. For example, runs on will get /tmp/hostpath-provisioner/learning/mypvc mounted path on . runs on will get /tmp/hostpath-provisioner/learning/mypvc mounted path on .
  3. Pods using different pvc get different mount paths on nodes irrespective if they run on same node or different nodes.