Dual Pod Templates
Configuring leader and worker pod templates in LeaderWorkerSet.
LeaderWorkerSet (LWS) is a Kubernetes API designed to deploy and manage a group of pods as a single unit of replication. It addresses common deployment patterns of distributed AI/ML workloads — such as multi-host inference and distributed fine-tuning — where a model is sharded across multiple accelerators spanning multiple nodes that must be scheduled, scaled, and managed together.
Under the hood, LeaderWorkerSet implements an API composition pattern on top of native Kubernetes primitives. Rather than managing individual pods directly, LWS composes two tiers of StatefulSets:
LeaderWorkerSet (spec.replicas = 2, size = 4)
│
└── Leader StatefulSet: "<lws-name>" (replicas = 2)
│
├── Replica 0
│ ├── Leader Pod: <lws-name>-0
│ └── Worker StatefulSet: "<lws-name>-0" (replicas = 3, startOrdinal = 1)
│ ├── Worker Pod: <lws-name>-0-1 (ordinal 1)
│ ├── Worker Pod: <lws-name>-0-2 (ordinal 2)
│ └── Worker Pod: <lws-name>-0-3 (ordinal 3)
│
└── Replica 1
├── Leader Pod: <lws-name>-1
└── Worker StatefulSet: "<lws-name>-1" (replicas = 3, startOrdinal = 1)
├── Worker Pod: <lws-name>-1-1 (ordinal 1)
├── Worker Pod: <lws-name>-1-2 (ordinal 2)
└── Worker Pod: <lws-name>-1-3 (ordinal 3)
<lws-name> with spec.replicas matching the LWS replica count. This StatefulSet generates the leader pods (<lws-name>-0, <lws-name>-1, …, <lws-name>-(R-1)), using leaderTemplate (or workerTemplate if leaderTemplate is omitted).<lws-name>-<replica-index> with replicas = size - 1 and startOrdinal = 1. This creates the worker pods (<lws-name>-<replica-index>-1 through <lws-name>-<replica-index>-(size-1)), using workerTemplate.StatefulSet was chosen as the underlying building block for both leader and worker pods due to several critical capabilities required by distributed AI/ML workloads:
Distributed training and inference frameworks (such as PyTorch DDP/FSDP, Megatron-LM, vLLM, TensorRT-LLM, and SGLang) rely on static rank assignment (RANK 0..N-1, WORLD_SIZE), peer identification, and deterministic rendezvous.
0, 1, ..., R-1).1, 2, ..., size-1).<pod-name>.<service-name>.<namespace>.svc.cluster.local), eliminating the need for complex dynamic discovery protocols or external service registries.volumeClaimTemplates)Distributed AI models often require dedicated local storage for caching large model checkpoints, tokenizers, or intermediate KV caches on high-speed NVMe drives attached to each node.
volumeClaimTemplates, which automatically provisions dedicated, persistent storage per pod ordinal.While traditional stateful services (such as databases) deploy pods sequentially, distributed AI/ML replicas require all pods in a group to start simultaneously to establish collective communication (MPI/NCCL) and avoid idle accelerator time.
podManagementPolicy: Parallel, allowing all worker pods in a group to be created and initialized concurrently while preserving their deterministic ordinal names and storage bindings.StatefulSet natively supports partition-based rolling updates (.spec.updateStrategy.rollingUpdate.partition).
maxUnavailable and maxSurge), updating replicas in controlled batches while keeping the active serving capacity intact.Building on top of StatefulSet adheres to Kubernetes design principles by reusing battle-tested core controllers:
Configuring leader and worker pod templates in LeaderWorkerSet.
Controlling worker creation order relative to the leader pod.
Co-locating LWS pod replicas onto exclusive topology domains for high-speed interconnects.
SubGroup scheduling, sizing, and heterogeneous placement in LeaderWorkerSet.
Configuring persistent storage for leader and worker pods using volumeClaimTemplates.
Rolling update configurations, maxUnavailable, and maxSurge in LeaderWorkerSet.
Learn how LeaderWorkerSet handles pod and node failures with configurable restart policies.
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.