在 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
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.-
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。
- 建立並啟用新的虛擬環境。
- 在虛擬環境中執行
pip install -U "ray[default]"指令,安裝最新穩定版 Ray。 - 如要驗證安裝,請執行下列指令:
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 \ --location=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 \ --location=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 叢集收集記錄。--location=us-central1:指定要建立 GKE 叢集的地理區域。請注意,加速器的可用性和支援類型因位置而異。詳情請參閱「GPU 區域和可用區」。--enable-autoprovisioning:根據工作負載需求,自動建立及刪除節點集區,並調整這些集區中的節點。--max-cpu:叢集可擴充的核心數量上限。--max-memory:叢集可擴充的記憶體容量上限 (以 GB 為單位)。
設定
kubectl,與叢集通訊:gcloud container clusters get-credentials my-ray-enabled-cluster \ --location=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 Operator 如何在 GKE 中佈建及管理實際的 Ray 叢集元件 (首要節點和 worker 節點)。根據預設,GKE 會將 head Pod 排定在 E2 機器系列的節點上。範例指令會使用下列設定:
kubectl ray create cluster my-ray-cluster:指定要建立的 Ray 叢集名稱。--worker-node-selectors:指示 Kubernetes 在標有cloud.google.com/compute-class=gpu-compute-class的節點上排定 worker Pod。
驗證 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 worker 節點的一或多個 Pod,名稱類似
my-ray-cluster-worker-group-0-xxxxx。worker 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 資訊主頁,您可以在這裡查看執行中工作的相關資訊,包括狀態、記錄和資源用量。 - Metrics Explorer:使用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 Operator:如要進一步瞭解如何使用 Ray Operator 管理 Ray 部署作業 (包括 Ray 工作),請參閱「關於 GKE 上的 Ray」。
- 使用 Kueue 進行工作佇列:瞭解 Kueue 這項 Kubernetes 原生工作佇列系統,如何協助管理 AI/機器學習工作負載並排定優先順序,進而提高資源使用率。詳情請參閱「針對混合式 AI/機器學習訓練和推論工作負載,盡可能提高 GKE 資源使用率」。