Skip to main content

08. Programming & Technologies

Docker and Kubernetes for Agent Deployment

Docker packages the agent service into an immutable image; Kubernetes (or managed container platforms) runs replicas with health checks, rollouts, secrets, and resource limits. Agents need stream-aware timeouts, hard CPU/memory caps, and never bake API keys into images β€” the same app you built in Python/TS deploys as a containerized API.

What you will learn

  • Explain Docker images vs containers for agent APIs
  • Sketch Ingress β†’ Service β†’ Deployment for an agent
  • Configure probes, resources, and timeouts for long agent runs
  • List secret-handling and hardening rules for production
  • Answer interview questions with short, solid answers
Docker image container and Kubernetes orchestration
Interview staple: Docker builds the artifact; Kubernetes runs and heals it.

Definition

Docker builds a container image: your agent API, runtime, and dependencies frozen as layers. A container is a running instance of that image. Kubernetes orchestrates many containers across a cluster β€” Deployments, Services, Ingress, ConfigMaps/Secrets, probes, and autoscaling.

You do not have to self-manage raw Kubernetes: ECS, Cloud Run, Container Apps, and similar platforms apply the same packaging ideas. Learn Docker first; K8s patterns transfer.

Introduction

This lesson (#9 in Programming & Technologies) follows Cloud Platforms. Cloud is where you run; Docker/K8s is how you package and operate the agent workload.

Next: Logging, Tracing and Observability β€” what you must emit from those containers.

Why containers for agents

  • Reproducible deps (Python/Node + native libs) across environments
  • Immutable rollbacks when a prompt/model pin misbehaves
  • Horizontal scale of the agent API under load
  • Isolation of tool side effects from the host
  • Fits cloud lessons: same image to AWS/Azure/GCP runtimes

Docker essentials

  • Dockerfile: slim base, non-root user, multi-stage builds
  • Pin base image digests in production when possible
  • COPY only what the service needs β€” smaller = safer/faster
  • Do not COPY .env or API keys into layers
  • Health endpoint in the app (e.g. /healthz) for orchestrators
  • Tag images with git SHA / semver β€” never only latest in prod

Kubernetes shape for an agent API

A typical agent service looks like any HTTP API β€” with longer timeouts.

Ingress Service Deployment pods and Secrets
Mount secrets as env or files; rotate without rebuilding images.
  • Deployment - N replicas of the agent container
  • Service - stable ClusterIP/DNS to those pods
  • Ingress/Gateway - TLS, auth, and external routing
  • Secret / external secret store - model and tool credentials
  • ConfigMap - non-secret config (model names, max steps)
  • HPA - scale on CPU/RPS carefully (LLM backends have their own limits)

Probes, resources, timeouts

Agent requests can run longer than typical CRUD APIs β€” configure explicitly.

Health resources timeouts and hardening checklist
Liveness should not call the LLM. Readiness can check dependency connectivity.
  • Liveness - process alive (cheap); avoid tool/LLM calls
  • Readiness - safe to receive traffic (deps up)
  • requests/limits for CPU and memory β€” prevent noisy neighbors
  • Gateway/idle timeouts sized for streaming agent responses
  • App-level max steps / wall-clock budget still required
  • Graceful shutdown: finish or cancel in-flight runs on SIGTERM

Workers and long jobs

  • Sync HTTP for interactive chat; queues for long batch agents
  • Separate Deployments for API vs workers if tools are heavy
  • Jobs/CronJobs for scheduled crews / evals
  • Do not hold Kubernetes liveness hostage to a 10-minute tool chain
  • Persist run state (SQL) so pods can die without losing the world

Security hardening

  • Run as non-root; read-only root filesystem when feasible
  • Drop Linux capabilities; no privileged containers
  • NetworkPolicies - agent pods only reach allowed tools/DBs
  • Image scanning in CI; block critical CVEs
  • RBAC least privilege for deploy service accounts
  • Never log secrets; redact prompts in shared clusters if required

Map to earlier modules

  • Image wraps FastAPI/Next route handlers from language lessons
  • Cloud lesson = managed K8s/container services hosting these images
  • DB/vector/HTTP tools are network dependencies β€” configure DNS/egress
  • Framework checkpoints need durable storage outside the container FS
  • Next lesson: emit structured logs/traces from every pod

Failure modes

  • Secrets baked into image layers (still in history after β€œdelete”)
  • No resource limits β€” one runaway agent OOMs the node
  • Liveness probe hitting the LLM β†’ flapping pods under provider lag
  • Default 60s ingress timeout killing valid streamed answers
  • Scaling pods 10x without provider rate-limit headroom

Program / implementation notes

Containerize the agent API with a /healthz, multi-stage Dockerfile, and env-based secrets. Deploy 2 replicas with readiness/liveness, CPU/memory limits, and an ingress timeout that matches your stream budget.

  • Prefer managed Kubernetes or Cloud Run-style platforms before raw clusters
  • Next: Logging, Tracing and Observability

Advantages and limitations

  • Advantage: reproducible deploys and fast rollback
  • Advantage: horizontal scale and self-healing pods
  • Advantage: portable artifact across clouds
  • Limitation: K8s complexity β€” use managed platforms when possible
  • Limitation: containers do not replace app-level agent budgets
  • Limitation: stateful agent memory must live outside local disk

Key points

  • Docker image = immutable agent service artifact.
  • Kubernetes runs replicas with Service/Ingress and rollouts.
  • Probes must be cheap; timeouts must allow streaming.
  • Set CPU/memory limits; scale with provider quotas in mind.
  • Mount secrets β€” never bake keys into images.
  • Persist run state outside the container filesystem.

FAQs

Do I need Kubernetes to deploy agents?

No. Docker images can run on Cloud Run, ECS, Container Apps, or a single VM. Kubernetes helps when you need cluster features at scale.

One container or many?

Start with one API container. Split workers when long tool jobs block interactive chat or need different scaling.

Where do model API keys live?

In a secret store mounted at runtime (K8s Secrets, AWS SM, etc.) β€” never in the Dockerfile or git.

How do you roll out a bad prompt change?

Images/config are versioned; roll back the Deployment/revision and keep model pins in config so rollback is deterministic.

Interview questions

  1. How do you deploy an agent with Docker?

    Answer Build a versioned image of the agent API (slim base, non-root, no secrets), push to a registry, and run it with env-injected credentials and a health endpoint.

  2. What Kubernetes objects does a typical agent API use?

    Answer A Deployment for pods, a Service for networking, Ingress/Gateway for TLS entry, and Secrets/ConfigMaps for credentials and non-secret config.

  3. How should probes work for LLM agents?

    Answer Liveness checks process health cheaply; readiness may check local deps. Neither should call the LLM or long tools on every probe.

  4. Why set resource limits on agent pods?

    Answer Agent loops and token buffers can spike memory/CPU; limits protect the node and make scheduling predictable.

  5. How do timeouts differ for agents?

    Answer Streaming and multi-tool runs need longer gateway/idle timeouts plus app-level max steps β€” default 60s APIs often cut valid work.

  6. Docker vs Kubernetes in one line?

    Answer Docker packages the agent; Kubernetes (or a container platform) schedules, heals, and scales those packages.

Related topics

Practice task

Complete each numbered practice step in a notebook or doc before opening the next lesson.

  1. Write a Dockerfile outline for a FastAPI agent (base image, non-root, health, no secrets).
  2. Sketch K8s resources: Deployment, Service, Ingress, Secret for that API.
  3. Define liveness vs readiness behavior in one sentence each.
  4. List timeout settings you would change for a streamed chat agent.
  5. Answer interview questions 1, 2, and 3 in two sentences each.