How would you implement a health check specifically for a gRPC service, distinct from the standard ASP.NET Core health check middleware designed for REST endpoints?
Short answer
The Grpc.HealthCheck package implements the standard gRPC Health Checking Protocol (a defined, language-agnostic protobuf service contract), registered via AddGrpcHealthChecks() and integrated with ASP.NET Core's HealthCheckService so existing IHealthCheck implementations…
Advertisement
AModel answer
The Grpc.HealthCheck package implements the standard gRPC Health Checking Protocol (a defined, language-agnostic protobuf service contract), registered via AddGrpcHealthChecks() and integrated with ASP.NET Core's HealthCheckService so existing IHealthCheck implementations automatically feed into the gRPC health status -- this lets gRPC-aware infrastructure (like Kubernetes readiness probes configured for gRPC, or a service mesh) query health using the standard gRPC health protocol rather than an HTTP-based health endpoint.
// Program.cs
builder.Services.AddGrpcHealthChecks()
.AddCheck("database", () => {
return _db.CanConnect() ? HealthCheckResult.Healthy() : HealthCheckResult.Unhealthy();
});
var app = builder.Build();
app.MapGrpcHealthChecksService();
# Kubernetes readiness probe using grpc-health-probe
readinessProbe:
exec:
command: ["/bin/grpc_health_probe", "-addr=:5001"]Real-world example
A Kubernetes-deployed gRPC service exposes its health via the standard gRPC Health Checking Protocol, letting Kubernetes readiness probes (configured with the grpc-health-probe binary) natively query gRPC health status rather than requiring the service to also expose a separate HTTP health endpoint just for probing.
Likely follow-ups
- How does this integrate with existing IHealthCheck implementations already used for REST health endpoints?
- What's the difference between the overall service health status and per-gRPC-service health status in this protocol?
Related topics