Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- Which StorageClass? (line 1) — the one marked
(default)is used when you do not name one. Note itsRECLAIMPOLICY(DeleteorRetain) andVOLUMEBINDINGMODE. - Create the PVC (quick answer). With
WaitForFirstConsumerit staysPendinguntil a Pod uses it — that is normal (the disk is created in the Pod's zone). - Mount it in the Pod:
spec:
containers:
- name: api
volumeMounts:
- name: uploads
mountPath: /var/lib/api/uploads
volumes:
- name: uploads
persistentVolumeClaim:
claimName: uploads- Access modes —
ReadWriteOnce(RWO): one node mounts it (block disks; a Deployment with this should have 1 replica orstrategy: Recreate).ReadWriteMany(RWX): many nodes (NFS, Azure Files, EFS, CephFS).ReadWriteOncePod: exactly one Pod. - File owner — if the app runs as a non-root user, set
securityContext.fsGroupso it may write to the volume. - Grow it — if the class has
allowVolumeExpansion: true, edit the request (for example 20Gi); most CSI drivers grow online:
kubectl -n shop patch pvc uploads -p '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}'Shrinking is not possible.
- Delete with care — with
reclaimPolicy: Deletedeleting the PVC deletes the disk and its data. For important data use a class withRetain, and back up with Velero or CSI snapshots. - More than one Pod with its own disk — use a StatefulSet with
volumeClaimTemplates.
Check that it worked
kubectl -n shop get pvc uploads shows STATUS Bound with a volume name and 10Gi; a file written into /var/lib/api/uploads is still there after kubectl -n shop delete pod <pod> (the new Pod mounts the same volume).
Common problems
| Problem | Cause | Fix |
|---|---|---|
PVC Pending forever | No default StorageClass, or wrong name. | Name an existing class; check kubectl describe pvc events. |
Pod ContainerCreating: Multi-Attach error | RWO volume still attached to a Pod on another node. | One replica / Recreate; or an RWX class. |
Permission denied writing | App user cannot write the volume. | securityContext.fsGroup; check the image user. |
| Pod stuck in another zone | Disk is zonal, Pod scheduled elsewhere. | WaitForFirstConsumer binding; topology-aware classes. |
| Data gone after deleting the app | Reclaim policy Delete. | Retain class for important data; backups. |
| Resize does nothing | Class without allowVolumeExpansion. | Use an expandable class; some drivers need a Pod restart. |
Go further
- Command pages: kubectl.
- Next how-tos: Run a database as a StatefulSet, Back up Kubernetes with Velero, Find why a Kubernetes Pod does not start, Share a folder with NFS.
- Learn it: CKA — Storage objects, CKA — Access modes, CKA — Dynamic provisioning.
1What does a PersistentVolumeClaim do?
It asks for storage of a size and access mode; the StorageClass then creates a volume and binds it to the claim.
2What happens to the data when you delete a PVC with reclaim policy Delete?
The volume and all its data are deleted too.