LeaderWorkerSet (LWS)

repository·main·Indexed 20 days ago

https://github.com/kubernetes-sigs/lws

Kubernetes APIs, including LeaderWorkerSet (LWS) and DisaggregatedSet (DS), designed to manage complex multi-node distributed workloads. Optimized for AI/ML inference patterns such as LLM sharding and disaggregated prefill/decode architectures. Features include gang scheduling with Volcano, subgroup support for accelerator islands, and configurable startup policies (LeaderCreated and LeaderReady).

Tokens
46.9K
Snippets
105
Records
178
Agent score
73%

What's inside lws

  1. What is LeaderWorkerSet (LWS)?

    main

    LeaderWorkerSet (LWS) is a Kubernetes API designed to deploy a group of pods as a single unit of replication. It is specifically optimized for AI/ML inference workloads, particularly multi-host inference where a Large Language Model (LLM) is sharded across multiple devices and nodes.

    Key characteristics of an LWS group include:

    • Unique Pod Identity: Each pod within a group is assigned a unique index from 0 to n-1.
    • Parallel Lifecycle: Pods in a group are created in parallel and share the same lifecycle.
    • Dual-Template Support: You can specify a template for workers and an optional separate template for the leader pod.
    • Group-Level Operations: Scaling, rolling updates, and rollouts are performed at the group level. When a group is updated, all pods within that group are updated together as a single unit.
    • Topology-Aware Placement: Supports opt-in co-location of pods within the same group in the same topology.
    • All-or-nothing Restart: Supports opt-in policies where all pods in a group are recreated if a single pod fails or a container restarts.
  2. What is DisaggregatedSet?

    main

    A DisaggregatedSet is a high-level Custom Resource Definition (CRD) provided by the LeaderWorkerSet (LWS) project. It is designed to orchestrate multiple LeaderWorkerSet resources as a single logical unit.

    This abstraction is specifically optimized for disaggregated LLM inference architectures (such as those used by vLLM or SGLang), where different phases of inference—like 'prefill' and 'decode'—run on separate infrastructure with different computational requirements.

    Key capabilities include:

    • Unified Management: Managing 2-10 different roles within a single resource.
    • Coordinated Rolling Updates: Using an N-dimensional algorithm to update all roles in lockstep.
    • Automated Service Creation: Automatically creating headless Services for each role per revision.
  3. What is a LeaderWorkerSet (LWS)?

    main

    A LeaderWorkerSet (LWS) is a Kubernetes resource that manages a group of pods organized into a leader-worker hierarchy. It uses two different templates—a leader template and a worker template—to define the pods and controls their collective lifecycle. This is particularly useful for distributed workloads where a set of leader pods must coordinate a corresponding set of worker pods.

    apiVersion: leaderworkerset.x-k8s.io/v1
    kind: LeaderWorkerSet
    metadata:
      name: leaderworkerset-sample
    spec:
      replicas: 3
      leaderWorkerTemplate:
        size: 4
        workerTemplate:
          spec:
            containers:
            - name: nginx
              image: nginxinc/nginx-unprivileged:1.27
  4. What is DisaggregatedSet and when to use it

    main

    A DisaggregatedSet is a Kubernetes controller and Custom Resource Definition (CRD) designed to support disaggregated inference workloads. It is specifically built for Large Language Model (LLM) inference services where different operational roles (such as prefill, decode, or encode) require different resource profiles or scaling patterns.

    When to use DisaggregatedSet

    You should use DisaggregatedSet instead of a plain LeaderWorkerSet (LWS) when:

    • You are running disaggregated LLM inference (e.g., vLLM with P/D disaggregation, SGLang).
    • Different inference phases require different GPU types or different pod group sizes.
    • You need to scale prefill and decode replicas independently based on distinct traffic patterns.

    When to use plain LeaderWorkerSet (LWS)

    Use plain LWS if:

    • All inference pods are homogeneous (same model, same resources).
    • You do not need to separate prefill from decode.
    • You are running training jobs or batch workloads without disaggregation.
  5. Overview of KEP-238: Controller-Revision

    main

    KEP-238 introduces the concept of Controller-Revision to the LeaderWorkerSet (LWS) controller. This mechanism allows the controller to store previous states of the LWS object.

    Key Benefits

    • Consistency during Rolling Updates: Ensures that if a replica is restarted during a rolling update, the controller uses the correct version of the worker pod spec. Previously, a mismatch could occur where workers used an updated spec while the leader used the original spec.
    • Improved Change Detection: Replaces string-based template hash comparisons with semantic comparisons of the LWS spec stored in the revision. This prevents unnecessary rolling updates when upgrading LWS controller versions if the deployed LWS object itself has not changed.
  6. What is DisaggregatedSet and when to use it?

    main

    The DisaggregatedSet operator is an extension of LeaderWorkerSet designed for workloads that require separating inference phases, such as separating prefill and decode phases onto different infrastructure.

    DisaggregatedSet provides:

    • Disaggregated Architecture Support: Optimized for workloads where different roles run on separate infrastructure.
    • Coordinated N-Dimensional Rollouts: Updates multiple roles (typically 2–10) in lockstep to ensure capacity ratios are preserved during the update process.
    • Unified Lifecycle Management: Manages multiple underlying LeaderWorkerSets as a single logical unit.
    • Automatic Service Orchestration: Automatically manages headless services for each role to enable discovery and revision-aware routing.
    • Advanced Failure Handling: Provides coordinated drain and restart policies across all roles within the disaggregated set.
  7. Enable DisaggregatedSet in LeaderWorkerSet

    main

    Starting from v0.9.0, DisaggregatedSet is bundled with the LWS controller manager.

    • kubectl/Kustomize: The standard v0.9.0+ manifests include the DisaggregatedSet CRD, controller permissions, and validating webhook automatically.
    • Helm: The CRD and permissions are installed by default, but the validating webhook and user-facing ClusterRoles are optional. To enable them, pass --set enableDisaggregatedSet=true during installation.

    Upgrading to v0.9.0+ with DisaggregatedSet: If upgrading from a version older than v0.9.0, you must manually apply the DisaggregatedSet CRD before running helm upgrade.

    # Enable DisaggregatedSet via Helm
    CHART_VERSION=0.9.0
    helm install lws oci://registry.k8s.io/lws/charts/lws \
      --version=$CHART_VERSION \
      --namespace lws-system \
      --create-namespace \
      --set enableDisaggregatedSet=true \
      --wait --timeout 300s
    
    # Manual CRD application for upgrades
    kubectl apply --server-side \
      -f https://raw.githubusercontent.com/kubernetes-sigs/lws/main/charts/lws/crds/disaggregatedset.x-k8s.io_disaggregatedsets.yaml
    
    helm upgrade lws oci://registry.k8s.io/lws/charts/lws \
      --namespace lws-system \
      --set enableDisaggregatedSet=true
  8. How the Pod Controller selects worker pod specs

    main

    The Pod Controller uses the mapping between the leader's template hash and the ControllerRevision to ensure workers always use the correct specification.

    When a pod is reconciled, the controller:

    1. Retrieves the templateHash from the pod's labels.
    2. Performs a lookup using GetLeaderWorkerSetRevisionFromTemplateHash to find the associated ControllerRevision.
    3. Uses that revision to construct the correct WorkerTemplate for the worker StatefulSet.
    // Logic used by the Pod Controller to select the correct spec
    func Reconcile() {
    	controllerRevision := GetLeaderWorkerSetRevisionFromTemplateHash(pod.Labels[templateHash])
    	constructWorkerStatefulSetApplyConfiguration(controllerRevision)
    }
    
    func constructWorkerStatefulSetApplyConfiguration(currentRevision) {
    	currentLws := controllerutils.ApplyRevision(lws, controllerRevision)
    	podTemplateSpec := **currentLws.WorkerTemplate.DeepCopy()
    }
  9. Configure Rolling Update rollout strategy

    main

    LWS supports a RollingUpdate strategy to enable zero-downtime updates for online services, such as LLM inference. You can control the update behavior using two parameters within rollingUpdateConfiguration:

    • maxUnavailable: The number of replicas allowed to be unavailable during the update. This value is relative to spec.replicas. Defaults to 1.
    • maxSurge: The number of extra replicas that can be deployed during the update. Defaults to 0.

    Important Constraints:

    • maxSurge and maxUnavailable cannot both be set to 0 at the same time.
    • The total number of replicas during an update is influenced by the sum of maxUnavailable and maxSurge.
    spec:
      rolloutStrategy:
        type: RollingUpdate
        rollingUpdateConfiguration:
          maxUnavailable: 2
          maxSurge: 2
      replicas: 4
  10. Use External Scaling with DisaggregatedSetRoleScaler

    main

    When a role is configured with scaling.mode: External, the DisaggregatedSet controller automatically creates a DisaggregatedSetRoleScaler resource for that role. This resource exposes the /scale subresource, allowing external autoscalers (HPA, KEDA, etc.) to drive the role's size.

    How it works:

    1. The external autoscaler writes the desired replica count to DisaggregatedSetRoleScaler.spec.replicas.
    2. The DisaggregatedSet controller reads this value and adjusts the underlying LeaderWorkerSet.

    DisaggregatedSetRoleScalerSpec:

    • spec.replicas: (Required) The desired replica count. The controller seeds this with 1 for new roles or the current LWS replica count during a Static $\rightarrow$ External transition to prevent silent draining to zero.

    DisaggregatedSetRoleScalerStatus:

    • status.replicas: The observed replica count (aggregated across all current revisions). HPA uses this for ratio math.
    • status.selector: A label selector (string) used to match one pod per LWS group (the leader) across all revisions. This ensures HPA's per-pod-metric averaging remains consistent even during rolling updates.
  11. How LeaderWorkerSet handles PodGroups for Gang Scheduling

    main

    To support gang scheduling, the LeaderWorkerSet controller manages the lifecycle of PodGroup resources and their association with pods. When a LeaderWorkerSet is created or updated, the controller performs the following steps:

    1. Create PodGroup resources: A unique PodGroup is created for each individual replica.
    2. Ensure Pod-to-PodGroup association: The controller ensures that the leader and worker pods belonging to a specific replica are correctly associated with that replica's PodGroup.

    This mechanism allows external schedulers to recognize the group requirements of the entire replica.