RayService 快速入门#

先决条件#

本指南主要关注 KubeRay v1.3.0 和 Ray 2.41.0 的行为。

什么是 RayService?#

RayService 管理以下组件:

  • RayCluster:管理 Kubernetes 集群中的资源。

  • Ray Serve 应用程序:管理用户的应用程序。

RayService 提供什么?#

  • 对 Ray 集群和 Ray Serve 应用程序的 Kubernetes 原生支持: 在使用 Kubernetes 配置定义 Ray 集群及其 Ray Serve 应用程序后,您可以使用 kubectl 创建集群及其应用程序。

  • Ray Serve 应用程序的原地更新: 有关更多详细信息,请参阅 RayService

  • Ray 集群的零停机升级: 有关更多详细信息,请参阅 RayService

  • 高可用服务: 有关更多详细信息,请参阅 RayService 高可用性

示例:使用 RayService 服务两个简单的 Ray Serve 应用程序#

步骤 1:使用 Kind 创建 Kubernetes 集群#

kind create cluster --image=kindest/node:v1.26.0

步骤 2:安装 KubeRay operator#

按照此文档从 Helm 仓库安装最新的稳定版 KubeRay operator。请注意,此示例中的 YAML 文件使用 serveConfigV2 来指定多应用程序 Serve 配置,该配置从 KubeRay v0.6.0 开始可用。

步骤 3:安装 RayService#

kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/v1.3.0/ray-operator/config/samples/ray-service.sample.yaml

步骤 4:验证 Kubernetes 集群状态#

# List all RayService custom resources in the `default` namespace.
kubectl get rayservice
NAME                SERVICE STATUS   NUM SERVE ENDPOINTS
rayservice-sample   Running          2
# List all RayCluster custom resources in the `default` namespace.
kubectl get raycluster
NAME                                 DESIRED WORKERS   AVAILABLE WORKERS   CPUS    MEMORY   GPUS   STATUS   AGE
rayservice-sample-raycluster-czjtm   1                 1                   2500m   4Gi      0      ready    4m21s
# List all Ray Pods in the `default` namespace.
kubectl get pods -l=ray.io/is-ray-node=yes
NAME                                                          READY   STATUS    RESTARTS   AGE
rayservice-sample-raycluster-czjtm-head-ldxl7                 1/1     Running   0          4m21s
rayservice-sample-raycluster-czjtm-small-group-worker-pk88k   1/1     Running   0          4m21s
# Check the `Ready` condition of the RayService.
# The RayService is ready to serve requests when the condition is `True`.
# Users can also use `kubectl describe rayservices.ray.io rayservice-sample` to check the condition section
kubectl get rayservice rayservice-sample -o json | jq -r '.status.conditions[] | select(.type=="Ready") | to_entries[] | "\(.key): \(.value)"'
lastTransitionTime: 2025-04-11T16:17:01Z
message: Number of serve endpoints is greater than 0
observedGeneration: 1
reason: NonZeroServeEndpoints
status: True
type: Ready
# List services in the `default` namespace.
kubectl get services -o json | jq -r '.items[].metadata.name'
kuberay-operator
kubernetes
rayservice-sample-head-svc
rayservice-sample-raycluster-czjtm-head-svc
rayservice-sample-serve-svc

当 Ray Serve 应用程序健康且准备就绪时,KubeRay 会为 RayService 自定义资源创建一个 head service 和一个 Ray Serve service。例如,rayservice-sample-head-svcrayservice-sample-serve-svc

这些服务有什么作用?

  • rayservice-sample-head-svc
    此服务指向活动 RayCluster 的 head pod,通常用于查看 Ray Dashboard(端口 8265)。

  • rayservice-sample-serve-svc
    此服务暴露 Ray Serve 的 HTTP 接口,通常在端口 8000 上。
    使用此服务向您部署的 Serve 应用程序发送 HTTP 请求(例如,REST API、ML 推理等)。

步骤 5:验证 Serve 应用程序的状态#

# (1) Forward the dashboard port to localhost.
# (2) Check the Serve page in the Ray dashboard at http://localhost:8265/#/serve.
kubectl port-forward svc/rayservice-sample-head-svc 8265:8265 > /dev/null &
  • 请参阅 rayservice-troubleshooting.md,了解有关 RayService 可观测性的更多详细信息。下面是 Ray dashboard 中 Serve 页面的截图示例。Ray Serve Dashboard

步骤 6:通过 Kubernetes serve service 向 Serve 应用程序发送请求#

# Step 6.1: Run a curl Pod.
# If you already have a curl Pod, you can use `kubectl exec -it <curl-pod> -- sh` to access the Pod.
kubectl run curl --image=radial/busyboxplus:curl --command -- tail -f /dev/null
# Step 6.3: Send a request to the calculator app.
kubectl exec curl -- curl -sS -X POST -H 'Content-Type: application/json' rayservice-sample-serve-svc:8000/calc/ -d '["MUL", 3]'
15 pizzas please!
# Step 6.2: Send a request to the fruit stand app.
kubectl exec curl -- curl -sS -X POST -H 'Content-Type: application/json' rayservice-sample-serve-svc:8000/fruit/ -d '["MANGO", 2]'
6

步骤 7:清理 Kubernetes 集群#

# Kill the `kubectl port-forward` background job in the earlier step
killall kubectl
kind delete cluster

后续步骤#

  • 请参阅 RayService 文档,了解 RayService 的全部功能列表,包括原地更新、零停机升级和高可用性。

  • 如果您遇到任何问题,请参阅 RayService 故障排除指南

  • 请参阅 示例,了解更多 RayService 示例。MobileNet 示例是一个很好的入门示例,因为它不需要 GPU 且易于在本地机器上运行。