本教學課程說明如何使用 Kueue 在 Google Kubernetes Engine (GKE) 排定工作,充分運用可用資源。在本教學課程中,您將瞭解如何使用 Kueue 有效管理及排定批次工作、提高資源使用率,以及簡化工作負載管理。您為兩個租戶團隊設定共用叢集,每個團隊都有自己的命名空間,且每個團隊都會建立共用全域資源的工作。您也可以設定 Kueue,根據您定義的資源配額排定工作。
本教學課程適用於有意使用 GKE 實作批次系統的 Cloud 架構師和平台工程師。如要進一步瞭解 Google Cloud內容中提及的常見角色和工作範例,請參閱「常見的 GKE 使用者角色和工作」。
閱讀本頁面前,請先熟悉下列內容:
背景
工作是指執行至完成的應用程式,例如機器學習、算繪、模擬、數據分析、CI/CD 和類似的工作負載。
Kueue 是雲端原生工作排程器,可與預設的 Kubernetes 排程器、工作控制器和叢集自動配置器搭配使用,提供端對端批次系統。Kueue 會實作工作佇列,根據配額和階層結構決定工作應等待或啟動的時間,以便在團隊之間公平共用資源。
Kueue 具有下列特性:
- 最適合用於資源異質、可互換且可擴充的雲端架構。
- 提供一組 API,用於管理彈性配額和工作排入佇列。
- 不會重新實作現有功能,例如自動調度資源、Pod 排程或工作生命週期管理。
- Kueue 內建支援 Kubernetes
batch/v1.JobAPI。 - 可與其他工作 API 整合。
Kueue 將使用任何 API 定義的工作稱為「工作負載」,以免與特定 Kubernetes Job API 混淆。
目標
- 建立 GKE 叢集
- 建立 ResourceFlavor
- 建立 ClusterQueue
- 建立 LocalQueue
- 建立工作並觀察已准許的工作負載
費用
本教學課程使用下列 Google Cloud計費元件:使用 Pricing Calculator 可根據您的預測使用量來產生費用預估。
完成本教學課程後,請刪除建立的資源,以免繼續計費。詳情請參閱「清除所用資源」一節。
事前準備
設定專案
- 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles. -
Verify that billing is enabled for your Google Cloud project.
Enable the GKE API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles. -
Verify that billing is enabled for your Google Cloud project.
Enable the GKE API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.
設定 Google Cloud CLI 的預設值
在 Google Cloud 控制台啟動 Cloud Shell 執行個體:
開啟 Cloud Shell下載這個範例應用程式的原始碼:
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples cd kubernetes-engine-samples/batch/kueue-intro設定預設環境變數:
gcloud config set project PROJECT_ID gcloud config set compute/region CONTROL_PLANE_LOCATION替換下列值:
建立 GKE 叢集
建立名為
kueue-autopilot的 GKE Autopilot 叢集:gcloud container clusters create-auto kueue-autopilot \ --release-channel "rapid" --location CONTROL_PLANE_LOCATIONAutopilot 叢集為全代管,並內建自動調度資源功能。進一步瞭解 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。取得叢集的驗證憑證:
gcloud container clusters get-credentials kueue-autopilot在叢集上安裝 Kueue:
VERSION=VERSION kubectl apply --server-side -f \ https://github.com/kubernetes-sigs/kueue/releases/download/$VERSION/manifests.yaml將 VERSION 替換為最新版本的 Kueue。如要進一步瞭解 Kueue 版本,請參閱 Kueue 版本。
等待 Kueue Pod 準備就緒:
watch kubectl -n kueue-system get pods輸出內容應與下方類似,才能繼續操作:
NAME READY STATUS RESTARTS AGE kueue-controller-manager-66d8bb946b-wr2l2 2/2 Running 0 3m36s建立兩個新的命名空間,分別命名為
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,不含標籤或污點。
kubectl apply -f flavors.yaml
建立 ClusterQueue
ClusterQueue 是叢集範圍內的物件,用於管理 CPU、記憶體、GPU 等資源集區。負責管理 ResourceFlavor、限制用量,以及決定工作負載的准入順序。
部署 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。
各團隊會將工作負載傳送至其命名空間中的 LocalQueue。接著由 ClusterQueue 分配資源。
部署 LocalQueue:
kubectl apply -f local-queue.yaml
建立工作並觀察已准許的工作負載
在本節中,您將在命名空間 team-a 中建立 Kubernetes 工作。Kubernetes 中的 Job 控制器會建立一或多個 Pod,並確保這些 Pod 成功執行特定工作。
命名空間 team-a 中的工作具有下列屬性:
- 指向
lq-team-aLocalQueue。 - 它會將
nodeSelector欄位設為nvidia-tesla-t4,藉此要求 GPU 資源。 - 其中包含三個 Pod,會平行休眠 10 秒。系統會根據
ttlSecondsAfterFinished欄位中定義的值,在 60 秒後清除工作。 - 由於有三個 Pod,因此需要 1,500 milliCPU、1536 Mi 記憶體、1,536 Mi 臨時儲存空間和三個 GPU。
工作也會在 job-team-b.yaml 檔案下建立,其命名空間屬於 team-b,並要求代表不同需求的不同團隊。
詳情請參閱「在 Autopilot 中部署 GPU 工作負載」。
在新的終端機中,觀察 ClusterQueue 的狀態,每兩秒重新整理一次:
watch -n 2 kubectl get clusterqueue cluster-queue -o wide在新的終端機中,觀察節點狀態:
watch -n 2 kubectl get nodes -o wide在新的終端機中,每 10 秒從命名空間
team-a和team-b建立工作至 LocalQueue:./create_jobs.sh job-team-a.yaml job-team-b.yaml 10觀察工作排入佇列、進入 ClusterQueue,以及節點透過 GKE Autopilot 啟動。
從命名空間
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複製上一個步驟中的工作名稱,並觀察工作的准入狀態和事件。
kubectl -n team-a describe job JOB_NAME當 ClusterQueue 中待處理的工作開始增加時,請在執行中的指令碼上按下
CTRL + C,結束指令碼。所有工作完成後,節點就會縮減。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除個別資源
刪除 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刪除 Kueue 資訊清單:
VERSION=VERSION kubectl delete -f \ https://github.com/kubernetes-sigs/kueue/releases/download/$VERSION/manifests.yaml刪除叢集:
gcloud container clusters delete kueue-autopilot --location=CONTROL_PLANE_LOCATION