Lifetimes

Where a program writes decides how long the data lives

10:00pod starts10:20container restarts10:40pod replacedThe container's own fileslost at every restartAn emptyDir volumelasts as long as its podA persistent volumeoutlives every podWhere a program writes decides how long the data lives.Files written inside a container vanish when it restarts. An emptyDir volume belongs to the pod:it survives a container crash and is shared by the pod's containers, but goes when the pod goes.Data that must outlive pods needs a persistent volume, which exists apart from any pod.

Container files: lost at restart. emptyDir: with the pod. Persistent: kept.

Claims and volumes

A claim asks for storage; the cluster finds or makes it

Pod db-0needs a diskClaim data10Gi, ReadWriteOnceyou write: a request for storageStorageClassstandard: makes disksVolume pvc-3f2a10Gi, made to ordera real cloud diskboundmountsA claim asks for storage. A volume is the storage. The cluster binds one to the other.The split lets an application say what it needs, 10 gibibytes it can write, without knowing wheredisks come from. A StorageClass creates a matching disk on demand and a volume object for it,and the claim is bound to that volume. The pod only ever names the claim.

A claim is the request. A volume is the disk. They are bound.

Claims and volumes · Going deeper · 1 of 2

The claim, and a pod that uses it

The claimapiVersion: v1kind: PersistentVolumeClaimmetadata: name: dataspec: accessModes: [ReadWriteOnce] resources: requests: { storage: 1Gi }A pod that uses itapiVersion: v1kind: Podmetadata: name: writerspec: containers: - name: writer image: busybox command: ["sleep", "infinity"] volumeMounts: - { name: data, mountPath: /data } volumes: - name: data persistentVolumeClaim: { claimName: data }No storage class is named,so the cluster's default is used.The pod names the claim, and says where to mount it. Nothing else.Save both parts in one file, data.yaml, separated by a line of three dashes, for the lab.

A claim for 1Gi, and a pod that mounts it at /data.

StorageClass · Going deeper · 2 of 2

A StorageClass as written: four fields decide what a claim gets

apiVersion: storage.k8s.io/v1kind: StorageClassmetadata: name: fast annotations: storageclass.kubernetes.io/is-default-class: "true"provisioner: ebs.csi.aws.comparameters: type: gp3reclaimPolicy: DeletevolumeBindingMode: WaitForFirstConsumerallowVolumeExpansion: trueclaims that name no class get this onethe CSI driver that makes the disks: here, AWS EBShanded to the driver: the kind of diskDelete is the default; Retain keeps the diskmake the disk when a pod needs it; default: Immediatea claim may be enlarged later, never shrunkA StorageClass is a recipe for disks. Four of its fields decide what happens to every claim that uses it.With Immediate, the disk is made as soon as the claim exists, perhaps in a zone where the pod cannot run. WaitForFirstConsumerwaits for the scheduler to choose a node and makes the disk in that zone: until a pod uses the claim it shows Pending, whichis normal. To grow a volume, raise storage in the claim. kubectl get storageclass marks the default class.

provisioner, reclaimPolicy, volumeBindingMode, allowVolumeExpansion.

Moving

When a pod moves, its disk follows it

BeforeNode A, zone adb-0disk, zone aNode B, zone aNode C, zone bAfterNode Ahas failedNode B, zone adb-0disk, zone aNode C, zone bthe disk cannot come hereWhen a pod moves, its volume is detached and attached to the new node.The data is not copied. The same disk follows the pod, which is why a database can lose itsnode and return with everything intact. But a cloud disk lives in one zone, so the pod can onlybe placed on nodes in that zone. The scheduler takes this into account.

The disk is re-attached, not copied. But it cannot leave its zone.

Moving · Going deeper

Lab: data that outlives its pod

TypeLook forkubectl get storageclasskubectl apply -f data.yamlkubectl get pvc datakubectl exec writer -- sh -c 'echo hello > /data/note'kubectl delete pod writer && kubectl apply -f data.yamlkubectl exec writer -- cat /data/notestandard (default)a claim and a pod createdSTATUS Bound, with a VOLUME named pvc-...nothing: the file is writtenthe pod deleted, then created againhello: the data outlived the podLab: write to a volume, delete the pod, and read the data back.data.yaml is the file from the earlier page, "The claim, and a pod that uses it". The claim stays when the pod goes.Clean up with "kubectl delete -f data.yaml". With this class, deleting the claim deletes the data.

Write a file, delete the pod, read the file from a new one.

Access modes

The access mode says how many nodes may mount at once

Access modeThree nodesWhat it allowsReadWriteOnceRWOrw--one node at a time: ordinary cloud disksReadOnlyManyROXrororomany nodes, for reading onlyReadWriteManyRWXrwrwrwmany nodes at once: needs a file share, such as NFSReadWriteOncePodRWOPrw--one single pod in the whole clusterThe access mode says how many nodes may mount a volume at once.Most cloud disks attach to one machine only, so they offer ReadWriteOnce. Sharing one volumebetween pods on different nodes needs storage built for it, such as a network file share. Askfor a mode the storage cannot give and the claim simply stays unbound.

ReadWriteOnce for disks. ReadWriteMany needs a file share.

Static volumes · Going deeper

A hand-made volume binds only if class, mode and size all fit

A volume made by handA claim that binds to itapiVersion: v1kind: PersistentVolumemetadata: name: pv-logsspec: capacity: { storage: 5Gi } accessModes: [ReadWriteOnce] storageClassName: manual persistentVolumeReclaimPolicy: Retain hostPath: { path: /mnt/logs }apiVersion: v1kind: PersistentVolumeClaimmetadata: name: logsspec: accessModes: [ReadWriteOnce] storageClassName: manual resources: requests: { storage: 2Gi }✓storageClassName: the same on both✓accessModes: the volume offers what is asked✓capacity: 5Gi covers the 2Gi askedBound, the claim holds the whole 5Gi, not 2Gi.A claim binds to a hand-made volume only when class, access mode and size all fit. Otherwise it waits, Pending.The usual trap: a claim that leaves storageClassName out is given the cluster's default class, so it never matches a volumeof class manual. A new disk is made for it instead, or it waits. Binding is one to one: the spare 3Gi cannot serve a secondclaim. hostPath suits single-node practice clusters only, because the data stays on one node.

Same storageClassName, a mode on offer, and capacity at least the request.

Reclaim

Deleting the claim may erase the disk

Deletethe usual defaultClaimdeletedVolumedeleted tooDiskerasedRetainset it for data that mattersClaimdeletedVolumekept, marked ReleasedDiskkept, with its dataDeleting a pod never touches the data. Deleting the claim may erase it.Each volume has a reclaim policy that says what happens when its claim is deleted. Volumes madeon demand usually take the policy Delete from their StorageClass: the disk goes with the claim.With Retain the disk is kept until an administrator decides. Neither replaces a backup.

Policy Delete: the disk goes too. Policy Retain: it is kept.

Reclaim · Going deeper · 1 of 2

StatefulSets: a claim for every replica

Pod db-0a replicaClaim data-db-010Gi, its own diskPod db-1a replicaClaim data-db-110Gi, its own diskPod db-2a replicaClaim data-db-210Gi, its own diskPod db-1replaced: same claimA StatefulSet gives every replica a claim of its own.A volumeClaimTemplate in the StatefulSet makes one claim for each pod, named after it. Whendb-1 is replaced, the new db-1 gets the claim data-db-1 again, and so the same data. Scalingdown leaves the claims in place, so scaling back up finds the data waiting.

Each replica has its own claim, and gets it back when replaced.

Volume phases · Going deeper · 2 of 2

Available, Bound, Released: and Released does not bind again

Availableno claim yetBoundtied to one claimReleasedclaim deleted, data keptFailedreclaim went wrongkubectl get pv (typical output, some columns left out)NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASSpv-logs 5Gi RWO Retain Available manualpvc-3f2a 10Gi RWO Delete Bound shop/data standardpv-old 10Gi RWO Retain Released shop/old manualA Released volume still remembers its old claim, so no new claim can bind to it.With Retain, deleting the claim leaves the volume Released: the data is on it and the old claim is still written inspec.claimRef. After saving or wiping the data, an administrator frees it: kubectl patch pv pv-old -p '{"spec":{"claimRef":null}}'makes it Available again. To protect a volume made with Delete, patch persistentVolumeReclaimPolicy to Retain first.

A Released volume keeps its old claimRef until someone clears it.

Lesson complete

You finished “Storage”

Nicely done.

Open in a new tab