Health checks
Liveness, readiness and startup probes over HTTP and gRPC, fed by checks that any module contributes.
ulo-health answers three probes with Kubernetes' meanings. A module contributes a Check, HealthModule collects every check, and a transport crate serves the answers.
| Crate | Contains |
|---|---|
ulo-health | Check, Health, Probe, Readiness and HealthModule. |
ulo-health-http | HealthHttp: the three probe routes. |
ulo-health-grpc | HealthGrpc: the overall grpc.health.v1 status, written from readiness. |
Setup
This is examples/34-health. The app imports the health module and one or both transports, and a module that owns a dependency contributes a check about it.
use ulo::{injectable, module};
use ulo_health::{Check, HealthModule};
use ulo_health_grpc::HealthGrpc;
use ulo_health_http::HealthHttp;
#[module(providers = [Cache, into dyn Check: [CacheCheck]], exports = [Cache])]
pub struct CacheModule;
#[module(imports = [
HealthModule::new().lag(LAG),
HealthHttp::new().detail(true),
HealthGrpc::new(),
CacheModule,
])]
pub struct AppModule;HealthModule is global, so every module reads one Readiness. It requires the application's Timer, which a runtime crate supplies.
Checks
A check is a named question about one dependency:
impl Check for CacheCheck {
fn name(&self) -> &str {
"cache"
}
fn check(&self) -> BoxFuture<'_, Health> {
let warm = self.cache.warm.load(Ordering::SeqCst);
Box::pin(async move { if warm { Health::Up } else { Health::down(Cold) } })
}
}Health::down takes a reason, which is redacted before it is shown. The trait has two more methods with defaults:
| Method | Default | Meaning |
|---|---|---|
bound() | Bound::Default, 2 s | How long the check may take. A check that does not answer in time is down. Bound::Unbounded opts out and Bound::After(Duration::ZERO) fails connect. |
probe() | Probe::Readiness | Which probe the check feeds. |
Checks run concurrently. A probe arriving while a run is in flight shares that run, and no result is kept between runs, so a probe never reads a stale "up". A check that panics is down for that run.
A module contributes a check with into dyn Check: [..] in providers, or m.contribute::<dyn Check>() inside a register method. A keyed module's contribution keeps its key, so two keyed databases contribute two checks. Every database module contributes one named after the store, and .without_health_check() leaves it out.
A module loaded lazily cannot contribute a check, because the collection is read at connect.
The probes
| Probe | True when |
|---|---|
| Liveness | The process answers, and every check declared Probe::Liveness is up. Integrations never contribute to it. |
| Readiness | The app is running and every readiness check is up. |
| Startup | The app has finished listen. Always up when it can be asked. |
Nothing answers a probe before listen. connect and listen are the application phases. Before listen, readiness is read in process, through Dep<Readiness>:
let readiness = connected.get::<Readiness>().await.unwrap();
let before = readiness.readiness().await;
assert!(!before.is_up());Shutdown
Readiness turns false when the application reaches Stopping. This happens in the health module's before-shutdown hook, which runs ahead of every check's hooks and before the first refusal. After Stopping, readiness answers one down entry named lifecycle with the reason the app is stopping, and liveness is unaffected.
The hook then waits HealthModule::lag, zero by default, so the endpoint change reaches kube-proxy and the load balancers before the app refuses work. Kubernetes marks a pod's endpoints terminating before its preStop hook runs. Leave the lag at zero when the pod has a preStop sleep, and set it to a few seconds when it has none. The hook is bounded at the lag plus one second, so the 10 second hook default does not cut a lag. A lag longer than the application's shutdown_timeout is cut by that cap.
During the drain the HTTP server answers every request with its own 503 before routing, probes included. kubelet stops the liveness and startup probes at termination, so a liveness failure during the drain cannot kill the pod.
HTTP
HealthHttp::new() serves /health/live, /health/ready and /health/started. Each path is configurable with live_path, ready_path and started_path.
A route answers 200 when its probe passes and 503 when it does not. Kubernetes counts 200 to 399 as success. The body follows the shape of the IETF health-check draft, which expired in April 2022 and is a shape followed rather than a contract. The body has media type application/health+json and a status of pass or fail.
By default the body carries nothing else, because a list of failing checks tells an outsider about internals. .detail(true) adds a checks object mapping each check's name to an array of results, each down check with its redacted reason:
{ "status": "fail", "checks": { "cache": [{ "status": "fail", "output": "the cache is cold" }] } }Once the cache is warm the same route answers 200 with { "status": "pass", "checks": { "cache": [{ "status": "pass" }] } }.
gRPC
HealthGrpc::new() writes the overall grpc.health.v1 status, the service name "", from readiness. It imports ulo_grpc::GrpcHealth::module() itself, so the gRPC server serves the reporter it writes.
The overall status is NOT_SERVING while any readiness check is down and turns NOT_SERVING when Stopping begins, so a Watch subscriber sees it as readiness flips. grpc.health.v1's Check reads a stored status, so a task on the app's runtime re-evaluates readiness every interval and writes on change. .interval(Bound) sets it: Bound::Default is 5 s, Bound::Unbounded leaves statuses changing at lifecycle transitions alone, and Bound::After(Duration::ZERO) is refused by wire().
"" has one writer per application. Named services keep the server's writes at bind and drain and the application's through GrpcHealth::set_named. A Watch subscriber receives a message only when a status changes.
tonic-health answers a Watch on a name nobody registered with NOT_FOUND, where the protocol streams SERVICE_UNKNOWN and keeps the call open. Kubernetes' gRPC probe sends Check with the service name defaulting to "".
The example binds both servers and warms the cache after listen. /health/ready answers 503 until that line has run:
let app = connected
.bind(Server::new(format!("127.0.0.1:{port}")))
.bind(ulo_grpc::Server::new(format!("127.0.0.1:{grpc_port}")))
.listen()
.await?;
// The probes answer from the moment the app listens; ready stays 503 until this line has run.
cache.warm_up();