Skip to content
Lucas Mauro

What is a Kubernetes Volume?

What do we need to do to read and write data?

kubernetes 2 min read

Containers have their own filesystem, and everything in it is gone the moment the container restarts. This is fine for the application code of course, because it comes from the container image, but not for anything the application writes at runtime. Logs, cache files, user uploads, database data – those are all gone.

Volumes solve this problem. A volume is simply a directory that gets attached to a Pods and made available to its Containers. Depending on the volume type, that directory can be empty, pulled from the node’s filesystem, or backed by durable network storage.

Not all volumes are the same, though. Some are ephemeral (they die with the pod), others persist:

  • emptyDir: starts empty, lives as long as the pod. Great for scratch space or sharing files between containers in the same pod;
  • hostPath: mounts a file or directory from the node itself. Handy for node-level data, but it ties the pod to that specific node;
  • configMap: mounts a ConfigMap as a directory (each key becomes a file);
  • secret: mounts a Secret as a directory (same idea as configMap, but for sensitive data);
  • persistentVolumeClaim: mounts durable storage that survives pod restarts and reschedules (more on this in Persistent Storage).

apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-app
image: my-app:1.0
volumeMounts:
- name: data-volume
mountPath: /app/data
volumes:
- name: data-volume
emptyDir: {}

The volumes section defines the volume at the pod level. The volumeMounts section tells the container where to mount it in its filesystem. In this case, the container sees an empty /app/data directory. If another container in the same pod also mounts data-volume, they share that directory. When the pod is removed, so is the volume.

Most volume types are ephemeral: they live and die with the pod. For anything that needs to survive a restart (databases, file uploads, stateful applications), we need a Persistent Volume Claim (PVC), which connects the pod to durable storage. That is a whole topic on its own, covered in Persistent Storage.

Comments