ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

Ray 集群部署指南:从单机到云端的集群架构、部署平台与快速上手

Ray 集群部署指南:从单机到云端的集群架构、部署平台与快速上手 人工智能分布式训练强化学习任务调度模型推理服务【免费下载链接】rayRay is an AI compute engine. Ray consists of a core distributed runtime and a set of AI Libraries for accelerating ML workloads.项目地址https://gitcode.com/gh_mirrors/ra/ray点击查看免费下载Ray 作为 AI 计算引擎既能在单机上通过一次ray.init()开箱即用地运行也能将负载平滑扩展到多节点集群。本文基于当前仓库 doc/source/cluster/getting-started.md 的系统梳理完整讲解 Ray 集群的架构组成head node / worker node / autoscaler、五大部署平台AWS、GCP、Azure、Kubernetes、Anyscale的选择矩阵以及 VM 集群与 KubernetesKubeRay两条官方快速上手路径。读完本文你将掌握从定义集群 YAML 配置、ray up拉起集群、ray exec/ray attach提交任务到ray down回收集群的完整实战闭环并了解配套的监控指标与 Dashboard 运维手段。什么是 Ray 集群从笔记本到多节点Ray 的设计目标是让工作负载可以从一台笔记本无缝扩展到大型集群。单机场景下只需要调用ray.init()即可使用 Ray但要在多节点上运行 Ray 应用就必须先部署一个 Ray 集群。一个 Ray 集群由一组连接到同一个 head node 的 worker 节点构成。集群可以是固定大小的也可以按照集群上应用请求的资源量自动扩缩容autoscale up and down。核心概念详见 Key Concepts其中明确定义了集群的组成要素Head Node头节点每个集群唯一除普通功能外还运行 autoscaler、GCSGlobal Control Service以及执行 Ray 任务的 driver 进程等单例管理进程。Ray 也可能把任务调度到头节点上大规模集群中通常不希望如此相关最佳实践参见 vms 用户指南。Worker Node工作节点不运行 head 节点的管理进程只负责执行用户代码中的 task 和 actor并参与分布式调度以及 Ray 对象object store在集群内存中的存储与分发。Autoscaler自动扩缩容器运行在 head 节点上Kubernetes 场景下作为 head Pod 中的 sidecar 容器。当工作负载的资源需求超过集群现有容量时autoscaler 尝试增加 worker 节点当 worker 节点空闲时则将其从集群移除。需要特别强调的是autoscaler 只对 task 和 actor 的资源请求做出反应不关注应用指标或物理资源利用率。另外2.10.0 版本在 KubeRay 上引入了 Autoscaling V2 的 alpha 版本。集群中的任务以Ray Job为单位组织一个 Ray Job 是来自同一个脚本的 Ray task、object 和 actor 的集合运行该 Python 脚本的 worker 被称为该 job 的 driver。在集群上运行 job 有两条途径推荐使用 Ray Jobs API 提交或直接在集群上运行 driver 脚本做交互式开发。部署平台矩阵在哪里可以部署 Ray 集群Ray 对以下技术栈提供原生集群部署支持平台支持类型说明AWS、GCP、AzureRay 官方支持通过 VM Cluster Launcher 在云上拉起集群详见 vms/index阿里云Aliyun、vSphere社区支持社区维护的集成Kubernetes官方支持通过官方 KubeRay 项目详见 kubernetes/indexAnyscale托管平台Ray 创建者提供的全托管 Ray 平台可自带 AWS/GCP/Azure/Kubernetes 集群或使用其托管计算层高级用户可以手动部署 Ray 集群on-prem或部署到未在列表中列出的平台。::: note 多节点 Ray 集群仅支持 Linux。如确有需要可在部署时设置环境变量RAY_ENABLE_WINDOWS_OR_OSX_CLUSTER1自行承担风险地部署 Windows 和 OSX 集群。 :::快速上手之一在云上 VM 部署 Ray 集群VM 快速入门完整版见 vms/getting-started.rst演示了 Ray 集群的端到端能力把一个原本跑在笔记本上的 Python 应用用几条命令扩展到云端集群执行。整体流程为创建基础Python 应用 → 在云提供商上启动集群 → 在云端运行应用。环境准备与依赖安装运行该演示需要开发机通常是笔记本安装 Python以及一个你偏好的云提供商账户AWS、GCP、Azure、Aliyun 或 vSphere。按平台安装 Ray 与云 SDK 依赖AWS官方支持pip install -U ray[default] boto3Azure官方支持pip install -U ray[default] azure-cli azure-coreGCP官方支持pip install -U ray[default] google-api-python-clientAliyun社区支持pip install -U ray[default] aliyun-python-sdk-core aliyun-python-sdk-ecsvSphere社区支持pip install -U ray[default]随后配置云凭据AWS 在~/.aws/credentials配置Azure 执行az login后用az account set -s subscription_id指定订阅GCP 设置GOOGLE_APPLICATION_CREDENTIALS环境变量Aliyun 需要为 RAM 用户授权并在集群配置文件中设置 AccessKey可参考 example-full.yaml注意账户余额不得低于 100 元人民币否则报InvalidAccountStatus.NotEnoughBalancevSphere 需确保 Ray supervisor 服务已启动。从笔记本应用改造为 Ray 应用先写一个跟踪任务执行所在机器 IP 的普通 Python 应用保存为script.pyfrom 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))直接运行python script.py大约需要 10 秒输出类似Counter({127.0.0.1: 10000})。做少量改动即可让它在 Ray 上运行f.remote()提交远程任务ray.get取回结果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))再加一些代码让输出信息更丰富打印集群节点数、CPU 总量以及每个 IP 上执行的任务数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定义集群配置 YAML 并启动集群集群配置定义在一个 YAML 文件中Cluster Launcher 用它启动 head nodeAutoscaler 用它启动 worker 节点。官方支持平台的最小示例AWS 完整版见 example-minimal.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. available_node_types: ray.head.default: # The node types 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: resources: {CPU: 1, GPU: 1, custom: 5} resources: {} node_config: InstanceType: m5.large ray.worker.default: # The minimum number of worker nodes of this type to launch. min_workers: 3 # The maximum number of worker nodes of this type to launch. max_workers: 3 resources: {} node_config: InstanceType: m5.largeAzure 最小配置# 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.pubGCP 最小配置# 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将配置文件保存为config.yaml后用 Ray 集群启动器在云上创建 head node 和 worker 节点$ ray up -y config.yaml配置文件还支持更多细节实例类型、要启动的 worker 最小/最大数量、自动扩缩容策略、要同步的文件等完整属性参考 cluster YAML 配置参考。在集群上运行应用与日常操作集群就绪后ray.init()会自动连接新建的集群。快速验证连接$ 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.需要交互式 shell 时可用ray attach建立到 head node 的 SSH 连接然后在 head 上直接执行命令# From a remote client: $ ray attach config.yaml # Now on the head node... $ python -c import ray; ray.init()集群管理 CLI 的完整命令清单ray start、ray stop、ray up、ray down、ray exec、ray submit、ray attach、ray get_head_ip、ray monitor参见 cli.rst。需要说明的是这些工具适合集群上的临时ad-hoc执行推荐的生产方式是用 Ray Jobs 提交应用参见 running-applications/index。任务结束关闭集群$ ray down -y config.yaml快速上手之二在 Kubernetes 上部署 RayKubeRayKubeRay 是官方开源的 Kubernetes Operator将每个 Ray 节点实现为 Kubernetes Pod——head node 即 head Podworker 节点即 worker Pod说明见 kubernetes/getting-started.md。KubeRay 提供 4 类 CRDRayClusterKubeRay 全生命周期管理 RayCluster包括创建/删除、自动扩缩容与容错。RayJob集群就绪后自动创建 RayCluster 并提交 job可配置 job 完成后自动删除 RayCluster。RayService由 RayCluster 与 Ray Serve 部署图两部分组成提供 RayCluster 零停机升级与高可用。RayCronJob基于 cron 表达式周期性地创建 RayJob适合批处理等定时负载。如何选择 CRD集群升级期间是否可接受停机如升级 Ray 版本不可接受 → 用 RayJob可配置 job 完成后自动删除集群且每次提交可切换 Ray 版本与配置可接受 → 用 RayClusterRay 不原生支持滚动升级需要手动关停并新建。是否需要按周期运行负载是 → 用 RayCronJob。是否部署在公有云AWS、GCP、Azure是 → 用 RayJobjob 完成后自动删除集群以降低成本。是否在意拉起 RayCluster 引入的延迟是 → 用 RayCluster集群只创建一次、可复用RayJob/RayCronJob 每次提交都会新建集群。官方推荐模型服务用 RayServiceRay 应用开发用 RayCluster。对应快速入门分别见 raycluster-quick-start、rayjob-quick-start、rayservice-quick-start、raycronjob-quick-start。配套运维指标监控与 Dashboard集群部署后需要可观测性支撑指标采集详见 metrics.mdRay 以 Prometheus 格式记录并输出时间序列指标每个节点运行一个 metrics agent 负责采集本地节点指标并以 Prometheus 格式暴露。使用ray[default]安装即可导出指标。本地快速演示ray metrics launch-prometheus一键启动本地 Prometheusray start --head --metrics-export-port8080指定导出端口然后在 Prometheus UI默认 http://localhost:9090查询ray_dashboard_api_requests_count_requests_total。停止用ray metrics shutdown-prometheusRay 2.40或手动 kill 进程。生产环境建议将 Prometheus 部署在 Ray 集群之外并利用 Ray 自动生成的 service discovery 文件head 节点/tmp/ray/prom_metrics_service_discovery.json或 HTTP 服务发现接口http://RayHeadnodeAddress:8265/api/prometheus/sd实现动态发现。可视化可用 GrafanaRay 提供开箱即用的默认 Dashboard JSON位于/tmp/ray/session_latest/metrics/grafana/dashboards。Dashboard 配置详见 configure-manage-dashboard.mdRay Dashboard 默认运行在 head 节点 8265 端口可用ray start --dashboard-port或ray.init(dashboard_port...)修改远程集群可通过ray dashboard cluster config file端口转发访问Kubernetes 下可用RayCluster name-head-svc:8265服务或kubectl port-forward service/${RAYCLUSTER_NAME}-head-svc 8265:8265访问。需要注意Dashboard 对集群同时具有读写权限公网暴露前必须做好鉴权还可通过RAY_GRAFANA_HOST、RAY_PROMETHEUS_HOST、RAY_PROMETHEUS_NAME、RAY_GRAFANA_IFRAME_HOST等环境变量将 Grafana 可视化嵌入 Dashboard。下一步学习路径原文档getting-started.md为不同诉求的用户规划了四条入口学习 Ray 集群核心概念阅读 key-concepts.md理解 head node、worker node、autoscaler、Ray Jobs 等关键概念及与集群交互的主要方式。在 Kubernetes 上运行 Ray按 kubernetes/getting-started.md 的教程操作可在 Kubernetes 集群或通过 Kind 在笔记本上完成。在云提供商上运行 Ray按 vms/getting-started.rst 把笔记本应用扩展到云端需要 AWS 或 GCP 账户。在已有集群上提交应用按 running-applications/index 的 Job 提交指南操作。其他常用参考还包括集群常见问题 faq.rst、集群包与依赖说明 package-overview.rst 以及用量统计 usage-stats.rst。无论选择哪条路径都可以从笔记本上的一行ray.init()出发沿着本文给出的架构认知与部署命令一步步将应用平滑扩展到真实的分布式集群。赞分享人工智能分布式训练强化学习任务调度模型推理服务【免费下载链接】rayRay is an AI compute engine. Ray consists of a core distributed runtime and a set of AI Libraries for accelerating ML workloads.项目地址https://gitcode.com/gh_mirrors/ra/ray点击查看免费下载相关推荐Ray VM 集群快速上手用 Ray Cluster Launcher 把笔记本 Python 应用一键部署到云端Ray VM 集群快速上手用 Ray Cluster Launcher 把笔记本 Python 应用一键部署到云端 本指南对应仓库文档 doc/source/人工智能分布式训练强化学习任务调度模型推理服务在 SLURM 集群上部署 Ray基于 ray symmetric-run 的端到端实战指南在 SLURM 集群上部署 Ray基于 ray symmetric run 的端到端实战指南 SLURMSimple Linux Utility for R人工智能分布式训练强化学习任务调度模型推理服务MCP服务器部署架构单机、集群与云原生部署方案MCP服务器部署架构单机、集群与云原生部署方案 概述 Model Context ProtocolMCP模型上下文协议作为AI模型与外部资源交互的开放标文档知识库创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表