Running AI Agents on Kubernetes: A Practical Guide for Scalable and Reliable Deployments
Published: July 18, 2026 • 15 min read
Introduction
AI agents have moved from experimental prototypes to production workloads powering customer support, code generation, data analysis, and autonomous workflows. As these agents grow in complexity and traffic, engineering teams face a critical decision: how to deploy and operate them reliably at scale.
Kubernetes has become the de facto standard for container orchestration, but it's not a magic solution. This guide walks through the practical realities of running AI agents on Kubernetes—covering architecture patterns, scaling strategies, state management, and cost optimization—so you can make informed decisions for your specific use case.
Key insight: Kubernetes orchestrates containers, not AI models directly. Your AI agents are applications that happen to call LLMs—they containerize and deploy like any other microservice.
What Are AI Agents?
AI agents are software systems that use large language models (LLMs) to reason, plan, and execute tasks autonomously. Unlike simple chatbots that respond to single prompts, agents can:
- Break complex goals into multi-step plans
- Call external tools and APIs
- Maintain context across long conversations
- Collaborate with other agents in multi-agent systems
- Persist state and learn from previous executions
Popular frameworks for building agents include LangGraph (stateful multi-actor workflows), CrewAI (role-based agent teams), AutoGen (conversational agents), Semantic Kernel (enterprise orchestration), OpenAI Agents SDK (native OpenAI tooling), and Model Context Protocol (MCP) (standardized tool integration).
These frameworks share a common deployment reality: they run as long-lived processes that make HTTP calls to LLM providers, query vector databases, cache in Redis, persist to PostgreSQL, and coordinate through message queues like Kafka or RabbitMQ.
Why Run AI Agents on Kubernetes?
Kubernetes solves several operational challenges that emerge when AI agents move to production:
- Automatic restarts when agent processes crash or OOM
- Horizontal scaling based on queue depth, CPU, or custom metrics
- Rolling updates for zero-downtime deployments
- Resource isolation preventing noisy neighbors
- Service discovery for agent-to-agent communication
- GPU scheduling for local model inference
However, Kubernetes adds operational complexity. Teams without dedicated platform engineers should weigh this cost against managed alternatives.
When Kubernetes Is the Right (and Wrong) Choice
Kubernetes makes sense when:
- You run multiple agent services with interdependencies
- Traffic patterns are unpredictable and require auto-scaling
- You need high availability across zones
- Your team has or can build Kubernetes expertise
- Compliance requires infrastructure control
Consider simpler options when:
- Solo developer or small team with one or two agent services
- Predictable, low traffic (Docker Compose on a VM works fine)
- Event-driven workloads with sporadic bursts (serverless functions)
- Rapid prototyping where infrastructure delays iteration
- No dedicated DevOps capacity
Practical rule: If you cannot explain why you need Kubernetes in one sentence, you probably don't need it yet.
Architecture Overview
A typical AI agent deployment on Kubernetes follows a microservices pattern:
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Ingress │────▶│ API Gateway │────▶│ Agent Service│
│ Controller │ │ (Kong/ │ │ (FastAPI/ │
│ (NGINX) │ │ Traefik) │ │ LangGraph) │
└─────────────┘ └──────────────┘ └──────┬──────┘
│
┌─────────────┐ ┌──────────────┐ │
│ Vector DB │◀───▶│ Redis Cache │◀───┤
│ (Pinecone/ │ │ (Sessions, │ │
│ Weaviate) │ │ Rate Limit)│ │
└─────────────┘ └──────────────┘ │
▼
┌─────────────┐ ┌──────────────┐ ┌────────┐
│ PostgreSQL │◀───▶│ Message Queue│▶▶│ LLM │
│ (State, │ │ (Kafka/ │ │ Provider│
│ History) │ │ RabbitMQ) │ │ (API) │
└─────────────┘ └──────────────┘ └────────┘
Each component runs in its own Deployment with appropriate scaling policies. The agent service is stateless—session state lives in Redis, conversation history in PostgreSQL, embeddings in the vector database.
Deployment Options Comparison
| Factor | Docker Compose | Kubernetes | Serverless (AWS Lambda, Cloud Run) | Virtual Machines | Managed AI Platforms (Vertex AI, Bedrock) |
|---|---|---|---|---|---|
| Scalability | Manual, single host | Horizontal, multi-node | Automatic, per-request | Vertical, manual | Platform-managed |
| High Availability | Single point of failure | Multi-zone, self-healing | Built-in | Requires manual setup | Built-in |
| Cost | Low (single server) | Medium-High (cluster + ops) | Pay-per-use | Fixed (reserved instances) | Premium (per-request) |
| Complexity | Low | High | Low | Medium | Low |
| Auto-scaling | None | HPA, VPA, KEDA | Native | Manual/ASG | Platform-managed |
| Best Use Cases | Dev, small apps, CI/CD | Production, multi-service, HA | Sporadic, event-driven | Legacy, GPU workloads | Quick MVPs, no ops team |
| Operational Overhead | Minimal | Significant | Minimal | Medium | Minimal |
Core Kubernetes Components for AI Agents
Pods
Pods are the smallest deployable units. An AI agent pod typically runs the agent process (FastAPI + LangGraph), a sidecar for log shipping, and optionally a local model server. Pods are ephemeral—any data written to the pod filesystem disappears on restart.
Deployments
Deployments manage replica sets and enable rolling updates. For agent services, configure maxSurge: 25% and maxUnavailable: 0 to maintain capacity during deployments.
Services
ClusterIP services expose agent endpoints internally. Use headless services (clusterIP: None) when agents need direct pod-to-pod communication for distributed workflows.
ConfigMaps and Secrets
Store non-sensitive configuration (model names, timeouts, feature flags) in ConfigMaps. Never put API keys in ConfigMaps—use Secrets for LLM provider keys, database passwords, and encryption keys. Mount secrets as files, not environment variables, to avoid accidental logging.
Ingress
Ingress controllers (NGINX, Traefik, Kong) handle TLS termination, rate limiting, and routing. Configure path-based routing to separate agent APIs from admin endpoints.
Persistent Volumes
Use PersistentVolumeClaims for any data that must survive pod restarts: PostgreSQL data directories, Redis AOF files, local model weights. AI agents are often stateful in their workflows, but Kubernetes Pods are designed to be ephemeral. Plan for external state storage from the beginning.
Jobs and CronJobs
Jobs handle one-off tasks like model fine-tuning, batch embedding generation, or database migrations. CronJobs schedule recurring work: daily conversation summarization, weekly model evaluation, monthly cost reports.
Horizontal Pod Autoscaler (HPA)
HPA scales replicas based on CPU, memory, or custom metrics. For AI agents, custom metrics like queue depth (RabbitMQ/Kafka lag) or concurrent sessions often trigger scaling more accurately than CPU.
Building an AI Agent Architecture on Kubernetes
API Gateway
Deploy Kong, Traefik, or NGINX Ingress Controller as the entry point. Configure:
- Authentication (JWT, API keys)
- Rate limiting per tenant
- Request/response transformation
- Canary routing for safe rollouts
Agent Service
Containerize your agent application (Python + FastAPI + LangGraph/CrewAI/AutoGen). Key Dockerfile practices:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV PYTHONUNBUFFERED=1
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Set resource requests/limits in the Deployment:
resources:
requests:
memory: "512Mi"
cpu: "250m"
limits:
memory: "2Gi"
cpu: "1000m"
LLM Provider Integration
Agents call external LLM APIs (OpenAI, Anthropic) or self-hosted models (vLLM, Ollama, TGI). For external APIs, implement:
- Retry logic with exponential backoff
- Circuit breakers to prevent cascade failures
- Token usage tracking for cost monitoring
- Fallback models for resilience
Vector Database
Deploy Pinecone, Weaviate, Qdrant, or Milvus for RAG workloads. On Kubernetes, use Operators where available (Weaviate Operator, Qdrant Operator) for automated backups, scaling, and upgrades.
Redis Cache
Redis handles session state, rate limiting, and conversation context. Deploy as a StatefulSet with persistence enabled. Use Redis Cluster for high availability at scale.
PostgreSQL
Store conversation history, agent configurations, and audit logs. Use CloudNativePG or Zalando Postgres Operator for automated failover, backups, and connection pooling (PgBouncer).
Message Queue
Kafka (Strimzi Operator) or RabbitMQ (RabbitMQ Operator) for async agent communication, task queues, and event sourcing. Horizontal scaling often requires shared memory stores or queues—design for this from day one.
Monitoring
Deploy Prometheus Operator for metrics, Grafana for dashboards, and Loki for logs. Key metrics: request latency, token usage, queue depth, error rates, active sessions.
Scaling AI Agents
AI agents have unique scaling characteristics: they're I/O-bound (waiting on LLM APIs), memory-intensive (context windows), and often stateful (conversation context).
Horizontal Pod Autoscaler Configuration
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: agent-service-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: agent-service
minReplicas: 3
maxReplicas: 50
metrics:
- type: External
external:
metric:
name: kafka_consumergroup_lag
target:
type: AverageValue
averageValue: "10"
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
KEDA for Event-Driven Scaling
KEDA (Kubernetes Event-driven Autoscaling) scales based on Kafka lag, RabbitMQ queue depth, HTTP requests, or custom Prometheus metrics. It's more responsive than standard HPA for bursty agent workloads.
Scaling Considerations
- Session affinity: Use Redis for shared session state so any pod can handle any request
- Connection pooling: Configure database and Redis connection pools per pod to avoid exhaustion
- LLM rate limits: Scale agent replicas within provider rate limits; implement client-side queuing
- Cold starts: Pre-warm pods with minimum replicas; use
preStophooks for graceful drain
Managing Long-Running Workflows
Agent workflows can run for minutes or hours (code generation, research, multi-step analysis). Kubernetes provides several patterns:
Jobs for Finite Workflows
Wrap each workflow execution in a Job. The Job pod runs to completion, retries on failure, and preserves logs. Use backoffLimit and activeDeadlineSeconds to control retries and timeouts.
Stateful Workflows with LangGraph
LangGraph checkpoints workflow state to PostgreSQL or Redis. On pod restart, the workflow resumes from the last checkpoint. Configure checkpoint frequency based on acceptable rework.
Async Pattern with Message Queues
API accepts request → publishes to queue → returns task ID → worker picks up → updates status in DB → client polls for result. This decouples request latency from execution time.
Webhooks for Completion Notification
Instead of polling, register a webhook URL. On completion, the agent service POSTs results. Handle retries with exponential backoff and dead-letter queues for failed deliveries.
State Management Strategies
AI agents accumulate state: conversation history, tool outputs, intermediate reasoning, user preferences. Kubernetes pod ephemerality means this state must live externally.
Conversation History
Store full message history in PostgreSQL (JSONB for flexibility). Index by session_id and user_id for fast retrieval. Archive old conversations to object storage (S3, GCS) after 90 days.
Working Memory
Redis stores active session context: current plan, tool results, variable bindings. TTL of 24-48 hours balances memory usage with resume capability.
Long-Term Memory
Vector databases store embeddings of past interactions for semantic recall. Partition by user_id or agent_id for isolation.
Checkpointing
For multi-step workflows, checkpoint after each major step. Store checkpoint data (state snapshot, next step) in PostgreSQL. On recovery, load latest checkpoint and continue.
Best practice: Design every agent as stateless compute with external state stores. This enables horizontal scaling, zero-downtime deployments, and crash recovery.
GPU Scheduling (Where Applicable)
Self-hosted models (Llama, Mistral, embedding models) need GPUs. Kubernetes supports GPU scheduling through device plugins.
NVIDIA Device Plugin
Install the NVIDIA device plugin DaemonSet. GPUs become schedulable resources like CPU and memory.
resources:
limits:
nvidia.com/gpu: 1
requests:
nvidia.com/gpu: 1
GPU Sharing
For smaller models, use NVIDIA MIG (Multi-Instance GPU) or time-sharing to run multiple model servers per GPU. vLLM and TGI support concurrent requests efficiently.
Node Pools
Create dedicated GPU node pools with appropriate instance types (g5.xlarge, p3.2xlarge). Use node affinity to schedule GPU workloads only on GPU nodes:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: nvidia.com/gpu
operator: Exists
Cost Optimization
GPU nodes are expensive. Use cluster autoscaler with GPU node groups that scale to zero when no GPU pods are pending. Spot instances work for fault-tolerant batch inference.
Observability and Monitoring
AI agents add new observability dimensions beyond standard microservices.
Key Metrics
- Request latency: P50, P95, P99 per agent type
- Token usage: Input/output tokens per request, per user, per model
- Cost per request: Derived from token usage and model pricing
- Queue depth: Pending workflows, Kafka lag
- Error rates: LLM API errors, tool failures, timeouts
- Active sessions: Concurrent users, memory pressure
Distributed Tracing
Use OpenTelemetry to trace requests across: API Gateway → Agent Service → LLM API → Vector DB → PostgreSQL. Propagate trace context through async queue boundaries.
Structured Logging
Log as JSON with fields: trace_id, span_id, agent_type, session_id, user_id, model, tokens_in, tokens_out, latency_ms, error. Ship to Loki or Elasticsearch.
Alerting
- P99 latency > 30s for 5 minutes
- Error rate > 5% for 2 minutes
- Queue lag > 100 for 10 minutes
- GPU utilization < 20% for 1 hour (cost waste)
- Daily token cost > budget threshold
Security Best Practices
Network Policies
Restrict pod-to-pod communication. Agent services only need egress to LLM APIs, databases, and message queues. Deny all by default, allow explicitly.
Secrets Management
Use External Secrets Operator to sync from AWS Secrets Manager, HashiCorp Vault, or GCP Secret Manager. Never bake secrets into images.
Runtime Security
Deploy Falco or Tetragon for runtime threat detection: unexpected process execution, file access, network connections.
Image Security
- Scan images with Trivy or Grype in CI/CD
- Use distroless or minimal base images
- Sign images with cosign; verify signatures at deploy
- Pin base image digests, not tags
RBAC
Follow least privilege. Service accounts per namespace with minimal permissions. No cluster-admin for application workloads.
Data Protection
Encrypt data in transit (mTLS via Istio/Linkerd) and at rest (storage class encryption). Redact PII from logs before shipping.
Cost Optimization Strategies
Right-Sizing Resources
Use Vertical Pod Autoscaler (VPA) in recommendation mode to find optimal CPU/memory requests. Review monthly and adjust.
Spot Instances for Fault-Tolerant Workloads
Batch embedding generation, model evaluation, and async workflows tolerate interruption. Use spot node pools with spot-instance-termination-notice handlers for graceful shutdown.
Model Selection and Caching
- Route simple queries to smaller/cheaper models
- Cache frequent LLM responses in Redis (semantic caching with embeddings)
- Use function calling to reduce prompt tokens
- Implement prompt compression for long contexts
Cluster Autoscaler
Configure scale-down utilization threshold (default 50%) and scale-down delay (default 10min). Set appropriate pod disruption budgets to prevent aggressive scale-down during traffic.
GPU Utilization
Monitor GPU utilization per pod. Consolidate multiple small models on shared GPUs. Use vLLM continuous batching for higher throughput.
Data Transfer Costs
Deploy in the same region as your LLM provider endpoints. Use VPC endpoints for cloud provider services to avoid NAT gateway charges.
Common Deployment Mistakes
| Mistake | Impact | Fix |
|---|---|---|
| Storing session state in pod memory | Lost on restart; prevents scaling | Use Redis for all session state |
| No resource limits on agent pods | OOM kills; noisy neighbors | Set requests/limits based on load testing |
| Hardcoding LLM API keys in images | Security breach; rotation impossible | External Secrets Operator + Vault |
| Scaling on CPU instead of queue depth | Slow scale-up; LLM-bound workloads | KEDA with Kafka/RabbitMQ metrics |
| Single replica for "simplicity" | Downtime on deploy/restart | Minimum 3 replicas + PDB |
| No graceful shutdown handling | In-flight requests fail | preStop hook + SIGTERM handling |
| Ignoring LLM rate limits | 429 errors; cascade failures | Client-side queue + circuit breaker |
| No pod disruption budgets | Voluntary disruptions cause outages | PDB with minAvailable: 50% |
| Over-provisioning GPU nodes | Wasted spend | Cluster autoscaler + spot instances |
Frequently Asked Questions
Do I need Kubernetes for my AI agent?
Not necessarily. For a single agent with predictable traffic, Docker Compose on a VM or a managed platform (Cloud Run, Fly.io, Railway) is simpler and cheaper. Kubernetes pays off when you have multiple services, need HA, or require fine-grained scaling control.
Can I run local LLMs on Kubernetes?
Yes. Deploy vLLM, TGI, or Ollama as a separate service with GPU resources. Your agent service calls the local model service via Kubernetes DNS (e.g., http://llm-service:8000). This avoids external API costs and latency.
How do I handle streaming responses from LLMs?
Configure your Ingress controller for streaming (disable buffering: proxy_buffering off; for NGINX). Agent services should stream via Server-Sent Events (SSE) or WebSockets. Set appropriate timeout values.
What's the best way to manage agent versions?
Use GitOps (ArgoCD, Flux) with Helm charts or Kustomize. Tag container images with semantic versions. Deploy to staging first, run integration tests, then promote to production with canary analysis.
How do I debug agent issues in production?
Structured logging with trace IDs, distributed tracing (Jaeger/Tempo), and ephemeral debug pods (kubectl debug) for live inspection. Replay failed requests in staging with production data (sanitized).
Should I use a service mesh?
For 5+ services with complex mTLS, traffic splitting, and observability needs, Istio or Linkerd adds value. For simpler deployments, the operational cost outweighs benefits. Start without; adopt when pain points justify it.
Final Verdict
Kubernetes is a powerful platform for running AI agents at scale, but it's not a default choice. The decision should be driven by:
- Team capacity: Do you have platform engineering expertise?
- Traffic patterns: Unpredictable, bursty traffic benefits most from K8s auto-scaling
- Service complexity: Multi-agent systems with interdependencies need service discovery and orchestration
- Compliance requirements: Data residency, audit trails, infrastructure control
- Cost sensitivity: Kubernetes has fixed overhead; serverless scales to zero
Start with the simplest deployment that meets your current needs. Migrate to Kubernetes when the pain of not using it exceeds the pain of adopting it. Many successful AI products run on simpler infrastructure for years before needing Kubernetes.
Remember: Kubernetes orchestrates containers, not AI models. Your agents are applications first—design them as stateless, observable, resilient microservices that happen to call LLMs.
Key Takeaways
- AI agents containerize like any microservice—Kubernetes manages the runtime, not the model.
- Externalize all state—Redis for sessions, PostgreSQL for history, vector DB for embeddings.
- Scale on queue depth, not CPU—KEDA with custom metrics matches agent workload patterns.
- Plan for long-running workflows—Jobs, checkpointing, async patterns, webhooks.
- GPU workloads need dedicated node pools—device plugin, MIG/time-sharing, cluster autoscaler.
- Observability requires agent-specific metrics—token usage, cost per request, LLM latency.
- Security starts with network policies and secrets management—never bake keys into images.
- Cost optimization is continuous—right-sizing, spot instances, model routing, caching.
- Kubernetes is a means, not an end—adopt when complexity justifies it, not before.
References
- Kubernetes Documentation
- Cloud Native Computing Foundation (CNCF)
- Docker Documentation
- OpenAI Documentation
- Anthropic Documentation
- LangGraph Documentation
- CrewAI Documentation
- AutoGen Documentation
- FastAPI Documentation
- Redis Documentation
- PostgreSQL Documentation
- Prometheus Documentation
- Grafana Documentation

Comments
0 comments
No comments yet
Start the discussion with a thoughtful note.