入门指南#
这个快速入门展示了 Ray 集群的功能。使用 Ray 集群,我们将一个设计为在笔记本电脑上运行的示例应用程序扩展到云端。Ray 只需几个命令即可启动集群并扩展 Python。
要手动启动 Ray 集群,您可以参考 本地集群设置 指南。
关于演示#
本演示将介绍一个端到端的流程:
创建一个(基本的)Python应用程序。
在云服务提供商上启动一个集群。
在云中运行应用程序。
要求#
要运行此演示,您需要:
Python 安装在你的开发机器上(通常是你的笔记本电脑),并且
您首选的云服务提供商(AWS、GCP、Azure、阿里云或vSphere)的账户。
设置#
在开始之前,您需要安装一些 Python 依赖项,如下所示:
$ pip install -U "ray[default]" boto3
$ pip install -U "ray[default]" google-api-python-client
$ pip install -U "ray[default]" azure-cli azure-core
$ pip install -U "ray[default]" aliyun-python-sdk-core aliyun-python-sdk-ecs
阿里云集群启动器维护者(GitHub 用户名):@zhuangzhuang131419, @chenk008
$ pip install -U "ray[default]" "git+https://github.com/vmware/vsphere-automation-sdk-python.git"
vSphere 集群启动器维护者(GitHub 用户名):@LaynePeng, @roshankathawate, @JingChen23
接下来,如果你还没有从命令行使用你的云服务提供商,你需要配置你的凭证:
使用 az login
登录,然后使用 az account set -s <subscription_id>
配置您的凭据。
按照 文档 中的描述获取并设置阿里云账号的AccessKey对。
确保为RAM用户授予必要的权限,并在集群配置文件中设置AccessKey对。参考提供的 aliyun/example-full.yaml 示例集群配置。
$ export VSPHERE_SERVER=192.168.0.1 # Enter your vSphere vCenter Address
$ export VSPHERE_USER=user # Enter your username
$ export VSPHERE_PASSWORD=password # Enter your password
创建一个(基本的)Python应用程序#
我们将编写一个简单的Python应用程序,用于跟踪在其任务执行的机器的IP地址:
from collections import Counter
import socket
import time
def f():
time.sleep(0.001)
# Return IP address.
return socket.gethostbyname("localhost")
ip_addresses = [f() for _ in range(10000)]
print(Counter(ip_addresses))
将此应用程序保存为 script.py
并通过运行命令 python script.py
来执行它。该应用程序应运行10秒并输出类似于 Counter({'127.0.0.1': 10000})
的内容。
通过一些小的改动,我们可以让这个应用在 Ray 上运行(关于如何做到这一点,请参考 Ray 核心演练):
from collections import Counter
import socket
import time
import ray
ray.init()
@ray.remote
def f():
time.sleep(0.001)
# Return IP address.
return socket.gethostbyname("localhost")
object_ids = [f.remote() for _ in range(10000)]
ip_addresses = ray.get(object_ids)
print(Counter(ip_addresses))
最后,让我们添加一些代码,使输出更有趣:
from collections import Counter
import socket
import time
import ray
ray.init()
print('''This cluster consists of
{} nodes in total
{} CPU resources in total
'''.format(len(ray.nodes()), ray.cluster_resources()['CPU']))
@ray.remote
def f():
time.sleep(0.001)
# Return IP address.
return socket.gethostbyname("localhost")
object_ids = [f.remote() for _ in range(10000)]
ip_addresses = ray.get(object_ids)
print('Tasks executed')
for ip_address, num_tasks in Counter(ip_addresses).items():
print(' {} tasks on {}'.format(num_tasks, ip_address))
运行 python script.py
现在应该输出类似的内容:
This cluster consists of
1 nodes in total
4.0 CPU resources in total
Tasks executed
10000 tasks on 127.0.0.1
在云服务提供商上启动一个集群#
要启动一个 Ray 集群,首先我们需要定义集群配置。集群配置是在一个 YAML 文件中定义的,该文件将由集群启动器用于启动头节点,并由自动缩放器用于启动工作节点。
一个最小的集群配置文件示例如下:
# An unique identifier for the head node and workers of this cluster.
cluster_name: aws-example-minimal
# Cloud-provider specific configuration.
provider:
type: aws
region: us-west-2
# The maximum number of workers nodes to launch in addition to the head
# node.
max_workers: 3
# Tell the autoscaler the allowed node types and the resources they provide.
# The key is the name of the node type, which is for debugging purposes.
# The node config specifies the launch config and physical instance type.
available_node_types:
ray.head.default:
# The node type's CPU and GPU resources are auto-detected based on AWS instance type.
# If desired, you can override the autodetected CPU and GPU resources advertised to the autoscaler.
# You can also set custom resources.
# For example, to mark a node type as having 1 CPU, 1 GPU, and 5 units of a resource called "custom", set
# resources: {"CPU": 1, "GPU": 1, "custom": 5}
resources: {}
# Provider-specific config for this node type, e.g., instance type. By default
# Ray auto-configures unspecified fields such as SubnetId and KeyName.
# For more documentation on available fields, see
# http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.ServiceResource.create_instances
node_config:
InstanceType: m5.large
ray.worker.default:
# The minimum number of worker nodes of this type to launch.
# This number should be >= 0.
min_workers: 3
# The maximum number of worker nodes of this type to launch.
# This parameter takes precedence over min_workers.
max_workers: 3
# The node type's CPU and GPU resources are auto-detected based on AWS instance type.
# If desired, you can override the autodetected CPU and GPU resources advertised to the autoscaler.
# You can also set custom resources.
# For example, to mark a node type as having 1 CPU, 1 GPU, and 5 units of a resource called "custom", set
# resources: {"CPU": 1, "GPU": 1, "custom": 5}
resources: {}
# Provider-specific config for this node type, e.g., instance type. By default
# Ray auto-configures unspecified fields such as SubnetId and KeyName.
# For more documentation on available fields, see
# http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.ServiceResource.create_instances
node_config:
InstanceType: m5.large
# A unique identifier for the head node and workers of this cluster.
cluster_name: minimal
# Cloud-provider specific configuration.
provider:
type: gcp
region: us-west1
# An unique identifier for the head node and workers of this cluster.
cluster_name: minimal
# Cloud-provider specific configuration.
provider:
type: azure
location: westus2
resource_group: ray-cluster
# How Ray will authenticate with newly launched nodes.
auth:
ssh_user: ubuntu
# you must specify paths to matching private and public key pair files
# use `ssh-keygen -t rsa -b 4096` to generate a new ssh key pair
ssh_private_key: ~/.ssh/id_rsa
# changes to this should match what is specified in file_mounts
ssh_public_key: ~/.ssh/id_rsa.pub
请参考 example-full.yaml。
确保您的账户余额不少于100元,否则您将收到错误 InvalidAccountStatus.NotEnoughBalance
。
# An unique identifier for the head node and workers of this cluster.
cluster_name: minimal
# Cloud-provider specific configuration.
provider:
type: vsphere
将此配置文件保存为 config.yaml
。您可以在配置文件中指定更多详细信息:要使用的实例类型、要启动的最小和最大工作节点数、自动扩展策略、要同步的文件等。有关可用配置属性的完整参考,请参阅 集群 YAML 配置选项参考。
在定义了我们的配置之后,我们将使用 Ray 集群启动器在云上启动一个集群,创建一个指定的“头节点”和工作节点。要启动 Ray 集群,我们将使用 Ray CLI。运行以下命令:
$ ray up -y config.yaml
在 Ray 集群上运行应用程序#
我们现在准备好在我们创建的Ray集群上执行一个应用程序。ray.init()
现在将自动连接到新创建的集群。
作为一个快速示例,我们在 Ray 集群上执行一个连接到 Ray 并退出的 Python 命令:
$ ray exec config.yaml 'python -c "import ray; ray.init()"'
2022-08-10 11:23:17,093 INFO worker.py:1312 -- Connecting to existing Ray cluster at address: <remote IP address>:6379...
2022-08-10 11:23:17,097 INFO worker.py:1490 -- Connected to Ray cluster.
你也可以选择使用 ray attach
获取远程shell并在集群上直接运行命令。此命令将创建一个到Ray集群头节点的SSH连接。
# From a remote client:
$ ray attach config.yaml
# Now on the head node...
$ python -c "import ray; ray.init()"
有关 Ray 集群 CLI 工具的完整参考,请参阅 集群命令参考。
虽然这些工具对于在 Ray 集群上的临时执行很有用,但在 Ray 集群上执行应用程序的推荐方法是使用 Ray Jobs。查看 快速入门指南 以开始使用!
删除一个 Ray 集群#
要关闭您的集群,请运行以下命令:
$ ray down -y config.yaml