Kubernetes Health Checks for ASP.NET Core Applications

Introduction

Ensuring the reliability and robustness of your applications is critical, especially when deploying them on Kubernetes. Health checks are a vital part of this process. They allow Kubernetes to monitor the state of your application and take necessary actions when things go wrong. In this post, I’ll guide you through setting up health checks for an ASP.NET Core application, covering everything from creating health check endpoints to configuring Kubernetes probes.

Thank me by sharing on Twitter 🙏

Learn documentation on health checks

Setting Up Health Check Endpoints in ASP.NET Core

First things first, you need to set up health check endpoints in your ASP.NET Core application. These endpoints will provide the necessary signals to Kubernetes about the application’s health and readiness.

To get started, add the Microsoft.AspNetCore.Diagnostics.HealthChecks package to your project. Open your terminal and run:

ShellScript
dotnet add package Microsoft.AspNetCore.Diagnostics.HealthChecks

With the package installed, you can now configure the health checks in your Program.cs file. Here’s how you can do it using the minimal API approach introduced in .NET 6:

C#
import { WebApplicationBuilder, WebApplication, HostEnvironment } from 'Microsoft.AspNetCore.Builder';
import { HealthCheckOptions } from 'Microsoft.AspNetCore.Diagnostics.HealthChecks';

// Create the builder and app instances
const builder: WebApplicationBuilder = WebApplication.CreateBuilder();
builder.Services.AddHealthChecks();
const app: WebApplication = builder.Build();

// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment()) {
    app.UseDeveloperExceptionPage();
}

app.MapHealthChecks('/healthz'); // Liveness check
app.MapHealthChecks('/readyz', new HealthCheckOptions({
    Predicate: _ => false
})); // Readiness check
app.MapHealthChecks('/startupz', new HealthCheckOptions({
    Predicate: _ => false
})); // Startup check

app.Run();

Understanding Kubernetes Probes

Kubernetes uses three types of probes to monitor the state of your application:

  1. Startup Probe: Determines if your application has started successfully. This is particularly useful for applications with long startup times.
  2. Readiness Probe: Checks if your application is ready to handle requests. If the readiness probe fails, Kubernetes will stop sending traffic to the container.
  3. Liveness Probe: Ensures your application is running. If the liveness probe fails, Kubernetes will restart the container.

By configuring these probes, you can ensure that Kubernetes effectively manages your application’s lifecycle, improving reliability and uptime.

Configuring Kubernetes Probes

Once you’ve set up the health check endpoints, the next step is to configure the Kubernetes probes in your deployment YAML file. Below is an example configuration that includes all three probes:

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnet-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: aspnet-app
  template:
    metadata:
      labels:
        app: aspnet-app
    spec:
      containers:
      - name: aspnet-app
        image: your-docker-image
        ports:
        - containerPort: 80
        startupProbe:
          httpGet:
            path: /startupz
            port: 80
          failureThreshold: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /readyz
            port: 80
          initialDelaySeconds: 10
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /healthz
            port: 80
          initialDelaySeconds: 30
          periodSeconds: 30

Explanation of Probe Configurations

  • Startup Probe:
  • Endpoint: /startupz
  • Purpose: Ensures the application has started correctly.
  • Configuration: The probe will retry for up to 5 minutes (failureThreshold: 30 and periodSeconds: 10) before declaring the startup a failure.
  • Readiness Probe:
  • Endpoint: /readyz
  • Purpose: Checks if the application is ready to handle traffic.
  • Configuration: The probe starts checking 10 seconds after the container starts and continues every 10 seconds (initialDelaySeconds: 10, periodSeconds: 10).
  • Liveness Probe:
  • Endpoint: /healthz
  • Purpose: Ensures the application is running healthily.
  • Configuration: The probe starts checking 30 seconds after the container starts and continues every 30 seconds (initialDelaySeconds: 30, periodSeconds: 30).

Applying the Configuration

After defining the probes, apply the deployment to your Kubernetes cluster with the following command:

ShellScript
kubectl apply -f your-deployment-file.yaml

This command updates your Kubernetes cluster with the new deployment configuration, enabling it to start using the health probes to monitor your application.

Conclusion

By setting up health checks and configuring Kubernetes probes, you ensure that your ASP.NET Core application runs reliably within a Kubernetes cluster. These probes provide Kubernetes with the necessary information to manage your application’s lifecycle effectively, improving both its stability and availability. With these configurations in place, you can focus on developing features and functionality, confident that your application’s health is being monitored and managed by Kubernetes.

Share this:

Leave a Reply