0
0
KubernetesConceptBeginner · 3 min read

What is emptyDir Volume in Kubernetes: Explanation and Example

emptyDir is a type of Kubernetes volume that provides a temporary directory shared between containers in a pod. It is created when the pod starts and deleted when the pod stops, storing data only during the pod's lifetime.
⚙️

How It Works

An emptyDir volume is like a temporary folder created fresh for a pod when it starts running. Imagine you have a workspace desk that is empty when you arrive and you can put files on it while you work. When you leave, the desk is cleared.

This volume is stored on the node's disk or memory and is shared between all containers in the same pod. It allows containers to share data easily during the pod's life. But once the pod is deleted or restarted, everything in the emptyDir is lost.

💻

Example

This example shows a pod with two containers sharing an emptyDir volume named shared-data. One container writes a file, and the other can read it.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: emptydir-example
spec:
  containers:
  - name: writer
    image: busybox
    command: ["sh", "-c", "echo Hello from writer > /data/message.txt; sleep 3600"]
    volumeMounts:
    - name: shared-data
      mountPath: /data
  - name: reader
    image: busybox
    command: ["sh", "-c", "sleep 3600"]
    volumeMounts:
    - name: shared-data
      mountPath: /data
  volumes:
  - name: shared-data
    emptyDir: {}
Output
Pod 'emptydir-example' runs with two containers sharing /data directory. The 'writer' container creates /data/message.txt. The 'reader' container can access the same file at /data/message.txt.
🎯

When to Use

Use emptyDir when you need temporary storage that lasts only while the pod runs. It is great for:

  • Sharing files between containers in the same pod.
  • Storing scratch data or caches that do not need to persist.
  • Working with data that can be recreated if lost.

It is not suitable for data you want to keep after pod restarts or failures, as the data will be deleted.

Key Points

  • emptyDir volume is created when a pod starts and deleted when it stops.
  • It provides a shared temporary directory for all containers in a pod.
  • Data in emptyDir is lost if the pod is removed or restarted.
  • Useful for temporary storage, caches, or sharing files between containers.
  • Not for persistent or important data that must survive pod restarts.

Key Takeaways

emptyDir is a temporary volume that exists only during a pod's lifetime.
It allows containers in the same pod to share files easily.
Data stored in emptyDir is deleted when the pod stops or restarts.
Use it for scratch space, caches, or temporary data that can be recreated.
Do not use emptyDir for data that needs persistence beyond the pod.