在 Kubernetes 上部署 MobileNet 图像分类器#

注意: Ray Serve 应用及其客户端的 Python 文件位于 ray-project/serve_config_examples 仓库中。

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

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

步骤 2:安装 KubeRay operator#

请按照 本文档 从 Helm 仓库安装最新的稳定版 KubeRay 运算符。请注意,此示例中的 YAML 文件使用 serveConfigV2。您需要 KubeRay v0.6.0 或更高版本才能使用此功能。

第 3 步:安装 RayService#

# Create a RayService
kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/v1.5.1/ray-operator/config/samples/ray-service.mobilenet.yaml
  • mobilenet.py 文件需要 tensorflow 作为依赖项。因此,YAML 文件使用 rayproject/ray-ml 镜像而不是 rayproject/ray 镜像。

  • 请求解析函数 starlette.requests.form() 需要 python-multipart,因此 YAML 文件在运行时环境中包含了 python-multipart

第 4 步:转发 Ray Serve 的端口#

# Wait for the RayService to be ready to serve requests
kubectl describe rayservice/rayservice-mobilenet
#  Conditions:
#   Last Transition Time:  2025-02-13T02:29:26Z
#   Message:               Number of serve endpoints is greater than 0
#   Observed Generation:   1
#   Reason:                NonZeroServeEndpoints
#   Status:                True
#   Type:                  Ready

# Forward the port for Ray Serve service
kubectl port-forward svc/rayservice-mobilenet-serve-svc 8000

第 5 步:向 ImageClassifier 发送请求#

  • 第 5.1 步:准备一个图像文件。

  • 第 5.2 步:更新 mobilenet_req.py 中的 image_path

  • 第 5.3 步:向 ImageClassifier 发送请求。

    python mobilenet_req.py
    # sample output: {"prediction":["n02099601","golden_retriever",0.17944198846817017]}