Pod lifecycle and graceful shutdown

Kubernetes · lesson 20 of 32 · 4 min read

Follow a pod from Pending to gone, and stop dropping requests every time one is replaced.

Open this lesson in the learning hub

Key points

  • Phases are Pending, Running, then Succeeded or Failed. Terminating is a timestamp, not a phase.
  • On delete, endpoint removal and SIGTERM start at the same time. Nothing promises the proxies update first.
  • A preStop sleep of a few seconds covers that gap, which is why the pattern turns up in every production chart.
  • terminationGracePeriodSeconds (30 by default) is the countdown to SIGKILL. Make it longer than your slowest request.
  • Set server.shutdown=graceful in Spring Boot so SIGTERM drains in-flight requests instead of cutting them off.
  • A PodDisruptionBudget stops a node drain or upgrade from removing too many replicas at once.

Example

spec:
  terminationGracePeriodSeconds: 45   # countdown to SIGKILL
  containers:
    - name: app
      image: ghcr.io/acme/orders:1.4.2
      lifecycle:
        preStop:
          exec:
            command: ["sh", "-c", "sleep 5"]   # let kube-proxy catch up
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: orders
spec:
  minAvailable: 2                # a drain may never go below this
  selector:
    matchLabels:
      app: orders
---
# application.yml
# server:
#   shutdown: graceful
# spring:
#   lifecycle:
#     timeout-per-shutdown-phase: 30s

SIGTERM is a request and SIGKILL is not. preStop buys the seconds the proxies need.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Kubernetes course, and every lesson in it is listed on the Kubernetes contents page.