Generic Ephemeral Volumes in Kubernetes
Use generic ephemeral volumes for per-pod temporary storage with CSI driver features. Scratch space, caching, and temp data without pre-provisioned PVCs.
π‘ Quick Answer: Use
ephemeralvolume type to get per-pod CSI-backed storage thatβs automatically created and deleted with the pod. Define an inlinevolumeClaimTemplate(like a PVC spec) in the pod spec. UnlikeemptyDir, you get real persistent storage features (encryption, performance tiers) without manual PVC lifecycle.
The Problem
You need temporary per-pod storage (scratch space, cache, temp files) but:
emptyDiris limited to node disk and has no storage class features- Pre-provisioned PVCs require manual lifecycle management
- You want encryption, IOPS guarantees, or specific storage classes for temp data
The Solution
apiVersion: apps/v1
kind: Deployment
metadata:
name: data-processor
spec:
replicas: 5
template:
spec:
containers:
- name: processor
image: data-processor:v2
volumeMounts:
- name: scratch
mountPath: /tmp/processing
- name: cache
mountPath: /var/cache/app
volumes:
- name: scratch
ephemeral:
volumeClaimTemplate:
metadata:
labels:
type: scratch
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: gp3-encrypted
resources:
requests:
storage: 50Gi
- name: cache
ephemeral:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: local-nvme
resources:
requests:
storage: 100GiHow It Works
graph LR
A[Pod Created] --> B[PVC auto-created]
B --> C[PV provisioned by CSI]
C --> D[Volume mounted to pod]
D --> E[Pod Deleted]
E --> F[PVC auto-deleted]
F --> G[PV reclaimed]The lifecycle is fully automatic:
- Pod starts β PVC created (owned by pod)
- CSI driver provisions the volume
- Volume mounted to pod
- Pod deleted β PVC garbage collected β PV reclaimed
Comparison with Other Volume Types
| Feature | emptyDir | hostPath | Generic Ephemeral | PVC |
|---|---|---|---|---|
| Per-pod lifecycle | β | β | β | β |
| Storage class support | β | β | β | β |
| Encryption | β | β | β | β |
| IOPS control | β | β | β | β |
| Survives pod restart | β | β οΈ | β | β |
| Size limits | β | β | β | β |
| No manual cleanup | β | β | β | β |
Use Cases
AI/ML Scratch Space
volumes:
- name: model-scratch
ephemeral:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: nvme-fast
resources:
requests:
storage: 500GiCI/CD Build Cache
volumes:
- name: build-cache
ephemeral:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: ssd
resources:
requests:
storage: 20GiVerify Ephemeral PVCs
# List PVCs created by ephemeral volumes
kubectl get pvc -l type=scratch
# PVC name follows pattern: <pod-name>-<volume-name>
# e.g., data-processor-7f8b9-scratch
kubectl get pvc | grep processorCommon Issues
| Issue | Cause | Fix |
|---|---|---|
| Pod stuck Pending | StorageClass canβt provision | Check CSI driver logs |
| PVC not deleted after pod | OwnerReference missing | Ensure K8s 1.23+ (GA) |
| Volume not large enough | Size underestimated | Increase storage request |
| Slow mount | Network-attached storage | Use local storage class for scratch |
Best Practices
- Use for truly temporary data β Donβt store anything you need after pod dies
- Choose appropriate storage class β Local NVMe for speed, network for encryption
- Set resource quotas β Ephemeral PVCs count toward namespace quota
- Label ephemeral volumes β Helps identify and audit auto-created PVCs
- Prefer over hostPath β Same performance, better security and lifecycle
Key Takeaways
- Generic ephemeral volumes = per-pod PVCs with automatic lifecycle
- Get CSI features (encryption, IOPS, snapshots) for temporary storage
- Automatically cleaned up when pod is deleted (owner reference)
- Ideal for scratch space, caches, and temp processing data
- Use instead of hostPath or oversized emptyDir

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
