K8s by Example: Graceful Shutdown
| Graceful shutdown ensures in-flight requests complete before a Pod terminates. When Kubernetes sends SIGTERM, apps should stop accepting new requests, finish existing ones, then exit. The terminationGracePeriodSeconds sets the deadline. Use for: zero-downtime deployments, preventing data loss, clean connection handling. |
| graceful-shutdown-basic.yaml | |
| The | |
| prestop-hook.yaml | |
| A preStop hook runs before SIGTERM is sent. Use it to deregister from service discovery, drain connections, or notify load balancers. The sleep gives time for endpoints to update before shutdown begins. | |
| The preStop sleep solves a race condition: Pod termination and endpoint removal happen concurrently. The sleep ensures kube-proxy updates before your app stops accepting connections. | |
| shutdown-sequence.yaml | |
| Complete shutdown-aware deployment. The app handles SIGTERM by stopping new connections, waiting for in-flight requests, closing database connections, and exiting cleanly. Combined with preStop hook and sufficient grace period. | |
| connection-draining.yaml | |
| For long-lived connections (WebSockets, gRPC streams), configure connection draining. The app tracks active connections and waits for them to close naturally or times them out during shutdown. | |
| job-graceful-shutdown.yaml | |
| Jobs and batch workloads need graceful shutdown too. If terminated mid-processing, the job should checkpoint progress or mark items for retry. The activeDeadlineSeconds limits total runtime. | |
| terminal | |
| Monitor shutdown behavior in Pod events. Look for preStop hook execution, SIGTERM timing, and whether the container exited cleanly or was killed after grace period. | |