Managing pod storage in Kubernetes can be tricky, especially when dealing with temporary storage solutions like emptyDir
. Kubernetes provides the --delete-emptydir-data
option to ensure that emptyDir
volumes are cleared when a pod is deleted. In this article, we’ll explore how emptyDir
works, what does the Kubernetes –delete-emptydir-data do, and how to use it effectively in your cluster.
TL;DR
The --delete-emptydir-data
option in Kubernetes is used to clean up emptyDir
volumes when a pod is deleted, ensuring no residual data remains on the node. Use this feature to maintain clean nodes and prevent storage issues in clusters with frequent pod deletions.
Example:
kubectl drain <node-name> --delete-emptydir-data --ignore-daemonsets --force
What Is an emptyDir Volume?
An emptyDir
volume in Kubernetes provides temporary storage that exists as long as the pod is running. It is often used for caching, temporary file processing, or inter-container communication within a pod.
Characteristics of emptyDir:
- Ephemeral Storage: Data is deleted when the pod stops.
- Shared Across Containers: All containers in the pod can access the same volume.
- Storage Medium Options:
- Default: Backed by the node’s filesystem.
- Memory: Uses
tmpfs
for faster, in-memory storage.
Example YAML for emptyDir:
apiVersion: v1
kind: Pod
metadata:
name: emptydir-example
spec:
containers:
- name: busybox
image: busybox
command: ["sh", "-c", "echo Hello > /data/hello && sleep 3600"]
volumeMounts:
- mountPath: /data
name: temp-storage
volumes:
- name: temp-storage
emptyDir: {}
What Does –delete-emptydir-data Do?
The –delete-emptydir-data flag in Kubernetes ensures that data stored in emptyDir
volumes is deleted from the node’s filesystem when the associated pod is deleted. Without this option, leftover data may accumulate on nodes, potentially leading to storage pressure over time.
How It Works
- With
--delete-emptydir-data
: Kubernetes cleans up the temporary directory used byemptyDir
volumes when the pod is terminated. - Without
--delete-emptydir-data
: The directory remains until manually removed or overwritten by a new pod.
How to Use –delete-emptydir-data
When Draining a Node
When draining a node, you can use the kubectl drain
command with the --delete-emptydir-data
flag to ensure no residual emptyDir
data remains on the node.
Example:
kubectl drain <node-name> --delete-emptydir-data --ignore-daemonsets --force
Cleaning Up Stale Data
Use this flag when cleaning up nodes that experience frequent pod churn, such as in development or testing clusters.
Best Practices for emptyDir and –delete-emptydir-data
- Use emptyDir for Temporary Data
- Limit its use to data that does not need to persist beyond the pod’s lifecycle.
- Enable Cleanup During Node Drains
- Always include
--delete-emptydir-data
inkubectl drain
commands to prevent leftover data.
- Always include
- Monitor Node Storage
- Use tools like Prometheus and Grafana to monitor node storage utilization and identify potential issues with
emptyDir
usage.
- Use tools like Prometheus and Grafana to monitor node storage utilization and identify potential issues with
- Prefer Memory-backed emptyDir
- For temporary data requiring high performance, use
medium: Memory
to leverage RAM instead of disk.
- For temporary data requiring high performance, use
Example:
- name: file-storage
emptyDir:
medium: Memory