Understanding Kubernetes Volumes — A Beginner’s Guide
Data on the Move: Unlocking the World of Kubernetes Volumes

Find Complete mind map of A Beginner’s Guide to Kubernetes
In Kubernetes, containers can have short lifecycles and may be frequently created and destroyed. When a container is terminated, any data saved within the container is also deleted. This can be problematic when data persistence is required.
To address this, Kubernetes introduced the concept of Volumes.
In this first post, we will introduce you to the basics of Kubernetes Volumes, including simple volume types like EmptyDir, HostPath, and NFS.
Check out “Understanding Kubernetes — A Beginner’s Guide” for the comprehensive series🚀
What is Volumes?
Volumes are shared directories within a Pod that can be accessed by multiple containers. They are defined within the Pod and then mounted to specific file directories within each container.
Kubernetes utilizes volumes to enable data sharing and persistent storage across different containers within a Pod. The lifecycle of a volume is independent of the lifecycle of a single container within the Pod. This means that when a container is terminated or restarted, the data within the volume is not lost.
Types of Volumes
Kubernetes supports various types of Volumes, including:
- EmptyDir: a temporary Volume that is created when a Pod is assigned to a Node and is deleted when the Pod is removed.
- HostPath: a Volume that mounts a file or directory from the host Node’s filesystem into the Pod.
- NFS: a network file system that allows a Pod to mount a shared network drive as a Volume.
- ConfigMap: a Volume that is used to provide configuration data to a Pod.
- Secret: a Volume that is used to store sensitive data, such as passwords or tokens.
Let’s get started!
Introduction to EmptyDir
EmptyDir is a basic type of Volume in Kubernetes that provides an empty directory for temporary storage.
It is created when a Pod is scheduled to a node and can be accessed by all containers in the Pod.
EmptyDir volumes are useful for scenarios where a container needs to write temporary data during the lifetime of a Pod, and the data doesn’t need to be persisted across Pod restarts.
EmptyDir volumes can also be used for sharing data between containers in a Pod. This is useful when multiple containers need to access a shared piece of data or communicate with each other. The EmptyDir volume is mounted at the same path in each container, and any changes made by one container are visible to all other containers that share the volume.

How to User EmptyDir in Kubernetes?
In order to use an EmptyDir volume, you need to define it in the Pod specification. Here is an example YAML definition of a Pod that contains an EmptyDir volume:
apiVersion: v1
kind: Pod
metadata:
name: multicontainer-pod
spec:
containers:
- name: producer
image: busybox
command: ["sh", "-c", "while true; do echo $(hostname) $(date) >> /var/log/index.html; sleep 10; done"]
volumeMounts:
- name: webcontent
mountPath: /var/log
- name: consumer
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: webcontent
mountPath: /usr/share/nginx/html
volumes:
- name: webcontent
emptyDir: {}
In this example, the first container, producer
, uses the BusyBox image and runs a command that writes the hostname and date to a log file located at /var/log/index.html
every 10 seconds. It also mounts the webcontent
EmptyDir volume at /var/log
directory to write the log file.
The second container, consumer
, uses the Nginx image and exposes port 80 to serve web content. It mounts the webcontent
EmptyDir volume at /usr/share/nginx/html
to read the log file generated by the producer
container.
So in this example, the producer
container is the writer and the consumer
container is the reader of the same EmptyDir volume. The producer
writes the log data to the shared volume, and the consumer
reads it to display on the Nginx web server.
Overall, EmptyDir volumes provide a simple and efficient way to share data between containers in a Pod or store temporary data during the lifetime of a Pod.
🔔 Stay tuned or subscribe to my series: “Understanding Kubernetes — A Beginner’s Guide” to explore everything about Kubernetes. 🚀
➕Join the Medium Membership Program to support my work and connect with other writers.
📝 Have questions or suggestions? Leave a comment or message me through Medium. Let’s connect!
Thank you for your support! 🌟