在 GCP 上启动 Ray 集群#

本指南详细介绍了在 GCP 中启动 Ray 集群所需的步骤。

要启动 GCP Ray 集群,您需要使用 Ray 集群启动器配合 Google API 客户端。

安装 Ray 集群启动器#

Ray 集群启动器是 ray CLI 的一部分。您可以使用 CLI 通过 ray upray downray attach 等命令启动、停止和连接到正在运行的 Ray 集群。您可以使用 pip 安装支持集群启动器的 Ray CLI。有关更详细的说明,请参阅Ray 安装文档

# install ray
pip install -U ray[default]

安装和配置 Google API 客户端#

如果您从未创建过 Google APIs Console 项目,请阅读 Google Cloud 的“管理项目”页面并在Google API Console中创建一个项目。然后,使用 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 配置#

使用 Service Account 运行工作节点#

默认情况下,只有头节点使用 Service Account (ray-autoscaler-sa-v1@<project-id>.iam.gserviceaccount.com) 运行。要使工作节点也能使用此 Service Account (以访问 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