Skip to content

Kubernetes EmptyDir Volume

An emptyDir volume is a type of Kubernetes storage that exists as long as the Pod using it is alive.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md] It is essentially a temporary directory created when a Pod is assigned to a node and deleted when the Pod is removed or evicted from that node.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]

Because it shares the exact lifecycle of the Pod, it is not suitable for persistent data.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md] Instead, its primary use cases involve data caching, temporary storage, and sharing files between containers running within the same Pod.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]

Configuration

The volume is defined in the spec.volumes field of a Pod manifest using the emptyDir nested field.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md] Key configuration options include:

  • medium: Defines the storage medium.
    • Default (empty): Uses the node's default storage medium (usually the disk).
    • Memory: Uses a RAM-backed filesystem (tmpfs).^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]
  • sizeLimit: Enforces a quota on the storage size.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]
    • Defaults to nil (unlimited).
    • It is strongly recommended to set this limit when using medium: Memory to prevent the container from consuming excessive node memory.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]

Use Cases and Examples

Sharing Data Between Containers

A common scenario for emptyDir is sharing data between containers in the same Pod.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md] For example, one container might act as a writer (generating content) while another serves as a consumer (serving the content).

In the example below, an alpine container writes text data to a mounted volume, while an nginx container mounts the same volume to serve that data via HTTP.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]

apiVersion: v1
kind: [Pod](<./pod.md>)
metadata:
  name: emptydir-pod
spec:
  volumes:
    - name: html
      emptyDir: {}
  containers:
    - name: nginx
      image: nginx:latest
      volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
    - name: alpine
      image: alpine
      volumeMounts:
        - name: html
          mountPath: /html
      command: [ "/bin/sh", "-c" ]
      args:
        - while true; do
          echo $(hostname) $(date) >> /html/index.html;
          sleep 10;
          done

High-Performance Caching (Memory Backed)

When performance is critical, the volume can be configured to use the node's RAM by setting the medium to Memory.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md] This utilizes tmpfs, which offers very fast read/write speeds at the cost of volatility (data is lost if the Pod restarts).^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]

apiVersion: v1
kind: [Pod](<./pod.md>)
metadata:
  name: emptydir-memory-pod
spec:
  volumes:
    - name: html
      emptyDir:
        medium: Memory
        sizeLimit: 256Mi
  # ... containers
  • Pod
  • [[Persistent Volume]]

Sources

^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]