Sub topic 5
Health groups and Kubernetes liveness and readiness probes
This chapter assumes you can add Actuator to a Spring Boot application, expose endpoints in application.yaml, and read the /actuator/health response. It also assumes you know what a Kubernetes Deployment is. You will learn how health groups work, how the built-in liveness and readiness probe groups differ, and how to wire them into Kubernetes.
Why one health status is not enough
The aggregated /actuator/health result combines every health indicator. That is a poor fit for Kubernetes, which asks two different questions:
- Liveness: Is the process broken in a way only a restart can fix? If not, Kubernetes restarts the container.
- Readiness: Can the application accept traffic right now? If not, Kubernetes stops routing requests to it but leaves it running.
If a database outage turned the single health status to DOWN and you used it as a liveness check, Kubernetes would restart every pod. The restarts would not fix the database, and they would make the outage worse. A health group lets you choose which indicators contribute to which question.
Enabling the probe groups
Set management.endpoint.health.probes.enabled to true. Spring Boot then creates two groups.
management:
endpoint:
health:
probes:
enabled: true
endpoints:
web:
exposure:
include: healthSpring Boot enables them automatically when it detects a Kubernetes environment, but setting the property makes the behavior explicit and works locally too. You can now call two extra paths:
| Path | Group | Default contents | Reports |
|---|---|---|---|
| `/actuator/health/liveness` | `liveness` | `livenessState` | `UP` or `DOWN` (the application's internal state) |
| `/actuator/health/readiness` | `readiness` | `readinessState` | `UP` or `OUT_OF_SERVICE` |
Each group answers with HTTP 200 when the status is UP. A DOWN or OUT_OF_SERVICE status returns HTTP 503.
How the states change
The states come from Spring's ApplicationAvailability. Readiness is REFUSING_TRAFFIC until startup finishes, and it becomes ACCEPTING_TRAFFIC after the application is ready. During graceful shutdown it flips back, so the pod leaves the load balancer before it stops.
Your code can change these states by publishing an event. Use this when the application is alive but should temporarily stop receiving traffic, for example while it rebuilds a cache.
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.stereotype.Service;
@Service
public class CacheRebuilder {
private final ApplicationEventPublisher publisher;
public CacheRebuilder(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void rebuild(Runnable work) {
AvailabilityChangeEvent.publish(publisher, this, ReadinessState.REFUSING_TRAFFIC);
try {
work.run();
} finally {
AvailabilityChangeEvent.publish(publisher, this, ReadinessState.ACCEPTING_TRAFFIC);
}
}
}Adding indicators to a group
By default the probe groups hold only the availability states. Do not add an external dependency to liveness. You can add one to readiness if the application cannot serve useful responses without it. Use the include property on the group:
management:
endpoint:
health:
probes:
enabled: true
group:
readiness:
include: readinessState,dbYou can also define your own groups. This one, named external, collects the paymentGateway indicator from the earlier chapter and is served at /actuator/health/external.
management:
endpoint:
health:
group:
external:
include: paymentGateway
show-details: alwaysConfiguring Kubernetes
Point each probe at its own path. The startup probe gives slow applications time to boot before liveness checks begin.
apiVersion: apps/v1
kind: Deployment
metadata:
name: orders
spec:
replicas: 2
selector:
matchLabels:
app: orders
template:
metadata:
labels:
app: orders
spec:
containers:
- name: orders
image: example/orders:1.0
ports:
- containerPort: 8080
startupProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
failureThreshold: 30
periodSeconds: 5
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
periodSeconds: 10
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
periodSeconds: 5If you moved Actuator to a separate management port, use that port in the probes.
Common errors
- Probes return 404. The
healthendpoint is not exposed, or the probes are not enabled. - Pods restart in a loop during an outage. A downstream check was added to the liveness group. Move it to readiness or remove it.
- Pods receive traffic while starting. The readiness probe points at
/actuator/healthrather than/actuator/health/readiness. - Probes fail after enabling security. Kubernetes sends no credentials, so keep the probe paths open, as in the earlier security example that permits
health.
Exercise
A service needs its database to answer requests, but restarting the service does not fix a database outage. In which probe group should the db indicator go, and what happens to the pods when the database goes down?
Show answer
Put db in the readiness group (include: readinessState,db). When the database goes down, readiness returns 503, so Kubernetes stops sending traffic to the pods but does not restart them. Liveness stays UP, and the pods start accepting traffic again once the database recovers.