Kubernetes core objects and a production-ready Deployment for ASP.NET Core (C# health checks)

Pod: the smallest unit: one or more containers sharing a network.

csharp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// ======================================================================
// Q: Explain the core Kubernetes objects and show a production-ready Deployment.
//
// - Pod: the smallest unit: one or more containers sharing a network. It is
//   ephemeral.
// - Deployment: keeps N replicas of a Pod, performs rolling updates and
//   rollbacks.
// - Service: a stable DNS name and load balancing to the Pods (Pods change IPs).
// - Ingress / Gateway API: HTTP routing from outside (host, path, TLS) to the
//   Services.
// - ConfigMap / Secret: configuration and sensitive data, as env vars or files.
// - Job / CronJob: tasks that run to completion: migrations (Q8), nightly
//   imports.
// - HPA: automatic scaling of the number of replicas.
// The liveness probe does not check the database or the ERP. If the database
// goes down, all pods would be restarted at the same time for no reason (restart
// storm). Dependencies are checked only in readiness.
// ======================================================================

// Program.cs: separate endpoints for liveness and readiness
builder.Services.AddHealthChecks()
    .AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["live"])
    .AddDbContextCheck<AppDbContext>(tags: ["ready"]);

app.MapHealthChecks("/health/live",  new() { Predicate = r => r.Tags.Contains("live") }).AllowAnonymous();
app.MapHealthChecks("/health/ready", new() { Predicate = r => r.Tags.Contains("ready") }).AllowAnonymous();
// AllowAnonymous: otherwise the fallback policy (Q36) returns 401
// and Kubernetes "thinks" the pod is dead