DeepSeek-V3 开启分布式推理时代,实践看这篇就够了
背景介绍
DeepSeek-V3是一款拥有671B参数规模的MoE大模型,自开源以来在AI圈迅速走红。其不仅在多项测试基准中表现出优异的性能,训练成本更是让人眼前一亮。虽然其庞大的参数规模为私有化部署带来了一定的挑战,但也预示着分布式推理时代的到来。
本文中采用vLLM 和 KubeRay作为分布式推理方案,向大家介绍DeepSeek-V3的私有化部署的最佳实践。
在该实践中,以“北京一区”作为示例来创建弹性集群。后续使用的镜像仓库等,也都是在“北京一区”的应用市场公共仓库。
准备工作
本次部署会用到Kubernetes,请确保本地有可用的Kubernestes客户端工具kubectl,安装请参考安装命令行工具(kubectl)。
部署前需要先开通弹性容器集群
,请跟随下面的步骤,完成前期准备工作。
资源需求
DeepSeek-V3模型的参数规模为6710亿,模型的文件大小约为642G。因此,在部署前,请确保开通的弹性容器集群
的资源满足下表中的配置要求。
配置项 | 配置需求 |
---|---|
GPU | H800 * 16 |
CPU | 128核 |
内存 | 512GB |
磁盘 | 1TB |
开通弹性容器集群
请参考弹性容器集群的开通弹性容器集群,完成集群的开通并了解基本的使用方法,用法可参考使用弹性容器集群。成功后,得到kubeconfig文件、弹性容器集群的信息、对象存储和Harbor仓库的相关信息。
需要先设置环境变量,export KUBECONFIG=kubeconfig文件路径,才能够执行kubectl命令
配置文件准备
为了方便操作,为大家准备了配套的配置文件及示例代码,请点击此处下载。
文件名 | 描述 |
---|---|
deepseek-secret.yaml | Harbor仓库的密钥,用于拉取镜像时的认证 |
prepare.yaml | 准备工作的运行环境,不使用GPU资源,用于模型下载等工作 |
kuberay-operator | KubeRay Operator的配置文件目录,用于启动operator |
ray-cluster/ray-cluster.yaml | KubeRay集群的配置文件,用于启动KubeRay集群 |
ray-cluster/ray-svcExporter-chat.yaml | 网络配置文件,用于暴露DeepSeek的推理服务端口,供外部访问 |
上表中各文件中的配置仅以“北京一区”为示例,若您选择在其他智算中心(北京二区,北京三区等)开通弹性容器集群进行本教程的实践,您需要修改以下配置文件:
-
prepare.yaml 文件中HF_ENDPOINT环境变量: 来使用模型的高速下载服务,其他中心的配置可查看模型下载加速。
-
prepare.yaml, ray-cluster/ray-cluster.yaml, kuberay-operator/values.yaml 各文件中对应的 "image", "registry"字段需要改成匹配的镜像仓库地址:
- 北京一区:registry.hd-01.alayanew.com:8443
- 北京二区:registry.hd-02.alayanew.com:8443
- 北京三区:registry.hd-03.alayanew.com:8443
模型准备
该模型文件较大,我们按照如下步骤,从模型市场中快速下载DeepSeek-V3模型文件。
创建Secret
首先,创建Secret用于拉取镜像时的验证。执行下面的命令,,创建Secret
资源。
# 创建namespace
1. kubectl create namespace deepseek
# 创建Secret
2. kubectl apply -f deepseek-secret.yaml
下载模型
执行kubectl apply -f prepare.yaml
命令,创建准备环境Pod
,用于下载模型。
Pod启动成功后,进入prepare的容器中,执行以下操作下载DeepSeek-V3模型。
# 进入prepare容器
1. kubectl exec -it $( kubectl get pod -n deepseek | awk ' NR>1 {print $1}' | grep prepare ) bash -n deepseek
# 安装huggingface工具
2. pip install huggingface
# 下载DeepSeek-V3模型
3. huggingface-cli download --resume-download deepseek-ai/DeepSeek-V3 --local-dir /model/deepseek-ai/DeepSeek-V3
模型文件大约642G,下载时间较长,请耐心等待
下载时间超长可能导致容器终端关闭,进入容器后,可以首先创建tmux终端服用器,详情参考tmux的使用
tmux new -s session_name
KubeRay集群部署
本方案使用KubeRay做为分布式计算框架来实现多机多卡的分布式推理环境。
安装KubeRay-Opertor
进入kuberay-operator目录,执行下面的命令,启动operator。
helm install kuberay-operator -n deepseek --version 1.2.2 .
部署成功后,可以执行下面的命令操作已部署的资源。
# 查看相关资源
1.helm list -n deepseek
# 删除相关资源
2.helm uninstall kuberay-operator -n deepseek
启动集群
完成KubeRay-Opertor安装后,执行kubectl apply -f ray-cluster.yaml
命令,启动KubeRay集群。
集群启动成功后,执行kubectl get pod -n deepseek
查看服务运行情况。
外部访问配置
在弹性容器集群中,无法直接使用NodePort
方式暴露服务。对于需要外部访问的服务,我们可以使用ServiceExporter
。ServiceExporter
是弹性容器集群中用于将服务暴露到外部的组件,将其与需要对外提供服务的Service
绑定,为用户提供外部访问的地址。
apiVersion: osm.datacanvas.com/v1alpha1
kind: ServiceExporter
metadata:
name: ray-svc-chat-exporter
namespace:r deepseek
spec:
serviceName: raycluster-kuberay-head-svc
servicePort: 8000
执行kubectl apply -f ray-svcExporter-chat.yaml
命令,创建ServiceExporter
资源。创建成功后,可以查看ServiceExporter
的信息获取服务访问的地址。通过ServiceExporter
方式暴露的服务端口均为22443
kubectl describe serviceExporter ray-svc-chat-exporter -n deepseek
输出结果
信息省略···
Spec:
Service Name: raycluster-kuberay-head-svc
Service Port: 8000
Status:
Conditions:
Last Transition Time: 2025-01-05T13:04:48Z
Message: IngressRoute successfully updated, url: https://raycluster-kuberay-head-svc-x-deepseek-x-vcw2y2htee7r.sproxy.hd-01.alayanew.com
···
当使用headless类型的svc时,只能暴露该svc上的一个端口。
DeepSeek-V3部署
部署模型
KubeRay集群启动成功后,进入配置了GPU的容器中,执行以下操作部署DeepSeek-V3模型。
1. kubectl exec -it $( kubectl get pod -n deepseek | awk ' NR>1 {print $1}' | grep kuberay-head ) bash -n deepseek
2. vllm serve /model/deepseek-ai/DeepSeek-V3 \
--tensor-parallel-size 16 \
--gpu-memory-utilization 0.95 \
--num-scheduler-steps 20 \
--max-model-len 8192 \
--trust-remote-code
模型加载预计耗时40~50分钟,请耐心等待。
访问模型
- Python
- cURL
- 输出
from openai import OpenAI
openai_api_key = "EMPTY"
openai_api_base = "https://raycluster-kuberay-head-svc-x-deepseek-x-vcw2y2htee7r.sproxy.hd-01.alayanew.com:22443/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
chat_response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a joke."},
],
stream=True
)
curl https://raycluster-kuberay-head-svc-x-deepseek-x-vcw2y2htee7r.sproxy.hd-01.alayanew.com:22443/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-ai/DeepSeek-V3",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a joke."}
]
}'
Sure! Here’s a joke for you:
Why did the scarecrow win the award?
Because he was outstanding in his field! 🌾
Hope that brought a smile to your face! 😄
总结
至此,我们完成了使用KubeRay和vLLM部署DeepSeek-V3模型的全部流程。本文为DeepSeek-V3私有化部署提供了从环境搭建到推理访问的完整技术路径。通过分布式推理模式,大尺寸模型的性能潜力得以充分释放,推动了 AI 应用的规模化落地。