使用 Kueue 部署批次系統

本教學課程說明如何使用 Kueue 在 Google Kubernetes Engine (GKE) 排定工作,充分運用可用資源。在本教學課程中,您將瞭解如何使用 Kueue 有效管理及排定批次工作、提高資源使用率,以及簡化工作負載管理。您為兩個租戶團隊設定共用叢集,每個團隊都有自己的命名空間,且每個團隊都會建立共用全域資源的工作。您也可以設定 Kueue,根據您定義的資源配額排定工作。

本教學課程適用於有意使用 GKE 實作批次系統的 Cloud 架構師和平台工程師。如要進一步瞭解 Google Cloud內容中提及的常見角色和工作範例,請參閱「常見的 GKE 使用者角色和工作」。

閱讀本頁面前,請先熟悉下列內容:

背景

工作是指執行至完成的應用程式,例如機器學習、算繪、模擬、數據分析、CI/CD 和類似的工作負載。

Kueue 是雲端原生工作排程器,可與預設的 Kubernetes 排程器、工作控制器和叢集自動配置器搭配使用,提供端對端批次系統。Kueue 會實作工作佇列,根據配額和階層結構決定工作應等待或啟動的時間,以便在團隊之間公平共用資源。

Kueue 具有下列特性:

  • 最適合用於資源異質、可互換且可擴充的雲端架構。
  • 提供一組 API,用於管理彈性配額和工作排入佇列。
  • 不會重新實作現有功能,例如自動調度資源、Pod 排程或工作生命週期管理。
  • Kueue 內建支援 Kubernetes batch/v1.Job API。
  • 可與其他工作 API 整合。

Kueue 將使用任何 API 定義的工作稱為「工作負載」,以免與特定 Kubernetes Job API 混淆。

目標

  1. 建立 GKE 叢集
  2. 建立 ResourceFlavor
  3. 建立 ClusterQueue
  4. 建立 LocalQueue
  5. 建立工作並觀察已准許的工作負載

費用

本教學課程使用下列 Google Cloud計費元件:

使用 Pricing Calculator 可根據您的預測使用量來產生費用預估。

完成本教學課程後,請刪除建立的資源,以免繼續計費。詳情請參閱「清除所用資源」一節。

事前準備

設定專案

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Roles required to create a project

    To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the GKE API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Roles required to create a project

    To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the GKE API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

設定 Google Cloud CLI 的預設值

  1. 在 Google Cloud 控制台啟動 Cloud Shell 執行個體:
    開啟 Cloud Shell

  2. 下載這個範例應用程式的原始碼:

    git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
    cd kubernetes-engine-samples/batch/kueue-intro
    
  3. 設定預設環境變數:

    gcloud config set project PROJECT_ID
    gcloud config set compute/region CONTROL_PLANE_LOCATION
    

    替換下列值:

    • PROJECT_ID:您的 Google Cloud 專案 ID。
    • CONTROL_PLANE_LOCATION:叢集控制層的 Compute Engine 區域。

建立 GKE 叢集

  1. 建立名為 kueue-autopilot 的 GKE Autopilot 叢集:

    gcloud container clusters create-auto kueue-autopilot \
      --release-channel "rapid" --location CONTROL_PLANE_LOCATION
    

    Autopilot 叢集為全代管,並內建自動調度資源功能。進一步瞭解 GKE Autopilot。

    Kueue 也支援具備自動佈建節點和一般自動調度資源節點集區的 Standard GKE。

    叢集建立後,結果會與下列內容相似:

      NAME: kueue-autopilot
      LOCATION: us-central1
      MASTER_VERSION: 1.26.2-gke.1000
      MASTER_IP: 35.193.173.228
      MACHINE_TYPE: e2-medium
      NODE_VERSION: 1.26.2-gke.1000
      NUM_NODES: 3
      STATUS: RUNNING
    

    其中 STATUS 是 kueue-autopilot 的 RUNNING。

  2. 取得叢集的驗證憑證:

    gcloud container clusters get-credentials kueue-autopilot
    
  3. 在叢集上安裝 Kueue:

    VERSION=VERSION
    kubectl apply --server-side -f \
      https://github.com/kubernetes-sigs/kueue/releases/download/$VERSION/manifests.yaml
    

    將 VERSION 替換為最新版本的 Kueue。如要進一步瞭解 Kueue 版本,請參閱 Kueue 版本。

  4. 等待 Kueue Pod 準備就緒:

    watch kubectl -n kueue-system get pods
    

    輸出內容應與下方類似,才能繼續操作:

    NAME                                        READY   STATUS    RESTARTS   AGE
    kueue-controller-manager-66d8bb946b-wr2l2   2/2     Running   0          3m36s
    
  5. 建立兩個新的命名空間,分別命名為 team-a 和 team-b:

    kubectl create namespace team-a
    kubectl create namespace team-b
    

建立 ResourceFlavor

ResourceFlavor 物件代表叢集中可用節點的變化版本,方法是將這些節點與節點標籤和污點建立關聯。舉例來說,您可以使用 ResourceFlavors 代表具有不同佈建保證 (例如 Spot 與隨選)、架構 (例如 x86 與 ARM CPU)、品牌和型號 (例如 Nvidia A100 與 T4 GPU) 的 VM。

在本教學課程中,kueue-autopilot 叢集具有同質資源。因此,請為 CPU、記憶體、臨時儲存空間和 GPU 建立單一 ResourceFlavor,不含標籤或污點。

apiVersion: kueue.x-k8s.io/v1beta1
kind: ResourceFlavor
metadata:
  name: default-flavor # This ResourceFlavor will be used for all the resources
部署 ResourceFlavor:

kubectl apply -f flavors.yaml

建立 ClusterQueue

ClusterQueue 是叢集範圍內的物件,用於管理 CPU、記憶體、GPU 等資源集區。負責管理 ResourceFlavor、限制用量,以及決定工作負載的准入順序。

apiVersion: kueue.x-k8s.io/v1beta1
kind: ClusterQueue
metadata:
  name: cluster-queue
spec:
  namespaceSelector: {} # Available to all namespaces
  queueingStrategy: BestEffortFIFO # Default queueing strategy
  resourceGroups:
  - coveredResources: ["cpu", "memory", "nvidia.com/gpu", "ephemeral-storage"]
    flavors:
    - name: "default-flavor"
      resources:
      - name: "cpu"
        nominalQuota: 10
      - name: "memory"
        nominalQuota: 10Gi
      - name: "nvidia.com/gpu"
        nominalQuota: 10
      - name: "ephemeral-storage"
        nominalQuota: 10Gi

部署 ClusterQueue:

kubectl apply -f cluster-queue.yaml

消耗順序由 .spec.queueingStrategy 決定,有兩種設定:

  • BestEffortFIFO

    • 預設佇列策略設定。
    • 工作負載准入遵循先進先出 (FIFO) 規則,但如果配額不足,無法准入佇列前端的工作負載,則會嘗試下一個工作負載。
  • StrictFIFO

    • 保證 FIFO 語意。
    • 佇列前端的工作負載可能會封鎖佇列,直到工作負載可獲准為止。

在 cluster-queue.yaml 中,您會建立名為 cluster-queue 的新 ClusterQueue。這個 ClusterQueue 會管理四項資源:cpu、memory、nvidia.com/gpu 和 ephemeral-storage,並使用在flavors.yaml建立的類別。工作負載 Pod 規格中的要求會耗用配額。

每個版本都有用量限制,以 .spec.resourceGroups[].flavors[].resources[].nominalQuota 表示。在這種情況下,ClusterQueue 只有在符合下列條件時,才會允許工作負載:

  • CPU 要求總和小於或等於 10
  • 記憶體要求總和小於或等於 10Gi
  • GPU 要求總和小於或等於 10
  • 儲存空間用量總和小於或等於 10Gi

建立 LocalQueue

LocalQueue 是命名空間物件,可接受命名空間中使用者的工作負載。不同命名空間的 LocalQueue 可以指向同一個 ClusterQueue,共用資源配額。在本例中,命名空間 team-a 和 team-b 的 LocalQueue 指向 .spec.clusterQueue 下的同一個 ClusterQueue cluster-queue。

apiVersion: kueue.x-k8s.io/v1beta1
kind: LocalQueue
metadata:
  namespace: team-a # LocalQueue under team-a namespace
  name: lq-team-a
spec:
  clusterQueue: cluster-queue # Point to the ClusterQueue
---
apiVersion: kueue.x-k8s.io/v1beta1
kind: LocalQueue
metadata:
  namespace: team-b # LocalQueue under team-b namespace
  name: lq-team-b
spec:
  clusterQueue: cluster-queue # Point to the ClusterQueue

各團隊會將工作負載傳送至其命名空間中的 LocalQueue。接著由 ClusterQueue 分配資源。

部署 LocalQueue:

kubectl apply -f local-queue.yaml

建立工作並觀察已准許的工作負載

在本節中,您將在命名空間 team-a 中建立 Kubernetes 工作。Kubernetes 中的 Job 控制器會建立一或多個 Pod,並確保這些 Pod 成功執行特定工作。

命名空間 team-a 中的工作具有下列屬性:

  • 指向 lq-team-a LocalQueue。
  • 它會將 nodeSelector 欄位設為 nvidia-tesla-t4,藉此要求 GPU 資源。
  • 其中包含三個 Pod,會平行休眠 10 秒。系統會根據 ttlSecondsAfterFinished 欄位中定義的值,在 60 秒後清除工作。
  • 由於有三個 Pod,因此需要 1,500 milliCPU、1536 Mi 記憶體、1,536 Mi 臨時儲存空間和三個 GPU。
apiVersion: batch/v1
kind: Job
metadata:
  namespace: team-a # Job under team-a namespace
  generateName: sample-job-team-a-
  labels:
    kueue.x-k8s.io/queue-name: lq-team-a # Point to the LocalQueue
spec:
  ttlSecondsAfterFinished: 60 # Job will be deleted after 60 seconds
  parallelism: 3 # This Job will have 3 replicas running at the same time
  completions: 3 # This Job requires 3 completions
  suspend: true # Set to true to allow Kueue to control the Job when it starts
  template:
    spec:
      nodeSelector:
        cloud.google.com/gke-accelerator: "nvidia-tesla-t4" # Specify the GPU hardware
      containers:
      - name: dummy-job
        image: gcr.io/k8s-staging-perf-tests/sleep:latest
        args: ["10s"] # Sleep for 10 seconds
        resources:
          requests:
            cpu: "500m"
            memory: "512Mi"
            ephemeral-storage: "512Mi"
            nvidia.com/gpu: "1"
          limits:
            cpu: "500m"
            memory: "512Mi"
            ephemeral-storage: "512Mi"
            nvidia.com/gpu: "1"
      restartPolicy: Never

工作也會在 job-team-b.yaml 檔案下建立,其命名空間屬於 team-b,並要求代表不同需求的不同團隊。

詳情請參閱「在 Autopilot 中部署 GPU 工作負載」。

  1. 在新的終端機中,觀察 ClusterQueue 的狀態,每兩秒重新整理一次:

    watch -n 2 kubectl get clusterqueue cluster-queue -o wide
    
  2. 在新的終端機中,觀察節點狀態:

    watch -n 2 kubectl get nodes -o wide
    
  3. 在新的終端機中,每 10 秒從命名空間 team-a 和 team-b 建立工作至 LocalQueue:

    ./create_jobs.sh job-team-a.yaml job-team-b.yaml 10
    
  4. 觀察工作排入佇列、進入 ClusterQueue,以及節點透過 GKE Autopilot 啟動。

  5. 從命名空間 team-a 取得工作:

    kubectl -n team-a get jobs
    

    結果大致如下:

    NAME                      COMPLETIONS   DURATION   AGE
    sample-job-team-b-t6jnr   3/3           21s        3m27s
    sample-job-team-a-tm7kc   0/3                      2m27s
    sample-job-team-a-vjtnw   3/3           30s        3m50s
    sample-job-team-b-vn6rp   0/3                      40s
    sample-job-team-a-z86h2   0/3                      2m15s
    sample-job-team-b-zfwj8   0/3                      28s
    sample-job-team-a-zjkbj   0/3                      4s
    sample-job-team-a-zzvjg   3/3           83s        4m50s
    
  6. 複製上一個步驟中的工作名稱,並觀察工作的准入狀態和事件。

    kubectl -n team-a describe job JOB_NAME
    
  7. 當 ClusterQueue 中待處理的工作開始增加時,請在執行中的指令碼上按下 CTRL + C,結束指令碼。

  8. 所有工作完成後,節點就會縮減。

清除所用資源

為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

刪除專案

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

刪除個別資源

  1. 刪除 Kueue 配額系統:

    kubectl delete -n team-a localqueue lq-team-a
    kubectl delete -n team-b localqueue lq-team-b
    kubectl delete clusterqueue cluster-queue
    kubectl delete resourceflavor default-flavor
    
  2. 刪除 Kueue 資訊清單:

    VERSION=VERSION
    kubectl delete -f \
      https://github.com/kubernetes-sigs/kueue/releases/download/$VERSION/manifests.yaml
    
  3. 刪除叢集:

    gcloud container clusters delete kueue-autopilot --location=CONTROL_PLANE_LOCATION
    

後續步驟