KubernetesXXXIII · Cordon, Drain and UncordonCordon, drain, uncordon
Drain with local data — emptyDir, hostPath, and the data-loss risk
What you'll learn
- Identify the local data types that the drain affects
- Trace the drain's behaviour with emptyDir, hostPath, and local PVs
- Apply the operational patterns for protecting the data
- Design Pods that survive the drain without data loss
Prerequisites
Verified against Kubernetes 1.34.x · kubeadm 1.34.x · kubectl 1.34.x · etcd 3.6.x · CoreDNS 1.11.x · containerd 1.7.x / 2.x · 2026-08-16
The drain is destructive for Pods with local data. The emptyDir volumes are deleted; the hostPath volumes are orphaned on the node; the local persistent volumes are preserved but unavailable. This lesson walks the drain’s interaction with local data, the data-loss risk, and the design patterns.
The local data types
flowchart TD
A[Pod with local data] --> B{Volume type?}
B -->|emptyDir| C[Data lost on eviction]
B -->|hostPath| D[Data preserved on node]
B -->|local PV| E[Data preserved on node]
C --> F[--delete-emptydir-data needed]
D --> G[Data lost on node replacement]
E --> G
G --> H[Backup required]
The local data types that the drain affects:
- emptyDir: a directory that is created when the Pod is created and deleted when the Pod is deleted. The data is lost when the Pod is evicted.
- hostPath: a directory or file on the node’s filesystem. The data is preserved on the node when the Pod is evicted.
- local PV: a persistent volume that is bound to a node. The volume survives the eviction, and its node affinity pins the replacement Pod to the same node.
The three types have different data-loss behaviours:
| Type | Data on eviction | Data on node replacement |
|---|---|---|
| emptyDir | Lost | Lost |
| hostPath | Preserved | Lost (node is gone) |
| local PV | Preserved | Lost (node is gone) |
The drain’s default behaviour is to reject the operation when the Pod has an emptyDir volume. The Pod’s data is lost; the operator must approve the loss.
The drain’s behaviour with emptyDir
The drain’s default behaviour:
# Substitute your own node name:
NODE=node-1
kubectl drain "$NODE"
error when evicting pod "default/cache-1": cannot evict pod with emptyDir volumes (use --delete-emptydir-data to ignore)
The drain rejects the operation. The Pod’s emptyDir volume is preserved; the Pod is not evicted.
The drain’s destructive behaviour:
# Substitute your own node name:
NODE=node-1
kubectl drain "$NODE" --delete-emptydir-data
node/node-1 cordoned
evicting pod "default/cache-1"
pod "default/cache-1" evicted
The drain is allowed. The Pod’s emptyDir volume is deleted; the Pod is evicted.
The drain’s behaviour with hostPath
The drain’s behaviour with hostPath:
# Substitute your own node name:
NODE=node-1
kubectl drain "$NODE"
The drain evicts the Pod. The hostPath data is preserved on the node. The Pod can be re-created on the same node (the data is still there).
The drain’s behaviour with hostPath when the node is replaced:
# The node is replaced.
# The new node does not have the hostPath data.
# The Pod is re-created on the new node without the data.
The hostPath data is lost when the node is replaced. The data is bound to the node’s filesystem; the new node does not have the data.
The production rule: hostPath is not a substitute for persistent storage. The data is bound to the node; the data is lost when the node is replaced.
The drain’s behaviour with local PVs
The drain’s behaviour with local PVs:
# Substitute your own node name:
NODE=node-1
kubectl drain "$NODE"
The drain evicts the Pod. The local PV is preserved on the node. The Pod can be re-created on the same node (the PV is still there).
The drain’s behaviour with local PVs when the node is replaced:
# The node is replaced.
# The new node does not have the local PV.
# The Pod is re-created on the new node without the PV.
The local PV is lost when the node is replaced. The PV is bound to the node; the new node does not have the PV.
The production rule: local PVs are not a substitute for
network storage. Replacing the node leaves the PV
unschedulable, and the Pod stays Pending with a
volume node affinity conflict.
The drain’s pattern with local data
The drain’s pattern with local data:
- Identify the local data type. The Pod’s spec shows the volumes.
- Choose the right flag. The
--delete-emptydir-dataflag is destructive; the default is to reject. - Drain the node. The drain evicts the Pods.
- Verify the local data. The data is preserved on the node (hostPath) or lost (emptyDir).
- Uncordon the node. The node is uncordoned.
The drain’s pattern is the cluster’s protection against the data-loss. The operator must approve the loss.
The design patterns for local data
The design patterns for Pods that survive the drain:
- Use network storage for persistent data. The cluster’s network storage (CSI, NFS, Ceph) is preserved across the drain.
- Use emptyDir for temporary data. The emptyDir volume is deleted when the Pod is evicted; the data is intentionally lost.
- Use hostPath for node-local data. The hostPath data is preserved on the node; the data is bound to the node.
- Use local PVs for node-local persistent data. The local PV is preserved on the node; the data is bound to the node.
The design patterns are the operator’s responsibility. The production rule is to design the workload to survive the drain.
The anti-patterns
The drain’s anti-patterns:
- Storing data in emptyDir. The emptyDir volume is deleted when the Pod is evicted; the data is lost.
- Storing data in hostPath. The hostPath data is bound to the node; the data is lost when the node is replaced.
- Storing data in local PVs for cluster-wide workloads. The local PV is bound to the node; the data is lost when the node is replaced.
- Not backing up local data. The local data is bound to the node; the data is lost when the node is replaced.
The anti-patterns are the cluster’s silent failures. Each one works through every drain until the node is replaced, and the loss surfaces only then.
The recovery from data loss
The recovery from data loss:
- Identify the lost data. The Pod’s events show the eviction.
- Recover from backup. The cluster’s backup is the primary recovery.
- Recover from the cluster’s storage. The cluster’s CSI snapshot is the secondary recovery.
- Recover from the application’s replication. The application’s replication is the tertiary recovery.
The recovery is the cluster’s protection against the data loss. The production rule is to have a backup strategy for the data.
Quiz
Knowledge check · 4 questions
Q1. What does `--delete-emptydir-data` acknowledge when draining?
Q2. A PersistentVolumeClaim's data survives a drain, while an emptyDir volume's data does not.
Q3. Deal with a drained Pod whose replacement cannot be scheduled anywhere in the cluster.
`node-5` was drained with `kubectl drain node-5 --ignore-daemonsets --delete-emptydir-data` to replace a failing disk. The drain completed. Twenty minutes later, `metrics/prometheus-0` is `Pending` with `0/40 nodes are available: 1 node(s) had volume node affinity conflict, 39 node(s) didn't find available persistent volumes to bind`. The PVC is bound to a PersistentVolume with `storageClassName: local-nvme` and `nodeAffinity` requiring `kubernetes.io/hostname in [node-5]`. `node-5` is still cordoned.
Q4. What happens to an emptyDir volume, a hostPath volume, and a `local` PersistentVolume when a drain evicts the Pod, and which of the three pins the replacement Pod to the same node?
Passing score: 75%. Answers are checked in this browser.
Production discipline
- The drain is destructive for Pods with local data. The emptyDir volumes are deleted; the hostPath volumes are orphaned; the local PVs are preserved but unavailable.
- Use
--delete-emptydir-datacarefully. The flag is destructive; the operator should use it carefully. - Use network storage for persistent data. The cluster’s network storage is preserved across the drain.
- Use emptyDir for temporary data. The emptyDir volume is deleted when the Pod is evicted; the data is intentionally lost.
- Design the workload to survive the drain. The design patterns are the operator’s responsibility.
- Audit the data at every release. The data is the cluster’s most valuable asset; the audit catches the failures.
- Test the drain in non-production. A staging cluster that mirrors production is the right place to test the drain.
- Monitor the data loss. The cluster’s alerts should fire on the data loss; the data is the cluster’s most valuable asset.