DRA を使用してワークロードにデバイスを動的に割り当てる

動的リソース割り当て(DRA)を使用すると、Google Kubernetes Engine(GKE)ワークロードのデバイスを柔軟にリクエストできます。このドキュメントでは、デバイスをリクエストする ResourceClaimTemplate を作成し、ワークロードを作成して、Kubernetes がデバイスを Pod に柔軟に割り当てる方法を確認する方法について説明します。

このドキュメントは、AI / ML やハイ パフォーマンス コンピューティング(HPC)などのワークロードを実行するアプリケーション オペレーターデータ エンジニアを対象としています。

DRA によるデバイスのリクエストについて

DRA 用に GKE インフラストラクチャを設定すると、ノードの DRA ドライバがクラスタ内に DeviceClass オブジェクトを作成します。DeviceClass は、ワークロードにリクエストできるデバイスのカテゴリ(GPU など)を定義します。必要に応じて、プラットフォーム管理者は、特定のワークロードでリクエストできるデバイスを制限する追加の DeviceClass をデプロイできます。

DeviceClass 内のデバイスをリクエストするには、次のいずれかのオブジェクトを作成します。

  • ResourceClaim を使用すると、Pod またはユーザーは DeviceClass 内の特定のパラメータをフィルタして、ハードウェア リソースをリクエストできます。
  • ResourceClaimTemplate は、Pod ごとの新しい ResourceClaim を自動的に作成するために Pod が使用できるテンプレートを定義します。

ResourceClaim と ResourceClaimTemplate の詳細については、ResourceClaim と ResourceClaimTemplate を使用する場面をご覧ください。

このページの例では、基本的な ResourceClaimTemplate を使用して、指定されたデバイス構成をリクエストします。指定できるすべてのフィールドの詳細については、ResourceClaimTemplate API リファレンスをご覧ください。

制限事項

  • ノードの自動プロビジョニングはサポートされていません。
  • Autopilot クラスタは DRA をサポートしていません。
  • 次の GPU 共有機能は使用できません。
    • 時間共有 GPU
    • マルチインスタンス GPU
    • マルチプロセス Service(MPS)

要件

DRA を使用するには、GKE バージョンが 1.34 以降である必要があります。

また、次の要件と制限事項にも注意してください。

始める前に

作業を始める前に、次のタスクが完了していることを確認してください。

  • Google Kubernetes Engine API を有効にする。
  • Google Kubernetes Engine API の有効化
  • このタスクに Google Cloud CLI を使用する場合は、gcloud CLI をインストールして初期化する。gcloud CLI をインストール済みの場合は、gcloud components update コマンドを実行して最新のバージョンを取得します。以前のバージョンの gcloud CLI では、このドキュメントのコマンドを実行できない場合があります。

DRA を使用してワークロードをデプロイする

Pod ごとのデバイス割り当てをリクエストするには、特定タイプの GPU など、リクエストしたデバイス構成を含む ResourceClaimTemplate を作成します。ResourceClaimTemplate を参照するワークロードをデプロイすると、Kubernetes は ResourceClaimTemplate に基づいてワークロード内の各 Pod の ResourceClaim を作成します。Kubernetes は、リクエストされたリソースを割り振り、対応するノードに Pod をスケジュールします。

DRA を使用してワークロード内のデバイスをリクエストするには、次のいずれかのオプションを選択します。

GPU

  1. 次のマニフェストを claim-template.yaml として保存します。

    apiVersion: resource.k8s.io/v1
    kind: ResourceClaimTemplate
    metadata:
      name: gpu-claim-template
    spec:
      spec:
        devices:
          requests:
          - name: single-gpu
            exactly:
              deviceClassName: gpu.nvidia.com
              allocationMode: ExactCount
              count: 1
    
  2. ResourceClaimTemplate を作成します。

    kubectl create -f claim-template.yaml
    
  3. ResourceClaimTemplate を参照するワークロードを作成するには、次のマニフェストを dra-gpu-example.yaml として保存します。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: dra-gpu-example
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: dra-gpu-example
      template:
        metadata:
          labels:
            app: dra-gpu-example
        spec:
          containers:
          - name: ctr
            image: ubuntu:22.04
            command: ["bash", "-c"]
            args: ["echo $(nvidia-smi -L || echo Waiting...)"]
            resources:
              claims:
              - name: single-gpu
          resourceClaims:
          - name: single-gpu
            resourceClaimTemplateName: gpu-claim-template
          tolerations:
          - key: "nvidia.com/gpu"
            operator: "Exists"
            effect: "NoSchedule"
    
  4. ワークロードをデプロイします。

    kubectl create -f dra-gpu-example.yaml
    

TPU

  1. 次のマニフェストを claim-template.yaml として保存します。

    apiVersion: resource.k8s.io/v1
    kind: ResourceClaimTemplate
    metadata:
      name: tpu-claim-template
    spec:
      spec:
        devices:
          requests:
          - name: all-tpus
            exactly:
              deviceClassName: tpu.google.com
              allocationMode: All
    

    この ResourceClaimTemplate は、GKE がすべての ResourceClaim に TPU ノードプール全体を割り振ることをリクエストします。

  2. ResourceClaimTemplate を作成します。

    kubectl create -f claim-template.yaml
    
  3. ResourceClaimTemplate を参照するワークロードを作成するには、次のマニフェストを dra-tpu-example.yaml として保存します。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: dra-tpu-example
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: dra-tpu-example
      template:
        metadata:
          labels:
            app: dra-tpu-example
        spec:
          containers:
          - name: ctr
            image: ubuntu:22.04
            command:
              - /bin/sh
              - -c
              - |
                echo "Environment Variables:"
                env
                echo "Sleeping indefinitely..."
                sleep infinity
            resources:
              claims:
              - name: all-tpus
          resourceClaims:
          - name: all-tpus
            resourceClaimTemplateName: tpu-claim-template
          tolerations:
          - key: "google.com/tpu"
            operator: "Exists"
            effect: "NoSchedule"
    
  4. ワークロードをデプロイします。

    kubectl create -f dra-tpu-example.yaml
    

ハードウェアの割り当てを確認する

ワークロードにハードウェアが割り当てられていることを確認するには、ResourceClaim を確認するか、Pod のログを調べます。GPU または TPU の割り当てを確認するには、次のいずれかのオプションを選択します。

GPU

  1. デプロイしたワークロードに関連付けられている ResourceClaim を取得します。

    kubectl get resourceclaims
    

    出力は次のようになります。

    NAME                                               STATE                AGE
    dra-gpu-example-64b75dc6b-x8bd6-single-gpu-jwwdh   allocated,reserved   9s
    
  2. Pod に割り当てられたハードウェアの詳細を取得します。

    kubectl describe resourceclaims RESOURCECLAIM
    

    RESOURCECLAIM は、前の手順の出力から取得した ResourceClaim のフルネームに置き換えます。

    出力は次のようになります。

       Name:         dra-gpu-example-68f595d7dc-prv27-single-gpu-qgjq5
       Namespace:    default
       Labels:       <none>
       Annotations:  resource.kubernetes.io/pod-claim-name: single-gpu
       API Version:  resource.k8s.io/v1
       Kind:         ResourceClaim
       Metadata:
       # Multiple lines are omitted here.
       Spec:
         Devices:
           Requests:
             Exactly:
               Allocation Mode:    ExactCount
               Count:              1
               Device Class Name:  gpu.nvidia.com
             Name:                 single-gpu
       Status:
         Allocation:
           Devices:
             Results:
               Device:   gpu-0
               Driver:   gpu.nvidia.com
               Pool:     gke-cluster-1-dra-gpu-pool-b56c4961-7vnm
               Request:  single-gpu
           Node Selector:
             Node Selector Terms:
               Match Fields:
                 Key:       metadata.name
                 Operator:  In
                 Values:
                   gke-cluster-1-dra-gpu-pool-b56c4961-7vnm
         Reserved For:
           Name:      dra-gpu-example-68f595d7dc-prv27
           Resource:  pods
           UID:       e16c2813-08ef-411b-8d92-a72f27ebf5ef
       Events:        <none>
       ```
    
  3. デプロイしたワークロードのログを取得します。

    kubectl logs deployment/dra-gpu-example --all-pods=true
    

    出力は次のようになります。

    [pod/dra-gpu-example-64b75dc6b-x8bd6/ctr] GPU 0: Tesla T4 (UUID: GPU-2087ac7a-f781-8cd7-eb6b-b00943cc13ef)
    

    この出力は、GKE がコンテナに 1 つの GPU を割り当てたことを示しています。

TPU

  1. デプロイしたワークロードに関連付けられている ResourceClaim を取得します。

    kubectl get resourceclaims | grep dra-tpu-example
    

    出力は次のようになります。

    NAME                                               STATE                AGE
    dra-tpu-example-64b75dc6b-x8bd6-all-tpus-jwwdh     allocated,reserved   9s
    
  2. Pod に割り当てられたハードウェアの詳細を取得します。

    kubectl describe resourceclaims RESOURCECLAIM -o yaml
    

    RESOURCECLAIM は、前の手順の出力から取得した ResourceClaim のフルネームに置き換えます。

    出力は次のようになります。

    apiVersion: resource.k8s.io/v1beta1
    kind: ResourceClaim
    metadata:
      annotations:
        resource.kubernetes.io/pod-claim-name: all-tpus
      creationTimestamp: "2025-03-04T21:00:54Z"
      finalizers:
      - resource.kubernetes.io/delete-protection
      generateName: dra-tpu-example-59b8785697-k9kzd-all-gpus-
      name: dra-tpu-example-59b8785697-k9kzd-all-gpus-gnr7z
      namespace: default
      ownerReferences:
      - apiVersion: v1
        blockOwnerDeletion: true
        controller: true
        kind: Pod
        name: dra-tpu-example-59b8785697-k9kzd
        uid: c2f4fe66-9a73-4bd3-a574-4c3eea5fda3f
      resourceVersion: "12189603"
      uid: 279b5014-340b-4ef6-9dda-9fbf183fbb71
    spec:
      devices:
        requests:
        - allocationMode: All
          deviceClassName: tpu.google.com
          name: all-tpus
    status:
      allocation:
        devices:
          results:
          - adminAccess: null
            device: "0"
            driver: tpu.google.com
            pool: gke-tpu-2ec29193-bcc0
            request: all-tpus
          - adminAccess: null
            device: "1"
            driver: tpu.google.com
            pool: gke-tpu-2ec29193-bcc0
            request: all-tpus
          - adminAccess: null
            device: "2"
            driver: tpu.google.com
            pool: gke-tpu-2ec29193-bcc0
            request: all-tpus
          - adminAccess: null
            device: "3"
            driver: tpu.google.com
            pool: gke-tpu-2ec29193-bcc0
            request: all-tpus
          - adminAccess: null
            device: "4"
            driver: tpu.google.com
            pool: gke-tpu-2ec29193-bcc0
            request: all-tpus
          - adminAccess: null
            device: "5"
            driver: tpu.google.com
            pool: gke-tpu-2ec29193-bcc0
            request: all-tpus
          - adminAccess: null
            device: "6"
            driver: tpu.google.com
            pool: gke-tpu-2ec29193-bcc0
            request: all-tpus
          - adminAccess: null
            device: "7"
            driver: tpu.google.com
            pool: gke-tpu-2ec29193-bcc0
            request: all-tpus
        nodeSelector:
          nodeSelectorTerms:
          - matchFields:
            - key: metadata.name
              operator: In
              values:
              - gke-tpu-2ec29193-bcc0
      reservedFor:
      - name: dra-tpu-example-59b8785697-k9kzd
        resource: pods
        uid: c2f4fe66-9a73-4bd3-a574-4c3eea5fda3f
    
  3. デプロイしたワークロードのログを取得します。

    kubectl logs deployment/dra-tpu-example --all-pods=true | grep "TPU"
    

    出力は次のようになります。

    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_CHIPS_PER_HOST_BOUNDS=2,4,1
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_TOPOLOGY_WRAP=false,false,false
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_SKIP_MDS_QUERY=true
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_RUNTIME_METRICS_PORTS=8431,8432,8433,8434,8435,8436,8437,8438
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_WORKER_ID=0
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_WORKER_HOSTNAMES=localhost
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_TOPOLOGY=2x4
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_ACCELERATOR_TYPE=v6e-8
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_HOST_BOUNDS=1,1,1
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_TOPOLOGY_ALT=false
    [pod/dra-tpu-example-59b8785697-tm2lc/ctr] TPU_DEVICE_0_RESOURCE_CLAIM=77e68f15-fa2f-4109-9a14-6c91da1a38d3
    

    この出力は、ノードプール内のすべての TPU が Pod に割り振られたことを示しています。

次のステップ