Graceful termination
Why a running request needs time to finish
Suppose a customer submits an order just as a bookstore Pod is being replaced. Stopping the process immediately may interrupt the request. Graceful termination means giving the application a bounded opportunity to stop accepting new work, complete or safely cancel existing work, release resources and exit. It cannot guarantee that no request fails: node crashes, forced termination and network delays are still possible. Applications should handle retries and duplicate work where appropriate.
Configure the shutdown budget
This illustrative Pod uses a 30-second termination grace period and an HTTP preStop hook. The placeholder app is assumed to implement /drain (mark itself unavailable for new work, then return promptly) and /ready (return not-ready after draining begins). It must also handle its configured stop signal and finish or cancel requests within the remaining time. No real server or registry is provided.
apiVersion: v1kind: Podmetadata: name: catalog-shutdown namespace: book-lab labels: app: catalogspec: terminationGracePeriodSeconds: 30 containers: - name: catalog image: registry.invalid/bytemind/catalog:demo resources: requests: cpu: 250m memory: 128Mi limits: cpu: 500m memory: 256Mi lifecycle: preStop: httpGet: path: /drain port: 8080 readinessProbe: httpGet: path: /ready port: 8080 periodSeconds: 5 timeoutSeconds: 2The grace countdown begins before the hook executes. The hook and application shutdown share the budget; preStop is not an extra 30 seconds. An HTTP hook is issued by the kubelet; the endpoint must be reachable and designed for the lab. Prefer a short, bounded, idempotent hook only when necessary. A normal app signal handler may be sufficient without a hook.
Follow the deletion flow
For ordinary graceful deletion, the API records a deletion timestamp and grace period. The kubelet begins local termination when it observes the deletion. If configured and the grace period is nonzero, preStop runs before the normal stop signal is sent to the container. On typical Linux containers this is SIGTERM, although image or runtime stop-signal settings can differ. The app should exit within the remaining budget. If it does not, remaining processes may be forcibly stopped. Kubernetes can request a small one-off extension for a still-running hook; do not rely on that extension as part of normal application design. A preStop hook is not guaranteed to run in every failure or when the container already stopped.
Endpoint changes are asynchronous
Meanwhile, the control plane updates relevant EndpointSlices. For an ordinary Service, terminating endpoints are marked not ready for normal routing, and the serving condition can describe whether they still serve traffic. The endpoint may remain represented during draining; it is eventually removed. Endpoint updates and local shutdown are concurrent processes, not a strict ordering guarantee. Existing connections or lagging load balancers can still send traffic. A Service configured to publish not-ready addresses has different readiness semantics. A successful drain must therefore account for application behavior and the actual network implementation.
Calculate a nominal time budget
Consider a 30-second grace period, a hook taking four seconds, and a process needing twelve seconds after receiving its stop signal. Nominally, 26 seconds remain after the hook; the process needs twelve of those seconds, leaving fourteen seconds of headroom. If the hook takes twenty seconds and the process needs fifteen, the nominal total is 35 seconds and does not fit. The simple arithmetic excludes kubelet/runtime delays and special extensions. Design a safety margin and verify in the actual deployment environment.
Common mistakes and verification
Do not treat a long sleep as proof that all connections are drained. Do not assume a hook runs after the stop signal, or force-delete a Pod to repair ordinary shutdown delays. Force deletion can remove the API object before confirming that processes stopped, creating risks for stateful systems. In a separately approved cluster, observe endpoint conditions, application logs, completion of in-flight requests and process exit using only lab resources. A Pod disappearing from kubectl output is not enough to prove zero lost work.
Practice and recap
Read shutdown-budget.json and endpoint-timeline.json. Calculate nominal remaining time after each hook, decide whether the process can finish, and explain why the endpoint can be terminating and still serving. Identify the difference between readiness and graceful shutdown. Next, combine Deployments, Services, configuration, probes and resource requests in a first multi-service application.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Graceful termination — knowledge check
Apply the lesson concepts to original offline scenarios.
When does the nominal termination grace countdown begin?
A 30-second budget includes a 20-second hook and 15-second shutdown. Does this fit nominally?
What does a terminating endpoint with ready=false and serving=true mean for an ordinary Service?
What should a graceful application do when receiving its stop signal?
Why is force deletion not a normal shutdown fix?