Understanding emptyDir and local Volumes in Kubernetes

 

Overview

Kubernetes provides different volume types depending on how long data should live and where it should live.
Two commonly confused options are:

  • emptyDir
  • local Persistent Volumes

Although both rely on node storage, they behave very differently in terms of lifecyclescope, and production suitability.


emptyDir: Pod-Level Ephemeral Storage

What is emptyDir?

emptyDir is an ephemeral volume that exists only for the lifetime of a Pod.

  • Created when a Pod is scheduled to a node
  • Deleted as soon as the Pod is removed
  • Initially empty
  • Shared between all containers inside the same Pod

Data strictly follows the Pod lifecycle.

Key Characteristics

  • Scoped to one Pod
  • Cannot be shared across Pods
  • Data is lost when:
    • Pod is deleted
    • Pod is rescheduled to another node

Typical Use Cases

  • Sharing temporary files between containers
  • Caching data
  • Scratch space
  • Temporary processing artifacts

Example

apiVersion: v1
kind: Pod
metadata:
  name: example-emptydir
spec:
  containers:
    - name: app
      image: busybox
      command: ["sh", "-c", "echo hello > /data/file.txt && sleep 3600"]
      volumeMounts:
        - name: cache
          mountPath: /data
  volumes:
    - name: cache
      emptyDir: {}

local Volumes: Node-Level Persistent Storage

What is a Local Volume?

local volume is a PersistentVolume backed by local disk on a specific node.

  • Data survives Pod restarts
  • Data is tied to one specific node
  • Data is lost if the node is removed or replaced

Data follows the Node lifecycle, not the Pod lifecycle.

Key Characteristics

  • Requires a PersistentVolume
  • Requires a PersistentVolumeClaim
  • Node affinity is mandatory
  • Supports static provisioning only
  • Pod must always run on the same node

Example PersistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /mnt/disks/data
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - node-1

Example PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: local-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: local-storage

Lifecycle Comparison

FeatureemptyDirlocal volume
ScopePodNode
Persistence❌ No✅ Yes (node-bound)
Requires PVC❌ No✅ Yes
Node Affinity❌ No✅ Mandatory
Survives Pod restart❌ No✅ Yes
Survives Node loss❌ No❌ No
ProvisioningDynamicStatic only

Production Considerations

⚠️ Important

Neither emptyDir nor local volumes are recommended for critical production data.

  • emptyDir → data disappears with the Pod
  • local → data disappears with the Node

When They Make Sense

VolumeWhen to Use
emptyDirTemporary files, cache, IPC between containers
localPerformance-sensitive workloads where data loss is acceptable

For real persistence, prefer:

  • Network storage (NFS, iSCSI)
  • Cloud volumes (EBS, PD, Azure Disk)
  • CSI-based storage solutions

Key Takeaway

Always think in terms of data lifecycle.

  • Pod lifecycle → emptyDir
  • Node lifecycle → local volume

If losing the data is not acceptable, do not use either.

Comments

Popular posts from this blog

Highlights from the 2025 Stack Overflow Developer Survey

Mastering Caddy Logging: A Complete Guide to Access, Error, and Structured Logs

psql: error: connection to server at "localhost" (127.0.0.1), port 5433 failed: ERROR: failed to authenticate with backend using SCRAM DETAIL: valid password not found