Kubernetes (K8s) (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
Zero-Downtime Rolling Updates: `maxSurge`, `maxUnavailable`, and `preStop` Hooks.
senior LevelCode ExampleHooksSystem DesignHTTP & REST APIs
In Simple Words: To achieve 100% zero dropped connections during a deployment update:
yaml
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25% # Can spin up 25% extra new pods above desired count
maxUnavailable: 0 # ZERO pods are terminated until new pods pass Readiness check!
The Connection Dropping Race Condition & preStop Hook:
When a pod is terminated:
- API Server deletes pod from EndpointSlice (async).
- API Server sends
SIGTERMto the container process. - If the container process shuts down immediately, incoming in-flight TCP packets routed by kube-proxy still hit the dying container, returning
502 Bad Gateway!
- Fix: Add a
preStopsleep hook:The container waits 15 seconds before processingyamllifecycle: preStop: exec: command: ["/bin/sh", "-c", "sleep 15"]SIGTERM, giving load balancers enough time to flush in-flight connections and stop routing new requests to the dying pod.