Dynamic Resource Allocation (DRA)

This guide describes how to allocate GPUs and other devices for TrainJobs with Kubernetes Dynamic Resource Allocation (DRA) instead of extended resources such as nvidia.com/gpu.

Important

DRA support was introduced in Kubeflow Trainer v2.4.0 behind the TrainJobDynamicResourceAllocation alpha feature gate. With the gate disabled, TrainJobs that set resourceClaimsPerNode or DRA fields in runtimePatches are rejected; claims defined directly in a runtime template are still applied.

Note

Make sure to follow the Getting Started guide to understand the basics of Kubeflow Trainer.

Prerequisites

  • Kubeflow Trainer v2.4.0 or later installed on your cluster.

  • Kubernetes v1.34 or later, where DRA is GA (resource.k8s.io/v1). The examples in this guide use the exactly device request shape from that API.

  • A DRA driver installed for your hardware (for example, the NVIDIA DRA driver for GPUs).

  • The TrainJobDynamicResourceAllocation alpha feature gate enabled on the Trainer controller. To enable it, pass the flag to the controller at startup:

    --feature-gates=TrainJobDynamicResourceAllocation=true
    

    Or, if deploying via Helm, set manager.config.featureGates.TrainJobDynamicResourceAllocation=true.

    The command-line flag takes precedence over any value set in the controller config file.

How it works

A ResourceClaimTemplate describes the devices a Pod needs. When a TrainJob references a template, Kubernetes creates a separate ResourceClaim for every training node Pod, so each node gets its own devices.

For every TrainJob, the Trainer controller adds the claims from spec.trainer.resourceClaimsPerNode to the training Pod’s resourceClaims and to the node container’s resources.claims.

The GPU count is not yet derived from the ResourceClaimTemplate, so set spec.trainer.numProcPerNode explicitly for DRA TrainJobs; otherwise the Torch runtime falls back to the CPU count.

Create a ResourceClaimTemplate

Create the template in the same namespace as the TrainJob. The following example requests exactly 8 GPUs per training node:

apiVersion: resource.k8s.io/v1
kind: ResourceClaimTemplate
metadata:
  name: gpu-claim-template
spec:
  spec:
    devices:
      requests:
        - name: gpu
          exactly:
            deviceClassName: gpu.nvidia.com
            count: 8

Use DRA in a TrainJob

Set spec.trainer.resourceClaimsPerNode to attach a ResourceClaimTemplate to every training node. Do not set resourcesPerNode.claims; the TrainJob is rejected if that field is set.

apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
  name: llm-finetune-dra
spec:
  runtimeRef:
    name: torch-distributed
  trainer:
    image: docker.io/my-training:latest
    numNodes: 4
    numProcPerNode: 8
    resourceClaimsPerNode:
      - name: gpu
        resourceClaimTemplateName: gpu-claim-template

Each training node Pod gets a gpu claim created from gpu-claim-template, and the node container consumes it. numProcPerNode matches the template’s device count so torchrun starts one worker per GPU.

If the runtime already defines a Pod-level claim with the same name, the TrainJob entry replaces it.

Attach claims to other containers

resourceClaimsPerNode only wires the claim into the node container. To share the same devices with a sidecar or init container, or to reference a pre-created ResourceClaim with resourceClaimName, use Runtime Patches. The following example assumes the runtime’s node job has a model-initializer init container:

apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
  name: llm-finetune-dra-init
spec:
  runtimeRef:
    name: torch-distributed
  trainer:
    image: docker.io/my-training:latest
    numNodes: 4
    numProcPerNode: 8
    resourceClaimsPerNode:
      - name: gpu
        resourceClaimTemplateName: gpu-claim-template
  runtimePatches:
    - manager: trainer.kubeflow.org/kubeflow-sdk
      trainingRuntimeSpec:
        template:
          spec:
            replicatedJobs:
              - name: node
                template:
                  spec:
                    template:
                      spec:
                        initContainers:
                          - name: model-initializer
                            resources:
                              claims:
                                - name: gpu

Every container resources.claims entry must reference a Pod-level resourceClaims entry. The Trainer admission webhook rejects TrainJobs with dangling references.

Merge semantics

  • Pod-level resourceClaims from the runtime, runtimePatches, and resourceClaimsPerNode are merged by name. resourceClaimsPerNode wins over the runtime and the patches.

  • A container resources.claims list set through runtimePatches replaces the runtime’s list for that container. For the node container, resourceClaimsPerNode entries are added on top of the patched list.

  • spec.trainer.resourcesPerNode still controls the requests and limits of the node container, and spec.trainer.resourceClaimsPerNode adds to its claims.

  • Once set, runtimePatches[].…resourceClaims is immutable (CEL-enforced); other runtimePatches changes are still allowed while the TrainJob is suspended.

Next steps