Tech Interview RoomStudy guides

Kubernetes

How do you configure liveness, readiness and startup probes and graceful shutdown for a Spring Boot app?

Advanced · 2 min read · Lesson 5 of 7 in Kubernetes

Probes

  • Startup probe: gives slow-starting applications time; liveness and readiness are not checked until it succeeds. Use it instead of a huge initialDelaySeconds.
  • Readiness probe: "can I receive traffic now?" Failing removes the pod from Service endpoints without restarting it (during warm-up, overload, or when a critical dependency is unavailable).
  • Liveness probe: "is the process irrecoverably broken?" Failing restarts the container. Keep it cheap and independent of external dependencies; otherwise a database outage restarts every pod.

Spring Boot support

Actuator exposes /actuator/health/liveness and /actuator/health/readiness (enabled automatically on Kubernetes, or with management.endpoint.health.probes.enabled=true). The application's LivenessState and ReadinessState can be changed programmatically, and you can add specific indicators to the readiness group.

Graceful shutdown

When a pod is deleted, Kubernetes in parallel sends SIGTERM to the container (after any preStop hook) and removes the pod from endpoints. Load balancers and kube-proxy rules take a few seconds to update, so an application that stops immediately drops requests.

  1. preStop hook sleeping 5–10 s so endpoint removal propagates.
  2. server.shutdown=graceful with spring.lifecycle.timeout-per-shutdown-phase=20s to drain in-flight requests.
  3. terminationGracePeriodSeconds larger than preStop plus drain time (for example 45 s).

Example

yaml
containers:
  - name: app
    image: registry.example.com/orders:3f2a91c
    ports: [{ containerPort: 8080 }, { name: mgmt, containerPort: 8081 }]
    startupProbe:
      httpGet: { path: /actuator/health/liveness, port: mgmt }
      periodSeconds: 5
      failureThreshold: 30          # up to 150 s to start
    readinessProbe:
      httpGet: { path: /actuator/health/readiness, port: mgmt }
      periodSeconds: 5
      failureThreshold: 3
    livenessProbe:
      httpGet: { path: /actuator/health/liveness, port: mgmt }
      periodSeconds: 10
      failureThreshold: 3
    lifecycle:
      preStop: { exec: { command: ["sh", "-c", "sleep 10"] } }
terminationGracePeriodSeconds: 45

Follow-up questions interviewers ask

Try answering each one before you open it.

What are common probe mistakes?

Using the same endpoint (or one that checks the database) for liveness and readiness; liveness timeouts shorter than GC pauses or slow startup, which cause restart loops; very aggressive failureThreshold: 1; probes on the public port that are subject to rate limiting or authentication; and readiness that never fails, so overloaded pods keep receiving traffic.