How do you add health checks to an ASP.NET Core app for Kubernetes probes?

IntermediateConcept.NET builds (dotnet CLI)
Answer

Short answer

Register health checks in Program.cs with AddHealthChecks() and map endpoints with MapHealthChecks(). Use a simple liveness endpoint (is the process alive?) and a readiness endpoint that also checks dependencies like the database, then point Kubernetes probes at them.

Example

builder.Services.AddHealthChecks()
    .AddNpgSql(builder.Configuration.GetConnectionString("Default")!, tags: new[] { "ready" });

var app = builder.Build();
app.MapHealthChecks("/health/live", new() { Predicate = _ => false });          // no dependency checks
app.MapHealthChecks("/health/ready", new() { Predicate = c => c.Tags.Contains("ready") });
livenessProbe:  { httpGet: { path: /health/live,  port: 8080 }, initialDelaySeconds: 10 }
readinessProbe: { httpGet: { path: /health/ready, port: 8080 }, periodSeconds: 5 }

Why split them

If liveness checked the database, a short DB outage would make Kubernetes restart every pod — making things worse. Readiness failing only removes pods from the Service until the DB is back.

What to say in the interview

"AddHealthChecks with MapHealthChecks: a lightweight live endpoint with no dependency checks and a ready endpoint that checks the database. Liveness restarts hung processes; readiness takes pods out of rotation without restarting them."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: .NET apps: dotnet build & publish

More on .NET builds (dotnet CLI)