配置 KubeRay 在 GKE 中使用 Google Cloud Storage 存储桶#
如果您已经熟悉 GKE 中的工作负载身份 (Workload Identity),则可以跳过本文档。要点是,在将您的 Kubernetes 服务账号与您的 Google Cloud 服务账号关联后,您需要在每个 Ray Pod 中指定一个服务账号。否则,请继续阅读。
此示例是文档 https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity 的缩写版本。如果您对详细信息感兴趣,完整文档值得一读。
在 GKE 上创建 Kubernetes 集群#
此示例使用 GKE 创建一个最小的 KubeRay 集群。
请在您的本地机器或 Google Cloud Shell 上运行此命令及所有后续命令。如果在本地机器上运行,请安装 Google Cloud SDK。
gcloud container clusters create cloud-bucket-cluster \
--num-nodes=1 --min-nodes 0 --max-nodes 1 --enable-autoscaling \
--zone=us-west1-b --machine-type e2-standard-8 \
--workload-pool=my-project-id.svc.id.goog # Replace my-project-id with your GCP project ID
此命令在 us-west1-b
区域创建一个名为 cloud-bucket-cluster
的 Kubernetes 集群,其中包含一个节点。此示例使用 e2-standard-8
机器类型,它具有 8 个 vCPU 和 32 GB RAM。
有关如何查找项目 ID 的更多信息,请参阅 https://support.google.com/googleapi/answer/7014113?hl=en 或 https://cloud.google.com/resource-manager/docs/creating-managing-projects。
现在获取集群凭据以便与 kubectl
一起使用
gcloud container clusters get-credentials cloud-bucket-cluster --zone us-west1-b --project my-project-id
创建 IAM 服务账号#
gcloud iam service-accounts create my-iam-sa
创建 Kubernetes 服务账号#
kubectl create serviceaccount my-ksa
将 Kubernetes 服务账号与 IAM 服务账号相互关联#
在以下两个命令中,如果您不使用默认命名空间,请将 default
替换为您的命名空间。
gcloud iam service-accounts add-iam-policy-binding [email protected] \
--role roles/iam.workloadIdentityUser \
--member "serviceAccount:my-project-id.svc.id.goog[default/my-ksa]"
kubectl annotate serviceaccount my-ksa \
--namespace default \
iam.gke.io/gcp-service-account=[email protected]
创建 Google Cloud Storage 存储桶并允许 Google Cloud 服务账号访问它#
请按照 https://cloud.google.com/storage/docs/creating-buckets 中的文档,使用 Google Cloud Console 或 gsutil
命令行工具创建一个存储桶。
此示例授予主体 my-iam-sa@my-project-id.iam.gserviceaccount.com
对存储桶的“Storage Admin”权限。在 Google Cloud Console(“存储桶”>“存储桶详情”下的“权限”选项卡)或使用以下命令启用权限
gsutil iam ch serviceAccount:[email protected]:roles/storage.admin gs://my-bucket
创建最小的 RayCluster YAML 清单#
您可以使用 curl
下载本教程的 RayCluster YAML 清单,如下所示
curl -LO https://raw.githubusercontent.com/ray-project/kuberay/v1.3.0/ray-operator/config/samples/ray-cluster.gke-bucket.yaml
关键部分是以下几行
spec:
serviceAccountName: my-ksa
nodeSelector:
iam.gke.io/gke-metadata-server-enabled: "true"
在 Ray 集群的每个 Pod 规约中都包含这些行。为简单起见,本示例使用单节点集群(1 个头节点和 0 个工作节点)。
创建 RayCluster#
kubectl apply -f ray-cluster.gke-bucket.yaml
从 RayCluster 测试 GCS 存储桶访问#
使用 kubectl get pod
获取 Ray 头节点 Pod 的名称。然后运行以下命令在 Ray 头节点 Pod 中获取一个 shell
kubectl exec -it raycluster-mini-head-xxxx -- /bin/bash
在 shell 中,运行 pip install google-cloud-storage
安装 Google Cloud Storage Python 客户端库。
(对于生产用例,您需要确保在集群的每个节点上都安装了 google-cloud-storage
,或者使用 ray.init(runtime_env={"pip": ["google-cloud-storage"]})
在运行时按需安装包 - 有关更多详细信息,请参阅 https://docs.rayai.org.cn/en/latest/ray-core/handling-dependencies.html#runtime-environments)。
然后运行以下 Python 代码测试对存储桶的访问
import ray
import os
from google.cloud import storage
GCP_GCS_BUCKET = "my-bucket"
GCP_GCS_FILE = "test_file.txt"
ray.init(address="auto")
@ray.remote
def check_gcs_read_write():
client = storage.Client()
bucket = client.get_bucket(GCP_GCS_BUCKET)
blob = bucket.blob(GCP_GCS_FILE)
# Write to the bucket
blob.upload_from_string("Hello, Ray on GKE!")
# Read from the bucket
content = blob.download_as_text()
return content
result = ray.get(check_gcs_read_write.remote())
print(result)
您应该看到以下输出
Hello, Ray on GKE!