Stage 3 · Build
Long-Running Daemons & Services
Graceful HTTP Shutdown
http.Server Shutdown, connection draining, readiness flips, and Kubernetes terminationGracePeriodSeconds.
Shutdown Flow
Graceful HTTP shutdown follows a specific sequence: stop accepting new connections, drain in-flight requests, and close idle connections. http.Server.Shutdown handles this automatically.
Connection Draining
Readiness Flip
HTTP Server Configuration
Shutdown Coordination
Kubernetes Integration
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
terminationGracePeriodSeconds: 60
containers:
- name: server
ports:
- containerPort: 8080
# Liveness probe
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10
periodSeconds: 15
# Readiness probe
readinessProbe:
httpGet:
path: /readyz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
# Startup probe
startupProbe:
httpGet:
path: /started
port: 8080
failureThreshold: 30
periodSeconds: 2
lifecycle:
preStop:
exec:
# Give time for endpoints to propagate
command: ["sh", "-c", "sleep 5"]terminationGracePeriodSeconds must exceed your shutdown timeout. preStop hook sleeps to allow endpoint propagation. readiness probe must fail after shutdown flip. liveness probe must not kill during shutdown.
Without the preStop sleep, Kubernetes sends SIGTERM immediately after removing the pod from endpoints. But endpoint propagation takes time. The sleep ensures the pod is fully removed from endpoints before shutdown begins.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.