Kubernetes interview question · Most asked
How do you make pods shut down gracefully without dropping requests?
Summary
When a pod is deleted, Kubernetes marks it terminating, removes it from Service endpoints, runs any preStop hook, sends SIGTERM to the containers and, after terminationGracePeriodSeconds (30 by default), SIGKILL. Endpoint removal and SIGTERM happen in parallel, so the app must keep serving briefly after SIGTERM, finish in-flight requests, then exit.
Longer Answer
Checklist: the application handles SIGTERM (stops accepting new work, drains in-flight requests and background jobs, closes connections); the process actually receives the signal (exec-form ENTRYPOINT, an init such as tini if needed, not a shell that swallows it); a short preStop sleep (a few seconds) covers the delay before load balancers and kube-proxy stop sending traffic; and the grace period is long enough for the longest request or job.
Without this, every deploy, scale-in and node drain produces a burst of 502s.
Key Points
- Endpoint removal and SIGTERM race.
- Handle SIGTERM: drain, then exit.
- preStop sleep for load balancer lag.
- Grace period >= longest request.
More most-asked Kubernetes questions
- ConfigMaps and Secrets: how do they differ, and how should secrets really be handled?
- How does the scheduler place pods? Explain resource requests, node affinity, taints and tolerations.
- Describe the Kubernetes architecture: what runs on the control plane and what runs on the nodes?
- How do rolling updates work in a Deployment, and how do you roll back?
- Explain the Service types: ClusterIP, NodePort, LoadBalancer, ExternalName and headless.
- What happens, step by step, when you run kubectl apply for a Deployment?
- Deployment, StatefulSet, DaemonSet, Job and CronJob: when do you use each?
- What is the difference between resource requests and limits, and what are the QoS classes?