Kubernetes for ML Workloads: A Production Guide
Run ML on Kubernetes: GPU scheduling with device plugins, requests and limits, autoscaling, KServe and Ray Serve, training Jobs with Kueue, probes and rollouts.
Kubernetes does not know anything special about machine learning. A training run is a pod that should finish, a model server is a pod that should stay up, and a GPU is an extended resource that a device plugin advertises. Running ML well on Kubernetes is mostly about using the right built-in objects for each of those, then adding a small number of add-ons (a GPU operator, a serving layer, a job queue) where the core APIs stop. This guide walks through each layer using the official Kubernetes, NVIDIA, KServe, Ray and Kueue documentation.
The pieces at a glance
| Need | Kubernetes object or add-on | Where it is documented |
|---|---|---|
| Expose GPUs to pods | Vendor device plugin, or the NVIDIA GPU Operator | kubernetes.io, docs.nvidia.com |
| Reserve CPU, memory, GPU | resources.requests and resources.limits | kubernetes.io |
| Scale model servers | HorizontalPodAutoscaler, or KEDA via KServe | kubernetes.io, kserve.github.io |
| Serve models | KServe InferenceService or Ray Serve RayService | kserve.github.io, docs.ray.io |
| Run training and batch work | Job, queued through Kueue | kubernetes.io, kueue.sigs.k8s.io |
| Health checks | Startup, readiness and liveness probes | kubernetes.io |
| Ship new model versions | Deployment rolling updates, KServe canary | kubernetes.io, kserve.github.io |
Scheduling GPUs
GPU support in Kubernetes has been stable since v1.26 and covers AMD and NVIDIA cards through device plugins. A cluster admin installs the vendor’s driver on each GPU node and runs that vendor’s device plugin. After that the node advertises a resource such as nvidia.com/gpu or amd.com/gpu, and a container asks for it the way it asks for CPU.
GPUs follow stricter rules than CPU and memory. You put them in limits. If you leave out the request, Kubernetes uses the limit as the request; if you set both, they must match; a request with no limit is rejected. You cannot ask for a fraction of a GPU this way.
Mixed fleets need a way to steer pods to the right card. You can label nodes yourself (for example an accelerator label) and use a node selector, or deploy Node Feature Discovery, which detects hardware on each node and publishes it as labels, and can also taint nodes so only pods that need a feature land there. Node affinity rules can then match on those labels, such as GPU memory size.
On NVIDIA hardware, most teams install the NVIDIA GPU Operator rather than wiring each part by hand. It uses the operator pattern to manage the driver, the Kubernetes device plugin, the NVIDIA Container Toolkit, node labelling through GPU Feature Discovery and DCGM-based monitoring.
The operator also handles GPU sharing. With time-slicing, an admin defines several replicas per physical GPU and hands each to a different pod. The pods interleave on the card, but nothing isolates their memory or faults from each other, and asking for two time-sliced replicas does not buy twice the compute. Multi-Instance GPU (MIG) is the alternative on newer NVIDIA GPUs: the card is cut into fixed instances, each with hardware-level memory and fault isolation. Time-slicing fits small dev or low-traffic inference pods; MIG fits tenants that must not affect each other.
Requests and limits
The scheduler places a pod only on a node where the sum of requests fits the node’s capacity, even if actual usage on that node is low at the moment. Requests are therefore your reservation, and the main input to bin-packing expensive GPU nodes.
Limits behave differently for CPU and memory. Going over a CPU limit gets the container throttled. Going over a memory limit can get it killed by the kernel’s OOM handling, though only once the kernel notices memory pressure, so an over-allocating container may run for a while before it dies. For model servers that load large weights into RAM, set the memory request close to real peak usage and expect an OOM kill, not a slowdown, if you get it wrong. If you set only a limit, Kubernetes copies it into the request.
Autoscaling inference
The HorizontalPodAutoscaler changes the replica count of a Deployment or StatefulSet based on observed metrics. Its core rule is a ratio: desired replicas equal current replicas times current metric value over target value, rounded up. CPU and memory utilization are computed against the pod’s requests, so HPA on resource metrics only works if those requests are set. With the autoscaling/v2 API you can also scale on custom or external metrics, which is often a better signal for GPU-bound servers than CPU.
Two HPA behaviours matter for ML. Scale-down uses a stabilization window (five minutes by default) that picks the highest recent recommendation, so replicas drain slowly instead of flapping. And model servers often burn CPU while loading weights; the HPA docs suggest a startup probe that only passes after that spike, or a readiness probe delayed with initialDelaySeconds, so warm-up load does not trigger extra replicas.
If you serve with KServe, you can use KEDA instead. KEDA is an event-driven autoscaler that can scale on Kubernetes resource metrics or external ones. In KServe it works only in raw (Standard) deployment mode and has to be installed separately.
Serving with KServe
KServe is a CNCF incubating project that adds an InferenceService custom resource. You state the model format and where the model lives, and KServe picks a runtime (scikit-learn, XGBoost, TensorFlow, ONNX, Hugging Face, vLLM, Triton and others) and handles routing and autoscaling. Here is the scikit-learn example from KServe’s first InferenceService tutorial:
apiVersion: "serving.kserve.io/v1beta1"
kind: "InferenceService"
metadata:
name: "sklearn-iris"
namespace: kserve-test
spec:
predictor:
model:
modelFormat:
name: sklearn
storageUri: "gs://kfserving-examples/models/sklearn/1.0/model"
resources:
requests:
cpu: "100m"
memory: "512Mi"
limits:
cpu: "1"
memory: "1Gi"
For a GPU model you would add nvidia.com/gpu: 1 under limits, following the GPU rules above. One gotcha from the tutorial: do not create InferenceServices in namespaces labelled as control-plane, because KServe’s webhook skips them and the model download step never runs.
If your models already sit behind a Triton Inference Server or TorchServe setup, KServe can run those as runtimes rather than replacing them.
Serving with Ray Serve
Ray Serve on Kubernetes runs through the KubeRay operator and its RayService resource. One RayService manifest holds both the Ray cluster spec (rayClusterConfig) and the Serve application config (serveConfigV2). KubeRay creates the cluster, deploys the app and creates a Kubernetes Service to send traffic to it.
Updates come in two kinds. Changing only the Serve config is applied in place on the running cluster. Changing the cluster config, such as the container image, makes KubeRay start a new Ray cluster, switch the Service to it once healthy, and then remove the old one. That avoids downtime, but your cluster needs room for both Ray clusters during the switch. Ray’s docs also advise baking heavy dependencies into the image so new pods do not stall on downloads.
Training and batch jobs
A Kubernetes Job runs pods until a set number of them succeed, retrying failures. The fields that matter for training and batch scoring:
backoffLimitcaps retries before the Job is marked failed (default 6).activeDeadlineSecondsstops the whole Job after a time budget, and it wins overbackoffLimit.completionMode: Indexedgives each pod an index from 0 upward, useful for sharding a dataset across pods.podFailurePolicylets you fail fast on exit codes that mean a code bug, and ignore failures caused by preemption or eviction so they do not burn retries.- Suspending a Job removes its running pods until you resume it.
Plain Jobs start as soon as they are created, which is a problem when many teams share a few GPU nodes. Kueue adds quota-based queueing on top of a normal cluster without replacing the scheduler or autoscaler. It decides when a job waits, when it is admitted and when it is preempted, and supports fair sharing between teams. It also integrates with Kubeflow training jobs, RayJob and RayCluster, JobSet and plain pods, and can manage serving Deployments alongside batch work.
To run a Job through Kueue, you add the kueue.x-k8s.io/queue-name label pointing at a LocalQueue in your namespace and set resource requests on each pod. Kueue holds the Job suspended through a webhook and releases it when quota is free; you do not create it suspended yourself.
Probes for model servers
Model servers are slow to start and can hang under load, so all three probe types earn their place:
- Startup probe: gives a slow loader time to finish. Size
failureThresholdtimesperiodSecondsto cover your worst-case load time; liveness checks only begin after it passes. - Readiness probe: keeps a pod out of Service traffic while it cannot serve, for example while large files are still loading. It runs for the pod’s whole life.
- Liveness probe: restarts a container that is stuck and will not recover.
Readiness and liveness are independent, so point readiness at a check that confirms the model is loaded, and liveness at a cheap check that the process responds. Our FastAPI model serving guide shows how to build those health endpoints.
Model storage and rollouts
Keep model weights out of the container image where you can. KServe’s storage layer reads from S3, Google Cloud Storage, Azure Blob, HTTP(S), Git, PVCs, the Hugging Face hub or OCI images. A storage initializer runs as an init container, downloads the model to a local path and handles credentials for the server container. Pair that with a model registry so every storageUri points at a fixed version; our post on ML model versioning covers that side.
For plain Deployments, a rolling update replaces pods a few at a time, and kubectl rollout undo returns to the previous revision. Readiness probes are what stop a broken model from taking traffic during that rollout.
KServe adds a canary rollout: update the storageUri and set canaryTrafficPercent, and a small share of requests goes to the new revision. Remove the field to promote it, or set it to 0 to send everything back to the last good model. This only works in KServe’s serverless deployment mode.
For more serving and infrastructure tools, browse the LLM infrastructure category.
Written by AI Agents Directory
Editorial team
Facts in this guide are checked against official documentation and repositories, which are linked in the article.