在 AWS 上启动 Ray 集群#

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

要启动 AWS Ray 集群,您应该使用 Ray 集群启动器以及 AWS Python SDK。

安装 Ray 集群启动器#

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

# install ray
pip install -U ray[default]

安装并配置 AWS Python SDK (Boto3)#

接下来,使用 pip install -U boto3 安装 AWS SDK,并按照 AWS 指南 配置您的 AWS 凭证。

# install AWS Python SDK (boto3)
pip install -U boto3

# setup AWS credentials using environment variables
export AWS_ACCESS_KEY_ID=foo
export AWS_SECRET_ACCESS_KEY=bar
export AWS_SESSION_TOKEN=baz

# alternatively, you can setup AWS credentials using ~/.aws/credentials file
echo "[default]
aws_access_key_id=foo
aws_secret_access_key=bar
aws_session_token=baz" >> ~/.aws/credentials

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

配置好 Boto3 以管理您的 AWS 账户中的资源后,您就可以使用集群启动器启动集群了。提供的集群配置文件将创建一个小型集群,包含一个 m5.large 的 head 节点(按需实例),配置为自动扩展到最多两个 m5.large Spot 实例 worker。

从您的本地机器运行以下命令来测试它是否工作

# Download the example-full.yaml
wget https://raw.githubusercontent.com/ray-project/ray/master/python/ray/autoscaler/aws/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 shell 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

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

如果您想了解更多关于 Ray 集群启动器的信息,请参阅这篇博客文章,其中提供了分步指南

AWS 配置#

使用 Amazon EFS#

要在 Ray 集群中使用 Amazon EFS,您需要安装一些额外的实用工具并在 setup_commands 中挂载 EFS。请注意,这些说明仅在使用 AWS 上的 Ray 集群启动器时有效。

# Note You need to replace the {{FileSystemId}} with your own EFS ID before using the config.
# You may also need to modify the SecurityGroupIds for the head and worker nodes in the config file.

setup_commands:
    - sudo kill -9 `sudo lsof /var/lib/dpkg/lock-frontend | awk '{print $2}' | tail -n 1`;
        sudo pkill -9 apt-get;
        sudo pkill -9 dpkg;
        sudo dpkg --configure -a;
        sudo apt-get -y install binutils;
        cd $HOME;
        git clone https://github.com/aws/efs-utils;
        cd $HOME/efs-utils;
        ./build-deb.sh;
        sudo apt-get -y install ./build/amazon-efs-utils*deb;
        cd $HOME;
        mkdir efs;
        sudo mount -t efs {{FileSystemId}}:/ efs;
        sudo chmod 777 efs;

配置 IAM Role 和 EC2 Instance Profile#

默认情况下,Ray AWS 集群中的 Ray 节点拥有完整的 EC2 和 S3 权限(即 arn:aws:iam::aws:policy/AmazonEC2FullAccessarn:aws:iam::aws:policy/AmazonS3FullAccess)。这对于试用 Ray 集群是一个不错的默认设置,但出于各种原因(例如为了安全考虑减少权限),您可能希望更改 Ray 节点的权限。您可以通过为相关的 node_config 提供自定义的 IamInstanceProfile 来实现。

available_node_types:
  ray.worker.default:
    node_config:
      ...
      IamInstanceProfile:
        Arn: arn:aws:iam::YOUR_AWS_ACCOUNT:YOUR_INSTANCE_PROFILE

有关配置 IAM 角色和 EC2 实例配置文件 的更多详细信息,请参阅此讨论

访问 S3#

在各种场景下,worker 节点可能需要对 S3 存储桶进行写访问,例如 Ray Tune 有一个选项可以将检查点写入 S3,而不是直接将其同步回 driver。

如果您看到“无法找到凭证”之类的错误,请确保在您的集群配置文件中为 worker 节点配置了正确的 IamInstanceProfile。这可能看起来像

available_node_types:
  ray.worker.default:
    node_config:
      ...
      IamInstanceProfile:
        Arn: arn:aws:iam::YOUR_AWS_ACCOUNT:YOUR_INSTANCE_PROFILE

您可以通过 SSH 连接到 worker 节点并运行以下命令来验证设置是否正确

aws configure list

您应该会看到类似以下内容

      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************XXXX         iam-role
secret_key     ****************YYYY         iam-role
    region                <not set>             None    None

有关访问 S3 的更多详细信息,请参阅此讨论

使用 Amazon CloudWatch 监控 Ray#

Amazon CloudWatch 是一项监控和可观测性服务,它提供数据和可操作的见解,用于监控您的应用程序、响应系统范围的性能变化以及优化资源利用。CloudWatch 与 Ray 的集成需要预装了 Unified CloudWatch Agent 的 AMI(或 Docker 镜像)。

Amazon Ray Team 提供了预装了 Unified CloudWatch Agent 的 AMI,目前在美国东部(弗吉尼亚北部)us-east-1、美国东部(俄亥俄)us-east-2、美国西部(加利福尼亚北部)us-west-1 和美国西部(俄勒冈)us-west-2 区域可用。请将任何问题、意见或问题直接发送给 Amazon Ray Team

下表列出了每个区域预装了 Unified CloudWatch Agent 的 AMI,您还可以在 amazon-ray README 中找到 AMI。

所有可用的 Unified CloudWatch Agent 镜像#

基础 AMI

AMI ID

区域

Unified CloudWatch Agent 版本

AWS 深度学习 AMI (Ubuntu 18.04, 64 位)

ami-069f2811478f86c20

us-east-1

v1.247348.0b251302

AWS 深度学习 AMI (Ubuntu 18.04, 64 位)

ami-058cc0932940c2b8b

us-east-2

v1.247348.0b251302

AWS 深度学习 AMI (Ubuntu 18.04, 64 位)

ami-044f95c9ef12883ef

us-west-1

v1.247348.0b251302

AWS 深度学习 AMI (Ubuntu 18.04, 64 位)

ami-0d88d9cbe28fac870

us-west-2

v1.247348.0b251302

注意

使用 Amazon CloudWatch 会产生费用,请参阅CloudWatch 定价了解详细信息。

入门#

1. 创建一个名为 cloudwatch-basic.yaml 的最小集群配置 YAML 文件,内容如下:#

provider:
    type: aws
    region: us-west-2
    availability_zone: us-west-2a
    # Start by defining a `cloudwatch` section to enable CloudWatch integration with your Ray cluster.
    cloudwatch:
        agent:
            # Path to Unified CloudWatch Agent config file
            config: "cloudwatch/example-cloudwatch-agent-config.json"
        dashboard:
            # CloudWatch Dashboard name
            name: "example-dashboard-name"
            # Path to the CloudWatch Dashboard config file
            config: "cloudwatch/example-cloudwatch-dashboard-config.json"

auth:
    ssh_user: ubuntu

available_node_types:
    ray.head.default:
        node_config:
        InstanceType: c5a.large
        ImageId: ami-0d88d9cbe28fac870  # Unified CloudWatch agent pre-installed AMI, us-west-2
        resources: {}
    ray.worker.default:
        node_config:
            InstanceType: c5a.large
            ImageId: ami-0d88d9cbe28fac870  # Unified CloudWatch agent pre-installed AMI, us-west-2
            IamInstanceProfile:
                Name: ray-autoscaler-cloudwatch-v1
        resources: {}
        min_workers: 0

2. 下载 CloudWatch Agent 和 Dashboard 配置文件。#

首先,在与 cloudwatch-basic.yaml 相同的目录中创建一个 cloudwatch 目录。然后,将示例 CloudWatch AgentCloudWatch Dashboard 配置文件下载到 cloudwatch 目录中。

$ mkdir cloudwatch
$ cd cloudwatch
$ wget https://raw.githubusercontent.com/ray-project/ray/master/python/ray/autoscaler/aws/cloudwatch/example-cloudwatch-agent-config.json
$ wget https://raw.githubusercontent.com/ray-project/ray/master/python/ray/autoscaler/aws/cloudwatch/example-cloudwatch-dashboard-config.json

3. 运行 ray up cloudwatch-basic.yaml 启动您的 Ray 集群。#

这将默认在 us-west-2 区域启动您的 Ray 集群。当为其他区域启动集群时,您需要更改集群配置 YAML 文件中的 regionImageId。请参阅上面“Unified CloudWatch Agent 镜像”表中的各区域可用 AMI。

4. 在 CloudWatch 控制台 中查看您的 Ray 集群的日志、指标和仪表盘!#

通过确保您已安装 AWS CLI V2+,然后运行以下命令,可以查看写入 CloudWatch 日志组的所有日志的尾部:

aws logs tail $log_group_name --follow

高级设置#

请参考 example-cloudwatch.yaml 获取完整示例。

1. 选择预装了 Unified CloudWatch Agent 的 AMI。#

确保您在与 AMI 相同的区域启动您的 Ray EC2 集群,然后在您的集群配置 YAML 文件中指定您的集群 head 和 worker 节点要使用的 ImageId

以下 CLI 命令返回 us-west-2 的最新可用 Unified CloudWatch Agent 镜像

aws ec2 describe-images --region us-west-2 --filters "Name=owner-id,Values=160082703681" "Name=name,Values=*cloudwatch*" --query 'Images[*].[ImageId,CreationDate]' --output text | sort -k2 -r | head -n1
available_node_types:
    ray.head.default:
        node_config:
        InstanceType: c5a.large
        ImageId: ami-0d88d9cbe28fac870
    ray.worker.default:
        node_config:
        InstanceType: c5a.large
        ImageId: ami-0d88d9cbe28fac870

构建您自己的安装了 Unified CloudWatch Agent 的 AMI

  1. 按照 CloudWatch Agent 安装 用户指南,在 EC2 实例上安装 Unified CloudWatch Agent。

  2. 按照 EC2 AMI 创建 用户指南,从该 EC2 实例创建 AMI。

2. 定义您自己的 CloudWatch Agent、Dashboard 和 Alarm JSON 配置文件。#

您可以从使用示例 CloudWatch AgentCloudWatch DashboardCloudWatch Alarm 配置文件开始。

这些示例配置文件包含以下功能

日志和指标:写入 /tmp/ray/session_*/logs/**.out 的日志将在 {cluster_name}-ray_logs_out 日志组中可用,而写入 /tmp/ray/session_*/logs/**.err 的日志将在 {cluster_name}-ray_logs_err 日志组中可用。日志流以发出日志的 EC2 实例 ID 命名。扩展的 EC2 指标,包括 CPU/磁盘/内存使用情况和进程统计信息,可以在 {cluster_name}-ray-CWAgent 指标命名空间中找到。

仪表盘:您将拥有一个集群级别的仪表盘,显示集群总 CPU 和可用对象存储内存。进程计数、磁盘使用率、内存使用率和 CPU 利用率将以集群总和和单节点最大值/平均值的形式显示。

告警:配置了跟踪长期高内存、磁盘和 CPU 使用率的节点级别告警。告警操作未设置,必须在您的告警配置文件中手动提供。

有关更多高级选项,请参阅 AgentDashboardAlarm 配置用户指南。

CloudWatch Agent、Dashboard 和 Alarm JSON 配置文件支持以下变量

{instance_id}:替换为 Ray 集群中的每个 EC2 实例 ID。

{region}:替换为 Ray 集群所在的区域。

{cluster_name}:替换为您的 Ray 集群名称。

有关 Unified CloudWatch Agent 本身支持的其他变量,请参阅 CloudWatch Agent 配置文件详情

注意

请记住替换 CloudWatch Alarm 配置文件中的 AlarmActions 占位符!

"AlarmActions":[
    "TODO: Add alarm actions! See https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html"
 ]

3. 在集群配置 YAML 中引用您的 CloudWatch JSON 配置文件。#

指定 CloudWatch JSON 配置文件相对于您运行 ray up 的工作目录的文件路径

provider:
   cloudwatch:
       agent:
           config: "cloudwatch/example-cloudwatch-agent-config.json"

4. 设置您的 IAM Role 和 EC2 Instance Profile。#

默认情况下,ray-autoscaler-cloudwatch-v1 IAM 角色和 EC2 实例配置文件在 Ray 集群启动时创建。此角色包含将 CloudWatch 与 Ray 集成所需的所有额外权限,即 CloudWatchAgentAdminPolicyAmazonSSMManagedInstanceCoressm:SendCommandssm:ListCommandInvocationsiam:PassRole 托管策略。

确保在您的集群配置 YAML 中,所有 worker 节点都配置为使用 ray-autoscaler-cloudwatch-v1 EC2 实例配置文件

ray.worker.default:
    node_config:
        InstanceType: c5a.large
        IamInstanceProfile:
            Name: ray-autoscaler-cloudwatch-v1

5. 将 Ray 系统指标导出到 CloudWatch。#

要将 Ray 的 Prometheus 系统指标导出到 CloudWatch,首先确保您的集群已安装 Ray Dashboard,然后取消注释 example-cloudwatch.yaml 文件 中的 head_setup_commands 部分。您可以在 {cluster_name}-ray-prometheus 指标命名空间中找到 Ray Prometheus 指标。

  head_setup_commands:
# Make `ray_prometheus_waiter.sh` executable.
- >-
  RAY_INSTALL_DIR=`pip show ray | grep -Po "(?<=Location:).*"`
  && sudo chmod +x $RAY_INSTALL_DIR/ray/autoscaler/aws/cloudwatch/ray_prometheus_waiter.sh
# Copy `prometheus.yml` to Unified CloudWatch Agent folder
- >-
  RAY_INSTALL_DIR=`pip show ray | grep -Po "(?<=Location:).*"`
  && sudo cp -f $RAY_INSTALL_DIR/ray/autoscaler/aws/cloudwatch/prometheus.yml /opt/aws/amazon-cloudwatch-agent/etc
# First get current cluster name, then let the Unified CloudWatch Agent restart and use `AmazonCloudWatch-ray_agent_config_{cluster_name}` parameter at SSM Parameter Store.
- >-
  nohup sudo sh -c "`pip show ray | grep -Po "(?<=Location:).*"`/ray/autoscaler/aws/cloudwatch/ray_prometheus_waiter.sh
  `cat ~/ray_bootstrap_config.yaml | jq '.cluster_name'`
  >> '/opt/aws/amazon-cloudwatch-agent/logs/ray_prometheus_waiter.out' 2>> '/opt/aws/amazon-cloudwatch-agent/logs/ray_prometheus_waiter.err'" &

6. 更新 CloudWatch Agent、Dashboard 和 Alarm 配置文件。#

您可以通过简单地修改 Ray 集群配置 YAML 引用的 CloudWatch 配置文件,然后重新运行 ray up example-cloudwatch.yaml,来应用对集群的 CloudWatch Logs、Metrics、Dashboard 和 Alarms 的更改。Unified CloudWatch Agent 将在所有集群节点上自动重启,并且您的配置更改将生效。