デフォルトで ComputeClass を Pod に適用する

このドキュメントでは、ComputeClass を明示的に選択していない Google Kubernetes Engine(GKE)Pod にデフォルトで ComputeClass を適用する方法について説明します。このページでは、名前空間内とクラスタ全体にデフォルトの ComputeClass を設定する手順について説明します。このドキュメントは、個々のワークロードとノード構成によって発生する手動オーバーヘッドを削減する必要があるクラスタ管理者を対象としています。

読者は カスタム ComputeClass に精通している必要があります。

デフォルトの ComputeClass について

デフォルトの ComputeClass を使用するように GKE クラスタまたは特定の名前空間を構成できます。構成したデフォルト クラスは、別の ComputeClass を選択しないクラスタまたは名前空間内のすべての Pod に適用されます。ComputeClass を選択しない Pod をデプロイすると、GKE は次の順序でデフォルトの ComputeClass を適用します。

  1. 名前空間にデフォルトの ComputeClass がある場合、GKE はその ComputeClass を選択するように Pod 仕様を変更します。
  2. 名前空間にデフォルトの ComputeClass がない場合、クラスタレベルのデフォルト クラスが適用されます。GKE は Pod 仕様を変更しません。

クラスタまたは名前空間にデフォルトの ComputeClass を設定すると、変更は新しい Pod にのみ適用されます。既存の Pod は、Pod が再作成されるまで影響を受けません。この動作は、ComputeClass でアクティブな移行が有効になっている場合でも適用されます。

デフォルトの ComputeClass の activeMigration.optimizeRulePriority フィールドが true に設定されている場合、この設定はクラスタ内のワークロードに影響する可能性があります。詳細については、デフォルトの ComputeClass でのアクティブな移行をご覧ください。

始める前に

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

  • Google Kubernetes Engine API を有効にする。
  • Google Kubernetes Engine API の有効化
  • このタスクに Google Cloud CLI を使用する場合は、gcloud CLI をインストールして初期化する。gcloud CLI をインストール済みの場合は、gcloud components update コマンドを実行して最新のバージョンを取得します。以前のバージョンの gcloud CLI では、このドキュメントのコマンドを実行できない場合があります。
  • クラスタレベルまたは名前空間レベルでデフォルトの ComputeClass を設定できるバージョンを実行している GKE Autopilot クラスタまたは Standard クラスタがある場合。詳細については、要件セクションをご覧ください。
  • Standard モードのクラスタを使用している場合は、次のいずれかの要件を満たしていることを確認してください。

  • カスタム ComputeClass を名前空間のデフォルトとして設定する場合は、カスタム ComputeClass をデプロイします。

要件

  • ComputeClass をクラスタレベルのデフォルトとして設定するには、クラスタで GKE バージョン 1.33.1-gke.1744000 以降を実行する必要があります。
  • DaemonSet Pod 以外の Pod に対してのみ ComputeClass を名前空間レベルのデフォルトとして設定するには、クラスタで GKE バージョン 1.33.1-gke.1788000 以降を実行する必要があります。

必要なロールと権限

クラスタレベルまたは名前空間レベルのデフォルトの ComputeClass を構成するために必要な権限を取得するには、 Google Cloud プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。

ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。

これらの事前定義ロールには、クラスタレベルまたは名前空間レベルのデフォルトの ComputeClass を構成するために必要な権限が含まれています。必要とされる正確な権限については、「必要な権限」セクションを開いてご確認ください。

必要な権限

クラスタレベルまたは名前空間レベルのデフォルトの ComputeClass を構成するには、次の権限が必要です。

  • container.customResourceDefinitions.create
  • container.customResourceDefinitions.update
  • container.customResourceDefinitions.get
  • container.customResourceDefinitions.list
  • container.namespaces.get
  • container.namespaces.list
  • container.pods.get
  • container.nodes.get
  • container.nodes.list
  • container.deployments.create
  • container.deployments.get
  • 名前空間にラベルを追加します: container.namespaces.update
  • クラスタレベルのデフォルト ComputeClass を有効にします: container.clusters.update

カスタムロールや他の事前定義ロールを使用して、これらの権限を取得することもできます。

名前空間のデフォルトの ComputeClass を構成する

クラスタ内の任意の Kubernetes 名前空間に、デフォルトとして使用する ComputeClass の名前で注釈を付けることが可能です。その名前空間にデプロイされた Pod が ComputeClass をまだ選択していない場合、GKE は Pod 仕様を変更して、名前空間のデフォルト クラスを選択します。カスタム ComputeClass または組み込みの ComputeClass をデフォルトとして設定できます。

  • デフォルトで名前空間内のすべての Pod に ComputeClass を適用するには、その名前空間に cloud.google.com/default-compute-class ラベルを追加します。

    kubectl label namespaces NAMESPACE_NAME \
        cloud.google.com/default-compute-class=COMPUTECLASS_NAME
    

    次のように置き換えます。

    • NAMESPACE_NAME: 更新する名前空間の名前。
    • COMPUTECLASS_NAME: 名前空間のデフォルトとして設定する ComputeClass の名前。

    次のエラー メッセージが表示されてコマンドが失敗した場合、名前空間にはすでにデフォルトの ComputeClass が存在します。

    error: 'cloud.google.com/default-compute-class' already has a value, and --overwrite is false
    

    このエラーを解決するには、名前空間のデフォルトの ComputeClass を更新します。

  • デフォルトで名前空間内の DaemonSet Pod 以外のすべての Pod に ComputeClass を適用するには、その名前空間に cloud.google.com/default-compute-class-non-daemonset ラベルを追加します。

    kubectl label namespaces NAMESPACE_NAME \
        cloud.google.com/default-compute-class-non-daemonset=COMPUTECLASS_NAME
    

    次のエラー メッセージが表示されてコマンドが失敗した場合、名前空間には DaemonSet Pod 以外の Pod のデフォルトの ComputeClass がすでに存在します。

    error: 'cloud.google.com/default-compute-class-non-daemonset' already has a value, and --overwrite is false
    

    このエラーを解決するには、名前空間のデフォルトの ComputeClass を更新します。

変更は、その名前空間の新しい Pod に適用されます。既存の Pod は影響を受けません。

名前空間の既存のデフォルトの ComputeClass を更新する

名前空間の既存のデフォルトの ComputeClass を上書きするには、次のいずれかのコマンドを実行します。

  • 名前空間内のすべての Pod のデフォルトの ComputeClass を更新します。

    kubectl label namespaces NAMESPACE_NAME   \
        cloud.google.com/default-compute-class=COMPUTECLASS_NAME \
        --overwrite
    

    次のように置き換えます。

    • NAMESPACE_NAME: 更新する名前空間の名前。
    • COMPUTECLASS_NAME: 名前空間の新しいデフォルトとして設定する ComputeClass の名前。
  • 名前空間内の DaemonSet Pod 以外の Pod のデフォルトの ComputeClass を上書きします。

    kubectl label namespaces NAMESPACE_NAME \
        cloud.google.com/default-compute-class-non-daemonset=COMPUTECLASS_NAME \
        --overwrite
    

システム名前空間のデフォルトの ComputeClass を構成する

GKE は、モニタリング エージェントなどのさまざまなマネージド ワークロードを実行するためにシステム名前空間を使用します。こうしたマネージド ワークロードの一部は DaemonSet であり、重要な機能を提供するためにクラスタ内のすべてのノードで実行する必要があります。ただし、システム名前空間の DaemonSet Pod 以外の Pod のデフォルトの ComputeClass を指定して、これらの Pod をワークロードとは別に実行できます。

システム名前空間のデフォルトの ComputeClass を設定する手順は次のとおりです。

  1. 次のスクリプトを apply-system-cc.sh として保存します。このスクリプトを使用すると、推奨の ComputeClass または既存の ComputeClass をシステム名前空間に適用できます。

    #!/bin/bash
    
    # The recommended default ComputeClass name for system pods.
    DEFAULT_SYSTEM_CC_NAME="system-default-computeclass"
    
    # The YAML manifest for the recommended default ComputeClass for system pods.
    read -r -d '' DEFAULT_CC_YAML << EOF
    apiVersion: cloud.google.com/v1
    kind: ComputeClass
    metadata:
      name: ${DEFAULT_SYSTEM_CC_NAME}
    spec:
      nodePoolAutoCreation:
        enabled: true
      whenUnsatisfiable: ScaleUpAnyway
      priorities:
      - machineFamily: t2d
    EOF
    
    # The label key to apply to namespaces.
    LABEL_KEY="cloud.google.com/default-compute-class-non-daemonset"
    
    
    # List of GKE system namespaces.
    SYSTEM_NAMESPACES=(
      "kube-system"
      "gke-gmp-system"
      "gmp-system"
      "gke-managed-cim"
      "gke-managed-volumepopulator"
      "gke-managed-checkpointing"
      "gkebackup"
      "gke-managed-lustrecsi"
    )
    
    
    # 1. Ask the user for their choice.
    echo "This script sets a default ComputeClass for GKE system namespaces."
    echo "--------------------------------------------------------------------"
    echo "The following ComputeClass is recommended for system Pods:"
    echo ""
    echo "$DEFAULT_CC_YAML"
    echo "--------------------------------------------------------------------"
    read -p "Do you want to use the recommended ComputeClass? (y/N): " user_choice
    
    CC_TO_APPLY=""
    
    # 2. Process the user's choice.
    if [[ "$user_choice" =~ ^[Yy]$ ]]; then
      # Path 1: User chose YES.
      echo "Installing the recommended default ComputeClass '${DEFAULT_SYSTEM_CC_NAME}'..."
    
      if ! echo "$DEFAULT_CC_YAML" | kubectl apply -f -; then
          # If kubectl apply fails, print an error and exit.
          echo "❌ Error: Failed to create the default ComputeClass. An internal error occurred."
          echo "Check for kubectl permissions or other cluster issues and re-run the script."
          exit 1
      fi
      # If the command succeeded, set the name to be used for labeling.
      CC_TO_APPLY="$DEFAULT_SYSTEM_CC_NAME"
    
    else
      read -p "Specify the name of an existing ComputeClass to set as the default for system Pods: " custom_cc_name
    
      # Validate that the user entered a name.
      while [[ -z "$custom_cc_name" ]]; do
        echo "Error: Name cannot be empty."
        read -p "Specify the name of an existing ComputeClass: " custom_cc_name
      done
    
      # Check if the specified ComputeClass actually exists in the cluster.
      echo "--> Verifying that ComputeClass '${custom_cc_name}' exists..."
      if kubectl get computeclass "$custom_cc_name" > /dev/null 2>&1; then
        echo "Verified."
        CC_TO_APPLY="$custom_cc_name"
      else
        echo "Error: ComputeClass '${custom_cc_name}' doesn't exist in the cluster."
        echo "Create the ComputeClass, and then then re-run this script."
        exit 1
      fi
    fi
    
    echo ""
    echo "Labelling system namespaces with ComputeClass: '${CC_TO_APPLY}'"
    echo "--------------------------------------------------------------------"
    
    # 3. Apply the label to all system namespaces.
    for ns in "${SYSTEM_NAMESPACES[@]}"; do
      # Gracefully handle namespaces that don't exist on this specific cluster.
      if kubectl get namespace "${ns}" > /dev/null 2>&1; then
        echo "--> Applying label to namespace: ${ns}"
        kubectl label namespace "${ns}" \
          "${LABEL_KEY}=${CC_TO_APPLY}" --overwrite
      else
        echo "--> Skipping namespace ${ns} (does not exist)"
      fi
    done
    
    echo ""
    echo "✅ Script finished successfully."

    このスクリプトは cloud.google.com/default-compute-class-non-daemonset ラベルを使用します。このラベルは、システム名前空間内の DaemonSet Pod 以外の Pod にのみ反映されます。

  2. スクリプトを実行します。

    ./apply-system-cc.sh
    

スクリプトが完了すると、新しい DaemonSet 以外のシステム Pod は、すでに ComputeClass が存在している場合を除き、デフォルトで指定された ComputeClass を使用します。これらの名前空間内の既存の Pod は影響を受けません。GKE が新しいシステム名前空間を追加した場合は、このスクリプトを再度実行して、それらの名前空間に変更を適用します。

システム名前空間のデフォルトの ComputeClass の設定を解除する手順は次のとおりです。

  1. 次のスクリプトを unset-system-cc.sh として保存します。

    #!/bin/bash
    
    # The recommended default ComputeClass name for system pods.
    DEFAULT_SYSTEM_CC_NAME="system-default-computeclass"
    
    # The YAML manifest for the recommended default ComputeClass for system pods.
    read -r -d '' DEFAULT_CC_YAML << EOF
    apiVersion: cloud.google.com/v1
    kind: ComputeClass
    metadata:
      name: ${DEFAULT_SYSTEM_CC_NAME}
    spec:
      nodePoolAutoCreation:
        enabled: true
      whenUnsatisfiable: ScaleUpAnyway
      priorities:
      - machineFamily: t2d
    EOF
    
    # The label key to apply to namespaces.
    LABEL_KEY="cloud.google.com/default-compute-class-non-daemonset"
    
    
    # List of GKE system namespaces.
    SYSTEM_NAMESPACES=(
      "kube-system"
      "gke-gmp-system"
      "gmp-system"
      "gke-managed-cim"
      "gke-managed-volumepopulator"
      "gke-managed-checkpointing"
      "gkebackup"
      "gke-managed-lustrecsi"
    )
    
    
    # 1. Ask the user for their choice.
    echo "This script sets a default ComputeClass for GKE system namespaces."
    echo "--------------------------------------------------------------------"
    echo "The following ComputeClass is recommended for system Pods:"
    echo ""
    echo "$DEFAULT_CC_YAML"
    echo "--------------------------------------------------------------------"
    read -p "Do you want to use the recommended ComputeClass? (y/N): " user_choice
    
    CC_TO_APPLY=""
    
    # 2. Process the user's choice.
    if [[ "$user_choice" =~ ^[Yy]$ ]]; then
      # Path 1: User chose YES.
      echo "Installing the recommended default ComputeClass '${DEFAULT_SYSTEM_CC_NAME}'..."
    
      if ! echo "$DEFAULT_CC_YAML" | kubectl apply -f -; then
          # If kubectl apply fails, print an error and exit.
          echo "❌ Error: Failed to create the default ComputeClass. An internal error occurred."
          echo "Check for kubectl permissions or other cluster issues and re-run the script."
          exit 1
      fi
      # If the command succeeded, set the name to be used for labeling.
      CC_TO_APPLY="$DEFAULT_SYSTEM_CC_NAME"
    
    else
      read -p "Specify the name of an existing ComputeClass to set as the default for system Pods: " custom_cc_name
    
      # Validate that the user entered a name.
      while [[ -z "$custom_cc_name" ]]; do
        echo "Error: Name cannot be empty."
        read -p "Specify the name of an existing ComputeClass: " custom_cc_name
      done
    
      # Check if the specified ComputeClass actually exists in the cluster.
      echo "--> Verifying that ComputeClass '${custom_cc_name}' exists..."
      if kubectl get computeclass "$custom_cc_name" > /dev/null 2>&1; then
        echo "Verified."
        CC_TO_APPLY="$custom_cc_name"
      else
        echo "Error: ComputeClass '${custom_cc_name}' doesn't exist in the cluster."
        echo "Create the ComputeClass, and then then re-run this script."
        exit 1
      fi
    fi
    
    echo ""
    echo "Labelling system namespaces with ComputeClass: '${CC_TO_APPLY}'"
    echo "--------------------------------------------------------------------"
    
    # 3. Apply the label to all system namespaces.
    for ns in "${SYSTEM_NAMESPACES[@]}"; do
      # Gracefully handle namespaces that don't exist on this specific cluster.
      if kubectl get namespace "${ns}" > /dev/null 2>&1; then
        echo "--> Applying label to namespace: ${ns}"
        kubectl label namespace "${ns}" \
          "${LABEL_KEY}=${CC_TO_APPLY}" --overwrite
      else
        echo "--> Skipping namespace ${ns} (does not exist)"
      fi
    done
    
    echo ""
    echo "✅ Script finished successfully."
  2. スクリプトを実行します。

    ./unset-system-cc.sh
    

クラスタのデフォルトのコンピューティング クラスを構成する

このセクションでは、ComputeClass をクラスタのデフォルトとして設定する方法について説明します。クラスタレベルのデフォルトの ComputeClass の場合、クラスタ内の既存のノードプールのノード taint とノードラベルを手動で指定しないでください。GKE は、ComputeClass のノード taint を持つノードプールをスケーリングしません。default ComputeClass のラベルを手動で追加すると、GKE はそのノードプールをスケーリングできます。ただし、デフォルトのクラスタレベルのコンピューティング クラスを使用するために、この手動構成は必要ありません。

  1. クラスタに対してクラスタレベルのデフォルトの ComputeClass の設定を有効にするには、--enable-default-compute-class フラグを指定して gcloud container clusters update コマンドを実行します。

    gcloud container clusters update CLUSTER_NAME \
        --location=CONTROL_PLANE_LOCATION \
        --enable-default-compute-class
    

    次のように置き換えます。

    • CLUSTER_NAME: クラスタの名前。
    • CONTROL_PLANE_LOCATION: クラスタ コントロール プレーンのロケーション(us-central1 など)。

    このフラグは、Autopilot クラスタまたは Standard クラスタの作成時に指定することもできます。

  2. 次に示す default という名前の ComputeClass を定義するマニフェストを保存します。

    apiVersion: cloud.google.com/v1
    kind: ComputeClass
    metadata:
      name: default
    spec:
      priorities:
      - machineFamily: n4
      - machineFamily: n2
      whenUnsatisfiable: ScaleUpAnyway
      nodePoolAutoCreation:
        enabled: true
    

    このマニフェストの例では、N4 インスタンスを使用するノードをリクエストします。N4 インスタンスを使用できない場合、ComputeClass は代わりに N2 インスタンスをリクエストします。default ComputeClass は、ComputeClass CustomResourceDefinition の使用可能な任意のフィールドを使用して構成できます。

  3. マニフェストをクラスタに適用します。

    kubectl apply -f PATH_TO_MANIFEST
    

    PATH_TO_MANIFEST は、ComputeClass のマニフェストへのパスに置き換えます。

クラスタレベルのデフォルトの ComputeClass を設定すると、GKE は次の両方の要件を満たすノードプールをスケーリングします。

  • ノード構成は、default ComputeClass の構成と同じです。
  • ノードプールには、別の ComputeClass の taint やラベルはありません。GKE は、default ComputeClass の taint とラベルを持つノードプールをスケーリングできます。

たとえば、default ComputeClass で N4 マシンシリーズが指定されている場合、GKE は N4 インスタンスを使用し、別の ComputeClass の taint やラベルがない既存のノードプールをスケーリングできます。

変更は、ComputeClass がまだないクラスタ内のすべての新しい Pod に適用されます。クラスタレベルのデフォルト ComputeClass のアクティブな移行設定に応じて、既存の Pod が影響を受ける可能性があります。詳細については、デフォルトの ComputeClass でのアクティブな移行をご覧ください。

デフォルトの ComputeClass の動作を確認する

名前空間またはクラスタに対して設定したデフォルトの ComputeClass が想定どおりに機能するかどうかを確認する手順は次のとおりです。

  1. 次の Deployment の例を確認します。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: helloweb
      labels:
        app: hello
    spec:
      selector:
        matchLabels:
          app: hello
          tier: web
      template:
        metadata:
          labels:
            app: hello
            tier: web
        spec:
          containers:
          - name: hello-app
            image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
            ports:
            - containerPort: 8080
            resources:
              requests:
                cpu: 200m

    この Deployment は ComputeClass を明示的にリクエストしていません。

  2. Deployment を作成します。

    kubectl apply --namespace=NAMESPACE_NAME \
        -f https://raw.githubusercontent.com/GoogleCloudPlatform/kubernetes-engine-samples/refs/heads/main/quickstarts/hello-app/manifests/helloweb-deployment.yaml
    

    確認する内容に応じて、NAMESPACE_NAME を次のいずれかに置き換えます。

    • デフォルトの ComputeClass を持つ名前空間の名前。
    • デフォルトの ComputeClass がない名前空間の名前。

    GKE が Pod を実行する新しいノードを作成するまでに時間がかかることがあります。

  3. サンプル Deployment の Pod を実行するノードを特定します。

    kubectl get pods --namespace=NAMESPACE_NAME \
        --selector=app=hello -o=wide
    

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

    NAME                        READY   STATUS    RESTARTS   AGE     IP          NODE                                                  NOMINATED NODE   READINESS GATES
    helloweb-7795fbf856-58n5l   1/1     Running   0          9m21s   10.52.2.3   gke-cluster-1-nap-n2-highcpu-2-3muqi8-f213e529-rx7d   <none>           <none>
    
  4. ノードラベルを取得します。

    kubectl get node NODE_NAME --show-labels \
        | grep "cloud.google.com/compute-class"
    

    NODE_NAME は、前の手順の出力に表示されたノードの名前に置き換えます。

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

    NODE_NAME   Ready    <none>   22m   v1.32.4-gke.1236007
    # lines are omitted from this output
    cloud.google.com/compute-class=COMPUTECLASS_NAME,cloud.google.com/gke-boot-disk=pd-balanced,cloud.google.com/gke-container-runtime=containerd
    

    COMPUTECLASS_NAME の値は次のいずれかです。

    • クラスタレベルのデフォルトの ComputeClass: GKE Autopilot またはノードプール自動作成によって作成されたノードの default。手動で作成された既存のノードプール内のノードには、cloud.google.com/compute-class ラベルがない場合があります。
    • 名前空間レベルのデフォルトの ComputeClass: 名前空間のデフォルトとして構成した ComputeClass の名前。

デフォルトの ComputeClass を無効にする

名前空間またはクラスタでデフォルトの ComputeClass を無効にするには、次のいずれかを行います。

  • すべての Pod の名前空間レベルのデフォルトの ComputeClass を無効にするには、名前空間から cloud.google.com/default-compute-class ラベルを削除します。

    kubectl label namespaces NAMESPACE_NAME \
      cloud.google.com/default-compute-class-
    

    ラベルキーの末尾にある - 文字によって、Kubernetes API の Namespace オブジェクトから、そのキーがあるラベルが削除されます。

  • DaemonSet Pod 以外のすべての Pod の名前空間レベルのデフォルトの ComputeClass を無効にするには、名前空間から cloud.google.com/default-compute-class-non-daemonset ラベルを削除します。

    kubectl label namespaces NAMESPACE_NAME \
      cloud.google.com/default-compute-class-non-daemonset-
    
  • クラスタレベルのデフォルト ComputeClass を無効にするには、--no-enable-default-compute-class フラグを指定して gcloud container clusters update コマンドを使用します。

    gcloud container clusters update CLUSTER_NAME \
        --location=CONTROL_PLANE_LOCATION \
        --no-enable-default-compute-class
    

次のステップ