在 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 operator。请注意,本示例中的 YAML 文件使用了 serveConfigV2。你需要 KubeRay v0.6.0 或更高版本才能使用此功能。

步骤 3:安装 RayService#

# Create a RayService
kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/v1.3.0/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]}