Skip to the page

Kubernetes: shipping a service · mid

Graceful shutdown and rolling deploys

On pod deletion Kubernetes sends SIGTERM and waits for a grace period before SIGKILL. If your process never sees the signal, every deploy cuts live connections at once.

The notes

What you must know

  • Pod deletion order: the pod leaves the Service endpoints (no new connections), the preStop hook runs, SIGTERM goes to the main process (PID 1), and after terminationGracePeriodSeconds (30 seconds by default) anything left gets SIGKILL.
  • Shell form CMD npm run start makes /bin/sh PID 1, then npm, then node, and the signal never reaches node. The drain code never runs, and the pod dies at 30 seconds (exit code 137). Use exec form CMD ["node", "server.js"], or exec at the end of a start script.
  • Handle SIGTERM: stop accepting new work, tell clients to reconnect (close WebSockets with a close code), finish in-flight requests, close pools and exit.
  • Endpoint removal and SIGTERM happen at nearly the same time, so some traffic can still arrive for a moment. A short preStop sleep gives load balancers time to stop sending traffic.
  • Set terminationGracePeriodSeconds longer than your drain time, for example for long jobs in a worker.
  • Reconnect storm: when every client returns at once, new pods must handle many TLS handshakes, token checks and subscriptions together. Use client backoff with jitter, spread the rollout (maxUnavailable, maxSurge), and drain in steps.
  • Rolling updates keep old and new pods together for a while, so both versions must work with the same database and clients.

The red pen

Common traps: mistake → fix

  • Mistake: Kubernetes waits for my requests to finish automatically

    Fix: It waits only the grace period, and only if your process handles SIGTERM.

  • Mistake: npm start is the same as node server.js in a container

    Fix: npm and sh sit in front and swallow the signal.

  • Mistake: Exit code 137 means my code crashed

    Fix: 137 is SIGKILL, from the grace period ending or from an out-of-memory kill.

Easy to hard

Questions interviewers ask, easy to hard

  1. What happens in order when a pod is deleted?
  2. Why does npm run start in CMD break graceful shutdown?
  3. What does exit code 137 mean?
  4. Every deploy drops all live tracking connections. List the causes and fixes.
  5. How would you avoid a reconnect storm on deploy?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview