{{announcement.body}}
{{announcement.title}}

Health Checks with ASP.NET Core and Kubernetes

DZone 's Guide to

Health Checks with ASP.NET Core and Kubernetes

Learn more about the difference between readiness and liveness checks, then learn how to run these checks using ASP.Net Core and Kubernetes.

· Web Dev Zone ·
Free Resource

Health checks are a fundamental part of our APIs. I guess they fall in that category of "non-functional-but-heavily-required" things. More or less like a good part of the infrastructure code.

They don't add business value per se but have an enormous impact for those in IT, like DDD and design patterns. You can normally see them in conjunction with container orchestration or monitoring tools to ensure that the system is alive and kicking.

There are mainly two categories of health checks: readiness and liveness.

Readiness health checks perform an in-depth check of all the application dependencies, such as databases, external services and so on. The system is booting and alive but not yet ready to serve incoming requests.

Liveness health checks are instead used to signal that the application is ready to serve traffic. They should execute fairly quickly and serve as an immediate probe to ensure everything is fine.

The idea is to first run the readiness checks. If they pass, rely only on the liveness ones for a specific amount of time.

A successful health check should return a 200 HTTP status and a basic report, especially for the readiness ones.

Setting up checks in an ASP.NET Core project is fairly easy. Just add a call to services.AddHealthChecks() in the ConfigureServices() method of our Startup.cs .

On GitHub, there are a few interesting repositories that add some nice extension methods. AspNetCore.Diagnostics.HealthChecks is one of the most famous, exposing checks for a wide range of systems like SQL Server, MySql, Oracle, Kafka, Redis, and many others.

Once you've registered the checks on the DI Container, the next step is to expose the endpoint:

C#
 







This is the simplest example possible, however, the  MapHealthChecks() methods also give us the ability to customize the output by specifying a Response Writer:

C#
 






Based on the checks you've added, this should return something like this:

C#
 




xxxxxxxxxx
1
10


 
1
{
2
  "status": "Healthy",
3
  "results": {
4
    "db": {
5
      "status": "Healthy",
6
      "description": null,
7
      "data": {}
8
    }
9
  }
10
}


Now, I mentioned "container orchestration" at the beginning of this article. This tends to be synonymous with Kubernetes, which has its own configuration for health checks. In your configuration.yml file you can specify both liveness and readiness:

C#
 






A few things to note here. First of all, the endpoints are different. As we discussed previously, we can (and should) split our checks in order to let the liveness checks to run as quickly as possible. 

This can be accomplished, for example, by simply skipping all the checks and returning a 200 right away:

C#
 




xxxxxxxxxx
1


1
endpoints.MapHealthChecks("/health/readiness", healthCheckOptions);
2
 
          
3
endpoints.MapHealthChecks("/health/liveness", new HealthCheckOptions(){
4
    Predicate = (_) => false
5
});



That Predicate allows filtering the checks based on various conditions like name or tags. Yes, those are a thing and can be specified. More details here.

Going back to our Kubernetes config, another thing worth mentioning is the different settings used for the checks. For example, timeoutSeconds is higher when probing for readiness as we are making sure that all our dependencies are alive. The same thing applies for periodSeconds: we want liveness checks to be executed more often.

Moreover, don't forget that if the failureThreshold is surpassed for liveness, the Pod will be killed. Failing readiness will cause the pod to be marked as Unhealthy instead, and not receive traffic any more.

Topics:
asp.net core ,c# ,cloud ,docker ,kubernetes ,liveness check ,microservice ,nuget ,readiness check ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}