Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 123/ 160
07. Deployment

Kubernetes Basics

Pods, deployments, services, and config maps for production AI workloads.

IntermediatePods · Deployments3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Kubernetes keeps the right number of copies of your app running across many machines, restarts them if they fail and updates them without downtime.

Think of it like…

An airport operations centre: you state the flight schedule you want, and controllers keep assigning gates, replacing delayed aircraft and routing passengers (traffic) so the schedule holds, without anyone manually steering each plane.

Where you’ve seen it

An LLM gateway runs as a Deployment of 3 replicas, each requesting 1 CPU and 2 GiB of memory with limits of 2 CPUs and 4 GiB. A node with 8 CPUs has about 7.5 allocatable after system pods, so it fits 7 such pods by CPU request. The rolling update settings maxSurge: 1 and maxUnavailable: 0 mean Kubernetes starts a fourth pod with the new image, waits for its readiness probe to pass, then terminates an old one, repeating until all three are new: capacity never drops below 3 ready pods. A startup probe allows up to 30 failures × 10 s = 300 s for a pod that loads a model; the readiness probe gates traffic; the liveness probe restarts a stuck process. Configuration lives in a ConfigMap (model IDs, timeouts) and provider keys in a Secret mounted as environment variables (better: an external secrets operator). A separate GPU Deployment requests nvidia.com/gpu: 1 per pod and uses a node selector and toleration so it lands only on GPU nodes; a HorizontalPodAutoscaler scales the gateway on CPU and a KEDA scaler on queue depth for workers. A Service gives the gateway a stable name, and an Ingress terminates TLS and routes /v1/* to it.

Words you’ll hear

Pod
The smallest schedulable unit: one or more containers sharing network and storage.
Deployment
Declares replicas and manages rolling updates and rollbacks.
Service / Ingress
Stable internal load-balanced endpoint, and external HTTP routing.
Requests and limits
Guaranteed and maximum resources; requests drive scheduling.

Visual diagram

Loading interactive diagram

Theory & deep dive

Core objects and how they fit together

A Pod wraps containers that share a network namespace and volumes; pods are ephemeral and get new IPs when recreated, so you don't address them directly. A Deployment declares the pod template and replica count, creates a ReplicaSet and performs rolling updates: with 3 replicas, maxSurge: 1 and maxUnavailable: 0, a new pod must become ready before an old one is removed, keeping full capacity throughout. kubectl rollout undo returns to the previous revision. A Service (ClusterIP by default) selects pods by label and load-balances across ready ones, giving a stable name such as gateway.default.svc. Ingress (or the newer Gateway API) routes external HTTP(S) traffic by host and path to Services; a LoadBalancer Service exposes a cloud load balancer directly.

Configuration: a ConfigMap holds non-secret settings, a Secret holds credentials (base64-encoded, not encrypted by default; enable encryption at rest and prefer an external secrets manager). Resources: requests are what the scheduler reserves (a node with 7.5 allocatable CPUs fits 7 pods requesting 1 CPU); limits cap usage (exceeding the memory limit gets the container OOM-killed; CPU is throttled). Probes: the startup probe protects slow starters (300 s for a model load), readiness controls whether the pod receives traffic, liveness restarts a hung container; badly tuned liveness probes cause restart loops. Use namespaces, RBAC, NetworkPolicies and PodDisruptionBudgets to organise and protect workloads.

Capacity maths

Node: 8 CPUs, 7.5 allocatable. Pod request 1 CPU → 7 pods by CPU (7 × 1 = 7 ≤ 7.5), memory 7 × 2 GiB = 14 GiB must also fit. A rolling update of 3 replicas needs room for a fourth temporary pod: 4 CPUs of requests.

Key takeaway: Deployments manage replicas and rollouts, Services provide stable endpoints, and requests plus probes decide placement and traffic.

AI-specific concerns: GPUs, config and scaling

GPUs: install the NVIDIA device plugin (or GPU Operator) so nodes advertise nvidia.com/gpu; request whole GPUs (limits: nvidia.com/gpu: 1; fractional sharing needs MIG or time-slicing); label GPU nodes and use nodeSelector/affinity and taints and tolerations so only GPU workloads land on expensive nodes and CPU pods stay off them. Model loading is slow: use a startup probe, a persistent volume or a node-local cache for weights and pre-pull images with a DaemonSet. Spot or preemptible GPU nodes cut cost but need graceful handling of eviction (terminationGracePeriodSeconds, draining in-flight requests).

Autoscaling: the Horizontal Pod Autoscaler scales on CPU or custom metrics (desired = ⌈current × metric / target⌉: 4 replicas at 90% CPU against a 60% target need ⌈4 × 0.9 / 0.6⌉ = 6); KEDA scales on queue depth or request rate (including to zero for workers); the cluster autoscaler or Karpenter adds nodes when pods are pending. Remember scale-up lag (image pull, model load) and keep minimum replicas and headroom accordingly. For batch work use Jobs and CronJobs; for training or embedding backfills consider Kueue or Argo Workflows. Operate with GitOps (Argo CD or Flux), Helm or Kustomize for templating and observability (metrics, logs, traces) for every workload, and keep clusters upgraded: managed Kubernetes versions have limited support windows.

HPA formula

4 replicas at 90% average CPU, target 60%: desired = ceil(4 × 90 / 60) = ceil(6.0) = 6 replicas. With model load taking 90 s, the HPA should react on a leading metric such as queue depth, not only CPU.

Key takeaway: Request whole GPUs with taints and node selectors, tune probes for slow model loads and scale on leading metrics with headroom.

How it works, step by step

Step 1 of 4
Engineer

1. Containerise and define a Deployment

Write the pod template with image, resource requests and limits, env from ConfigMap and Secret, and replica count.

3 replicas · 1 CPU / 2 GiB requests
Key numbers at this step
Pin
image tag by SHA

When to use it & trade-offs

What it is

Kubernetes (K8s) is a container orchestrator: you declare the desired state (run 3 replicas of this image with these resources, expose them on this port) and controllers continuously reconcile reality to match it. The core objects for AI workloads are Pods (one or more containers scheduled together), Deployments (manage replica sets and rolling updates), Services (a stable virtual IP and DNS name that load-balances across pods), Ingress or Gateway (HTTP routing from outside), ConfigMaps and Secrets (configuration and credentials), resource requests and limits (CPU, memory, and nvidia.com/gpu for GPUs) and probes (liveness, readiness, startup). Higher-level add-ons provide autoscaling (HPA, KEDA) and jobs for batch and training workloads.

Why it matters: Production AI systems need replicas for availability, rollouts without downtime, automatic restarts when a process dies, scaling with load and a way to place GPU workloads on the right nodes. Kubernetes provides these primitives, and managed offerings (EKS, GKE, AKS) remove much of the cluster operations burden. But it adds real complexity, so it pays off when you have multiple services, teams and scaling needs; for a single small service, a managed container platform may be simpler.

When to reach for it

  • Production LLM gateways, workers and inference servers
  • Rolling updates with automatic rollback
  • Scheduling GPU workloads

When to avoid or be careful

  • Using Kubernetes for one tiny service with no scaling needs
  • Omitting resource requests, causing noisy-neighbour problems
  • Liveness probes that kill pods during slow model loads

Running containers in production

Kubernetes (managed)
Best for: Multiple services, teams and GPU workloads

Strengths: Rich ecosystem; rollouts; scaling; portability.

Watch out: Complexity and operational overhead.

Managed container service (Cloud Run, ECS Fargate)
Best for: Simple HTTP services without cluster management

Strengths: Little operations; autoscaling built in.

Watch out: Less control; GPU and networking limits.

Single VM with Compose
Best for: Small internal tools

Strengths: Simple and cheap.

Watch out: No self-healing or rolling updates across nodes.

Interview signals & numbers to know

When this topic comes up

  • “Roll out without downtime”
  • Pods, Deployments and Services
  • Resource requests and limits
  • GPU node taints
  • HPA and KEDA

Benchmarks & rules of thumb

maxSurge 1 / maxUnavailable 0 -
zero-downtime rolling update settings
⌈current × metric / target⌉ -
HPA replica formula
300 s -
startup probe budget in the example

Common mistakes & follow-ups

Common interview mistakes

  • Setting only limits and no requests, so scheduling is unpredictable.
  • Treating Secrets as encrypted by default.
  • Scaling on CPU for I/O-bound LLM gateways.

Likely interviewer follow-ups

  • How does a rolling update avoid downtime?
  • How would you schedule a GPU inference workload?
  • What is the difference between readiness and liveness probes?
Interview tip: Explain rolling-update parameters and the three probes. They show you understand how Kubernetes protects traffic during change.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Kubernetes Basics with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Docker Compose
Next topic
CI/CD Pipelines