Skip to main content

Volumes, PV & PVC

  • The pod doesn’t talk directly to a PV — it uses a PVC, which in turn is bound to a PV.
    • PVC is a contract between PV and the pod.
  • A PersistentVolume can be created standalone, without binding.
  • A PVC does not have to name a PV. Kubernetes will bind it automatically to a suitable PV (manual binding is also possible but uncommon).
  • A Pod uses a PVC to claim access to storage — the Pod never uses a PV directly.
  • PV to PVC binding is one to one, always.
  • One PVC can be used by many Pods if the mode allows.

Pod using PV

apiVersion: v1
kind: Pod
metadata:
name: random-number-gen
spec:
containers:
- name: alpine
image: alpine
command: ["/bin/sh", "-c"]
args: ["shuf -i 0-100 -n 1 >> /opt/number.out && sleep 3600"]
volumeMounts:
- mountPath: /opt
name: data-volume
volumes:
- name: data-volume
hostPath:
path: /data
type: Directory
- name: myPd
persistentVolumeClaim:
claimName: myclaim

Persistent Volume

apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-vol1
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
hostPath:
path: "/pv/log"
awsElasticBlockStore:
volumeID: <id>
fsType: ext4
persistentVolumeReclaimPolicy: Retain

Persistent Volume Providers

NFS, GlusterFS, Flocker, ceph, ScaleIO, aws, gcp...

PVC

PersistentVolumeClaim

k get persistentvolumeclaim # pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500mi

Storage Class

StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: cloud-storage
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
fsType: ext4
reclaimPolicy: Retain
allowVolumeExpansion: true
volumeBindingMode: Immediate|WaitForFirstConsumer
PVC with storage class
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: cloud-storage
resources:
requests:
storage: 10Gi