Volume 5 · Chapter 4 — Advanced .NET Cloud Native Architecture

.NET on Kubernetes

Goutham Mayaluri 7 min read .NETC#KubernetesCloud Native

4.1 Introduction

Running ASP.NET Core on Kubernetes combines the strengths of both: Kubernetes manages the runtime environment (scheduling, scaling, healing) while .NET provides the application capabilities. To be a good Kubernetes citizen, a .NET service must be stateless, externalize configuration, expose health probes, honor resource limits, and shut down gracefully - the properties that let the orchestrator manage its lifecycle correctly.

4.2 Health Probes: The Key Integration Point

Kubernetes manages a .NET app through health probes. Alivenessprobe answers “is the process wedged and in need of a restart?”; areadinessprobe answers “can it serve traffic now?” (dependencies reachable, warm-up done); astartupprobe covers slow-starting apps. Conflating liveness and readiness is dangerous - a brief dependency blip should fail readiness (stop routing) but not liveness (kill the pod).


builder.Services.AddHealthChecks()
    .AddCheck("self", () => HealthCheckResult.Healthy(), tags: new[] { "live" })
    .AddSqlServer(conn, tags: new[] { "ready" })
    .AddRedis(redis, tags: new[] { "ready" });

app.MapHealthChecks("/health/live",  new() { Predicate = c => c.Tags.Contains("live") });
app.MapHealthChecks("/health/ready", new() { Predicate = c => c.Tags.Contains("ready") });
app.Lifetime.ApplicationStopping.Register(() => { /* drain in-flight work */ });  // graceful shutdown

4.3 Configuration, Secrets, and Resource Limits

A cloud-native .NET app externalizes configuration - from environment variables, ConfigMaps, Secrets, and external stores - rather than baking environment-specific values into the image. Resourcerequeststell the scheduler how much to reserve (and feed the autoscaler);limitscap usage (exceeding a memory limit gets the container OOM-killed). Setting these correctly is essential for stable, efficiently packed, autoscaling workloads.

4.4 Scaling and Background Services

Kubernetes scales .NET workloads with the Horizontal Pod Autoscaler (on CPU/memory or custom/event metrics via KEDA) and the Cluster Autoscaler (adding nodes when pods can’t be scheduled). .NET Worker Services run as Kubernetes workloads for background processing - message consumers, scheduled jobs, batch work - benefiting from the same orchestration.

Senior Interview Question

How does Kubernetes know a .NET application is healthy, and why must liveness and readiness probes be separate?

Kubernetes determines a .NET app’s health through health probes that the app exposes - HTTP endpoints (or commands/TCP checks) that Kubernetes calls periodically - and there are three kinds serving distinct purposes, which is why liveness and readiness must be separate. A liveness probe answers “is this process fundamentally broken and in need of a restart?” If it fails, Kubernetes kills and restarts the pod, on the assumption that restarting will fix it. Therefore a liveness probe should check only the application itself (is it wedged, deadlocked, unresponsive?), not its dependencies - because restarting the pod won’t fix a downstream outage. A readiness probe answers “can this instance serve traffic right now?” If it fails, Kubernetes removes the pod from the Service’s load-balancing pool (stops routing traffic to it) but does NOT restart it; it can legitimately check dependencies (database, cache reachable) and warm-up state, because those can make an instance temporarily unable to serve even though the process is fine. A startup probe handles slow-starting apps, giving them time to initialize before liveness/readiness kick in. Separating liveness and readiness is critical because conflating them is dangerous: if you put a dependency check in the liveness probe, then a transient database blip - which should merely make the instance temporarily not-ready (stop sending it traffic until the database recovers) - would instead fail liveness and cause Kubernetes to kill and restart the pod. Worse, if that database is shared and briefly unavailable, every pod’s liveness fails simultaneously, triggering a cluster-wide restart storm that turns a small transient problem into a major outage, and the restarts don’t even help because the dependency, not the app, is the problem. Whereas failing readiness on the dependency blip correctly takes the affected instances out of rotation temporarily and puts them back when the dependency recovers, with no restarts. So in a .NET app I implement separate endpoints: a liveness check that only verifies the app is alive and responsive (self-check), and a readiness check that verifies it and its required dependencies are ready to serve. ASP.NET Core’s health-checks framework makes this straightforward with tagged checks mapped to separate/health/liveand/health/readyendpoints. The key insight to convey is that liveness governs restart decisions (so check only the app), readiness governs traffic-routing decisions (so check dependencies too), and getting this separation wrong - especially putting shared-dependency checks in liveness - is a common, dangerous mistake that turns transient issues into restart storms and outages.

Principal Engineer Question

What must a .NET service do to be a good Kubernetes citizen and support zero-downtime deployments?

To run well on Kubernetes and support zero-downtime deployments, a .NET service must be designed to let the orchestrator manage its lifecycle correctly, which comes down to several properties working together. First, statelessness: the service holds no per-request state in instance memory, pushing session/state to a shared store (Redis) or the client, so any instance can serve any request and Kubernetes can freely add, remove, and replace pods during scaling and deployments without losing state - this is the foundation of both horizontal scaling and rolling updates. Second, correct health probes: separate liveness (app-only, governs restarts) and readiness (includes dependencies and warm-up, governs traffic routing) endpoints, so Kubernetes only routes traffic to a pod once it reports ready and only restarts genuinely wedged pods - readiness is what makes rolling updates zero-downtime, because a new pod receives traffic only after it’s ready, and old pods keep serving until the new ones are up. Third, graceful shutdown: when Kubernetes sends the termination signal (SIGTERM) during a rolling update or scale-down, the app must stop accepting new requests, finish in-flight requests, and release resources within the termination grace period, so no in-flight request is dropped - in ASP.NET Core this means handling the shutdown signal (the host does this, and I hookApplicationStoppingto drain work), and pairing it with a readiness change and a preStop hook/ delay so the pod is taken out of the load balancer before it stops. Fourth, resource requests and limits set correctly: requests so the scheduler places pods well and the HPA scales on a sound basis, and limits so a pod can’t starve neighbors (and so the platform can pack efficiently) - without these, scheduling and autoscaling misbehave and one pod can destabilize a node. Fifth, externalized configuration and secrets (from environment, ConfigMaps, and a vault) so the same image runs across environments and deployments don’t require rebuilds. Sixth, fast startup (ReadyToRun/AOT where it helps) so new pods come up quickly for autoscaling and rolling updates. And I complement the app properties with deployment configuration: rolling update strategy with sensible surge/unavailable settings, Pod Disruption Budgets so voluntary disruptions (node upgrades, scale-down) never take too many replicas at once, and multiple replicas across zones. So being a good Kubernetes citizen and achieving zero-downtime deploys requires: stateless design, separate liveness/readiness probes, graceful shutdown that drains in-flight work, correct resource requests/limits, externalized config/ secrets, fast startup, and the right deployment settings (rolling updates, PDBs, multi-replica). Together these let Kubernetes route, scale, heal, and update the service without dropping requests or losing state - whereas a stateful, config-baked service with no probes and no graceful shutdown will drop requests on every deploy and fight the orchestrator. The principle is that the app and the platform cooperate: the app exposes the signals and behaviors (health, graceful drain, statelessness) that let Kubernetes’ rolling-update and self-healing mechanisms do their job with zero downtime.

4.5 Enterprise Banking Scenario

A bank deploys ASP.NET Core microservices on Kubernetes with separate liveness and readiness probes (so a transient database blip takes an instance out of rotation without restart storms), externalized configuration and vault-backed secrets, tuned resource requests/limits, and graceful shutdown that drains in-flight requests. Rolling updates gated by readiness give zero-downtime deploys, HPA and KEDA scale the services and their message-consuming Worker Services on load and queue depth, and Pod Disruption Budgets keep upgrades non-disruptive.

Chapter Revision Notes

  • A .NET service must be stateless, externally configured, health-probed, and gracefully shutting down.
  • Separate liveness (restart) from readiness (route traffic) probes - conflating them causes restart storms.
  • Set resource requests (scheduling/HPA basis) and limits (hard caps) correctly.
  • Scale with HPA/KEDA (pods) and Cluster Autoscaler (nodes); run Worker Services for background work.
  • Zero-downtime deploys come from readiness gating, graceful drain, PDBs, and multi-replica rollout.