使用 Kubernetes

使用 Kubernetes#

使用 Kubernetes 部署 vLLM 是一种可扩展且高效的方式来服务机器学习模型。本指南将引导您完成使用 Kubernetes 部署 vLLM 的过程,包括必要的前提条件、部署步骤和测试。

前提条件#

在开始之前,请确保您已具备以下条件

  • 一个正在运行的 Kubernetes 集群

  • NVIDIA Kubernetes 设备插件 (k8s-device-plugin):可以在 https://github.com/NVIDIA/k8s-device-plugin/ 找到

  • 集群中可用的 GPU 资源

部署步骤#

  1. 为 vLLM 创建 PVC、Secret 和 Deployment

    PVC 用于存储模型缓存,它是可选的,您可以使用 hostPath 或其他存储选项

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mistral-7b
      namespace: default
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 50Gi
      storageClassName: default
      volumeMode: Filesystem
    

    Secret 是可选的,仅在访问门控模型时才需要,如果您不使用门控模型,则可以跳过此步骤

    apiVersion: v1
    kind: Secret
    metadata:
      name: hf-token-secret
      namespace: default
    type: Opaque
    stringData:
      token: "REPLACE_WITH_TOKEN"
    

    接下来创建部署文件,用于 vLLM 运行模型服务器。以下示例部署了 Mistral-7B-Instruct-v0.3 模型。

    这里有两个使用 NVIDIA GPU 和 AMD GPU 的示例。

    NVIDIA GPU

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mistral-7b
      namespace: default
      labels:
        app: mistral-7b
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mistral-7b
      template:
        metadata:
          labels:
            app: mistral-7b
        spec:
          volumes:
          - name: cache-volume
            persistentVolumeClaim:
              claimName: mistral-7b
          # vLLM needs to access the host's shared memory for tensor parallel inference.
          - name: shm
            emptyDir:
              medium: Memory
              sizeLimit: "2Gi"
          containers:
          - name: mistral-7b
            image: vllm/vllm-openai:latest
            command: ["/bin/sh", "-c"]
            args: [
              "vllm serve mistralai/Mistral-7B-Instruct-v0.3 --trust-remote-code --enable-chunked-prefill --max_num_batched_tokens 1024"
            ]
            env:
            - name: HUGGING_FACE_HUB_TOKEN
              valueFrom:
                secretKeyRef:
                  name: hf-token-secret
                  key: token
            ports:
            - containerPort: 8000
            resources:
              limits:
                cpu: "10"
                memory: 20G
                nvidia.com/gpu: "1"
              requests:
                cpu: "2"
                memory: 6G
                nvidia.com/gpu: "1"
            volumeMounts:
            - mountPath: /root/.cache/huggingface
              name: cache-volume
            - name: shm
              mountPath: /dev/shm
            livenessProbe:
              httpGet:
                path: /health
                port: 8000
              initialDelaySeconds: 60
              periodSeconds: 10
            readinessProbe:
              httpGet:
                path: /health
                port: 8000
              initialDelaySeconds: 60
              periodSeconds: 5
    

    AMD GPU

    如果使用像 MI300X 这样的 AMD ROCm GPU,您可以参考下面的 deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mistral-7b
      namespace: default
      labels:
        app: mistral-7b
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mistral-7b
      template:
        metadata:
          labels:
            app: mistral-7b
        spec:
          volumes:
          # PVC
          - name: cache-volume
            persistentVolumeClaim:
              claimName: mistral-7b
          # vLLM needs to access the host's shared memory for tensor parallel inference.
          - name: shm
            emptyDir:
              medium: Memory
              sizeLimit: "8Gi"
          hostNetwork: true
          hostIPC: true
          containers:
          - name: mistral-7b
            image: rocm/vllm:rocm6.2_mi300_ubuntu20.04_py3.9_vllm_0.6.4
            securityContext:
              seccompProfile:
                type: Unconfined
              runAsGroup: 44
              capabilities:
                add:
                - SYS_PTRACE
            command: ["/bin/sh", "-c"]
            args: [
              "vllm serve mistralai/Mistral-7B-v0.3 --port 8000 --trust-remote-code --enable-chunked-prefill --max_num_batched_tokens 1024"
            ]
            env:
            - name: HUGGING_FACE_HUB_TOKEN
              valueFrom:
                secretKeyRef:
                  name: hf-token-secret
                  key: token
            ports:
            - containerPort: 8000
            resources:
              limits:
                cpu: "10"
                memory: 20G
                amd.com/gpu: "1"
              requests:
                cpu: "6"
                memory: 6G
                amd.com/gpu: "1"
            volumeMounts:
            - name: cache-volume
              mountPath: /root/.cache/huggingface
            - name: shm
              mountPath: /dev/shm
    

    您可以从 ROCm/k8s-device-plugin 获取包含步骤和示例 yaml 文件的完整示例。

  2. 为 vLLM 创建 Kubernetes Service

    接下来,创建一个 Kubernetes Service 文件来暴露 mistral-7b 部署

    apiVersion: v1
    kind: Service
    metadata:
      name: mistral-7b
      namespace: default
    spec:
      ports:
      - name: http-mistral-7b
        port: 80
        protocol: TCP
        targetPort: 8000
      # The label selector should match the deployment labels & it is useful for prefix caching feature
      selector:
        app: mistral-7b
      sessionAffinity: None
      type: ClusterIP
    
  3. 部署和测试

    使用 kubectl apply -f <filename> 应用部署和服务配置

    kubectl apply -f deployment.yaml
    kubectl apply -f service.yaml
    

    要测试部署,请运行以下 curl 命令

    curl http://mistral-7b.default.svc.cluster.local/v1/completions \
      -H "Content-Type: application/json" \
      -d '{
            "model": "mistralai/Mistral-7B-Instruct-v0.3",
            "prompt": "San Francisco is a",
            "max_tokens": 7,
            "temperature": 0
          }'
    

    如果服务部署正确,您应该收到来自 vLLM 模型的响应。

结论#

使用 Kubernetes 部署 vLLM 可以有效扩展和管理利用 GPU 资源的 ML 模型。通过遵循上述步骤,您应该能够在 Kubernetes 集群中设置和测试 vLLM 部署。如果您遇到任何问题或有任何建议,请随时为文档做出贡献。