在 Kubernetes 上使用 vLLM 服务大语言模型#
本指南演示了如何使用 KubeRay 在 Kubernetes 上使用 vLLM 服务大语言模型。本指南中的示例在 Google Kubernetes Engine (GKE) 上部署 Hugging Face 的 meta-llama/Meta-Llama-3-8B-Instruct
模型。
先决条件#
此示例从 Hugging Face 下载模型权重。您需要完成以下先决条件才能成功完成本指南
一个具有 gated repos 读取权限的 Hugging Face 访问令牌。
访问 Llama 3 8B 模型。通常需要您在 Hugging Face 上签署协议才能访问此模型。请访问 Llama 3 模型页面 了解更多详细信息。
在 GKE 上创建 Kubernetes 集群#
创建具有 GPU 节点池的 GKE 集群
gcloud container clusters create kuberay-gpu-cluster \
--machine-type=g2-standard-24 \
--location=us-east4-c \
--num-nodes=2 \
--accelerator=type=nvidia-l4,count=2,gpu-driver-version=latest
此示例使用 L4 GPU。每个模型副本使用 vLLM 的张量并行性使用 2 个 L4 GPU。
安装 KubeRay Operator#
按照 部署 KubeRay Operator 从 Helm 仓库安装最新的稳定版 KubeRay operator。如果您正确设置了 GPU 节点池的污点,KubeRay operator Pod 必须位于 CPU 节点上。
创建包含 Hugging Face 访问令牌的 Kubernetes Secret#
创建包含 Hugging Face 访问令牌的 Kubernetes Secret
export HF_TOKEN=<Hugging Face access token>
kubectl create secret generic hf-secret --from-literal=hf_api_token=${HF_TOKEN} --dry-run=client -o yaml | kubectl apply -f -
本指南将在后续步骤中使用的 RayCluster 中将此 Secret 引用为环境变量。
部署 RayService#
创建 RayService 自定义资源
kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/master/ray-operator/config/samples/vllm/ray-service.vllm.yaml
此步骤配置 RayService 以部署一个 Ray Serve 应用,该应用运行 vLLM 作为 Llama 3 8B Instruct 模型的服务引擎。您可以在 GitHub 上 找到此示例的代码。您可以检查 Serve Config 了解有关 Serve 部署的更多详细信息
serveConfigV2: |
applications:
- name: llm
route_prefix: /
import_path: ray-operator.config.samples.vllm.serve:model
deployments:
- name: VLLMDeployment
num_replicas: 1
ray_actor_options:
num_cpus: 8
# NOTE: num_gpus is set automatically based on TENSOR_PARALLELISM
runtime_env:
working_dir: "https://github.com/ray-project/kuberay/archive/master.zip"
pip: ["vllm==0.5.4"]
env_vars:
MODEL_ID: "meta-llama/Meta-Llama-3-8B-Instruct"
TENSOR_PARALLELISM: "2"
等待 RayService 资源准备就绪。您可以运行以下命令检查其状态
$ kubectl get rayservice llama-3-8b -o yaml
输出应包含以下内容
status:
activeServiceStatus:
applicationStatuses:
llm:
healthLastUpdateTime: "2024-08-08T22:56:50Z"
serveDeploymentStatuses:
VLLMDeployment:
healthLastUpdateTime: "2024-08-08T22:56:50Z"
status: HEALTHY
status: RUNNING
发送 Prompt#
确认 Ray Serve 部署健康后,您可以为 Serve 应用建立端口转发会话
$ kubectl port-forward svc/llama-3-8b-serve-svc 8000
请注意,KubeRay 在 Serve 应用准备就绪并运行后创建此 Kubernetes Service。此过程可能在 RayCluster 中所有 Pod 运行后需要几分钟。
现在您可以向模型发送 Prompt
$ curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
"model": "meta-llama/Meta-Llama-3-8B-Instruct",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Provide a brief sentence describing the Ray open-source project."}
],
"temperature": 0.7
}'
输出应与以下内容类似,包含模型生成的响应
{"id":"cmpl-ce6585cd69ed47638b36ddc87930fded","object":"chat.completion","created":1723161873,"model":"meta-llama/Meta-Llama-3-8B-Instruct","choices":[{"index":0,"message":{"role":"assistant","content":"The Ray open-source project is a high-performance distributed computing framework that allows users to scale Python applications and machine learning models to thousands of nodes, supporting distributed data processing, distributed machine learning, and distributed analytics."},"logprobs":null,"finish_reason":"stop","stop_reason":null}],"usage":{"prompt_tokens":32,"total_tokens":74,"completion_tokens":42}}