Kubernetes AI 算力集群环境搭建指南
从内核调优、containerd、kubeadm 到 GPU Operator 与 vLLM 部署:AI 算力集群的环境搭建清单,含算力调度隔离、弹性伸缩与交付验收项。
目录36 节
1. 概述与架构总览
1.1 架构分层
AI 算力集群采用分层架构设计,确保底层硬件与上层 AI 框架解耦:
- 基础设施层:GPU/CPU 异构算力、RDMA 高速网络、NVMe/分布式存储。
- 系统容器层:Linux Kernel、Containerd、NVIDIA Driver & CUDA。
- K8s 调度层:K8s Core、Device Plugin、Volcano/Kueue(批处理调度)、网络/存储 CSI。
- AI 服务层:vLLM/Triton(推理)、DeepSpeed/Megatron(训练)、Kubeflow/Ray(编排)。
1.2 推荐软件版本矩阵
| 组件 | 推荐版本 | 备注 |
|---|---|---|
| Kubernetes | 1.28 - 1.31 | 1.28+ 支持更好的 DRA(动态资源分配) |
| Containerd | 1.7.x | 必须配置 Systemd Cgroup |
| NVIDIA Driver | 535+ 或 550+ | 需支持 CUDA 12.x |
| CUDA Toolkit | 12.1 - 12.4 | 匹配 PyTorch 2.x 版本 |
| GPU Operator | v23.9+ | 自动化管理 GPU 驱动与插件 |
| Calico/Cilium | v3.27+ / v1.15+ | 推荐 Cilium 以利用 eBPF 提升网络性能 |
2. 硬件基础设施规划
2.1 GPU 节点选型建议
| 模型规模 | 显存需求 (FP16/BF16) | 推荐 GPU 型号 (单卡/多卡) | 节点配置建议 |
|---|---|---|---|
| 7B - 14B | 16GB - 32GB | L20, RTX 4090, A10 (24G) | 单机单卡 / 单机多卡 |
| 32B - 70B | 64GB - 140GB | A800 (80G), H20, L40S | 单机 4卡/8卡 (张量并行) |
| 100B+ / MoE | 300GB+ | H100/H800 (80G), A100 | 多机 8卡 (3D 并行: TP+PP+DP) |
2.2 网络与存储规划
- 网络:
- 管理/业务网:25Gbps/100Gbps 以太网,用于 K8s 控制面、业务流量。
- 参数面(RDMA):100Gbps/200Gbps/400Gbps RoCE v2 或 InfiniBand,用于多机多卡 NCCL 通信,必须开启 SR-IOV 和 GPUDirect RDMA。
- 存储:
- 系统盘:500GB NVMe SSD。
- 模型/数据盘:分布式并行文件系统(如 JuiceFS, Alluxio, WekaIO),要求高吞吐(>10GB/s)以加速 Checkpoint 保存和模型加载。
3. 操作系统与内核调优
3.1 基础环境配置
推荐使用 Ubuntu 22.04 LTS 或 Rocky Linux 9。在所有节点执行:
# 1. 关闭 Swap
swapoff -a
sed -i '/ swap / s/^$.*$$/#\1/g' /etc/fstab
# 2. 关闭防火墙与 SELinux
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# 3. 加载必要内核模块
cat <<EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
nvidia
nvidia_uvm
EOF
modprobe overlay br_netfilter nvidia nvidia_uvm
3.2 内核参数调优 (sysctl)
针对大模型高并发和网络通信优化:
cat <<EOF | tee /etc/sysctl.d/99-k8s-ai.conf
# 网络优化
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
# 文件描述符
fs.file-max = 2097152
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 8192
# GPU 内存映射优化
vm.max_map_count = 262144
EOF
sysctl --system
4. 容器运行时安装
使用 containerd 作为标准运行时。
# 安装 containerd
apt-get update && apt-get install -y containerd.io
# 生成默认配置
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml
# 修改 Cgroup 驱动为 systemd (关键!)
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
# 配置 NVIDIA Container Runtime (若不使用 GPU Operator 手动安装时需配置)
# 推荐直接使用 GPU Operator 自动注入,此处可跳过手动修改 config.toml 中的 nvidia 运行时配置。
systemctl restart containerd
systemctl enable containerd
5. Kubernetes 集群搭建
5.1 安装 kubeadm 组件
# 以 Ubuntu 为例,添加 K8s 1.30 仓库
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | tee /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
5.2 初始化 Master 节点
创建 kubeadm-config.yaml:
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
kubernetesVersion: v1.30.0
controlPlaneEndpoint: "192.0.2.100:6443" # 高可用 VIP 地址
networking:
podSubnet: "10.244.0.0/16"
serviceSubnet: "10.96.0.0/12"
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: InitConfiguration
nodeRegistration:
criSocket: unix:///var/run/containerd/containerd.sock
执行初始化:
kubeadm init --config=kubeadm-config.yaml --upload-certs
# 配置 kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
6. 网络插件部署
推荐使用 Cilium,其基于 eBPF 技术,在大规模节点和高吞吐场景下性能优于传统 iptables/IPVS。
# 安装 Cilium CLI
CILIUM_CLI_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/cilium-cli/main/stable.txt)
curl -L --remote-name-all https://github.com/cilium/cilium-cli/releases/download/${CILIUM_CLI_VERSION}/cilium-linux-amd64.tar.gz{,.sha256sum}
tar xzvfC cilium-linux-amd64.tar.gz /usr/local/bin
# 安装 Cilium (开启 Hubble 观测)
cilium install --version 1.15.0
cilium hubble enable
cilium status --wait
7. GPU 算力资源纳管
使用 NVIDIA GPU Operator 实现驱动、Device Plugin、DCGM 等组件的自动化生命周期管理。
7.1 安装 GPU Operator
# 添加 NVIDIA Helm 仓库
helm repo add nvidia https://helm.ngc.nvidia.com/nvidia
helm repo update
# 安装 GPU Operator (假设节点已预装 NVIDIA Driver,若未预装则去掉 --set driver.enabled=false)
helm install gpu-operator nvidia/gpu-operator \
--namespace gpu-operator --create-namespace \
--set driver.enabled=false \
--set toolkit.enabled=true \
--set dcgmExporter.enabled=true
7.2 验证 GPU 识别
# 查看节点 GPU 资源
kubectl get nodes -o json | jq '.items[].status.allocatable."nvidia.com/gpu"'
# 运行测试 Pod 验证 CUDA 可用性
kubectl run nvidia-smi --rm -it --image=nvidia/cuda:12.4.0-base-ubuntu22.04 -- nvidia-smi
8. 算力调度与隔离
8.1 节点标签与污点管理
将 GPU 节点隔离,防止普通业务误调度:
# 为 GPU 节点打标签
kubectl label nodes gpu-node-1 accelerator=nvidia-h800 gpu-mem=80G
# 设置污点 (仅允许带有 toleration 的 Pod 调度)
kubectl taint nodes gpu-node-1 dedicated=gpu:NoSchedule
8.2 大模型推理 Pod 调度示例
apiVersion: v1
kind: Pod
metadata:
name: llm-inference
spec:
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: accelerator
operator: In
values: ["nvidia-h800"]
containers:
- name: vllm
image: vllm/vllm-openai:latest
resources:
limits:
nvidia.com/gpu: 2 # 申请 2 张卡
memory: "128Gi"
cpu: "32"
9. 存储方案
大模型加载(几十GB到上百GB)和 Checkpoint 读写对存储 I/O 要求极高。推荐部署 JuiceFS 或 NFS (仅测试环境)。
9.1 模型文件共享挂载 (HostPath/EmptyDir 替代方案)
在生产环境,建议通过 CSI 驱动挂载分布式存储:
volumes:
- name: model-weights
persistentVolumeClaim:
claimName: juicefs-llm-pvc # 预先创建的 PVC,指向分布式存储中的模型目录
containers:
- name: vllm
volumeMounts:
- name: model-weights
mountPath: /models/Qwen2.5-72B
10. 模型服务封装与部署
以部署 vLLM (Qwen2.5-72B) 为例,展示生产级 Deployment 配置。
apiVersion: apps/v1
kind: Deployment
metadata:
name: qwen2-72b-vllm
labels:
app: qwen2-72b
spec:
replicas: 1
selector:
matchLabels:
app: qwen2-72b
template:
metadata:
labels:
app: qwen2-72b
spec:
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"
containers:
- name: vllm
image: vllm/vllm-openai:v0.5.4
args:
- "--model"
- "/models/Qwen2.5-72B-Instruct"
- "--tensor-parallel-size"
- "4"
- "--max-model-len"
- "8192"
- "--gpu-memory-utilization"
- "0.9"
ports:
- containerPort: 8000
name: http
resources:
limits:
nvidia.com/gpu: 4
memory: "256Gi"
cpu: "48"
volumeMounts:
- name: model-storage
mountPath: /models
# 大模型加载慢,必须配置 StartupProbe 防止被 K8s 误杀重启
startupProbe:
httpGet:
path: /health
port: 8000
initialDelaySeconds: 60
periodSeconds: 10
failureThreshold: 60 # 允许最长 10 分钟加载时间
readinessProbe:
httpGet:
path: /health
port: 8000
periodSeconds: 5
livenessProbe:
httpGet:
path: /health
port: 8000
periodSeconds: 15
volumes:
- name: model-storage
persistentVolumeClaim:
claimName: llm-models-pvc
11. 弹性伸缩配置
11.1 基于 GPU 利用率的 HPA
需确保 Prometheus Adapter 已配置将 DCGM 指标映射为 K8s 自定义指标。
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: qwen2-72b-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: qwen2-72b-vllm
minReplicas: 1
maxReplicas: 5
metrics:
- type: Pods
pods:
metric:
name: DCGM_FI_DEV_GPU_UTIL # GPU 利用率指标
target:
type: AverageValue
averageValue: "75" # 当平均 GPU 利用率超过 75% 时扩容
behavior:
scaleUp:
stabilizationWindowSeconds: 60 # 扩容冷却期
scaleDown:
stabilizationWindowSeconds: 300 # 缩容冷却期,防止频繁抖动
12. 监控与可观测性
12.1 监控栈部署
推荐使用 kube-prometheus-stack:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install kube-prometheus prometheus-community/kube-prometheus-stack \
--namespace monitoring --create-namespace
12.2 核心监控指标 (Grafana Dashboard)
GPU Operator 会自动部署 dcgm-exporter,重点监控以下指标:
DCGM_FI_DEV_GPU_UTIL:GPU 计算利用率DCGM_FI_DEV_FB_USED/FREE:显存使用量DCGM_FI_DEV_GPU_TEMP:GPU 温度DCGM_FI_DEV_POWER_USAGE:功耗- 推理业务指标:TTFT (Time To First Token)、TPOT (Time Per Output Token)、请求队列长度。
13. 安全与多租户
13.1 资源配额 (ResourceQuota)
限制特定租户(如 team-ai-research)最多使用 8 张 GPU:
apiVersion: v1
kind: ResourceQuota
metadata:
name: gpu-quota
namespace: team-ai-research
spec:
hard:
requests.nvidia.com/gpu: 8
limits.nvidia.com/gpu: 8
requests.memory: "512Gi"
limits.memory: "1024Gi"
13.2 权限控制 (RBAC)
确保普通开发人员只能查看 Pod 日志,无法删除节点或修改 GPU 调度策略。
14. 多集群架构(进阶)
对于跨地域、多数据中心的超大规模算力池,建议采用多集群管理:
- 控制面:使用 Karmada 或 KubeFed 实现跨集群的工作负载下发。
- 流量调度:结合 Istio 或云厂商 GSLB,根据各集群 GPU 空闲状态进行智能路由。
- 联邦调度:当 A 集群 GPU 满载时,自动将推理任务溢出(Overflow)调度至 B 集群。
15. 故障排查与运维
15.1 常见故障排查命令
# 1. 查看 GPU 插件日志 (Pod 无法调度或识别不到 GPU 时)
kubectl logs -n gpu-operator -l app=nvidia-device-plugin-daemonset
# 2. 查看节点 GPU 硬件状态 (Xid 错误排查)
kubectl exec -it <gpu-pod-name> -- nvidia-smi -q | grep -i "xid"
# 或查看宿主机 dmesg
dmesg -T | grep -i nvidia | grep -i xid
# 3. NCCL 通信超时排查 (多机多卡训练时)
# 检查 RDMA 网络连通性,确认 NCCL_DEBUG=INFO 环境变量已开启以获取详细日志。
15.2 优雅重启与升级
- 驱逐 GPU 节点:升级驱动前,先
kubectl cordon <node>禁止新调度,再kubectl drain <node> --ignore-daemonsets迁移现有推理服务。 - 滚动更新:推理服务 Deployment 配置
RollingUpdate策略,确保升级期间服务不中断。
16. 环境验收清单
在集群交付业务团队前,请逐项核对以下验收标准:
| 维度 | 验收项 | 验收标准 | 状态 |
|---|---|---|---|
| 硬件 | GPU 识别 | 所有节点 nvidia-smi 正常,K8s 识别到正确数量的 nvidia.com/gpu |
[ ] |
| 硬件 | 网络连通 | 多机节点间 Ping 延迟 < 1ms,NCCL 带宽测试达到标称值的 85% 以上 | [ ] |
| 调度 | 污点隔离 | 普通 CPU Pod 无法调度到 GPU 节点 | [ ] |
| 调度 | 拓扑感知 | 多卡 Pod 能够成功调度,且 NCCL 拓扑识别正确 (NVLink/NVSwitch) | [ ] |
| 服务 | 模型加载 | vLLM/Triton 能够成功从共享存储加载模型,StartupProbe 不超时 | [ ] |
| 服务 | 高可用 | 杀掉一个推理 Pod,K8s 自动重建,服务短暂不可用后恢复 | [ ] |
| 监控 | 指标采集 | Grafana 能够实时展示 GPU 利用率、显存、温度等 DCGM 指标 | [ ] |
| 弹性 | HPA 触发 | 压测增加并发,HPA 能够根据 GPU 利用率或队列长度自动扩容 Pod | [ ] |