
GRPC healthcheck is a crucial component of any microservices architecture, ensuring that services are running smoothly and can be easily monitored. It's a standard protocol for checking the health of GRPC services.
The GRPC healthcheck protocol uses a simple and efficient approach to monitor service health by sending periodic requests to the service and checking for a response. This allows for quick identification of issues.
In a Kubernetes environment, deploying GRPC healthcheck can be done using a combination of Kubernetes resources, such as ConfigMaps, Services, and Deployment objects. This enables easy management and scaling of GRPC services.
Kubernetes provides a built-in healthcheck mechanism that can be leveraged to monitor GRPC services, making it easier to integrate healthcheck into existing Kubernetes deployments.
You might enjoy: C# Grpc Service
Recommended read: GRPC
What is gRPC Health Check
gRPC Health Check is a feature that allows clients to monitor the health of their servers and adjust their behavior accordingly. This is achieved by enabling health checks against the servers the client connects to.
A gRPC client can be configured to perform health checks by modifying the service config of the channel. This can be done using a JSON format, where you specify the service you want to monitor.
Here are the states a subchannel goes through based on the health of the service it's connecting to:
Enabling health checking changes some behavior around calling a server, including the client calling the Watch RPC on the health check service when a connection is established. If a healthy service becomes unhealthy, the client will no longer send requests for that service.
Consider reading: Grpc New Client
Enabling Health Checks
Enabling health checks is a crucial step in ensuring the reliability and performance of your gRPC service. You can configure a gRPC client to perform health checks by modifying the service config of the channel.
To monitor the health of a specific service, you would use the empty string ("") service, signifying the health of the whole server. This allows you to enable health checking for the entire server.
Enabling health checking changes some behavior around calling a server. Here are some key points to consider:
- The client will additionally call the Watch RPC on the health check service when a connection is established.
- Requests won’t be sent until the health check service sends a healthy status for the service being called.
- If a healthy service becomes unhealthy, the client will no longer send requests for that service.
- The calls will resume if the service later becomes healthy.
- Some load balancing policies can choose to disable health checking if the feature does not make sense with the policy.
In terms of state changes, the subchannel (representing the physical connection to the server) goes through several states based on the health of the service it is connecting to.
Setting Up Health Checks
To set up health checks, you need to configure your gRPC client to perform health checks against the servers it connects to. This involves modifying the service config of the channel, as shown in Example 1.
You can use an empty string ("") service to monitor the health of the whole server. Enabling health checking changes some behavior around calling a server, including additional calls to the Watch RPC on the health check service and requests being sent only when the health check service sends a healthy status.
Some load balancing policies can choose to disable health checking if it doesn't make sense with the policy, such as the pick_first policy. The specifics on how to enable client-side health checking vary by language, as shown in the Language Support section.
To set up gRPC health checks in an ASP.NET Core app, you need to add a Grpc.AspNetCore.HealthChecks package reference and register the gRPC health checks service. This involves implementing IHealthCheck or using the AddCheck method to add health checks.
The health checks service is added to the server app, and .NET health checks are periodically executed for health results. By default, there's a 5-second delay after app startup, and then health checks are executed every 30 seconds.
Health results determine what the gRPC service reports. You can customize the health checks execution interval using HealthCheckPublisherOptions at startup, as shown in Example 3.
Here are the default publisher intervals:
You can configure the gRPC health checks service to use a subset of health checks by using the MapService method, as shown in Example 4. This involves mapping health results to service names and filtering health results using a predicate.
Security and Configuration
Security and Configuration is a crucial aspect of gRPC Health Checking.
To enable health checking in gRPC, you can use the `grpc.health.v1.Health` service, which is enabled by default in gRPC.
The `grpc.health.v1.Health` service allows clients to send a `GET /health` request to check the status of a server.
A server can also report its status by implementing the `grpc.health.v1.Health` service, which can be done using a custom implementation or by using a third-party library.
To configure the `grpc.health.v1.Health` service, you can use the `health_check` option in the gRPC service configuration.
The `health_check` option allows you to specify the interval at which the server will report its status to the client.
By default, the server will report its status every 10 seconds, but this can be adjusted using the `health_check` option.
You can also use the `health_check` option to specify the timeout for the health check request.
The timeout can be adjusted to ensure that the health check request does not take too long to complete.
It's worth noting that the `health_check` option is not enabled by default in some gRPC implementations, so you may need to enable it manually.
You might enjoy: When to Use Grpc
gRPC Protocol and Kubernetes
Kubernetes now supports gRPC health checking, allowing you to define a gRPC liveness probe in your Pod specification if your cluster version supports it.
You can use grpc_health_probe as a solution if your Kubernetes version doesn't support gRPC health checking or if you need advanced features.
grpc_health_probe can be bundled in your container image by downloading a binary release in your Dockerfile.
In your Kubernetes Pod specification manifest, you can specify a livenessProbe and/or readinessProbe for the container, providing proper readiness/liveness checking to your applications that implement the gRPC Health Checking Protocol.
A fresh viewpoint: Grpc Version
gRPC Protocol
The gRPC Protocol is a key component of modern service discovery and health checking in Kubernetes. To implement gRPC Health Checking, you must register the Health service and implement the rpc Check that returns a SERVING status.
The Health Checking protocol is part of the gRPC core, which means you can leverage pre-built libraries for languages supported by gRPC, such as Go, Java, Python, C#/NuGet, and Ruby. These libraries provide helper functions that hide implementation details, making it easier to get started.
To use the gRPC Health Checking Protocol, you'll need to implement the Check rpc yourself, unless you're using one of the supported languages with pre-built libraries.
Kubernetes
Kubernetes is a powerful tool for managing containerized applications, and when it comes to gRPC servers, it's essential to configure Kubernetes probes to ensure their health and stability.
You can use gRPC health checking on Kubernetes, which checks if your application is running and ready to receive traffic. This feature is supported in Kubernetes versions that support gRPC health checking.
To define a gRPC liveness probe in your Pod specification, you can refer to the Kubernetes documentation. However, if your Kubernetes version doesn't support gRPC health checking, you can use grpc_health_probe to health-check your gRPC server.
Here are the key Kubernetes probes you need to configure:
- livenessProbe: Checks if your application is running. If this fails, Kubernetes will restart the pod.
- readinessProbe: Checks if your application is ready to receive traffic. If this fails, Kubernetes will temporarily stop sending traffic to this pod.
By setting up these probes, you ensure that your gRPC server's health is continually monitored by Kubernetes, providing stability and reliability to your deployment.
Implementing Health Checks
Implementing health checks is a crucial step in ensuring the reliability and stability of your gRPC service. Enabling client health checking allows clients to monitor the health of the servers they connect to, which can be done by modifying the service config of the channel.
To enable client health checking, you can use a JSON format to specify the health check service, such as "foo" or an empty string to monitor the health of the whole server. This will cause the client to call the Watch RPC on the health check service when a connection is established.
The state of the subchannel, representing the physical connection to the server, goes through several states based on the health of the service it is connecting to. The specifics on how to enable client-side health checking vary by language, so be sure to check the examples in the Language Support section.
To set up gRPC health checks on the server side, you can use the Grpc.AspNetCore.HealthChecks package in ASP.NET Core. This package provides built-in support for gRPC health checks, which can be enabled by adding a health checks service to the server app.
Here's an overview of the steps to set up gRPC health checks on the server side:
- Add a Grpc.AspNetCore.HealthChecks package reference.
- Register the gRPC health checks service.
- Add health checks by implementing IHealthCheck or using the AddCheck method.
The health checks service is added to the server app, and .NET health checks registered with the app are periodically executed for health results. By default, there's a 5-second delay after app startup, and then health checks are executed every 30 seconds. The health results determine what the gRPC service reports.
In terms of health check settings, you can specify the interval, timeout, protocol, unhealthy threshold, and healthy threshold. For example, if you set the interval to 30 seconds and the timeout to 5 seconds, the health check will behave as follows:
- Multiple redundant systems are simultaneously configured with the health check parameters.
- Each health check prober initiates a connection every 30 seconds.
- A backend is considered unhealthy when at least one health check probe system receives a response that does not match the protocol-specific success criteria.
- A backend is considered healthy when at least one health check probe system receives two consecutive responses that match the protocol-specific success criteria.
Here's an example of the timing of a health check prober:
- t=0: Start probe A.
- t=5: Stop probe A.
- t=30: Start probe B.
- t=35: Stop probe B.
- t=60: Start probe C.
- t=65: Stop probe C.
Example and Success Criteria
gRPC health checks are used for health checking gRPC backends, and Google Cloud supports two types of gRPC health checks: GRPC_WITH_TLS (Preview) and GRPC.
gRPC health checks are used for health checking insecure gRPC backends, which don't support authentication and encryption, so they can't be used for gRPC backends with TLS enabled.
To set up a health check, you'll need to specify the interval, timeout, protocol, and unhealthy threshold. For example, suppose you set up a health check with an interval of 30 seconds, a timeout of 5 seconds, and an HTTP protocol.
Multiple redundant systems are simultaneously configured with the health check parameters, and each health check prober initiates a connection every 30 seconds. The timeout must always be less than or equal to the interval, and the timeout never increases the interval.
The timing of each prober looks like this: start probe A at t=0, stop probe A at t=5, start probe B at t=30, stop probe B at t=35, and so on.
Here are the specific settings for the example health check:
The gRPC service must send the RPC response with the status OK and the status field set to SERVING or NOT_SERVING accordingly.
GitHub Repositories (4)
The GitHub repository `asynkron/protoactor-dotnet` is a popular choice, with over 1.8K stars. It's a high-performance distributed actor framework for Go, C#, and Java/Kotlin.
You can see the top 4 popular GitHub repositories that depend on Grpc.HealthCheck in the following table:
The `mehdihadeli/food-delivery-microservices` repository is a practical example of cloud-native architecture, built with .NET Aspire, .NET 9, MassTransit, and more.
The `cloudnative-netcore/netcorekit` repository is a toolkit for building cloud-native apps on the .NET platform, with 289 stars.
Featured Images: pexels.com


