Ray集群部署与维护

Ray集群部署与维护

一、环境准备

1.1 安装依赖

根据不同云平台,执行以下命令安装必要依赖:

AWS
pip install -U "ray[default]" boto3
GCP
pip install -U "ray[default]" google-api-python-client
Azure
pip install -U "ray[default]" azure-cli azure-core

1.2 配置云平台凭证

AWS

配置~/.aws/credentials文件,参考AWS文档

GCP

设置环境变量:

export GOOGLE_APPLICATION_CREDENTIALS="path/to/credentials.json"
Azure

登录并配置订阅:

az login
az account set -s <subscription_id>

二、集群部署

2.1 创建配置文件

创建config.yaml文件,以下是各平台的最小配置示例:

AWS
cluster_name: minimal
provider:
    type: aws
    region: us-west1
auth:
    ssh_user: ubuntu
GCP
cluster_name: minimal
provider:
    type: gcp
    region: us-west1
auth:
    ssh_user: ubuntu
Azure
cluster_name: minimal
provider:
    type: azure
    location: westus2
    resource_group: ray-cluster
auth:
    ssh_user: ubuntu
    ssh_private_key: ~/.ssh/id_rsa
    ssh_public_key: ~/.ssh/id_rsa.pub

2.2 启动集群

ray up -y config.yaml

三、集群使用

3.1 提交作业

ray exec config.yaml 'python -c "import ray; ray.init()"'

3.2 连接到集群

ray attach config.yaml

3.3 运行示例应用

创建script.py文件:

from collections import Counter
import socket
import time
import ray

ray.init()

print(f'''This cluster consists of
    {
              len(ray.nodes())} nodes in total
    {
              ray.cluster_resources()['CPU']} CPU resources in total
''')

@ray.remote
def f():
    time.sleep(0.001)
    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(f'    {
              num_tasks} tasks on {
              ip_address}')

在集群上运行:

ray exec config.yaml 'python script.py'

四、集群维护

4.1 关闭集群

ray down -y config.yaml

4.2 集群监控

集群启动后,可通过以下方式访问Ray Dashboard:

使用ray attach连接到 head 节点
在浏览器中访问 http://localhost:8265

4.3 扩展集群

修改配置文件中的min_workersmax_workers参数,然后执行:

ray up -y config.yaml

五、注意事项

多节点Ray集群仅在Linux上受支持
确保云平台账号有足够权限创建和管理实例
生产环境中应使用更详细的配置文件,指定实例类型、磁盘大小等
定期备份重要配置和数据

六、参考资料

Ray官方文档
Ray GitHub仓库
Ray集群配置参考## 七、常用集群管理命令详解

7.1 集群基础操作命令

启动Ray节点
# 启动头节点
ray start --head --node-ip-address=<节点IP> --port=6379 --include-dashboard=true

# 启动工作节点
ray start --address=<头节点IP>:6379 --node-ip-address=<工作节点IP>
停止Ray节点
# 停止当前节点
ray stop

# 强制停止节点
ray stop -f

7.2 集群生命周期管理

创建/更新集群
# 创建集群
ray up -y config.yaml

# 覆盖配置的最小/最大工作节点数
ray up -y config.yaml --min-workers=2 --max-workers=10

# 仅重启Ray服务,不中断运行中的作业
ray up -y config.yaml --restart-only
关闭集群
# 完全关闭集群
ray down -y config.yaml

# 仅关闭工作节点,保留头节点
ray down -y config.yaml --workers-only

7.3 集群状态监控

获取头节点IP
ray get_head_ip config.yaml
监控集群自动扩展日志
# 监控日志,默认显示最后10行
ray monitor config.yaml

# 指定显示的日志行数
ray monitor config.yaml --lines=50
访问Ray Dashboard
# 启动时指定Dashboard主机和端口
ray start --head --include-dashboard=true --dashboard-host=0.0.0.0 --dashboard-port=8265

# 本地端口转发(通过ray attach时)
ray attach config.yaml -p 8265:8265

7.4 作业管理

在集群上执行命令
# 执行简单命令
ray exec config.yaml 'python -c "import ray; ray.init()"'

# 端口转发
ray exec config.yaml 'python script.py' -p 8080:8080

# 在screen中运行命令
ray exec config.yaml 'python long_running_script.py' --screen
提交作业到集群
# 提交Python脚本
ray submit config.yaml script.py

# 提交带参数的脚本
ray submit config.yaml script.py -- --input data.csv --epochs 10

# 在tmux中运行
ray submit config.yaml script.py --tmux

7.5 节点连接与管理

连接到集群头节点
# 基本连接
ray attach config.yaml

# 强制创建新的screen会话
ray attach config.yaml --new

# 端口转发
ray attach config.yaml -p 8888:8888 -p 6006:6006

7.6 高级配置命令

自定义资源配置
ray start --head --resources='{"GPU": 2, "TPU": 1}'
内存配置
# 指定对象存储内存大小(10GB)
ray start --head --object-store-memory=10737418240
日志配置
# 禁用日志重定向
ray start --head --no-redirect-output

# 设置日志样式
ray start --head --log-style=record --log-color=false

八、集群故障排查常用命令

8.1 检查节点状态

# 在头节点上执行,查看所有节点状态
ray nodes

# 查看集群资源使用情况
python -c "import ray; ray.init(); print(ray.cluster_resources())"

8.2 查看日志

# 头节点日志位置
ls /tmp/ray/session_latest/logs/

# 监控自动扩展器日志
ray monitor config.yaml

8.3 资源隔离配置

# 启用资源隔离
ray start --head --enable-resource-isolation 
  --system-reserved-cpu=0.5 
  --system-reserved-memory=1073741824  # 1GB

九、命令速查表

命令用途 命令示例
启动头节点 ray start --head --port=6379
加入集群 ray start --address=<head_ip>:6379
创建集群 ray up -y config.yaml
关闭集群 ray down -y config.yaml
提交作业 ray submit config.yaml script.py
连接集群 ray attach config.yaml
获取头节点IP ray get_head_ip config.yaml
监控集群 ray monitor config.yaml
停止节点 ray stop

10.1 推荐安装命令

根据用户需求,使用以下命令安装Ray(使用Python 3.10及豆瓣镜像源):

# 安装基础版Ray
pip3.10 install -U ray -i https://pypi.doubanio.com/simple

# 安装完整版(包含Dashboard、Serve、Tune等模块)
pip3.10 install -U "ray[default]" -i https://pypi.doubanio.com/simple

# 安装所有可选组件(完整版+额外功能)
pip3.10 install -U "ray[all]" -i https://pypi.doubanio.com/simple

注意:ray[default]已包含大部分常用组件,包括Dashboard、Serve、Tune等;ray[all]包含全部可选依赖,适合生产环境使用。

十一、集群状态监控命令

11.1 集群健康检查

# 检查集群健康状态
ray health-check --address=<head-node-ip>:6379

# 输出示例:
# Healthy: True
# Ray version: 2.47.1
# GCS address: 10.0.0.1:6379

11.2 集群状态查看

虽然官方未提供ray status命令,但可通过以下方式获取集群状态:

# 方法1:使用Python API
python -c "import ray; ray.init(address='auto'); print(ray.cluster_resources()); print(ray.nodes())"

# 方法2:使用Dashboard(推荐)
ray start --head --include-dashboard=true
# 访问 http://<head-node-ip>:8265 查看详细集群状态

11.3 节点资源监控

# 查看节点资源使用情况
ray exec config.yaml 'python -c "import ray; ray.init(); print(ray.cluster_resources())"'

# 查看特定节点信息
ray exec config.yaml 'python -c "import ray; ray.init(); print(ray.nodes()[0])"'

十二、官方容器化部署方案

12.1 Docker单机部署

# 拉取官方镜像
docker pull rayproject/ray:latest

# 启动Ray head节点容器
docker run -d --name ray-head -p 6379:6379 -p 8265:8265 rayproject/ray:latest ray start --head --node-ip-address=0.0.0.0 --include-dashboard=true

# 查看容器日志
docker logs -f ray-head

# 启动worker节点容器
docker run -d --name ray-worker --link ray-head:ray-head rayproject/ray:latest ray start --address=ray-head:6379 --node-ip-address=0.0.0.0

12.2 Kubernetes部署(KubeRay)

12.2.1 安装KubeRay Operator
# 安装最新版KubeRay Operator
kubectl apply -k "github.com/ray-project/kuberay/ray-operator/config/default?ref=v1.1.0"

# 验证operator是否运行
kubectl get pods -n ray-system
12.2.2 创建Ray集群

创建ray-cluster.yaml文件:

apiVersion: ray.io/v1alpha1
kind: RayCluster
metadata:
  name: ray-cluster
spec:
  rayVersion: '2.47.1'
  headGroupSpec:
    serviceType: ClusterIP
    replicas: 1
    rayStartParams:
      "--head": ""
      "--dashboard-host": "0.0.0.0"
    template:
      spec:
        containers:
        - name: ray-head
          image: rayproject/ray:latest
          ports:
          - containerPort: 6379
            name: gcs
          - containerPort: 8265
            name: dashboard
          resources:
            limits:
              cpu: "2"
              memory: "4Gi"
            requests:
              cpu: "1"
              memory: "2Gi"
  workerGroupSpecs:
  - groupName: small-workers
    replicas: 2
    rayStartParams:
      "--address": "$RAY_HEAD_SERVICE_HOST:$RAY_HEAD_SERVICE_PORT_GCS"
    template:
      spec:
        containers:
        - name: ray-worker
          image: rayproject/ray:latest
          resources:
            limits:
              cpu: "2"
              memory: "4Gi"
            requests:
              cpu: "1"
              memory: "2Gi"

部署集群:

kubectl apply -f ray-cluster.yaml

# 查看集群状态
kubectl get rayclusters
kubectl get pods
12.2.3 提交作业到K8s集群
# 创建作业配置文件 ray-job.yaml
apiVersion: ray.io/v1alpha1
kind: RayJob
metadata:
  name: ray-test-job
spec:
  entrypoint: "python -c 'import ray; ray.init(); print(ray.cluster_resources())'"
  rayClusterSpec:
    rayVersion: '2.47.1'
    headGroupSpec:
      # 与上述RayCluster配置相同
    workerGroupSpecs:
      # 与上述RayCluster配置相同
  shutdownAfterJobFinishes: true
  ttlSecondsAfterFinished: 60

提交作业:

kubectl apply -f ray-job.yaml

# 查看作业状态
kubectl get rayjobs

十三、常见问题与解决方法

13.1 安装问题

问题:安装速度慢或超时

解决方法:使用国内镜像源

pip3.10 install -U "ray[default]" -i https://pypi.doubanio.com/simple
# 或使用阿里云镜像
pip3.10 install -U "ray[default]" -i https://mirrors.aliyun.com/pypi/simple/
问题:安装后导入ray失败

解决方法:检查Python版本和依赖冲突

# 确保Python版本为3.7-3.10
python --version

# 检查依赖
pip3.10 list | grep ray

# 重新安装可能解决问题
pip3.10 uninstall -y ray
pip3.10 install -U "ray[default]" -i https://pypi.doubanio.com/simple

13.2 集群部署问题

问题:节点无法加入集群

解决方法

检查网络连接和防火墙设置,确保6379、8265等端口开放
验证头节点地址是否正确
检查节点间时钟同步

# 在worker节点上测试连接头节点
telnet <head-node-ip> 6379
问题:Dashboard无法访问

解决方法

确保启动时指定--include-dashboard=true--dashboard-host=0.0.0.0
检查防火墙设置
查看Dashboard日志

# 头节点上查看Dashboard日志
cat /tmp/ray/session_latest/logs/dashboard.log

13.3 资源与性能问题

问题:集群资源不足

解决方法

调整集群配置文件中的min_workersmax_workers
增加单个节点的资源配额
启用自动扩展

# 在config.yaml中配置自动扩展
autoscaler:
  upscaling_speed: 1.0
  downscaling_speed: 1.0
  idle_timeout_minutes: 5
问题:任务执行缓慢或卡住

解决方法

检查资源使用情况,确保没有资源争用
调整任务并行度
检查是否有任务死锁

# 查看节点资源使用情况
ray exec config.yaml 'top'

13.4 Kubernetes部署问题

问题:Ray pods无法启动

解决方法

检查资源是否充足
查看pod日志

kubectl logs <ray-pod-name> -c ray-head
问题:KubeRay operator安装失败

解决方法

检查Kubernetes版本是否兼容(推荐1.21+)
检查RBAC权限

kubectl describe pods -n ray-system
© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容