在 vSphere 上启动 Ray 集群#

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

要在 vSphere 上启动 Ray 集群,您需要使用 Ray 集群启动器以及 VMware vSphere Automation SDK for Python。

准备 vSphere 环境#

如果您尚未部署 vSphere,可以通过阅读 vSphere 文档了解更多信息。vSphere Ray 集群启动器需要 vSphere 8.0 或更高版本,以及创建 Ray 集群所需的以下先决条件。

另一种准备 vSphere 环境的方法是使用 VMware Cloud Foundation (VCF)。VCF 是一个统一的软件定义数据中心 (SDDC) 平台,它将 vSphere、vSAN 和 NSX 无缝集成到一个原生集成栈中,为私有云和公共云环境提供企业级云基础设施。如果您使用 VCF,可以参考 VCF 文档创建工作负载域来运行 Ray 集群。VCF 工作负载域包含一个或多个 vSphere 集群、vSAN 等共享存储以及由 NSX 管理的软件定义网络。您还可以使用 VCF 创建 NSX Edge 集群并为 Ray VM 网络创建 Segment。

准备冻结 VM#

vSphere Ray 集群启动器要求 vSphere 环境中有一个处于冻结状态的 VM,用于部署 Ray 集群。此 VM 已安装所有依赖项,之后可用于通过 VMware 的即时克隆技术快速创建头节点和工作节点。使用冻结 VM 配置 Ray 集群过程的详细信息可在这份 Ray on vSphere 架构文档中找到。

您可以按照 vm-packer-for-ray 的文档使用 Packer 创建和设置冻结 VM,或者创建一组冻结 VM,其中每个 VM 都将托管在 vSphere 集群中不同的 ESXi 主机上。默认情况下,Ray 集群的头节点和工作节点 VM 将放置在与冻结 VM 相同的资源池中。在构建和部署冻结 VM 时,有几点需要注意:

  • VM 的网络适配器应连接到上面部分配置的端口组或 NSX Segment。并且应勾选 Connect At Power On 复选框。

  • 冻结 VM 构建完成后,将在当前用户的 HOME 目录下生成一个私钥文件 (ray-bootstrap-key.pem) 和一个公钥文件 (ray_bootstrap_public_key.key)。如果想从另一台机器部署 Ray 集群,应将这些文件复制到该机器的 HOME 目录下,以便 vSphere 集群启动器能够找到它们。

  • 内容库中将生成一个 OVF。如果想在其他 vSphere 部署中部署 Ray 集群,可以使用内容库的发布和订阅功能将冻结 VM 的模板同步到另一个 vSphere 环境。然后,可以利用 Ray Cluster Launcher 帮助您首先创建一个或多个冻结 VM,然后再帮助您创建 Ray 集群,请查阅文档了解如何编写 yaml 文件以帮助从 OVF 模板部署冻结 VM。

安装 Ray 集群启动器#

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

# install ray
pip install -U ray[default]

安装 VMware vSphere Automation SDK for Python#

接下来,安装 VMware vSphere Automation SDK for Python。

# Install the VMware vSphere Automation SDK for Python.
pip install 'git+https://github.com/vmware/vsphere-automation-sdk-python.git'

您可以附加版本标签来安装特定版本。

# Install the v8.0.1.0 version of the SDK.
pip install 'git+https://github.com/vmware/[email protected]'

使用 Ray 集群启动器启动 Ray#

vSphere Automation SDK 安装完成后,您就可以使用集群启动器启动集群了。提供的集群配置文件将创建一个小型集群,其中包含一个头节点,该头节点配置为自动扩缩容至最多两个工作节点。

请注意,您需要通过设置环境变量或将其添加到 Ray 集群配置 YAML 文件中来配置您的 vSphere 凭据和 vCenter 服务器地址。

通过在本地机器上运行以下命令来测试它是否工作

# Download the example-full.yaml
wget https://raw.githubusercontent.com/ray-project/ray/master/python/ray/autoscaler/vsphere/example-full.yaml

# Setup vSphere credentials using environment variables
export VSPHERE_SERVER=vcenter-address.example.com
export VSPHERE_USER=foo
export VSPHERE_PASSWORD=bar

# Edit the example-full.yaml to update the frozen VM related configs under vsphere_config. there are 3 options:
# 1. If you have a single frozen VM, set the "name" under "frozen_vm".
# 2. If you have a set of frozen VMs in a resource pool (one VM on each ESXi host), set the "resource_pool" under "frozen_vm".
# 3. If you don't have any existing frozen VM in the vSphere cluster, but you have an OVF template of a frozen VM, set the "library_item" under "frozen_vm". After that, you need to either set the "name" of the to-be-deployed frozen VM, or set the "resource_pool" to point to an existing resource pool for the to-be-deployed frozen VMs for all the ESXi hosts in the vSphere cluster. Also, the "datastore" must be specified.
# Optionally configure the head and worker node resource pool and datastore placement.
# If not configured via environment variables, the vSphere credentials can alternatively be configured in this file.

# 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

恭喜,您已在 vSphere 上启动了一个 Ray 集群!

配置 vSAN 文件服务作为 Ray AI Libraries 的持久存储#

从 Ray 2.7 开始,Ray AI Libraries (Train and Tune) 将要求用户在运行分布式训练或调优作业时提供云存储或 NFS 路径。在带有 vSAN 数据存储的 vSphere 环境中,您可以利用 vSAN 文件服务功能将 vSAN 用作共享持久存储。您可以参考这份 vSAN 文件服务文档创建和配置 vSAN 支持的 NFS 文件共享。一般步骤如下:

  1. 启用 vSAN 文件服务并配置域名信息和 IP 地址池。

  2. 创建一个使用 NFS 作为协议的 vSAN 文件共享。

  3. 查看文件共享信息以获取 NFS 导出路径。

创建文件共享后,可以将其挂载到头节点和工作节点,并将挂载路径用作 Ray Train 和 Tune 中 RunConfig 参数的 storage_path