集群启动器命令#

本文档概述了使用 Ray 集群启动器的常用命令。有关如何自定义配置文件,请参阅 集群配置 文档。

启动一个集群 (ray up)#

这将启动云中的机器,安装您的依赖项并运行您拥有的任何设置命令,自动配置 Ray 集群,并准备扩展您的分布式系统。请参阅 ray up文档。示例配置文件可以在此处访问 这里

小技巧

工作节点只有在头节点完成启动后才会开始启动。要监控集群设置的进度,您可以运行 ray monitor <cluster yaml>

# Replace '<your_backend>' with one of: 'aws', 'gcp', 'kubernetes', or 'local'.
$ BACKEND=<your_backend>

# Create or update the cluster.
$ ray up ray/python/ray/autoscaler/$BACKEND/example-full.yaml

# Tear down the cluster.
$ ray down ray/python/ray/autoscaler/$BACKEND/example-full.yaml

更新现有集群 (ray up)#

如果你想更新你的集群配置(添加更多文件,更改依赖项),请在现有集群上再次运行 ray up

此命令检查本地配置是否与集群的已应用配置不同。这包括对配置中 file_mounts 部分指定的同步文件所做的任何更改。如果是这样,新文件和配置将被上传到集群。随后,Ray 服务/进程将被重启。

小技巧

不要对云服务提供商的规格进行此类操作(例如,在运行中的集群上从AWS改为GCP),也不要更改集群名称(因为这将启动一个新集群,并使原始集群成为孤儿)。

你也可以运行 ray up 来重启一个集群,如果它似乎处于不良状态(即使没有配置更改,这也会重启所有 Ray 服务)。

在现有集群上运行 ray up 将执行以下所有操作:

  • 如果头节点匹配集群规范,文件挂载将被重新应用,并且 setup_commandsray start 命令将被运行。这里可能会有一些缓存行为来跳过设置/文件挂载。

  • 如果头节点与指定的 YAML 文件不一致(例如,head_node_type 在 YAML 中发生了变化),那么过期的节点将被终止,并提供一个新节点来替换它。设置/文件挂载/ray start 将被应用。

  • 在头节点达到一致状态后(在 ray start 命令完成后),上述过程将应用于所有工作节点。ray start 命令倾向于运行 ray stop + ray start,因此这将终止当前正在运行的作业。

如果你不希望更新时重启服务(例如因为更改不需要重启),请在更新调用时传递 --no-restart

如果你想强制重新生成配置以获取云环境中可能的变化,请在更新调用时传递 --no-config-cache

如果你想跳过设置命令,只在所有节点上运行 ray stop/ray start,请在更新调用时传递 --restart-only

查看 ray up文档

# Reconfigure autoscaling behavior without interrupting running jobs.
$ ray up ray/python/ray/autoscaler/$BACKEND/example-full.yaml \
    --max-workers=N --no-restart

在集群上运行shell命令(ray exec#

您可以使用 ray exec 方便地在集群上运行命令。请参阅 ray exec文档

# Run a command on the cluster
$ ray exec cluster.yaml 'echo "hello world"'

# Run a command on the cluster, starting it if needed
$ ray exec cluster.yaml 'echo "hello world"' --start

# Run a command on the cluster, stopping the cluster after it finishes
$ ray exec cluster.yaml 'echo "hello world"' --stop

# Run a command on a new cluster called 'experiment-1', stopping it after
$ ray exec cluster.yaml 'echo "hello world"' \
    --start --stop --cluster-name experiment-1

# Run a command in a detached tmux session
$ ray exec cluster.yaml 'echo "hello world"' --tmux

# Run a command in a screen (experimental)
$ ray exec cluster.yaml 'echo "hello world"' --screen

如果你想在集群上运行可以通过网页浏览器访问的应用程序(例如,Jupyter notebook),你可以使用 --port-forward。本地打开的端口与远程端口相同。

$ ray exec cluster.yaml --port-forward=8899 'source ~/anaconda3/bin/activate tensorflow_p36 && jupyter notebook --port=8899'

备注

对于Kubernetes集群,执行命令时不能使用 port-forward 选项。要进行端口转发并运行命令,你需要分别调用两次 ray exec

在集群上运行 Ray 脚本(ray submit#

你也可以使用 ray submit 在集群上执行 Python 脚本。这将 rsync 指定的文件到头节点集群并使用给定的参数执行它。查看 文档 以获取 ray submit 的更多信息。

# Run a Python script in a detached tmux session
$ ray submit cluster.yaml --tmux --start --stop tune_experiment.py

# Run a Python script with arguments.
# This executes script.py on the head node of the cluster, using
# the command: python ~/script.py --arg1 --arg2 --arg3
$ ray submit cluster.yaml script.py -- --arg1 --arg2 --arg3

连接到运行中的集群 (ray attach)#

你可以使用 ray attach 来连接到集群上的交互式屏幕会话。查看 文档 中的 ray attach 部分,或运行 ray attach --help

# Open a screen on the cluster
$ ray attach cluster.yaml

# Open a screen on a new cluster called 'session-1'
$ ray attach cluster.yaml --start --cluster-name=session-1

# Attach to tmux session on cluster (creates a new one if none available)
$ ray attach cluster.yaml --tmux

从集群同步文件(ray rsync-up/down#

要下载或上传文件到集群头节点,请使用 ray rsync_downray rsync_up

$ ray rsync_down cluster.yaml '/path/on/cluster' '/local/path'
$ ray rsync_up cluster.yaml '/local/path' '/path/on/cluster'

监控集群状态 (ray dashboard/status)#

Ray 还附带了一个在线仪表板。该仪表板可以通过 HTTP 在头节点上访问(默认情况下它在 localhost:8265 上监听)。您还可以使用内置的 ray dashboard 来自动设置端口转发,使远程仪表板可以在您的本地浏览器中通过 localhost:8265 查看。

$ ray dashboard cluster.yaml

你可以通过在主节点上运行以下命令来监控集群使用情况和自动扩展状态:

$ ray status

要查看状态的实时更新:

$ watch -n 1 ray status

Ray 自动扩展器还以实例标签的形式报告每个节点的状态。在您的云服务提供商控制台中,您可以点击一个节点,进入“标签”窗格,并将 ray-node-status 标签添加为列。这样您就可以一目了然地查看每个节点的状态:

../../../_images/autoscaler-status.png

常见工作流程:同步 git 分支#

一个常见的用例是将特定的本地 git 分支同步到集群的所有工作节点。然而,如果你只是在设置命令中放入 git checkout <branch>,自动缩放器将不知道何时重新运行该命令以拉取更新。通过在输入中包含 git SHA(如果分支更新,文件的哈希值将改变),可以很好地解决这个问题:

file_mounts: {
    "/tmp/current_branch_sha": "/path/to/local/repo/.git/refs/heads/<YOUR_BRANCH_NAME>",
}

setup_commands:
    - test -e <REPO_NAME> || git clone https://github.com/<REPO_ORG>/<REPO_NAME>.git
    - cd <REPO_NAME> && git fetch && git checkout `cat /tmp/current_branch_sha`

这告诉 ray up 将当前 git 分支的 SHA 从您的个人电脑同步到集群上的一个临时文件(假设您已经推送了分支头)。然后,设置命令读取该文件以确定节点上应检出的 SHA。请注意,每个命令在其自己的会话中运行。更新集群的最终工作流程变为如下:

  1. 对 git 分支进行本地更改

  2. 使用 git commitgit push 提交更改

  3. 使用 ray up 更新您的 Ray 集群上的文件