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:
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:
Genesis: Artificial Intelligence, Hope, and the Human Spirit
$17.05 (as of November 21, 2024 15:46 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Syntech USB C to USB Adapter Pack of 2 USB C Male to USB 3.0 Female Adapter Compatible with MacBook Pro Air 2024, Microsoft Surface, iPad,Samsung Notebook, Dell XPS and More Type C Devices,Space Grey
$9.99 (as of November 20, 2024 06:18 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)The Art and Making of Arcane (Gaming)
$54.00 (as of November 21, 2024 15:46 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)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:
- Startup Probe: Determines if your application has started successfully. This is particularly useful for applications with long startup times.
- Readiness Probe: Checks if your application is ready to handle requests. If the readiness probe fails, Kubernetes will stop sending traffic to the container.
- 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:
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
andperiodSeconds: 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:
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.