🎓 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:
Result: All data disappears when pod restartsThe Solution:
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:
Mistake 2: PVC for Temporary Files¶
# ❌ Wasteful - PVC not needed
volumes:
- name: build-cache
persistentVolumeClaim:
claimName: cache-pvc # Overkill for temp cache
Fix:
Mistake 3: Not Setting Size Limit¶
Fix:
🏗️ 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¶
-
Use emptyDir for temporary:
-
Use PVC for persistent:
-
emptyDir with medium: Memory:
-
Document storage type:
🎯 Key Takeaways¶
- emptyDir = Temporary - Data tied to pod lifecycle
- PVC = Persistent - Data independent of pod
- Choose based on requirements - Can data be lost?
- Set size limits on emptyDir - Prevent node disk fill
- 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! 🎉🚀