在 Azure Kubernetes 上部署语言模型:完整的初学者指南
在 Azure Kubernetes 服务 (AKS) 上部署语言模型:详细的分步实现指南 简介 本综合指南详细介绍了如何使用 vLLM 服务引擎在 Azure Kubernetes 服务上部署大型语言模型 (LLM)。每个步骤都进行了详细解释,包括其必要性以及如何促进整体部署。
目录 先决条件
基础设施设置
Kubernetes 配置
模型部署
测试与验证
生产考虑
维护与监控
先决条件 Azure 基础设施要求 Azure 订阅
什么:已启用计费的活跃 Azure 订阅
为什么:创建和管理 Azure 资源所需
如何验证
az account show Azure CLI
什么:用于管理 Azure 资源的命令行工具
为什么:实现自动化资源创建和管理
安装
适用于 Ubuntu/Debian
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
适用于 macOS
brew install azure-cli Kubernetes 工具
什么:kubectl 及相关工具
为什么:与 Kubernetes 集群交互所需
安装
安装 kubectl
az aks install-cli 所需权限 Azure 权限
订阅/资源组上的“参与者”角色或更高权限
用于虚拟网络配置的“网络参与者”
为什么:允许创建和管理所有必需资源
Hugging Face 账户(针对受限模型)
具有受限模型批准访问权限的账户
具有读取权限的访问令牌
为什么:下载和使用 Llama 等受限模型所需
基础设施设置
- 环境准备
设置环境变量
export MY_RESOURCE_GROUP_NAME="llm-deployment-rg" export MY_AKS_CLUSTER_NAME="llm-cluster" export LOCATION="eastus" 为什么要使用这些变量?
资源组名称:相关资源的逻辑容器
集群名称:AKS 集群的唯一标识符
位置:决定数据中心位置(根据延迟要求选择)
- 资源组创建 az group create
--name $MY_RESOURCE_GROUP_NAME
--location $LOCATION 目的
为所有部署资源创建逻辑容器
便于资源管理和账单跟踪
允许批量操作和访问控制
- AKS 集群创建 az aks create
--resource-group $MY_RESOURCE_GROUP_NAME
--name $MY_AKS_CLUSTER_NAME
--node-count 1
--generate-ssh-keys
--network-plugin azure
--network-policy azure 关键配置解释
node-count: 初始节点数(从小开始,根据需要扩展)
generate-ssh-keys: 自动生成用于节点访问的 SSH 密钥
network-plugin: Azure CNI,用于高级网络功能
network-policy: 启用网络策略强制执行
- 节点池配置 系统节点池 az aks nodepool add
--resource-group $MY_RESOURCE_GROUP_NAME
--cluster-name $MY_AKS_CLUSTER_NAME
--name system
--node-count 3
--node-vm-size D2s_v3 为什么是这些规格?
node-count: 3:为系统组件提供高可用性
D2s_v3:系统服务的 CPU/内存平衡
系统组件的专用池确保稳定性
GPU 节点池 az aks nodepool add
--resource-group $MY_RESOURCE_GROUP_NAME
--cluster-name $MY_AKS_CLUSTER_NAME
--name gpunp
--node-count 1
--node-vm-size Standard_NC4as_T4_v3
--node-taints sku=gpu:NoSchedule
--enable-cluster-autoscaler
--min-count 1
--max-count 3 配置详情
Standard_NC4as_T4_v3:用于优化 LLM 推理的 T4 GPU
node-taints:确保只有 GPU 工作负载在此昂贵节点上运行
enable-cluster-autoscaler:根据需求自动扩展
min-count/max-count:成本控制的扩展边界
- NVIDIA 设备插件安装 apiVersion: apps/v1 kind: DaemonSet metadata: name: nvidia-device-plugin-daemonset namespace: kube-system spec: selector: matchLabels: name: nvidia-device-plugin-ds template: metadata: labels: name: nvidia-device-plugin-ds spec: tolerations
- key: "sku" operator: "Equal" value: "gpu" effect: "NoSchedule" priorityClassName: "system-node-critical" containers
- image: nvcr.io/nvidia/k8s-device-plugin:v0.14.0 name: nvidia-device-plugin-ctr securityContext: allowPrivilegeEscalation: false capabilities: drop: ["ALL"] volumeMounts
- name: device-plugin mountPath: /var/lib/kubelet/device-plugins
每个组件的重要性
DaemonSet:确保插件在所有 GPU 节点上运行
tolerations:允许在被 GPU 污染的节点上运行
priorityClassName:确保插件不会被驱逐
securityContext:实现安全最佳实践
模型部署配置
- 持久卷设置 apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mistral-7b namespace: default spec: accessModes
- ReadWriteOnce resources: requests: storage: 50Gi storageClassName: default 各项设置的目的
50Gi 存储:容纳模型权重和缓存
ReadWriteOnce:单节点访问以确保数据一致性
default 存储类:使用 Azure 托管磁盘
- 服务配置 apiVersion: v1 kind: Service metadata: name: mistral-7b namespace: default spec: ports
- name: http-mistral-7b port: 80 targetPort: 8000 selector: app: mistral-7b type: LoadBalancer 关键组件解释
LoadBalancer 类型:提供外部访问
端口映射:将外部端口 80 路由到容器端口 8000
选择器:将服务链接到特定部署
- 部署配置 apiVersion: apps/v1 kind: Deployment metadata: name: mistral-7b spec: replicas: 1 template: spec: containers
- name: mistral-7b image: vllm/vllm-openai:latest resources: limits: nvidia.com/gpu: 1 memory: 20G requests: nvidia.com/gpu: 1 memory: 6G volumeMounts
- mountPath: /root/.cache/huggingface name: cache-volume
- name: mistral-7b image: vllm/vllm-openai:latest resources: limits: nvidia.com/gpu: 1 memory: 20G requests: nvidia.com/gpu: 1 memory: 6G volumeMounts
配置详情
replicas: 1:每个 GPU 一个实例
资源限制:防止内存问题
卷挂载:持久化模型缓存
健康探测:确保容器健康
部署过程
- 基本部署
创建命名空间
kubectl create namespace llm-serving
应用配置
kubectl apply -f volume.yaml kubectl apply -f service.yaml kubectl apply -f deployment.yaml 2. 验证部署
检查 pod 状态
kubectl get pods -n llm-serving kubectl describe pod
验证服务
kubectl get service mistral-7b 测试与验证
- API 测试
获取外部 IP
export SERVICE_IP=$(kubectl get service mistral-7b -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
测试 API
curl --location "http://$SERVICE_IP/v1/completions"
--header 'Content-Type: application/json'
--data '{ "model": "mistralai/Mistral-7B-Instruct-v0.1", "prompt": "Test prompt", "max_tokens": 50 }' 2. 性能测试
监控 GPU 使用情况
kubectl exec -it -- nvidia-smi
检查响应时间
time curl -X POST "http://$SERVICE_IP/v1/completions" ... 生产考虑 实施安全 网络安全
创建网络策略
kubectl apply -f network-policy.yaml
启用 Azure DDoS 防护
az network ddos-protection enable ... API 安全
实施身份验证
设置速率限制
启用监控
成本优化 资源监控
监控成本
az cost management query ...
根据使用情况进行扩展
kubectl scale deployment mistral-7b --replicas=0 成本削减策略
对非关键工作负载使用点播实例
实施自动扩展
监控和优化资源使用情况
维护过程
- 定期更新
更新部署
kubectl set image deployment/mistral-7b mistral-7b=vllm/vllm-openai:new-version
验证更新
kubectl rollout status deployment/mistral-7b 2. 备份和恢复
备份持久卷
velero backup create llm-backup
如有需要则恢复
velero restore create --from-backup llm-backup 故障排除指南 常见问题和解决方案 未检测到 GPU
验证 NVIDIA 插件安装
检查节点标签和污点
验证 GPU 驱动程序安装
内存问题
调整资源限制
监控内存使用情况
检查内存泄漏
网络问题
验证网络策略配置
检查服务终结点可用性
验证负载均衡器配置
监控设置
- 指标收集
安装 Prometheus
helm install prometheus prometheus-community/prometheus
配置 Grafana
helm install grafana grafana/grafana 2. 日志管理
启用日志分析
az monitor log-analytics workspace create ...
配置容器洞察
az aks enable-addons -a monitoring ... 更多资源和参考文献 文档
Azure Kubernetes 服务
vLLM 文档
Hugging Face 模型
社区支持
Azure Kubernetes 服务 GitHub
vLLM Discord 社区
Hugging Face 论坛