Kubernetes Volumes
Pods are ephemeral. When a Pod stops or is deleted, any data written inside its containers is lost. Volumes provide a way to persist data beyond the lifetime of a Pod.
What Are Kubernetes Volumes?
A volume is a directory accessible to all containers in a Pod. Unlike the container’s filesystem, the data in a volume survives container restarts. The exact behavior depends on the volume type.
Volumes decouple data from the container lifecycle, allowing data to persist.
Common Volume Types
- emptyDir: Empty directory created when a Pod is assigned to a node. Data lasts as long as the Pod runs. Useful for temporary scratch space.
- hostPath: Mounts a file or directory from the host node’s filesystem. Dangerous for multi‑node clusters but useful for testing.
- configMap / secret: Special volume types for injecting configuration or sensitive data (covered later).
- persistentVolumeClaim: Used to attach persistent storage (covered in next chapter).
Example: emptyDir Volume
This Pod uses an
emptyDir volume to share a file between two containers:apiVersion: v1
kind: Pod
metadata:
name: shared-volume-pod
spec:
volumes:
- name: shared-data
emptyDir: {}
containers:
- name: writer
image: busybox
command: ["sh", "-c", "echo Hello > /data/hello.txt; sleep 3600"]
volumeMounts:
- name: shared-data
mountPath: /data
- name: reader
image: busybox
command: ["sh", "-c", "cat /data/hello.txt; sleep 3600"]
volumeMounts:
- name: shared-data
mountPath: /dataCreate the Pod:kubectl apply -f pod.yamlCheck the reader’s log:kubectl logs shared-volume-pod readerhostPath Example
Use with caution (only single‑node testing):
volumes:
- name: host-volume
hostPath:
path: /data/hostTwo Minute Drill
- Volumes provide storage that survives container restarts.
emptyDiris temporary, deleted when Pod is removed.hostPathmounts host directory (not recommended for production).- Define volumes in
spec.volumesand mount in containers withvolumeMounts.
Need more clarification?
Drop us an email at career@quipoinfotech.com
