在 GKE 上部署 GPU 加速的 Ray,用於 AI 工作負載
本頁面說明如何在 Google Kubernetes Engine (GKE) 上設定及執行啟用 GPU 的 Ray 叢集,以調度 AI/機器學習工作負載資源。
為什麼要使用 Ray?
在 GKE 上使用 Ray (由 KubeRay 輔助),即可擴充 Python 和 AI 應用程式。Ray 會處理應用程式資源調度,GKE 則會管理基礎伺服器和資源。
這個組合可讓您輕鬆管理資源、自動調整容量、確保可靠性,以及在各種環境中部署應用程式。將分散式 Ray 應用程式連結至 GKE 上現有的雲端基礎架構和工具,即可簡化作業,並使用標準 Kubernetes 方法,為複雜工作佈建強大且可擴充的平台。
詳情請參閱「Why GKE for your Ray AI workloads」網誌文章。
事前準備
開始之前,請確認你已完成下列工作:
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the GKE API.
-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 安裝
kubectl
:kubectl
是與 GKE 叢集互動的主要指令列工具。如果您使用 Cloud Shell,則已安裝kubectl
。否則,請在終端機中執行gcloud components install kubectl
指令來安裝。如要驗證安裝作業,請執行下列指令:
kubectl version --client
- 安裝 Ray:強烈建議您先建立並進入 Python 虛擬環境 (
venv
),再安裝 Ray,這樣才能隔離其依附元件。- 安裝 Python。
- 建立並啟用新的虛擬環境。
- 在虛擬環境中執行下列指令,安裝最新穩定版 Ray:
pip install -U "ray[default]"
。 - 如要驗證安裝作業,請執行這項指令:
ray --version
。請記下輸出內容中的 Ray 版本 (例如 2.44.1)。
- 請確認 Google Cloud 專案有足夠的 L4 GPU 配額。詳情請參閱「關於 GPU」和「分配配額」。
建立 GKE 叢集
您可以在 GKE Autopilot 或 Standard 叢集中部署工作負載。建議您使用 Autopilot 叢集,享受全代管的 Kubernetes 體驗。如要選擇最適合工作負載的 GKE 作業模式,請參閱「選擇 GKE 作業模式」。
Autopilot
在 Autopilot 模式中,Google 會管理叢集設定,包括資源調度、安全性及其他預設設定。Autopilot 叢集經過最佳化調整,適用於多數正式環境工作負載,並根據您的 Kubernetes 資訊清單佈建運算資源。
在終端機中執行下列
gcloud container clusters create-auto
指令:gcloud container clusters create-auto my-ray-enabled-cluster \ --enable-ray-operator \ --enable-ray-cluster-monitoring \ --enable-ray-cluster-logging \ --location=us-central1
這項 Google Cloud CLI 指令會建立名為
my-ray-enabled-cluster
的 Autopilot 叢集,並採用下列設定:--enable-ray-operator
:在叢集上安裝 Ray 運算子,簡化 Kubernetes 中 Ray 叢集的管理作業。--enable-ray-cluster-monitoring
:設定與 Cloud Monitoring 的整合,以取得 Ray 叢集指標。--enable-ray-cluster-logging
:設定與 Cloud Logging 的整合,從 Ray 叢集收集記錄。--location=us-central1
:指定要建立 GKE 叢集的地理區域。請注意,加速器支援情形和支援類型會因所在地區而異。詳情請參閱 GPU 地區和區域。
Autopilot 叢集會根據工作負載需求,自動佈建及調度節點 (包括使用運算類別的 GPU 節點),同時提高使用率並降低成本。Autopilot 叢集會自動佈建及調度資源,簡化資源管理作業,讓您專注於模型,而非基礎架構。
如果您使用 GKE Standard 叢集,啟用 Ray 的旗標和設定可能有所不同。詳情請參閱「在 GKE 上啟用 Ray 運算子」。
設定
kubectl
與叢集通訊:gcloud container clusters get-credentials my-ray-enabled-cluster \ --region=us-central1
標準
在標準模式下,您可以更全面地控管 GKE 叢集的底層基礎架構。您可以直接管理節點集區、節點大小和其他設定,進一步自訂設定。標準叢集適合需要特定設定或嚴格控管資源的工作負載。
在終端機中執行下列
gcloud container clusters create
指令:gcloud container clusters create my-ray-enabled-cluster \ --addons=RayOperator \ --enable-ray-cluster-monitoring \ --enable-ray-cluster-logging \ --region=us-central1 \ --enable-autoprovisioning \ --max-cpu=10 \ --max-memory=64
這項 Google Cloud CLI 指令會建立名為
my-ray-enabled-cluster
的 Standard 叢集,並使用下列設定:--addons=RayOperator
:在叢集上安裝 Ray 運算子,簡化 Kubernetes 中 Ray 叢集的管理作業。--enable-ray-cluster-monitoring
:設定與 Cloud Monitoring 的整合,以取得 Ray 叢集指標。--enable-ray-cluster-logging
:設定與 Cloud Logging 的整合,從 Ray 叢集收集記錄。--region=us-central1
:指定要建立 GKE 叢集的地理區域。請注意,加速器支援情形和支援類型會因所在地區而異。詳情請參閱 GPU 地區和區域。--enable-autoprovisioning
:根據工作負載需求,自動建立及刪除節點集區,並在這些集區中調整節點大小。--max-cpu
:叢集可擴充的核心數量上限。--max-memory
:叢集可擴充的記憶體 GB 數上限。
設定
kubectl
與叢集通訊:gcloud container clusters get-credentials my-ray-enabled-cluster \ --region=us-central1
安裝 KubeRay kubectl ray
外掛程式
kubectl ray
外掛程式可簡化 Kubernetes 上常見的 Ray 工作流程。如需最新安裝操作說明,請參閱 KubeRay 說明文件。
前往發布頁面,然後下載適用於您平台的二進位檔。舉例來說,如要在 Linux amd64 上安裝
kubectl ray
外掛程式 1.3.2 版,請執行下列指令:curl -LO https://github.com/ray-project/kuberay/releases/download/v1.3.2/kubectl-ray_v1.3.2_linux_amd64.tar.gz tar -xvf kubectl-ray_v1.3.2_linux_amd64.tar.gz cp kubectl-ray ~/.local/bin
將
~/.local/bin
替換為PATH
中的目錄。驗證安裝狀態:
kubectl ray version
如果看到類似
KubeRay operator installation cannot be found: no KubeRay operator deployments found in any namespace.
的警告,可以放心忽略。 如果安裝成功,輸出內容會是kubectl ray
外掛程式的版本。
在叢集中建立自訂運算類別
在 GKE 中,您可以透過自訂運算類別,依優先順序定義特定硬體需求,例如 GPU 類型和數量。GKE 會在為工作負載佈建節點時,使用這項優先順序。
如要為本指南建立範例自訂運算類別,請按照下列步驟操作:
將下列運算類別規格儲存為
nvidia-l4-compute-class.yaml
:apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: name: gpu-compute-class spec: priorities: - gpu: type: nvidia-l4 count: 1 spot: true - gpu: type: nvidia-l4 count: 1 spot: false nodePoolAutoCreation: enabled: true whenUnsatisfiable: DoNotScaleUp
執行下列指令,建立運算類別:
kubectl apply -f nvidia-l4-compute-class.yaml
套用自訂運算類別後會發生什麼事? 套用運算類別 YAML 檔案後,當 Ray 叢集要求節點時,GKE 會自動佈建搭載一個 NVIDIA L4 GPU 的節點。系統會優先使用資訊清單中定義的 Spot VM。
whenUnsatisfiable: DoNotScaleUp
設定會指示 GKE 不要建立不符合指定需求的節點。
建立及驗證 Ray 叢集
您現在可以在 GKE 叢集上建立 Ray 叢集。
建立 Ray 叢集。使用
kubectl ray create cluster
指令定義及建立 Ray 叢集。這個指令會處理基礎 Kubernetes 資源建立作業,簡化程序。kubectl ray create cluster my-ray-cluster \ --worker-replicas=1 \ --worker-cpu=2 \ --worker-memory=4Gi \ --worker-gpu=1 \ --worker-node-selectors="cloud.google.com/compute-class=gpu-compute-class"
kubectl ray create cluster
指令會使用 KubeRay 外掛程式,將規格轉換為 RayCluster 自訂資源定義,並傳送至 Kubernetes API 來建立 RayCluster 物件。這個物件會做為藍圖,告知 Ray 運算子如何在 GKE 中佈建及管理實際的 Ray 叢集元件 (主節點和工作站節點)。根據預設,GKE 會將主 Pod 排定至 E2 機器系列的節點。範例指令會使用下列設定:
kubectl ray create cluster my-ray-cluster
:指定要建立的 Ray 叢集名稱。--worker-node-selectors
:指示 Kubernetes 將工作人員 Pod 排定在標有cloud.google.com/compute-class=gpu-compute-class
的節點上。
驗證 Ray 叢集狀態。使用這些指令,確認 Ray 叢集已啟動並執行。
執行下列指令,檢查 RayCluster 資源:
kubectl ray get cluster
這個指令會列出 Kubernetes 命名空間中的所有 Ray 叢集。您應該會看到列出的
my-ray-cluster
。叢集可能需要幾分鐘才能完成初始化。執行下列指令,檢查 Kubernetes Pod:
kubectl get pods
這個指令會列出 Kubernetes 命名空間中執行的所有 Pod。您應該會看到與 Ray 叢集相關的 Pod:
- Ray 主要節點的 Pod,名稱類似
my-ray-cluster-head-0
。 - 一或多個 Ray 工作站節點的 Pod,名稱類似
my-ray-cluster-worker-group-0-xxxxx
。工作站 Pod 的數量取決於 Ray 叢集的初始設定,以及可能發生的任何自動調度資源作業。
- Ray 主要節點的 Pod,名稱類似
提交 Ray 工作
Ray 叢集準備就緒後,您可以將 Ray 工作提交至 GKE 上執行的 Ray 叢集。
如要這麼做,請使用 kubectl ray session
指令啟動互動式工作階段,並使用 ray job submit
指令啟動工作執行程序。
啟動 Ray 互動工作階段。如要與 Ray 叢集建立本機連線,請執行下列指令:
kubectl ray session my-ray-cluster
這項指令會在本機與 GKE 叢集中的 Ray 主要節點之間啟動通訊埠轉送功能。請注意,工作階段啟用期間,終端機會處於忙碌狀態;如要繼續操作,請開啟另一個終端機執行個體。
建立要執行的程式碼範例。將下列範例程式碼儲存至名為
sample_code.py
的檔案。import ray import os import requests ray.init() @ray.remote class Counter: def __init__(self): # Used to verify runtimeEnv self.name = os.getenv("counter_name") assert self.name == "test_counter" self.counter = 0 def inc(self): self.counter += 1 def get_counter(self): return "{} got {}".format(self.name, self.counter) counter = Counter.remote() for _ in range(5): ray.get(counter.inc.remote()) print(ray.get(counter.get_counter.remote())) # Verify that the correct runtime env was used for the job. assert requests.__version__ == "2.26.0"
開啟新的終端機並提交 Ray 工作。
- 在本機開啟新的終端機,如果您為 Ray 安裝作業建立了虛擬環境,請務必在這個新終端機中啟用該環境。
- 前往儲存
sample_code.py
檔案的目錄。 執行下列指令:
ray job submit \ --working-dir=. \ --runtime-env-json='{"pip": ["requests==2.26.0"], "env_vars": {"counter_name": "test_counter"}}' \ --address http://localhost:8265 python sample_code.py
範例指令會使用下列設定:
--working-dir=.
:指定目前目錄做為工作目錄,供叢集上的 Ray 工作使用。理想情況下,這個目錄只會包含您要在 Ray 上執行的應用程式原始碼。在本範例中,這是sample_code.py
檔案。--runtime-env-json='{"pip": ["requests==2.26.0"], "env_vars": {"counter_name": "test_counter"}}'
:定義工作的執行階段環境。這個指令會指定必要的 Python 程式庫版本 (requests==2.26.0
),並為工作執行作業設定環境變數 (counter_name
)。
查看 Ray 工作。您可以透過下列任一方式監控工作:
- Ray 資訊主頁:開啟網路瀏覽器並前往
http://localhost:8265
。這個網址會開啟 Ray 資訊主頁,您可以在這裡查看執行中工作的相關資訊,包括狀態、記錄和資源用量。 - 指標探索器:使用Google Cloud 控制台中的預先建構指標資訊主頁。詳情請參閱「收集及查看 GKE 上 Ray 叢集的記錄和指標」。
- Ray 資訊主頁:開啟網路瀏覽器並前往
終止 Ray 工作階段。與 Ray 叢集互動或監控工作完成後,您可以在執行
kubectl ray session
指令的終端機中按下Ctrl+C
,終止互動式工作階段。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。
如果您是在新的 Google Cloud 專案中部署快速入門導覽課程,且不再需要該專案,請完成下列步驟來刪除專案:
如果您沒有建立新的 Google Cloud 專案,且不再需要 GKE 叢集,可以執行下列指令刪除叢集:
gcloud container clusters delete my-ray-enabled-cluster \
--location=us-central1
後續步驟
- 探索更多範例、最佳做法和適用於 GKE 上 Ray 的工具:詳情請參閱 ai-on-gke GitHub 存放區。
- 進一步瞭解如何在 GKE 上使用加速器:在機器學習和資料分析工作中,使用 GPU 進行平行處理。使用 TPU 加速大規模深度學習模型訓練和推論。
- 進一步瞭解 Ray 運算子:如要進一步瞭解如何使用 Ray 運算子管理 Ray 部署作業 (包括 Ray 工作),請參閱「關於 GKE 上的 Ray」。
- 使用 Kueue 處理工作佇列:瞭解 Kubernetes 原生工作佇列系統 Kueue 如何管理及優先處理 AI/機器學習工作負載,進而提升資源使用率。詳情請參閱「針對混合式 AI/ML 訓練和推論工作負載,最佳化 GKE 資源用量」。