Sub topic 5
Health groups and Kubernetes liveness and readiness probes
Prerequisites and scope
This chapter assumes you can add Actuator to a Spring Boot application and expose endpoints (see the earlier chapters on exposure and the health endpoint). It also assumes you know what a Kubernetes Pod and a Deployment are. You do not need to administer a cluster.
By the end you will be able to:
- Explain what a health group is and define your own.
- Enable the liveness and readiness probe groups.
- Point Kubernetes probes at the correct URLs.
- Avoid the common mistake of tying liveness to external dependencies.
What a health group is
The /actuator/health endpoint combines the results of many HealthIndicator beans, such as the disk space, database, and your own indicators. A health group is a named subset of those indicators. Each group has its own URL, its own status aggregation, and its own display settings.
A group is reached at /actuator/health/{groupName}. Without any configuration, only the root /actuator/health exists. You create a group by listing the indicators it includes under management.endpoint.health.group.<name>.
management:
endpoint:
health:
group:
infra:
include: db,diskSpace
show-details: always
payments:
include: paymentGatewayThe names in include are the indicator names, which come from the bean name minus the HealthIndicator suffix. The PaymentGatewayHealthIndicator from the previous chapter is named paymentGateway. With this configuration:
/actuator/health/infrareports only the database and disk space./actuator/health/paymentsreports only the payment gateway./actuator/healthstill reports everything.
A group can also use exclude instead of include, which is useful when you want "everything except one slow check".
Group settings
Each group can override several settings that otherwise come from the main health endpoint.
| Property | Purpose |
|---|---|
| `include` / `exclude` | Which indicators belong to the group. |
| `show-details` | Whether components and details appear in the response. |
| `show-components` | Show component names and statuses without the detail maps. |
| `status.http-mapping.*` | Map a status to an HTTP code for this group. |
| `status.order` | Order used to pick the overall status. |
| `additional-path` | Serve the group on another path, such as the main or management server. |
How status aggregation works
By default the overall status is the worst status among the members. The built-in order is DOWN, OUT_OF_SERVICE, UP, UNKNOWN. If any member is DOWN, the group is DOWN. The default HTTP mapping returns 200 for UP and 503 for DOWN and OUT_OF_SERVICE. Kubernetes treats any status from 200 up to but not including 400 as success, so a 503 response makes the probe fail.
Liveness and readiness probes
Kubernetes asks two different questions about a container, and each has its own probe.
- Liveness: is the process healthy enough to keep running? If the probe fails repeatedly, Kubernetes restarts the container.
- Readiness: can the process accept traffic right now? If the probe fails, Kubernetes removes the Pod from the Service endpoints but does not restart it.
Spring Boot models these as two built-in health groups, liveness and readiness. They are backed by the application availability state, which the application holds in memory:
LivenessStateisCORRECTorBROKEN.ReadinessStateisACCEPTING_TRAFFICorREFUSING_TRAFFIC.
The groups map these to UP/DOWN for liveness and UP/OUT_OF_SERVICE for readiness.
Enabling the probe groups
When Spring Boot detects that it is running on Kubernetes, it enables the probes automatically. Elsewhere, switch them on explicitly:
management:
endpoint:
health:
probes:
enabled: trueThis registers:
/actuator/health/liveness/actuator/health/readiness
A successful response looks like this:
{"status":"UP"}The probe groups do not show details by default, which keeps the response small and safe for unauthenticated callers.
Application lifecycle and the probe states
During startup and shutdown, the readiness state changes so Kubernetes does not send requests to an application that is not ready.
The key point is that readiness only becomes ACCEPTING_TRAFFIC after the application context has refreshed and all ApplicationRunner and CommandLineRunner beans have finished. A slow runner delays readiness, which is usually what you want.
Configuring Kubernetes probes
Here is a Deployment fragment that uses the two endpoints, plus a startup probe to protect slow boots.
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.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
failureThreshold: 3
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
periodSeconds: 5
failureThreshold: 3The startup probe gives the application up to 150 seconds (30 attempts at 5 second intervals) to come up. The liveness and readiness probes are held off until it succeeds, so a slow start does not cause a restart loop.
If you moved Actuator to another port, as described in the management port chapter, change port to match (for example 9090). Probes then bypass the application's main security rules and do not compete with user traffic for the same connector.
Adding your own checks to a probe group
You can put indicators into the readiness or liveness group, because they are ordinary groups.
management:
endpoint:
health:
probes:
enabled: true
group:
readiness:
include: readinessState,dbWith this configuration, a database outage turns the Pod unready. Kubernetes stops routing traffic to it, and traffic resumes when the database returns.
Be selective. The two groups carry different risks.
Do not put external dependencies in liveness
A liveness failure restarts the container. If liveness depends on the database and the database goes down, Kubernetes restarts every Pod in the Deployment at once. Restarting does not fix the database, and the restarts add load and turn a partial outage into a complete one.
Use these rules:
- Liveness reports only whether the application itself is irrecoverably broken, such as a deadlock or corrupted internal state.
- Readiness may include dependencies the application cannot serve requests without.
- Prefer a shared, non-critical dependency to be reported in a regular group, not in either probe.
Readiness and cascading failure
Readiness has a similar trap. If every replica depends on the same downstream service and that service fails, all replicas become unready. The Service then has no endpoints, and callers receive errors from the platform rather than from your application. Include a dependency in readiness only when serving without it would produce wrong results, not merely degraded ones.
Changing availability state from code
Application code can publish availability changes. This is how you tell Kubernetes that the instance is broken or temporarily busy.
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.LivenessState;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
@Service
public class AvailabilityService {
private final ApplicationEventPublisher publisher;
public AvailabilityService(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void pauseTraffic() {
AvailabilityChangeEvent.publish(publisher, this, ReadinessState.REFUSING_TRAFFIC);
}
public void resumeTraffic() {
AvailabilityChangeEvent.publish(publisher, this, ReadinessState.ACCEPTING_TRAFFIC);
}
public void markBroken() {
AvailabilityChangeEvent.publish(publisher, this, LivenessState.BROKEN);
}
}pauseTraffic is useful for draining an instance during a long maintenance task. markBroken should be rare. It asks Kubernetes to restart the container.
To read the state, inject ApplicationAvailability and call getReadinessState() or getLivenessState().
Graceful shutdown
Readiness works together with graceful shutdown. Enable it so in-flight requests finish before the process exits:
server:
shutdown: graceful
spring:
lifecycle:
timeout-per-shutdown-phase: 30sOn shutdown, Spring Boot first flips readiness to REFUSING_TRAFFIC, then waits for active requests. Kubernetes may still send a few requests for a short time after SIGTERM, because endpoint removal propagates asynchronously. A short preStop sleep in the container spec, or a terminationGracePeriodSeconds larger than the timeout above, covers this gap.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Probe returns 404 | Probe groups not enabled, or `health` not exposed. | Set `probes.enabled: true` and include `health` in the exposure list. |
| Probe times out on the main port | Actuator moved to a management port. | Point the probe at the management port. |
| Probe returns 401 or 403 | Security rules cover the probe path. | Permit `health` in your Actuator filter chain. |
| All Pods restart during a database outage | Database included in liveness. | Remove it from liveness. |
| Pod is ready before warm-up finishes | Warm-up work runs after the context is ready. | Run it in a runner, or hold readiness with an availability event. |
| Group URL returns 404 | Indicator name misspelled in `include`. | Check the names in the root `/actuator/health` response. |
Summary
- A health group selects a subset of indicators and gives it a separate URL, details policy, and status mapping.
- The
livenessandreadinessgroups are built in, driven by in-memory availability state, and enabled withmanagement.endpoint.health.probes.enabled. - Liveness restarts the container. Readiness only controls traffic. Keep dependencies out of liveness.
- Publish
AvailabilityChangeEventto change state from code, and combine readiness with graceful shutdown.
Exercise
Your application's readiness group currently includes db. During a short database outage, all of your Pods were restarted, not just removed from traffic. You did not change the readiness group. What most likely caused the restarts, and how do you fix it?
Show answer
The liveness probe most likely points at /actuator/health (the root endpoint) or a group that includes db, so the database failure made liveness fail and Kubernetes restarted the containers. Point the liveness probe at /actuator/health/liveness, which depends only on the application's own liveness state, and keep db only in readiness (or a separate group that no probe uses).
Exercise
Write the YAML that creates a group named critical containing only the db and paymentGateway indicators, and makes /actuator/health/critical show full details only to authorized users.
Show answer
management: endpoint: health: group: critical: include: db,paymentGateway show-details: when-authorized