Skip to content

🎓 LEVEL 40 DEBRIEF: emptyDir vs PersistentVolumeClaim

Congratulations! You've completed World 4 and mastered the difference between ephemeral and persistent storage!


📊 What You Fixed

The Problem:

volumes:
- name: data
  emptyDir: {}  # ❌ Ephemeral! Data lost on restart
Result: All data disappears when pod restarts

The Solution:

volumes:
- name: data
  persistentVolumeClaim:
    claimName: app-data  # ✅ Persistent!
Result: Data survives pod restarts, deletions, and recreations


🔍 emptyDir vs PersistentVolumeClaim

emptyDir (Ephemeral Storage)

Characteristics: - Created when pod assigned to node - Initially empty - Deleted when pod removed - Fast (local node storage) - No PVC/PV needed

Use Cases:

# Scratch space
emptyDir: {}

# Cache
emptyDir:
  sizeLimit: "1Gi"

# Shared temp data between containers
emptyDir: {}

Good for: - Build artifacts during CI/CD - Temporary caches - Scratch space - Container-to-container data sharing

NOT for: - Databases - User uploads - Application state - Anything that must survive pod restart

PersistentVolumeClaim (Persistent Storage)

Characteristics: - Backed by PersistentVolume - Survives pod lifecycle - Data independent of pod - Can be reattached - May be slower (network storage)

Use Cases:

persistentVolumeClaim:
  claimName: database-storage  # Database data

persistentVolumeClaim:
  claimName: user-uploads  # User files

persistentVolumeClaim:
  claimName: app-state  # Application state

Good for: - Databases (PostgreSQL, MySQL, MongoDB) - User-generated content - Application logs (if must persist) - Configuration that must survive


💥 Common Mistakes

Mistake 1: emptyDir for Database

# ❌ WRONG - Data lost on restart!
kind: Pod
spec:
  volumes:
  - name: db-data
    emptyDir: {}  # Database data will be lost!

Fix:

# ✅ Correct
volumes:
- name: db-data
  persistentVolumeClaim:
    claimName: db-pvc

Mistake 2: PVC for Temporary Files

# ❌ Wasteful - PVC not needed
volumes:
- name: build-cache
  persistentVolumeClaim:
    claimName: cache-pvc  # Overkill for temp cache

Fix:

# ✅ More efficient
volumes:
- name: build-cache
  emptyDir:
    sizeLimit: "5Gi"

Mistake 3: Not Setting Size Limit

emptyDir: {}  # ❌ Can fill up node disk!

Fix:

emptyDir:
  sizeLimit: "1Gi"  # ✅ Prevents disk exhaustion


🏗️ Decision Matrix

Requirement Use emptyDir Use PVC
Must survive pod restart ❌ ✅
Shared between pods ❌ ✅
Fast local storage ✅ ❌
Temporary data ✅ ❌
Database storage ❌ ✅
Build artifacts ✅ ❌
User uploads ❌ ✅
Cache (can rebuild) ✅ ❌
Logs (must keep) ❌ ✅

🛡️ Best Practices

  1. Use emptyDir for temporary:

    emptyDir:
      sizeLimit: "1Gi"  # Always set limit!
    

  2. Use PVC for persistent:

    persistentVolumeClaim:
      claimName: my-data
    

  3. emptyDir with medium: Memory:

    emptyDir:
      medium: Memory  # tmpfs, very fast
      sizeLimit: "128Mi"
    

  4. Document storage type:

    metadata:
      annotations:
        storage-type: "ephemeral-emptydir"
        reason: "build cache, can be lost"
    


🎯 Key Takeaways

  1. emptyDir = Temporary - Data tied to pod lifecycle
  2. PVC = Persistent - Data independent of pod
  3. Choose based on requirements - Can data be lost?
  4. Set size limits on emptyDir - Prevent node disk fill
  5. Use right tool for the job - Don't waste resources

🎊 World 4 Complete!

You've mastered all storage concepts: - ✅ PV/PVC binding and configuration - ✅ Volume mount paths - ✅ Access modes (RWO, RWX, ROX) - ✅ StatefulSet volumeClaimTemplates - ✅ StorageClass and dynamic provisioning - ✅ ConfigMaps and Secrets - ✅ Volume permissions and fsGroup - ✅ Reclaim policies - ✅ emptyDir vs persistent storage

Total XP Earned: 2,600 XP


Congratulations! You're now a Kubernetes storage expert! 🎉🚀