启动本地集群#
本文档介绍了如何设置本地 Ray 集群,即在裸机或私有云上运行 Ray。我们提供两种启动本地集群的方法。
手动设置 Ray 集群#
本节假设你有一系列机器,并且集群中的节点共享同一网络。它还假设 Ray 已安装在每台机器上。你可以使用 pip 安装支持集群启动器的 ray 命令行工具。有关更多详细信息,请遵循Ray 安装说明。
# install ray
pip install -U "ray[default]"
启动头节点#
选择任意一个节点作为头节点并运行以下命令。如果省略 --port
参数,Ray 将首先选择端口 6379,如果 6379 正在使用,则回退到随机端口。
ray start --head --port=6379
该命令将打印出 Ray 集群地址,该地址可以传递给其他机器上的 ray start
命令来启动工作节点(见下文)。如果你收到 ConnectionError,请检查你的防火墙设置和网络配置。
启动工作节点#
然后在其他每个节点上,运行以下命令连接到你刚创建的头节点。
ray start --address=<head-node-address:port>
确保将 head-node-address:port
替换为头节点上命令打印出的值(它应该看起来像 123.45.67.89:6379)。
请注意,如果你的计算节点位于具有网络地址转换 (NAT) 的自己的子网中,则从该子网之外的机器连接时,头节点打印的地址将无法工作。你需要使用可以从远程机器访问的头节点地址。如果头节点具有域名地址(例如 compute04.berkeley.edu),你可以直接使用它代替 IP 地址并依赖 DNS。
Ray 会自动检测每个节点上可用的资源(例如,CPU),但你也可以通过将自定义资源传递给 ray start
命令来手动覆盖此设置。例如,如果你希望指定一台机器拥有 10 个 CPU 和 1 个 GPU 供 Ray 使用,可以使用 --num-cpus=10
和 --num-gpus=1
标志来实现。有关更多信息,请参阅配置页面。
故障排除#
如果你看到 Unable to connect to GCS at ...
,这意味着在给定的 --address
处无法访问头节点。一些可能的原因包括
头节点实际上没有运行;
在指定地址运行的是不同版本的 Ray;
指定的地址错误;
或者有防火墙设置阻止访问。
如果连接失败,要检查从节点是否可以访问每个端口,你可以使用 nmap 或 nc 等工具。
$ nmap -sV --reason -p $PORT $HEAD_ADDRESS
Nmap scan report for compute04.berkeley.edu (123.456.78.910)
Host is up, received echo-reply ttl 60 (0.00087s latency).
rDNS record for 123.456.78.910: compute04.berkeley.edu
PORT STATE SERVICE REASON VERSION
6379/tcp open redis? syn-ack
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
$ nc -vv -z $HEAD_ADDRESS $PORT
Connection to compute04.berkeley.edu 6379 port [tcp/*] succeeded!
如果节点无法访问该 IP 地址上的该端口,你可能会看到
$ nmap -sV --reason -p $PORT $HEAD_ADDRESS
Nmap scan report for compute04.berkeley.edu (123.456.78.910)
Host is up (0.0011s latency).
rDNS record for 123.456.78.910: compute04.berkeley.edu
PORT STATE SERVICE REASON VERSION
6379/tcp closed redis reset ttl 60
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
$ nc -vv -z $HEAD_ADDRESS $PORT
nc: connect to compute04.berkeley.edu port 6379 (tcp) failed: Connection refused
使用 Ray 集群启动器#
Ray 集群启动器是 ray
命令行工具的一部分。它允许你使用 ray up
、ray down
和 ray attach
等命令启动、停止和附加到正在运行的 Ray 集群。你可以使用 pip 安装它,或遵循安装 Ray以获取更详细的说明。
# install ray
pip install "ray[default]"
使用 Ray 集群启动器启动 Ray#
提供的 example-full.yaml 集群配置文件将根据节点列表创建 Ray 集群。
请注意,你需要填写模板中的 head_ip、worker_ips 列表和 ssh_user 字段
通过在本地机器上运行以下命令来测试是否正常工作
# Download the example-full.yaml
wget https://raw.githubusercontent.com/ray-project/ray/master/python/ray/autoscaler/local/example-full.yaml
# Update the example-full.yaml to update head_ip, worker_ips, and ssh_user.
# 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.
# Tear down the cluster.
ray down example-full.yaml
恭喜,你已成功启动本地 Ray 集群!