Node Failure
What happens, and when, after a node dies
A drain is polite: Kubernetes moves pods before the node goes away. A node
that loses power gives no warning. node_failure.py stops a worker's
container (instantly, like pulling the plug) with 10 requests per second
flowing through the Gateway, and logs every change.
./scripts/lab.sh node_failure.py
stopping node lab-worker (runs web-7cb7f4d4cd-rszjq); redis and the Gateway run on ['lab-worker3']
0s node Ready=True taints=- in endpoints=True old pod=Running new pod=- ready=3 errors=0
45s node Ready=Unknown taints=unreachable,unreachable in endpoints=False old pod=Running new pod=- ready=2 errors=7
345s node Ready=Unknown taints=unreachable,unreachable in endpoints=False old pod=terminating new pod=swksc@lab-worker2 ready=2 errors=7
352s node Ready=Unknown taints=unreachable,unreachable in endpoints=False old pod=terminating new pod=swksc@lab-worker2 ready=3 errors=7
after 352s. traffic: 2944 requests, 2937 ok, errors: {'ReadTimeout': 7}, slowest 5.01s0 to 45 seconds: nobody knows
The node is dead, but its kubelet's last report said Ready. Its pod is
still in the Service's endpoints, and the Gateway keeps sending it a third
of the traffic. Those requests go to an IP where nothing answers: 7
requests hung for 5 seconds each (the client's timeout) before the node
was declared unknown.
The node lifecycle controller waits for the node monitor grace period
without a heartbeat (the kubelet's Lease) before marking the node
Unknown. That took 45 seconds here, in two runs.
These first seconds are where clients feel a node failure. Shorten them on the client side: short connect and read timeouts, retries of idempotent requests on another backend, and outlier detection in the proxy if it supports it (Envoy and Istio do; NGINX's passive checks only help after failures). Tuning the grace period down trades false alarms for faster detection.
45 seconds: out of rotation
When the node became Unknown, two things happened together: the node got
the taint node.kubernetes.io/unreachable:NoExecute, and its pods'
Ready condition went false, which removed them from the endpoints.
From then on, zero errors. But only 2 of 3 pods were serving: the
"Running" pod on the dead node still counts toward the Deployment, so
nothing replaced it.
345 seconds: evicted and replaced
Every pod gets default tolerations for that taint:
kubectl -n shop get pod web-... -o jsonpath='{.spec.tolerations}'
[{"key":"node.kubernetes.io/not-ready","effect":"NoExecute","tolerationSeconds":300},
{"key":"node.kubernetes.io/unreachable","effect":"NoExecute","tolerationSeconds":300}]300 seconds after the taint (45 + 300 = 345 s), the pod was evicted:
marked for deletion, so the ReplicaSet created a replacement, which the
scheduler put in zone-b (the zone-a node was tainted and excluded by
nodeTaintsPolicy: Honor). It was ready 7 seconds later.
The old pod stays terminating for as long as the node is unreachable. Nobody can confirm it stopped, so its object isn't deleted until the node comes back (or someone force-deletes it).
For a Deployment, a stuck terminating pod is harmless. For a
StatefulSet it blocks recovery: redis-0 can't be recreated while
the old redis-0 might still be running and writing to the same
volume. That's deliberate (two writers would corrupt the data), and it's
why stateful workloads need more thought. See
StatefulSets & Volumes.
Tuning the 5 minutes
Shorter tolerations replace pods sooner, but a brief network blip then causes a wave of rescheduling. With enough replicas spread across zones, the default 5 minutes costs capacity, not availability: the surviving pods carry the load. Size replicas so losing a zone still leaves enough.
Lab
- Run
node_failure.py. Then settolerationSeconds: 30on web and run it again. When is the replacement created now? - Stop the node that runs
redis-0and watch/api/visitsand the pod. What do you have to do to recover before the node comes back? - Stop a node, then start it again after 60 seconds. What happens to the pods that were on it?