DevOps · Kubernetes & OpenShift

Your liveness probe is not a database check

Raffael Hühnerschulte · 5 min read

The database hiccups for 30 seconds. Ten minutes later every pod of your app has restarted, twice. The database was not the outage. The liveness probe was.

In my OpenShift operations training, teams run one small app with a PostgreSQL database through every module. By the time we reach health checks, the app already depends on that database, and that is exactly when the question “what should a probe check?” becomes dangerous. In YAML the two probes look almost identical, so it is tempting to point both at the same /health endpoint, and that endpoint usually pings the database.

Liveness vs readiness: why the difference decides your next outage

That is why I treat a dependency check in the liveness probe as one of the most expensive one-line mistakes on Kubernetes and OpenShift. Readiness asks “can this pod take traffic right now?” and a failure only removes the pod from the Service. Liveness asks “is this process stuck beyond repair?” and a failure makes the kubelet kill and restart the container.

Put the database into liveness and a short slowdown fails every pod at once, every pod restarts at once, they all reconnect at once, the database gets slower, and the loop feeds itself. A pod that cannot reach its database should stop getting traffic, not get killed.

ProbeQuestion it answersWhat a failure doesShould it check the database?
LivenessIs this process stuck beyond repair?Kubelet kills and restarts the containerNo
ReadinessCan this pod take traffic right now?Pod is removed from the Service endpoints, stays runningYes, if the pod cannot serve without it
StartupHas the app finished starting?Liveness and readiness wait until it succeedsNo

The split I teach, and the check to run on your own cluster

So this is the split I teach, and the check I make teams run on their own cluster:

  1. Liveness checks only the process: does the app answer at all, without touching the database, a cache or another service.
  2. Readiness checks the dependencies the pod needs to serve requests.
  3. A slow start gets a startup probe, not a huge initialDelaySeconds on liveness.
  4. Look at the RESTARTS column of oc get pods (or kubectl get pods). Restarts that line up with dependency incidents point at the liveness probe.
  5. Prove it in a test namespace: make the database unreachable on purpose. Correct result: pods go to READY 0/1 and stay alive; when the database is back, they return to 1/1 without a single restart.

In the Deployment it looks like this: three probes, three endpoints, and only readiness knows about the database.

containers:
  - name: app
    image: registry.example.com/shop/app:1.4.2
    ports:
      - containerPort: 8080
    startupProbe:            # gives a slow start time, then hands over
      httpGet: { path: /health/live, port: 8080 }
      periodSeconds: 5
      failureThreshold: 30   # up to 150 s to start
    livenessProbe:           # process only, no database, no cache
      httpGet: { path: /health/live, port: 8080 }
      periodSeconds: 10
      failureThreshold: 3
    readinessProbe:          # dependencies, including the database
      httpGet: { path: /health/ready, port: 8080 }
      periodSeconds: 5
      failureThreshold: 3
Spring Boot shortcut Spring Boot Actuator already exposes both groups: /actuator/health/liveness and /actuator/health/readiness. By default neither contains the database check. If the database belongs in readiness, add it there explicitly (management.endpoint.health.group.readiness.include=readinessState,db) and keep the liveness group free of dependencies.

FAQ: liveness and readiness probes

What is the difference between a liveness and a readiness probe?

A readiness probe decides whether a pod receives traffic: when it fails, the pod is taken out of the Service but keeps running. A liveness probe decides whether the container is restarted: when it fails, the kubelet kills the container and starts it again.

Should a liveness probe check the database?

No. If the database is slow or briefly unreachable, every pod fails its liveness probe at the same time and restarts, which adds reconnect load to a database that is already struggling. Dependencies belong in the readiness probe, so pods stop taking traffic and recover on their own.

When do I need a startup probe?

When the app needs noticeably longer to start than to answer a health check later, for example a JVM application that warms up caches. The startup probe holds back liveness and readiness until the app is up, so liveness does not kill it during the start.

How do I find out if my liveness probe causes restarts?

Compare the RESTARTS column of kubectl get pods or oc get pods with the times of database or network incidents, and look at the pod events for “Liveness probe failed”. Then make the dependency unreachable in a test namespace: pods should go to not ready, not restart.

Go Deeper

Restart storms on your cluster?

In the training, teams break this on purpose and repair it with their own probes. Tell me your team size and what you run on the cluster, and I’ll send you the outline with a suggested focus.