Kubernetes cookbook · Intermediate
Handle SIGTERM in your application for Kubernetes
Pods drop in-flight requests or jobs when they're terminated.
Approach
On SIGTERM, stop accepting new work, finish in-flight work, close connections and exit before the grace period ends. Combine with a preStop delay for load balancer propagation.
Solution
import signal
import threading
shutting_down = threading.Event()
def on_sigterm(signum, frame):
shutting_down.set() # readiness endpoint starts returning 503, workers stop taking jobs
signal.signal(signal.SIGTERM, on_sigterm)
def worker_loop(queue):
while not shutting_down.is_set():
job = queue.get(timeout=1)
if job:
job.process() # finish the current job before exiting
apiVersion: v1
kind: Pod
metadata: {name: worker}
spec:
terminationGracePeriodSeconds: 120
containers:
- name: worker
image: ghcr.io/acme/worker:2.0.0
lifecycle: {preStop: {sleep: {seconds: 5}}}
Pitfalls
- The app runs under a shell that doesn't forward signals (use exec form).
- Jobs longer than the grace period; checkpoint and resume, or raise the grace period.
Variations
- For queue consumers, use visibility timeouts so unfinished messages return to the queue.
- For HTTP servers, use the framework's graceful shutdown (server.close, Shutdown(ctx)).
Further reading: Pod termination
Reference: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination
More Kubernetes recipes
- Autoscale a Deployment on CPU and memory
- Debug a distroless pod with an ephemeral container
- Default-deny network policies that keep DNS working
- Enforce the restricted Pod Security Standard
- Expose a service over HTTPS with automatic certificates
- Give a CI pipeline least-privilege deploy access
- Manage dev, staging and prod with Kustomize overlays
- Restart pods automatically when configuration changes