Drain without downtime: single-replica deployments
An app down for minutes during node maintenance on a healthy cluster usually means one replica, no PDB and no readiness probe. The right order before a drain.
It was planned node maintenance. The cluster was healthy, the other nodes had room, and kubectl drain finished without trouble. Still, one application was unreachable for several minutes. Kubernetes promises "maintenance without downtime", but it can only deliver if the application is ready for it.
Symptoms
- During the drain, one particular application returns 502/503 or doesn't respond at all.
- The outage ends when the new pod pulls its image and starts.
- Other applications aren't affected by the same maintenance.
Why does it happen?
It happens when a few gaps line up:
- The deployment has 1 replica. Or it has 2 replicas, but both are on the same node.
- There is no PodDisruptionBudget. The drain doesn't know how many pods may go at once.
- There is no readiness probe. The pod gets traffic as soon as it is "Running", while the application isn't ready yet.
kubectl drain evicts the pod. That means the old one is deleted first, and then the controller creates a new one on another node. Pulling the image and starting the new pod takes time. Meanwhile there is no running copy. Or, without a readiness probe, traffic goes to a pod that isn't ready.
Fix
For single-replica deployments: cordon the node before draining, then restart those deployments with rollout restart. RollingUpdate brings up the new pod (on another node, because of the cordon) and deletes the old one once the new one is ready.
# 1. No new pods on this node
kubectl cordon <node>
# 2. Find the single-replica deployments on that node
kubectl get pods -A -o wide --field-selector spec.nodeName=<node>
# 3. New one up first, then old one down
kubectl -n <ns> rollout restart deployment/<ad>
kubectl -n <ns> rollout status deployment/<ad>
# 4. The node can now be drained
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
For this to work, the strategy must not delete the old pod first:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
For 2 or more replicas: add a PDB and spread the pods across nodes.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: <ad>
spec:
minAvailable: 1
selector:
matchLabels:
app: <ad>
spec:
template:
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: <ad>
Add a readiness probe in every case. Traffic should arrive only when the application is actually ready.
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
failureThreshold: 3
Watch out
- PVC-backed (RWO) single replicas. The volume can attach to only one node at a time. The new pod can't get the volume until the old one releases it. So downtime is unavoidable. Plan a maintenance window for these and tell users in advance.
- If
minAvailableequals the replica count, the drain waits forever. 2 replicas withminAvailable: 2means no pod can ever be evicted.minAvailablemust be lower than the replica count, or usemaxUnavailable: 1. - GitOps self-heal.
rollout restartadds arestartedAtannotation to the pod template. The GitOps tool may see it as "drift" and revert it. That's harmless. The pods are already renewed; the revert may trigger another rollout, and that's all.
# To see PDBs that hold up a drain
kubectl get pdb -A
# Those with 0 in the ALLOWED DISRUPTIONS column block the drain
Lesson
A healthy cluster doesn't mean an application can move without downtime. Maintenance without downtime isn't a cluster feature; each deployment earns it through its own settings.
Checklist
- [ ] Was the node cordoned before the drain?
- [ ] Were single-replica deployments moved with
rollout restart? - [ ] Does RollingUpdate have
maxUnavailable: 0? - [ ] Do deployments with 2+ replicas have a PDB and
topologySpreadConstraints? - [ ] Is
minAvailablein the PDB lower than the replica count? - [ ] Is a readiness probe defined?
- [ ] Is a maintenance window planned for PVC-backed single replicas?