Cloud Computing
VERIFIED FIXES

Kubernetes rolling update causes 503s: long preStop hook and readiness removal drop in-flight requests

Environment

Environment

Managed cluster (GKE-style) running a microservice as a Deployment behind an NGINX IngressController. Pods define a preStop hook that performs a graceful shutdown script (closes resources, flushes queues) and a readinessProbe HTTP endpoint. terminationGracePeriodSeconds set to default 30s.

Symptoms

Symptoms

During Deployments/rollouts, users see frequent 503 Service Unavailable responses and aborted in-flight POSTs. Issues happen even when pods report Ready prior to rollout and when new pods become Ready quickly.

Observations

Observations

Pod lifecycle logs show controller sends SIGTERM, preStop starts and runs for ~25s. ReadinessProbe appears to stop returning 200 only after preStop begins (or sometimes after a delay). Ingress logs show upstream marked as unhealthy and NGINX returns 503s while preStop is still running; new pod not yet fully serving some internal caches, so requests hit removed-but-still-shutting pods.

Already attempted

Already attempted

Increased terminationGracePeriodSeconds to 60s (partial mitigation) and attempted to make preStop faster by reducing cleanup steps; adding explicit readinessProbe failures inside preStop was attempted but timing remained flaky. Rolling update strategy set to maxUnavailable 1, maxSurge 1.

Question

Question

How to ensure pods stop receiving new requests immediately on SIGTERM and allow preStop/termination to finish without causing 503s? What pod spec/readiness changes are safest and reversible?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
feliciafix Answer
user
Posted 4w ago • 0 confirmations

Make the pod stop receiving new traffic as soon as it begins shutdown. Modify the app to: on SIGTERM, immediately set readiness to false (or close the HTTP listener) and continue background cleanup; this tells kubelet/Ingress to remove the pod from rotation before preStop runs. Add a short readiness endpoint change and keep terminationGracePeriodSeconds long enough for cleanup. Verify by hitting the service with continuous requests and doing a rollout: healthy pods should accept traffic while the shutting pod finishes outstanding requests without 503s. Roll back by restoring previous signal handling if behavior is undesirable.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

Application doesn't fail readiness or close listener immediately on SIGTERM; preStop holds long cleanup while load balancer continues routing 90%
preStop executes before readiness change; preStop script itself doesn't trigger readinessProbe failure 70%
Ingress health check interval or slow upstream health detection causes lag in removing pod from rotation 50%

Summary

503s during rolling updates indicate ingress still routing to pods that are no longer ready to accept new traffic while preStop holds SIGTERM. Root causes: preStop running before the pod informs the readiness gate/ingress to stop sending traffic; the readinessProbe continues returning 200 during preStop; Ingress healthchecks mark backend unhealthy slowly; and lack of a coordinated graceful shutdown endpoint. Best practice is to make the application respond to SIGTERM by immediately failing readiness and stop accepting new connections, while background cleanup continues asynchronously, and to use readinessGate or lifecycle hooks to coordinate with the controller.

Suggested Fixes

Diagnostic step 1
Update application to handle SIGTERM by: (a) closing the HTTP listener or immediately returning non-200 on readiness endpoint, and (b) finishing cleanup asynchronously before exit. Deploy this change in a canary first.
Diagnostic step 2
Use readinessGate or add an explicit 'drain' readiness endpoint: on SIGTERM set readiness false immediately, which signals kubelet and ingress to stop routing new traffic.
Diagnostic step 3
Increase terminationGracePeriodSeconds to allow background cleanup, and tune Ingress healthcheck intervals to detect readiness change faster if safe.

Continue troubleshooting

Matched from the title, description and tags