Volume types and projections
Choose a volume by the lifetime of its data
A Kubernetes volume makes files or a block device available to a container. Start by asking what must survive. In our bookstore, a downloaded search index can be rebuilt, configuration belongs to the application release, and the inventory must survive a replacement Pod. These three needs call for different volume types. A volume belongs to the Pod specification, while a container uses volumeMounts to choose where a filesystem appears. Several containers can mount the same volume at different paths. A mounted volume can hide files that the image originally contained at that path. The mount is not a copy of the image directory.
Mount temporary, projected and persistent data together
Read this offline Pod manifest. It assumes the storage-study namespace, a ConfigMap named catalog-settings containing mode=study, and an existing PVC catalog-data. The image is intentionally non-deployable. The scratch directory is writable, the settings directory exposes mode.txt and pod-name.txt, and the inventory is mounted read-only. Read-only here describes this consumer mount, not a global ban on other writers. The container would need an application that actually reads these paths; the manifest alone does not demonstrate that behavior.
apiVersion: v1kind: Podmetadata: name: catalog-reader namespace: storage-studyspec: automountServiceAccountToken: false restartPolicy: Never containers: - name: reader image: registry.example.invalid/catalog-reader:study resources: requests: {cpu: 50m, memory: 32Mi, ephemeral-storage: 32Mi} limits: {cpu: 200m, memory: 128Mi, ephemeral-storage: 128Mi} volumeMounts: - {name: scratch, mountPath: /work} - {name: settings, mountPath: /settings, readOnly: true} - {name: data, mountPath: /data, readOnly: true} volumes: - name: scratch emptyDir: sizeLimit: 64Mi - name: settings projected: sources: - configMap: name: catalog-settings items: - {key: mode, path: mode.txt} - downwardAPI: items: - path: pod-name.txt fieldRef: {fieldPath: metadata.name} - name: data persistentVolumeClaim: claimName: catalog-data readOnly: trueUnderstand emptyDir and resource pressure
An emptyDir begins empty when the Pod is assigned to a node. Its files can survive a container crash and restart within that same Pod. Once the Pod is removed from the node, its emptyDir data is not retained for a replacement Pod, even if the replacement has the same name. The usual disk-backed variant uses node ephemeral storage; a 64Mi sizeLimit is not a reservation or guarantee that 64Mi is available. Node pressure and other writes may constrain usable space. With medium: Memory, the volume is RAM-backed and writes count toward memory usage of the writing container. Use it for bounded temporary work, not as a substitute for a persistent database. A new Pod identity starts with new scratch storage.
Project configuration and metadata into files
A ConfigMap volume exposes nonsecret configuration; a Secret volume exposes sensitive values and needs access controls, protected storage and careful log handling. A downwardAPI volume exposes selected Pod metadata or resource information without an application API request. A projected volume combines supported sources in one directory. Here the ConfigMap key mode is renamed mode.txt and metadata.name becomes pod-name.txt. Sources such as ConfigMaps and Secrets must be in the same namespace as the Pod. Use distinct target paths and read-only mounts; do not make a configuration projection your application write directory. Projected data updates can lag, and subPath mounts do not receive automated updates. Applications must reopen or reload files when appropriate. Never treat a Secret volume as evidence that its underlying data is encrypted. Service account token projection is a separate identity mechanism; avoid granting tokens when a workload does not need them.
When a temporary volume needs a storage driver
Generic ephemeral volumes use a PVC template inside the Pod specification. The control plane creates a PVC owned by the Pod, then normal provisioning and binding can occur. This is useful for temporary scratch storage that needs a StorageClass or storage-backed features. A generated claim name follows the Pod-name plus volume-name convention; inspect ownership rather than assuming a same-named existing claim belongs to your Pod. The fixture would generate catalog-indexer-work. Deleting the Pod normally triggers deletion of its owned claim, subject to normal controller/finalizer behavior; the PV reclaim policy governs backend cleanup. A Retain policy can leave storage behind. CSI inline ephemeral volumes instead use a csi source in the Pod and require a driver that supports that lifecycle mode; they do not follow the same automatic PVC workflow. Do not equate either kind with free local disk. The following example is illustrative only and uses the fictional class from Chapter 20.
apiVersion: v1kind: Podmetadata: name: catalog-indexer namespace: storage-studyspec: automountServiceAccountToken: false restartPolicy: Never containers: - name: indexer image: registry.example.invalid/catalog-indexer:study resources: requests: {cpu: 50m, memory: 32Mi} limits: {cpu: 200m, memory: 128Mi} volumeMounts: - {name: work, mountPath: /work} volumes: - name: work ephemeral: volumeClaimTemplate: spec: accessModes: [ReadWriteOnce] storageClassName: study-zonal resources: requests: {storage: 2Gi}Practice choosing the right storage
Use lifecycle-cases.json and the two Pod manifests in lab-fixtures/volume-csi. Predict what a replacement Pod sees after a same-Pod restart and after Pod deletion. A cache can be lost; a PVC-backed inventory may still be accessible, assuming the claim and storage remain healthy. A configuration projection can be reconstructed from its source object. Verify the volume names and mount paths. Do not mount hostPath or a real host disk to fix a missing volume. For actual workloads, confirm capacity, access modes, permissions and application behavior in an approved isolated environment. Next: the CSI control path and snapshots.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Volume types and projections — knowledge check
Reason about Kubernetes storage using synthetic scenarios.
Which volume is appropriate for a rebuildable cache that should survive a container restart in the same Pod?
What happens to emptyDir when the Pod is replaced?
Which source exposes the Pod name as a projected file?
What does a generic ephemeral volume normally create?
What should you conclude from emptyDir.sizeLimit: 64Mi?