运行 RayService

在 Kueue 上运行 RayService 的指南。

本页演示如何利用 Kueue 的调度与资源管理能力运行 RayService 。

Kueue 可以直接管理 RayService,类似其直接管理 RayJob。

本指南面向对 Kueue 有基本了解的、对外提供服务的用户。 更多信息,请参见 Kueue 概览。

开始之前

  1. 请确保你使用的是 Kueue v0.17.0 或更高版本,以及 KubeRay v1.3.0 或更高版本。

  2. 请参见 管理集群配额了解初始 Kueue 设置的详细信息。

  3. 请参见 KubeRay 安装说明了解 KubeRay 的安装和配置详情。

RayService 定义

在 Kueue 上运行 RayService 时,请考虑以下方面:

a. 队列选择

目标 本地队列应在 RayService 配置的 metadata.labels 部分指定,该标签会被传递到其 RayCluster。

metadata:
  labels:
    kueue.x-k8s.io/queue-name: user-queue

b. 配置资源需求

工作负载的资源需求可以在 spec.rayClusterConfig 中配置。

spec:
  rayClusterConfig:
    headGroupSpec:
    template:
      spec:
        containers:
          - resources:
              requests:
                cpu: "1"
    workerGroupSpecs:
    - template:
        spec:
          containers:
            - resources:
                requests:
                  cpu: "1"

c. Suspend 控制

Kueue 控制 RayService 的 spec.rayClusterConfig.suspend 字段。当 RayService 被 Kueue 接纳时,Kueue 会通过将 spec.rayClusterConfig.suspend 设置为 false 来取消暂停,无论其之前的值是什么。

d. 限制事项

  • 有限的 Worker Group:由于 Kueue 工作负载最多可以有 18 个 PodSet,所以 spec.rayClusterConfig.workerGroupSpecs 的最大数量为 17。

动态扩容(也称为 InTreeAutoscaling)

Ray 自动扩缩可以根据资源使用需求自动添加或移除 Ray worker pod 实例。

该特性从 Kueue v0.19.0 开始支持 RayService。

自动扩缩仅支持弹性 RayService 对象。

如何在 RayService 中启用动态扩容

  1. 启用 弹性工作负载(Workload Slices) 特性门控:

    ElasticJobsViaWorkloadSlices: true
    
  2. 为 RayService 对象添加注解:

    metadata:
      annotations:
        kueue.x-k8s.io/elastic-job: "true"
    
  3. 设置以下字段启用 RayService 的 Ray 自动扩缩器:

    spec:
      rayClusterConfig:
        enableInTreeAutoscaling: true
    

滚动升级限制

Kueue 的工作负载切片特性目前仅管理单个活跃集群的配额。启用工作负载切片时,暂不支持创建二级临时集群的升级策略(spec.upgradeStrategy.type: NewCluster 或 NewClusterWithIncrementalUpgrade),因为待处理集群的 Pod 会保持被门控状态。要在使用工作负载切片的同时进行自动扩缩,请使用 spec.upgradeStrategy.type: None 或进行就地更新。

RayService 示例

RayService 如下所示:

apiVersion: ray.io/v1
kind: RayService
metadata:
  name: test-rayservice
  namespace: default
  labels:
    kueue.x-k8s.io/queue-name: user-queue
spec:
  upgradeStrategy:
    type: None
  serveConfigV2: |
    applications:
      - name: fruit_app
        import_path: fruit.deployment_graph
        route_prefix: /fruit
        runtime_env:
          working_dir: "https://github.com/ray-project/test_dag/archive/78b4a5da38796123d9f9ffff59bab2792a043e95.zip"
        deployments:
          - name: MangoStand
            num_replicas: 2
            max_replicas_per_node: 1
            user_config:
              price: 3
            ray_actor_options:
              num_cpus: 0.1
          - name: OrangeStand
            num_replicas: 1
            user_config:
              price: 2
            ray_actor_options:
              num_cpus: 0.1
          - name: PearStand
            num_replicas: 1
            user_config:
              price: 1
            ray_actor_options:
              num_cpus: 0.1
          - name: FruitMarket
            num_replicas: 1
            ray_actor_options:
              num_cpus: 0.1
      - name: math_app
        import_path: conditional_dag.serve_dag
        route_prefix: /calc
        runtime_env:
          working_dir: "https://github.com/ray-project/test_dag/archive/78b4a5da38796123d9f9ffff59bab2792a043e95.zip"
        deployments:
          - name: Adder
            num_replicas: 1
            user_config:
              increment: 3
            ray_actor_options:
              num_cpus: 0.1
          - name: Multiplier
            num_replicas: 1
            user_config:
              factor: 5
            ray_actor_options:
              num_cpus: 0.1
          - name: Router
            num_replicas: 1
  rayClusterConfig:
    rayVersion: '2.55.1'
    headGroupSpec:
      rayStartParams: {}
      template:
        spec:
          containers:
          - name: ray-head
            image: rayproject/ray:2.55.1
            resources:
              limits:
                cpu: "2"
                memory: "5Gi"
              requests:
                cpu: "2"
                memory: "2Gi"
    workerGroupSpecs:
    - replicas: 1
      minReplicas: 1
      maxReplicas: 5
      groupName: small-group
      rayStartParams: {}
      template:
        spec:
          containers:
          - name: ray-worker
            image: rayproject/ray:2.55.1
            resources:
              limits:
                cpu: "1"
                memory: "2Gi"
              requests:
                cpu: "500m"
                memory: "2Gi"

最后修改 September 30, 2026: Update main with the latest v0.20.0 (36c42c8df)