Sub topic 12
Avoiding dropped requests during a rollout
This chapter assumes you can read Deployment manifests, know what a readiness probe does, and have followed the earlier chapters on maxSurge, maxUnavailable, and minReadySeconds. A rolling update that keeps enough Pods available can still drop requests. The cause is usually how old Pods shut down, not how new Pods start.
Why requests get dropped
When the Deployment scales down an old ReplicaSet, the kubelet deletes a Pod. Two things then happen at the same time:
- The Pod's containers receive
SIGTERM, and the kubelet starts a grace period timer. - The Pod is removed from the Endpoints (EndpointSlices) of its Services.
These two steps are not coordinated. Removal from endpoints must reach every kube-proxy, ingress controller, or load balancer. If the container exits before that finishes, clients are sent to a Pod that no longer listens. They see connection resets or 502 and 503 errors.
Three settings that close the gap
A preStop delay
A preStop hook runs before SIGTERM is sent. A short sleep keeps the container serving while endpoint removal spreads through the cluster.
An application that handles SIGTERM
On SIGTERM, the process should stop accepting new connections, finish requests already in progress, and then exit. Many frameworks need this enabled explicitly. A process that exits at once on SIGTERM drops in-flight requests regardless of the other settings.
Shells often hide the problem. If your container runs sh -c "app", the shell may not forward the signal to app. Use the exec form of command, or exec app in the script.
A sufficient terminationGracePeriodSeconds
The grace period is 30 seconds by default. It covers the preStop hook and the shutdown after SIGTERM. When it expires, the kubelet sends SIGKILL. Set it to at least the preStop delay plus your longest expected request.
Example
This Pod template extends the Deployment from earlier chapters. The nginx image has a sleep binary, so the hook can run it directly.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 4
minReadySeconds: 5
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: web
spec:
terminationGracePeriodSeconds: 45
containers:
- name: web
image: nginx:1.26
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
periodSeconds: 5
lifecycle:
preStop:
exec:
command: ["sleep", "10"]Here the Pod serves for 10 more seconds after deletion starts. NGINX then receives SIGTERM and finishes active connections. The 45-second grace period leaves 35 seconds for that shutdown.
Changing lifecycle or terminationGracePeriodSeconds edits the Pod template, so applying this manifest starts a rollout. Watch it as before:
kubectl apply -f web.yaml
kubectl rollout status deployment/webCommon errors
- Grace period shorter than the hook plus shutdown. The kubelet sends
SIGKILLmid-request. RaiseterminationGracePeriodSeconds. - Signal never reaches the app. A shell wrapper swallows
SIGTERM, and the Pod waits out the full grace period before being killed. - Readiness probe too optimistic. New Pods receive traffic before they can serve it. This causes errors on the startup side of the rollout.
- External load balancers with slow deregistration. Cloud load balancers may need longer than a
sleep 10. Check their deregistration delay.
Exercise
A Deployment rolls out with maxUnavailable: 0, yet clients see connection resets when old Pods terminate. The container has no preStop hook and exits immediately on SIGTERM. What two changes would you make to the Pod template?
Show answer
Add a preStop hook with a short sleep (for example 10 seconds) so the Pod keeps serving while it is removed from endpoints. Also make the application drain in-flight requests on SIGTERM, and set terminationGracePeriodSeconds above the sleep plus the longest request time.