🏗️ 一、整体架构拓扑(生产级)
Security Observability Model & Storage Kubernetes Cluster Vault Secrets Content Filter Audit Log > SLS/ELK Prometheus + DCGM Exporter Loki + Promtail Grafana Harbor / ModelScope 私有镜像 MinIO / S3 模型存储 pgvector RAG 向量库 KServe InferenceService
vLLM Pod A10 x1 Istio Ingress mTLS KServe InferenceService
vLLM MoE Pod A100 x4 Model Cache NFS / EBS RAG Service Optional Client WAF + CDN Kong / Traefik API Gateway
✅ 核心原则:
- GPU 资源隔离:专用 GPU 节点池 + taint/toleration
- 零信任网络:服务间 mTLS(Istio)
- 模型不可变:Docker 镜像封装量化模型
- 全链路可观测:指标 + 日志 + 链路追踪
🧱 二、详细部署步骤
1. 基础设施准备
Kubernetes 集群要求:
- 版本:v1.26+
- GPU 节点池:
- 实例类型:g5.2xlarge(1×A10)或 p4d.24xlarge(8×A100)
- 标签:node.kubernetes.io/gpu-type=A10
- Taint:dedicated=gpu:NoSchedule
- 安装组件:
- NVIDIA Device Plugin
- DCGM Exporter(GPU 监控)
- Istio(1.22+,启用 mTLS)
2. 构建 vLLM 生产镜像(含量化模型)
Dockerfile(Qwen-7B-AWQ 示例):
bash
FROM nvidia/cuda:12.1-runtime-ubuntu22.04
# 安装依赖
RUN apt update && apt install -y python3-pip git
RUN pip install --no-cache-dir vllm==0.4.3 modelscope==1.14.0
# 复制 AWQ 量化模型(由 CI 流水线生成)
COPY ./models/qwen/Qwen-7B-Chat-AWQ /models/qwen-7b-chat-awq
# 非 root 运行
RUN useradd -m -u 1001 vllm && chown -R vllm:vllm /models
USER 1001
EXPOSE 8000
CMD ["python", "-m", "vllm.entrypoints.openai.api_server", \
"--model", "/models/qwen-7b-chat-awq", \
"--trust-remote-code", \
"--dtype", "auto", \
"--max-model-len", "8192", \
"--gpu-memory-utilization", "0.92", \
"--port", "8000"]
🔑 关键参数说明:
- --trust-remote-code:Qwen/ChatGLM 必须
- --gpu-memory-utilization=0.92:避免 OOM
- --max-model-len=8192:支持长上下文
构建并推送:
bash
docker build -t harbor.internal/llm/vllm-qwen-7b-awq:v1.0 .
docker push harbor.internal/llm/vllm-qwen-7b-awq:v1.0
3. Kubernetes 部署(KServe + HPA)
KServe InferenceService YAML:
yaml
# vllm-qwen-isvc.yaml
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: qwen-7b-vllm
namespace: llm-prod
spec:
predictor:
minReplicas: 3
maxReplicas: 20
scaleMetric: concurrency # 基于并发请求数扩缩容
containers:
- name: kserve-container
image: harbor.internal/llm/vllm-qwen-7b-awq:v1.0
resources:
limits:
nvidia.com/gpu: 1
memory: 32Gi
cpu: "8"
requests:
nvidia.com/gpu: 1
memory: 16Gi
cpu: "4"
ports:
- containerPort: 8000
livenessProbe:
httpGet: { path: /health, port: 8000 }
initialDelaySeconds: 120
readinessProbe:
httpGet: { path: /health, port: 8000 }
initialDelaySeconds: 60
volumeMounts:
- name: model-cache
mountPath: /models
volumes:
- name: model-cache
persistentVolumeClaim:
claimName: pvc-nfs-models # NFS 共享存储(多副本共享模型)
GPU 指标 HPA(基于利用率):
yaml
# hpa-gpu.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: qwen-7b-vllm-hpa
spec:
scaleTargetRef:
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
name: qwen-7b-vllm
metrics:
- type: Pods
pods:
metric:
name: DCGM_FI_DEV_GPU_UTIL # 来自 DCGM Exporter
target:
type: AverageValue
averageValue: "70" # GPU 利用率 70% 触发扩容
minReplicas: 3
maxReplicas: 20
💡 HPA 前提:已部署 Prometheus Adapter 将 GPU 指标暴露给 K8s
4. 网络与安全
Istio mTLS + 授权策略:
yaml
# peer-authentication.yaml
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
spec:
mtls:
mode: STRICT # 强制服务间 mTLS
yaml
# authorization-policy.yaml
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: vllm-access
spec:
selector:
matchLabels:
app: qwen-7b-vllm
rules:
- from:
- source:
principals: ["cluster.local/ns/llm-prod/sa/api-gateway"]
API Gateway 配置(Kong):
- 插件:JWT 认证、限流(100 req/min)、CORS
- 路由:POST /v1/chat/completions → http://qwen-7b-vllm.llm-prod.svc:8000
5. 可观测性体系
Prometheus 指标(vLLM 自动暴露 /metrics):
| 指标 | 说明 |
|---|---|
| vllm:request_duration_seconds | 请求延迟(P99 < 300ms) |
| vllm:tokens_processed_total | Token 吞吐量 |
| DCGM_FI_DEV_GPU_UTIL | GPU 利用率 |
Grafana 仪表盘关键 Panel:
- 实时 QPS(按模型版本)
- 平均首 Token 延迟(TTFT)
- GPU 显存使用趋势
- 错误率(HTTP 5xx)
结构化日志(JSON):
json
{
"timestamp": "2025-12-05T10:00:00Z",
"service": "qwen-7b-vllm",
"request_id": "req-a1b2c3",
"user_id": "user_123",
"prompt_tokens": 128,
"completion_tokens": 64,
"total_time_ms": 185,
"status_code": 200
}
🔒 日志脱敏:通过 Fluent Bit 过滤器移除 prompt/response 原文
6. 高可用与灾备
- 多可用区部署:GPU 节点分布在 2 个 AZ
- 模型缓存:NFS 或 EBS Multi-Attach(避免每次拉取模型)
- 蓝绿发布:通过 KServe Canary 流量切分(5% → 100%)
- 自动恢复:Pod Crash 后自动重建(K8s 自愈)
⚙️ 三、资源规划与成本(A10 GPU)
| 组件 | 规格 | 数量 | 月成本(USD) |
|---|---|---|---|
| GPU 节点 | g5.2xlarge (1×A10) | 10 | $12,000 |
| CPU 节点 | c6i.4xlarge | 5 | $3,000 |
| 存储 | EBS gp3 1TB | 10 | $800 |
| 总计 | ~$15,800/月 |
💡 成本优化:
- 使用 AWQ/GPTQ 量化:显存↓40%,单卡并发↑50%
- 夜间缩容至 0:KServe 支持 scale-to-zero
- Spot 实例:非核心服务(如 Embedding)使用 Spot
✅ 四、生产 Checklist
- 使用 量化模型(AWQ/GPTQ)降低显存
- 启用 --trust-remote-code(Qwen/ChatGLM 必须)
- GPU 节点 专用 taint/toleration
- 部署 DCGM Exporter + HPA
- 服务间 mTLS(Istio)
- 结构化日志 + 脱敏
- Grafana 监控 P99 延迟
- API Gateway 限流 + JWT
- 蓝绿发布流程
📦 五、交付物清单(可直接使用)
- Helm Chart:vllm-kservice(含 HPA/探针/卷挂载)
- Dockerfile 模板:支持 Qwen/Llama/Mistral
- Grafana Dashboard JSON:vLLM 专属监控
- Istio Policy 集:mTLS + RBAC
- 压测脚本:locust 模拟高并发对话
💡 最后建议:
不要直接使用裸 vLLM 进程部署 ------缺乏扩缩容、健康检查、服务发现。
生产首选 KServe / Triton + vLLM,已在阿里云百炼、AWS SageMaker 验证。