CKAD — Ephemeral Volumes
Why Ephemeral Volumes?
Pods can use ephemeral volumes to share data between containers or store temporary files. These volumes last only as long as the Pod — when the Pod is deleted, the data is gone.
Ephemeral volumes are part of the Application Design and Build domain (20% of the exam). You need to know when and how to use them.
emptyDir
An emptyDir volume is created when a Pod starts and deleted when the Pod is removed. It is the most commonly used ephemeral volume type.
Use Cases
| Use Case | Description |
|---|---|
| Shared data between containers | Writer container produces files, reader container consumes them |
| Scratch space | Temporary storage for sorting, caching, or processing |
| Checkpointing | Save intermediate results for long computations |
Basic emptyDir Example
apiVersion: v1
kind: Pod
metadata:
name: shared-data
spec:
containers:
- name: writer
image: busybox:1.36
command: ["/bin/sh", "-c", "echo 'hello from writer' > /data/msg.txt && sleep 3600"]
volumeMounts:
- name: shared
mountPath: /data
- name: reader
image: busybox:1.36
command: ["/bin/sh", "-c", "sleep 5 && cat /data/msg.txt && sleep 3600"]
volumeMounts:
- name: shared
mountPath: /data
volumes:
- name: shared
emptyDir: {}
kubectl apply -f shared-data.yaml
kubectl logs shared-data -c reader
# Expected: hello from writer
emptyDir with Memory-Backed Storage
For high-performance temporary storage, you can use memory (tmpfs) instead of disk:
volumes:
- name: cache
emptyDir:
medium: Memory
sizeLimit: 256Mi
Warning: Memory-backed emptyDir counts against the container’s memory limit. If the volume grows beyond the size limit, the Pod gets evicted.
emptyDir with Size Limit
volumes:
- name: scratch
emptyDir:
sizeLimit: 1Gi
hostPath
A hostPath volume mounts a file or directory from the host node into the Pod. It provides access to the node’s filesystem.
hostPath Types
| Type | Behavior |
|---|---|
"" (empty) |
No checks. Creates path if it does not exist. |
DirectoryOrCreate |
Creates directory if missing. |
Directory |
Directory must already exist. |
FileOrCreate |
Creates file if missing. |
File |
File must already exist. |
hostPath Example
apiVersion: v1
kind: Pod
metadata:
name: log-reader
spec:
containers:
- name: reader
image: busybox:1.36
command: ["/bin/sh", "-c", "tail -f /host-logs/syslog"]
volumeMounts:
- name: host-logs
mountPath: /host-logs
readOnly: true
volumes:
- name: host-logs
hostPath:
path: /var/log
type: Directory
When to Use hostPath
| Scenario | Appropriate? |
|---|---|
| Development/testing | Yes — quick access to host files |
| Reading node logs | Yes — mount /var/log read-only |
| Production workloads | No — data is node-specific, not portable |
| Shared storage between Pods | No — use PersistentVolumes instead |
Exam tip: hostPath is rarely the right choice in production. Data is tied to a specific node and is not portable. Use emptyDir for temporary data sharing and PersistentVolumes for data that must survive Pod restarts.
Ephemeral Volume Comparison
| Feature | emptyDir | hostPath |
|---|---|---|
| Created | When Pod starts | References existing host path |
| Deleted | When Pod is deleted | Data persists on host |
| Scope | Pod-level | Node-level |
| Portable | Yes (works on any node) | No (tied to specific node) |
| Use case | Temp data, inter-container sharing | Access host files, development |
| Memory option | Yes (medium: Memory) |
No |
| Size limit | Yes (sizeLimit) |
No built-in limit |
Resource Requests and Limits (Preview)
Every Pod should declare how much CPU and memory it needs. We cover this in detail in Chapter 11: Resource Management, but here is the basic idea:
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
containers:
- name: app
image: nginx:1.27
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
- Requests — minimum guaranteed resources. Used for scheduling.
- Limits — maximum allowed resources. Container gets killed (OOMKilled) if it exceeds the memory limit.
Next: Learn how Kubernetes handles container exits with Restart Policies →