在 GCP 上启动 Ray 集群#
本指南详细介绍了在 GCP 中启动 Ray 集群所需的步骤。
要启动 GCP Ray 集群,您将使用 Ray 集群启动器和 Google API 客户端。
安装 Ray 集群启动器#
Ray 集群启动器是 ray CLI 的一部分。您可以使用 CLI 通过 ray up、ray down 和 ray attach 等命令来启动、停止和附加到正在运行的 Ray 集群。您可以使用 pip 安装支持集群启动器的 ray CLI。有关更详细的说明,请遵循Ray 安装文档。
# install ray
pip install -U ray[default]
安装和配置 Google API 客户端#
如果您从未创建过 Google API 控制台项目,请阅读 Google Cloud 的 管理项目页面,并在 Google API 控制台 中创建一个项目。接下来,使用 pip install -U google-api-python-client 安装 Google API 客户端。
# Install the Google API Client.
pip install google-api-python-client
使用 Ray 集群启动器启动 Ray#
一旦 Google API 客户端配置为管理您的 GCP 账户上的资源,您就可以启动您的集群了。提供的 集群配置文件 将创建一个小型集群,其中包含一个按需的 n1-standard-2 头节点,并配置为自动扩展到最多两个 n1-standard-2 抢占式工作节点。请注意,您需要在这些模板中填入您的 GCP project_id。
通过从本地机器运行以下命令来测试它是否正常工作
# Download the example-full.yaml
wget https://raw.githubusercontent.com/ray-project/ray/master/python/ray/autoscaler/gcp/example-full.yaml
# Edit the example-full.yaml to update project_id.
# vi example-full.yaml
# Create or update the cluster. When the command finishes, it will print
# out the command that can be used to SSH into the cluster head node.
ray up example-full.yaml
# Get a remote screen on the head node.
ray attach example-full.yaml
# Try running a Ray program.
python -c 'import ray; ray.init()'
exit
# Tear down the cluster.
ray down example-full.yaml
恭喜,您已在 GCP 上成功启动了 Ray 集群!
GCP 配置#
使用服务账号运行工作节点#
默认情况下,只有头节点会使用服务账号 (ray-autoscaler-sa-v1@<project-id>.iam.gserviceaccount.com) 运行。要使工作节点能够使用相同的服务账号运行(以便访问 Google Cloud Storage 或 GCR),请将以下配置添加到 worker_node 配置中:
available_node_types:
ray.worker.default:
node_config:
...
serviceAccounts:
- email: ray-autoscaler-sa-v1@<YOUR_PROJECT_ID>.iam.gserviceaccount.com
scopes:
- https://www.googleapis.com/auth/cloud-platform