Docker, Kubernetes and Azure without the cargo cult

Most teams running Kubernetes do not need it, and most teams who do need it are using about eight of its features. Here is the small set that actually decides whether your deploys are safe.

What a container actually is

A container is not a small virtual machine. It is a normal process on the host kernel, with its view of the world restricted — its own filesystem, network and process tree, and a ceiling on how much CPU and memory it may use.

Which explains several things that confuse people the first time:

  • There is no boot. A container starts as fast as a process starts, because that is all it is.
  • The kernel is shared. Windows containers need a Windows host; Linux containers need a Linux one. There is no magic layer in between.
  • PID 1 is your process, which means signal handling is yours. A process that ignores a termination signal gets killed hard after the grace period, mid-request.
  • The filesystem vanishes when the container does. Anything written inside it and not on a mounted volume is gone.

Images: smaller, and not running as root

An image is a stack of read-only layers. Each instruction in the build adds one, and layers are cached by content — which is the whole basis of a fast build.

# Build in one image, ship from a smaller one.
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src

# Restore before copying the source: dependencies change far less
# often than code, so this layer stays cached across most builds.
COPY *.csproj .
RUN dotnet restore

COPY . .
RUN dotnet publish -c Release -o /app

# The runtime image carries no compiler and no source.
FROM mcr.microsoft.com/dotnet/aspnet:10.0
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
ENTRYPOINT ["dotnet", "MyApi.dll"]

Three habits that pay for themselves:

  • Copy the dependency manifest before the source. It is the single biggest build speed win and the one most often missed.
  • Multi-stage, always. The build tools are an attack surface you do not need to ship and do not want to pay to store.
  • Do not run as root. It costs one line. A container escape from a root process is a much worse day than one from an unprivileged one.
Never bake secrets into an image A layer is permanent. Deleting a file in a later layer does not remove it from the earlier one — it is still in the image, readable by anyone who pulls it. Secrets come from the environment or a vault at run time, not from the build.

The Kubernetes objects you need

Kubernetes has a large API. A normal web service needs five objects, and you can go a long way before needing a sixth.

Ingress Service Deployment replicas: 3 Pod your container Pod your container Pod your container creates and replaces ConfigMap Secret host and path rules, TLS one stable address, load balances
Ingress is the front door, Service is the stable address, Deployment keeps the right number of Pods alive.

The mental model worth holding: Pods are cattle. They are created, killed and replaced constantly — by a deploy, by a node being drained, by an autoscaler. Anything your app keeps in memory and cares about will be lost, without warning, at an inconvenient moment. Sessions belong in a shared cache; files belong in object storage.

Probes: the two that are not the same

This is the single most common misconfiguration I see, and it causes outages that look like everything else.

Readiness Liveness
Question Can I serve traffic right now? Am I broken beyond recovery?
On failure Removed from the load balancer; keeps running Killed and restarted
Should check Dependencies needed to serve, warm-up complete Almost nothing — just that the process responds
The outage this causes Put a database check in your liveness probe. The database has a slow minute. Every pod fails liveness and is killed. They restart, find the database still slow, and are killed again — now you have a crash loop and zero capacity during a blip you would otherwise have survived. A dependency check belongs in readiness: stop taking traffic, stay alive, recover.
readinessProbe:
  httpGet: { path: /health/ready, port: 8080 }
  periodSeconds: 5
livenessProbe:
  httpGet: { path: /health/live, port: 8080 }   # returns 200 if the process is up
  periodSeconds: 20
  failureThreshold: 3
startupProbe:
  httpGet: { path: /health/live, port: 8080 }   # slow start without a loose liveness probe
  failureThreshold: 30
  periodSeconds: 5

Requests and limits

Two numbers with very different consequences. Requests are what the scheduler reserves — they decide where a pod lands. Limits are the ceiling, and the two resources behave completely differently at that ceiling:

  • Over the CPU limit: throttled. The app gets slow. Nothing crashes, nothing logs an error, and the symptom looks like a performance problem rather than a configuration one.
  • Over the memory limit: killed. Immediately, with no grace period and no chance to finish the current request. You see the exit code and nothing else.
Set memory requests and limits equal; be generous with CPU Memory is incompressible — there is no "slightly less memory", only enough or dead — so predictable is better than optimistic. CPU is compressible, so a tight limit mostly buys you mysterious latency. A good default is to set the CPU request from observed usage and leave the CPU limit high or unset.

Deploying without dropping requests

A rolling update replaces pods gradually, which only works if the app participates. The sequence when a pod is told to stop:

terminating deploy starts out of rotation no new requests SIGTERM your app must listen drain finish in-flight exit removal from the load balancer is not instant — a short preStop sleep covers the gap
Dropped requests during deploys are nearly always one of two things: the app ignoring SIGTERM, or exiting before the load balancer stopped sending it traffic.

The fixes are small and specific:

  • Handle SIGTERM and stop accepting new work while finishing what is in flight. Most modern web frameworks do this if you let them shut down gracefully instead of calling Environment.Exit.
  • Add a short preStop sleep — a few seconds. Endpoint removal propagates asynchronously, so without it a proxy can still send you a request after you have begun shutting down.
  • Set a termination grace period longer than your slowest reasonable request.
  • Add a PodDisruptionBudget so voluntary disruptions — node upgrades, scale-downs — cannot take every replica at once.

Choosing an Azure service

The main skill here is not using Kubernetes. Pick the least capable service that does the job, and move up only when you hit a wall you can name.

Service Good for You give up
App Service A web app or API with predictable shape. Deploy slots, autoscale and TLS are built in, and there is no cluster to run. Fine-grained control over networking and scheduling.
Container Apps Containers and background workers without a cluster. Scales on HTTP traffic or queue depth, including to zero. This is the sweet spot for most microservice work. Direct access to the Kubernetes API and custom operators.
Functions Event-driven, short-lived, bursty work. Pay for execution rather than uptime. Long-running processes, and a cold-start tax on latency-sensitive paths.
AKS Many services, a platform team, real need for custom networking, operators or node-level control. Weekends. A cluster is a product you now own, upgrade and secure.
Two things to set up before the first deploy, on any of them Managed identity instead of connection strings — the platform hands your app a token for the database and the storage account, and there is no secret to leak or rotate. And infrastructure as code from day one; the resource created by hand at 11pm is the one nobody can recreate when the region has a bad day.

The thread running through all of this: containers made deployment boring, and the orchestration layer exists to keep it boring. Every feature you adopt is a thing you maintain. The teams I have seen running comfortably use a small, well-understood subset and spend their attention on the application instead.

← All articles