Skip to content
ImagineWorld

Cloud, Clusters, and Kubernetes

node_idnode-book_building-frontier-llm-from-scratch_chapter-35-cloud-clusters-and-kubernetes
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num35
chapter_titleCloud, Clusters, and Kubernetes
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 35 — Cloud, Clusters, and Kubernetes

Section titled “Chapter 35 — Cloud, Clusters, and Kubernetes”

Chapter 19 explained how training is split across many GPUs; this chapter explains how those GPUs are actually obtained, connected and scheduled. Few teams own their hardware. They rent it from the cloud and orchestrate it with Kubernetes, the open-source system that has become the standard way to run distributed workloads on rented machines. Understanding this operational layer is the difference between a training script that works on one GPU and a training run that reliably uses hundreds. This chapter covers why the cloud fits model training, how Kubernetes schedules GPU work, and — concretely — how to launch a distributed training job on it.

Training demand is spiky: a team might need 256 GPUs for two weeks, then a handful for months of fine-tuning. Buying for the peak wastes capital; the cloud’s elasticity — rent hundreds of GPUs for a run, release them after — matches the workload. The cloud also provides access to the latest accelerators (H100, H200, Blackwell) without a hardware refresh cycle, the high-bandwidth interconnect (InfiniBand, NVLink) that the parallelism of Chapter 19 requires, and managed storage fast enough to feed data-hungry training. The trade-off is cost (Chapter 20): rented GPUs are expensive per hour, so utilization — keeping them busy — is the economic imperative, and the orchestration layer exists largely to maximize it.

A training run is not one program on one machine; it is many cooperating processes across many machines, any of which can fail. Kubernetes solves exactly this class of problem. It treats a fleet of machines as one pool, schedules containerized work onto them, restarts failed processes, and exposes GPUs as a schedulable resource. For training specifically it offers three things that matter: GPU scheduling (requesting nvidia.com/gpu as a resource), gang scheduling (starting all workers of a distributed job together, or none — essential because partial starts waste GPUs), and resilience (rescheduling a failed worker rather than losing the run). The Kubeflow Training Operator adds first-class support for distributed PyTorch through a custom PyTorchJob resource, and schedulers like Volcano provide the gang scheduling that naive Kubernetes lacks.

At the lowest level, a pod requests GPUs by asking for them as a resource. The NVIDIA device plugin, installed on the cluster, advertises each node’s GPUs and binds them into the container:

apiVersion: v1
kind: Pod
metadata:
name: train-smoke-test
spec:
containers:
- name: trainer
image: my-registry/llm-trainer:latest
command: ["python", "train.py"]
resources:
limits:
nvidia.com/gpu: 8 # whole GPUs, scheduled by the device plugin
restartPolicy: Never

YAML — a pod requesting 8 GPUs

Real training spans many nodes. The Kubeflow Training Operator’s PyTorchJob declares a master and a set of workers, wiring up the rendezvous environment (MASTER_ADDR, RANK, WORLD_SIZE) that torchrun and PyTorch’s distributed backend expect — the same world-size and rank that Chapter 19’s data parallelism uses. You describe the desired shape declaratively and the operator creates and supervises the pods:

apiVersion: kubeflow.org/v1
kind: PyTorchJob
metadata:
name: llm-pretrain
spec:
pytorchReplicaSpecs:
Master:
replicas: 1
template:
spec:
containers:
- name: pytorch
image: my-registry/llm-trainer:latest
command: ["torchrun", "train.py", "--config", "v3.yaml"]
resources:
limits: { nvidia.com/gpu: 8 }
Worker:
replicas: 3
# 3 workers + 1 master = 4 nodes
template:
spec:
containers:
- name: pytorch
image: my-registry/llm-trainer:latest
command: ["torchrun", "train.py", "--config", "v3.yaml"]
resources:
limits: { nvidia.com/gpu: 8 }

YAML — a 4-worker distributed PyTorchJob (each with 8 GPUs = 32 total)

Submitting and monitoring it is a few commands. The operator creates the pods, the workers discover each other, and training begins as one synchronized job:

Terminal window
kubectl apply -f llm-pretrain.yaml # submit the job
kubectl get pytorchjob llm-pretrain # check status
kubectl get pods -l job-name=llm-pretrain # see the 4 pods
kubectl logs -f llm-pretrain-master-0 # stream training logs

Shell — launch and observe the job

Production setups add several layers. Gang scheduling (via Volcano) ensures all 4 nodes start together, so GPUs are never held idle waiting for stragglers. Checkpointing to object storage (S3, GCS) lets a job resume after a node failure rather than restart — the reproducibility and resilience of Chapter 21 made operational. Ray on Kubernetes offers a higher-level alternative, expressing distributed training, data processing, and serving as one application over the same cluster. And autoscaling node pools grow and shrink the GPU fleet with demand, returning expensive accelerators the moment a run ends. The common goal is the one from Section 35.1: keep costly GPUs busy, and recover automatically when the inevitable failure strikes.

The operational half of training The model code is only half of a training system; the other half is getting hundreds of GPUs, connecting them with fast interconnect, scheduling a synchronized job across them, and surviving failures over a multi-week run. Kubernetes with the Training Operator is how most teams do this on rented hardware. The manifests above are the real interface between the algorithms of Part V and the machines that run them.

35.6 Worked Example: A Kubernetes Inference Deployment

Section titled “35.6 Worked Example: A Kubernetes Inference Deployment”

In production, a serving engine does not run on a lone machine; it runs as a managed, replicated workload, and Kubernetes is the standard substrate. A minimal deployment wraps the vLLM container of Chapter 28, requests the GPUs it needs, and asks for two replicas behind a service so traffic can be load- balanced and a failed pod replaced automatically:

apiVersion: apps/v1
kind: Deployment
metadata:
name: llm-inference
spec:
replicas: 2
template:
spec:
containers:
- name: vllm
image: vllm/vllm-openai:latest
args: ["--model", "zai-org/GLM-5.2"]
resources:
limits:
nvidia.com/gpu: 8 # one node's worth per replica

YAML — k8s/inference-deployment.yaml: a replicated GPU serving workload

Three things turn this from a toy into production. The GPU request (nvidia.com/gpu) lets the Kubernetes scheduler place each replica on a node with enough accelerators, the same resource-accounting problem as Section 35.2. Replicas plus a service give horizontal scaling and failover — a dead pod is recreated and traffic rerouted without human intervention. And in a real cluster this pairs with a readiness probe (so traffic arrives only after the multi-gigabyte model has loaded) and an autoscaler keyed to request load or queue depth, closing the loop between the serving economics of Section 28.7 and the infrastructure that enforces them.

Serving is a workload, not a script The engine of Chapter 28 becomes production by running as a scheduled, replicated, checked Kubernetes workload. The manifest is short; the val in what the platform does around it — placement, scaling, ar recovery you no longer perform by hand.

A service used around the world cannot be served from one p The speed of light alone adds tens to hundreds of milliseconds to a round trip across continents, and for an interactive assistant that latency is felt. Global deployment therefore means running model replicas in multiple geographic regions, each serving the users nearest it — which introduces coordination problems beyond those of a single cluster (Section 35.6).

Three complications recur. GPU availability varies by region: the accelerators a deployment needs may be plentiful in one location and back-ordered in another, constraining where capacity can actually live. Cost varies too, as energy and contract prices differ across regions, so the cheapest place to serve and the closest place to the user are not always the same. And consistency becomes work: the same model version, configuration, and safety policies must be kept in sync across every region, or users in different places receive subtly different behavior. A global routing layer ties it together, directing each request to the nearest healthy region and failing over when one degrades. The result is the same serving stack of this chapter, replicated and coordinated across geography — latency bought with the complexity of running many clusters as one system.

Proximity costs coordination Low latency worldwide means replicas near users in many regions — but GPU supply, cost, and version consistency all vary by region. A global router sends each request to the nearest healthy cluster; the price of closeness is operating many clusters as one.

Cloud GPUs come in two pricing tiers, and exploiting the cheaper one is among the largest cost levers available. On-demand instances are reliable and expensive: once allocated, they are yours until you release them. Spot (or preemptible) instances are the cloud’s spare capacity, offered at a steep discount — often two to three times cheaper — with one catch: the provider can reclaim them at any moment, with little warning.

Capturing that discount is an engineering problem, not a billing choice. For training, the answer is exactly the fault tolerance of Section 20.5: checkpoint frequently, and structure the run so that a preempted job resumes from its last checkpoint on a new instance with minimal lost work. A training run engineered this way can live on spot capacity and absorb preemptions as routine events rather than disasters, paying a fraction of the on-demand price. For inference, spot capacity demands redundancy — enough replicas (Section 28.9) that losing one to preemption degrades capacity rather than taking the service down, with fast failover to absorb the loss. The general principle is that cost optimization at scale is largely the discipline of running expensive workloads on cheaper, less-reliable compute, and buying back the reliability with engineering — checkpointing, redundancy, and failover — rather than paying the premium for hardware that never disappears. It is the same trade as activation checkpointing (Section 17.11), one level up: spend engineering to save money.

Buy reliability back with engineering Spot GPUs cost a fraction of on-demand but can vanish anytime. Frequent checkpointing for training and replica redundancy for serving turn that unreliability into a routine event — capturing the discount by engineering around it.