云原生教程【免费下载链接】kubernetes-handbookKubernetes Handbook Kubernetes指南 https://kubernetes.feisky.xyz项目地址https://gitcode.com/gh_mirrors/kub/kubernetes-handbook点击查看免费下载本指南以 Kubernetes Handbook 实践篇的 GPU 章节为核心系统讲解在 Kubernetes 集群中为容器分配 NVIDIA GPU 的完整方案从 v1.8 起基于 Device Plugin 的nvidia.com/gpu资源模型、v1.26 引入的 Dynamic Resource AllocationDRA动态分配机制到 v1.33 的 MIG 分区、设备污点等新特性以及面向 AI/ML 推理场景的 Gateway API Inference Extension 网关管理。读者按本文操作可以完成 GPU 驱动与插件部署、编写可运行的 GPU 工作负载清单并掌握多型号 GPU 调度与推理服务治理的实战方法。概述Kubernetes 中的 GPU 支持Kubernetes 支持容器直接请求 GPU 资源目前仅支持 NVIDIA GPU这在深度学习训练、模型推理等场景中有着大量应用。GPU 与 CPU、内存等常规资源不同它属于扩展资源Extended Resource使用方式经历了三个阶段阶段版本资源模型分配方式加速器时代v1.6 ~ v1.7alpha.kubernetes.io/nvidia-gpuKubelet 内建 Accelerators 特性已删除Device Plugin 时代v1.8 起v1.10 Betanvidia.com/gpu设备插件通过 gRPC 向 Kubelet 注册设备DRA 时代v1.26 起AlphaResourceClass / ResourceClaim调度器 DRA 驱动动态分配设备从源码结构与官方演进路径看后两种方式正是本文的核心Device Plugin 解决了设备厂商无需修改 Kubernetes 核心代码即可接入新硬件的问题而 DRA 则在此基础上提供了动态分配、设备分区与共享等更细粒度的控制能力。仓库中的 Device 插件原理、特性开关 与 DRA 资源管理 文档可作为对照阅读。一、Device Plugin 原理GPU 资源如何接入 Kubernetes在动手部署前先理解 GPU 为什么能以nvidia.com/gpu这种资源名出现在 Pod 中。从 Kubernetes v1.8 开始GPU 以Device Plugin的形式实现其本质是一个 gRPC 接口设备厂商只需实现ListAndWatch()和Allocate()等方法并将 gRPC Server 监听在/var/lib/kubelet/device-plugins/目录下的 Unix Socket 上例如/var/lib/kubelet/device-plugins/nvidiaGPU.sock即可接入 Kubernetes无需修改核心代码。结合 extension/device.md 的实现说明设备插件的完整工作流程为注册插件启动时通过/var/lib/kubelet/device-plugins/kubelet.sock向 Kubelet 注册同时上报插件的 Unix Socket 名称、API 版本号和插件名称格式为vendor-domain/resource如nvidia.com/gpu。Kubelet 会将这些设备暴露到 Node 状态中供调度器使用。上报与分配插件启动后向 Kubelet 发送插件列表、按需分配设备并持续监控设备的实时状态。故障恢复插件要持续监控 Kubelet 的状态在 Kubelet 重启后重新注册自己。Kubelet 刚启动后会清空/var/lib/kubelet/device-plugins/目录因此插件作者可以通过监控自己监听的 Unix Socket 是否被删除来感知这一事件并重新注册。因此设备插件一般推荐使用DaemonSet方式部署并把/var/lib/kubelet/device-plugins以 Volume 形式挂载进容器从而获得失败自动恢复的能力也可以手动运行但那样就没有自动恢复功能了。特性开关Device Plugin 对应DevicePlugins特性门控。在 setup/feature-gates.md 中可以看到DevicePlugins从 v1.8 开始为 Alpha默认关闭v1.10 起升级为 Beta默认开启true。二、Kubernetes v1.8 及更新版本基于 Device Plugin 使用 GPU从 Kubernetes v1.8 开始GPU 以 Device Plugin 形式实现。使用前需要完成以下配置在 kubelet、kube-apiserver、kube-controller-manager 上配置--feature-gatesDevicePluginstruev1.10 之后默认开启可省略在所有的 Node 上安装 NVIDIA 驱动包括 NVIDIA CUDA Toolkit 和 cuDNN 等Kubelet 配置使用 docker 容器引擎默认就是 docker其他容器引擎暂不支持该特性。2.1 NVIDIA 设备插件部署NVIDIA 官方提供了基于 Device Plugin 接口的 GPU 插件 NVIDIA/k8s-device-plugin它依赖nvidia-dockerNVIDIA Container Toolkit安装步骤如下# Install docker-ce curl https://get.docker.com | sh \ sudo systemctl --now enable docker # Add the package repositories distribution$(. /etc/os-release;echo $ID$VERSION_ID) \ curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \ curl -s -L https://nvidia.github.io/libnvidia-container/experimental/$distribution/libnvidia-container.list | \ sed s#deb https://#deb [signed-by/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g | \ sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list # Install nvidia-docker2 and reload the Docker daemon configuration sudo apt-get install -y nvidia-docker2 sudo systemctl restart docker # Test nvidia-smi with the latest official CUDA image sudo docker run --rm --gpus all nvidia/cuda:11.6.2-base-ubuntu20.04 nvidia-smi部署 NVIDIA 设备插件以 DaemonSet 方式运行在集群中kubectl create -f https://raw.githubusercontent.com/NVIDIA/k8s-device-plugin/v0.13.0/nvidia-device-plugin.yml补充说明仓库 extension/device.md 还记录了手动编译插件的方法docker build -t nvidia-device-plugin:1.0.0 .并强调使用该插件时需要配置nvidia-docker 2.0且将nvidia设为 Docker 默认运行时即 docker daemon 的--default-runtimenvidia。较旧的安装方式如 Ubuntu 16.04 下通过sudo apt-key add添加 nvidia-docker 仓库见 setup/addon-list/gpu.md。2.2 GCE/GKE GPU 插件Google Cloud 的 GCE/GKE 提供了另一套 GPU 插件它不需要 nvidia-docker并且支持 CRI 容器运行时适用于 Google Container Engine 环境# Install NVIDIA drivers on Container-Optimized OS: kubectl create -f https://github.com/GoogleCloudPlatform/container-engine-accelerators/raw/master/daemonset.yaml # Install NVIDIA drivers on Ubuntu (experimental): kubectl create -f https://github.com/GoogleCloudPlatform/container-engine-accelerators/raw/master/nvidia-driver-installer/ubuntu/daemonset.yaml # Install the device plugin: kubectl create -f https://github.com/kubernetes/kubernetes/raw/master/cluster/addons/device-plugins/nvidia-gpu/daemonset.yaml2.3 NVIDIA GPU Operator 部署NVIDIA GPU Operator 是一个 Kubernetes Operator用于在集群中自动部署和管理 NVIDIA GPU 相关组件驱动、容器运行时、设备插件等通过 Helm 一条命令即可安装helm install --wait --generate-name \ -n gpu-operator --create-namespace \ nvidia/gpu-operator2.4 请求nvidia.com/gpu资源部署好插件后在 Pod 的resources.limits中声明nvidia.com/gpu即可申请 GPU。下面是一个完整的 GPU Pod 示例$ cat EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: gpu-pod spec: restartPolicy: Never containers: - name: cuda-container image: nvcr.io/nvidia/k8s/cuda-sample:vectoradd-cuda10.2 resources: limits: nvidia.com/gpu: 1 # requesting 1 GPU tolerations: - key: nvidia.com/gpu operator: Exists effect: NoSchedule EOF注意这里的tolerationsNVIDIA 设备插件会为 GPU 节点添加nvidia.com/gpu污点Pod 需要对应的容忍度才能被调度到 GPU 节点上。三、历史版本Kubernetes v1.6 和 v1.7 的 GPU 用法⚠️alpha.kubernetes.io/nvidia-gpu已在 v1.10 中删除新版本请使用nvidia.com/gpu。在 Kubernetes v1.6 和 v1.7 中使用 GPU需要预先配置在所有的 Node 上安装 NVIDIA 驱动包括 NVIDIA CUDA Toolkit 和 cuDNN 等在 apiserver 和 kubelet 上开启--feature-gatesAcceleratorstrueAccelerators特性在 setup/feature-gates.md 中记录为 v1.6 ~ v1.10 的 Alpha 特性已废弃Kubelet 配置使用 docker 容器引擎默认就是 docker其他容器引擎暂不支持该特性。使用资源名alpha.kubernetes.io/nvidia-gpu指定请求 GPU 的个数例如一个 TensorFlow GPU PodapiVersion: v1 kind: Pod metadata: name: tensorflow spec: restartPolicy: Never containers: - image: gcr.io/tensorflow/tensorflow:latest-gpu name: gpu-container-1 command: [python] env: - name: LD_LIBRARY_PATH value: /usr/lib/nvidia args: - -u - -c - from tensorflow.python.client import device_lib; print device_lib.list_local_devices() resources: limits: alpha.kubernetes.io/nvidia-gpu: 1 # requests one GPU volumeMounts: - mountPath: /usr/local/nvidia/bin name: bin - mountPath: /usr/lib/nvidia name: lib - mountPath: /usr/lib/x86_64-linux-gnu/libcuda.so name: libcuda-so - mountPath: /usr/lib/x86_64-linux-gnu/libcuda.so.1 name: libcuda-so-1 - mountPath: /usr/lib/x86_64-linux-gnu/libcuda.so.375.66 name: libcuda-so-375-66 volumes: - name: bin hostPath: path: /usr/lib/nvidia-375/bin - name: lib hostPath: path: /usr/lib/nvidia-375 - name: libcuda-so hostPath: path: /usr/lib/x86_64-linux-gnu/libcuda.so - name: libcuda-so-1 hostPath: path: /usr/lib/x86_64-linux-gnu/libcuda.so.1 - name: libcuda-so-375-66 hostPath: path: /usr/lib/x86_64-linux-gnu/libcuda.so.375.66创建并查看日志验证 TensorFlow 是否识别到 GPU$ kubectl create -f pod.yaml pod tensorflow created $ kubectl logs tensorflow ... [name: /cpu:0 device_type: CPU memory_limit: 268435456 locality { } incarnation: 9675741273569321173 , name: /gpu:0 device_type: GPU memory_limit: 11332668621 locality { bus_id: 1 } incarnation: 7807115828340118187 physical_device_desc: device: 0, name: Tesla K80, pci bus id: 0000:00:04.0 ]使用 GPU 的注意事项无论采用哪个版本请求 GPU 资源时都要遵守以下约束GPU 资源必须在resources.limits中请求写在resources.requests中无效容器可以请求 1 个或多个 GPU不能只请求一部分例如 0.5 个 GPU 是不允许的多个容器之间不能共享 GPUDevice Plugin 时代每个 GPU 设备是整卡分配默认假设所有 Node 安装了相同型号的 GPU异构型号需要结合 Node Affinity 处理见下文。四、Dynamic Resource AllocationDRA方式使用 GPU从 Kubernetes v1.26 开始可以使用DRADynamic Resource Allocation方式来管理 GPU 资源。相比传统的 Device Plugin 方式DRA 提供了更灵活的 GPU 分配和管理能力是 extension/device.md 中描述的 Device 插件下一代演进方案。从调度器角度看concepts/components/scheduler.md 记录了 DRA 调度的工作流程筛选阶段检查节点设备是否满足 ResourceClaim、验证设备污点与容忍度匹配评分阶段按设备利用率对节点打分绑定阶段由 DRA 驱动分配具体设备并更新 ResourceClaim 状态。4.1 DRA 的核心对象DRA 引入三个核心 API 对象resource.k8s.io/v1alpha2ResourceClass由管理员定义描述可用设备类型与配置指定driverName和参数ResourceClaim由用户创建声明需要某种资源可指定allocationMode如WaitForFirstConsumer等第一个消费者出现再分配Pod.resourceClaimsPod 通过claims引用 ResourceClaim 来使用资源。DRA 的完整工作流程为资源类定义 → 资源声明 → 调度决策 → 驱动分配设备 → Pod 使用 → Pod 完成后释放资源。4.2 创建 GPU ResourceClassapiVersion: resource.k8s.io/v1alpha2 kind: ResourceClass metadata: name: nvidia-gpu-class spec: driverName: gpu.nvidia.com parameters: # GPU 内存大小 memory: 16Gi # 计算能力 compute: 7.5 # v1.33 新特性支持 GPU 分区 partitionable: true maxPartitions: 7 # MIG 分区数 # 支持的 CUDA 版本 cudaVersion: 12.0 --- # 用于 AI/ML 工作负载的高性能 GPU 类 apiVersion: resource.k8s.io/v1alpha2 kind: ResourceClass metadata: name: high-perf-gpu-class spec: driverName: gpu.nvidia.com parameters: memory: 80Gi # A100 GPU compute: 8.0 tensorCores: true nvlink: true --- # 共享 GPU 资源类 apiVersion: resource.k8s.io/v1alpha2 kind: ResourceClass metadata: name: shared-gpu-class spec: driverName: gpu.nvidia.com parameters: shared: true maxUsers: 4 timeSlicing: true4.3 创建 GPU ResourceClaim# 独占 GPU 资源声明 apiVersion: resource.k8s.io/v1alpha2 kind: ResourceClaim metadata: name: exclusive-gpu-claim namespace: ml-training spec: resourceClassName: nvidia-gpu-class allocationMode: WaitForFirstConsumer --- # v1.33 特性优先级列表 - 尝试多种 GPU 类型 apiVersion: resource.k8s.io/v1alpha2 kind: ResourceClaim metadata: name: flexible-gpu-claim namespace: ml-training spec: # 按优先级尝试不同的 GPU 类型 resourceClassNames: - high-perf-gpu-class # 优先使用高性能 GPU - nvidia-gpu-class # 备选标准 GPU - shared-gpu-class # 最后尝试共享 GPU allocationMode: WaitForFirstConsumer优先级列表Prioritized List是 v1.33 的 Alpha 特性调度器会按resourceClassNames的顺序尝试分配最佳可用设备。practice/resource-management.md 也给出了类似的备选资源类用法。4.4 使用 DRA GPU 的 Pod# 机器学习训练任务 apiVersion: v1 kind: Pod metadata: name: ml-training-pod namespace: ml-training spec: containers: - name: trainer image: tensorflow/tensorflow:latest-gpu command: [python, train.py] env: - name: NVIDIA_VISIBLE_DEVICES value: all resources: claims: - name: gpu-resource limits: memory: 32Gi cpu: 8 resourceClaims: - name: gpu-resource source: resourceClaimName: exclusive-gpu-claim --- # 推理服务使用共享 GPU apiVersion: v1 kind: Pod metadata: name: inference-pod namespace: ml-inference spec: containers: - name: inference-server image: tensorrt-inference:latest ports: - containerPort: 8080 resources: claims: - name: shared-gpu limits: memory: 4Gi cpu: 2 resourceClaims: - name: shared-gpu source: resourceClaimName: shared-gpu-claim4.5 v1.33 DRA GPU 新特性1. GPU 分区MIG 支持通过partitionable、migEnabled、migProfile等参数可以将一块物理 GPU 划分为多个 MIG 分区供多个工作负载使用# 支持 NVIDIA MIG 分区的 ResourceClass apiVersion: resource.k8s.io/v1alpha2 kind: ResourceClass metadata: name: mig-gpu-class spec: driverName: gpu.nvidia.com parameters: # MIG 配置 migEnabled: true migProfile: 1g.5gb # 1/7 GPU 5GB 内存 partitionable: true --- # 请求 MIG 分区的 Pod apiVersion: v1 kind: Pod metadata: name: mig-workload spec: containers: - name: light-ml-task image: pytorch/pytorch:latest resources: claims: - name: mig-partition resourceClaims: - name: mig-partition source: resourceClaimName: mig-gpu-claim2. GPU 污点和容忍度v1.33 允许在 ResourceSlice 的设备上打设备污点将 GPU 标记为维护、热限流等状态Pod 需要相应的容忍度才能被调度到这些设备上# 将 GPU 标记为维护状态 apiVersion: resource.k8s.io/v1alpha2 kind: ResourceSlice metadata: name: gpu-node-maintenance spec: driverName: gpu.nvidia.com devices: - name: gpu-0 basic: capacity: memory: 16Gi # GPU 污点标记为维护状态 taints: - key: maintenance value: scheduled effect: NoSchedule - key: thermal-throttling value: detected effect: PreferNoSchedule --- # 容忍 GPU 污点的 Pod apiVersion: v1 kind: Pod metadata: name: maintenance-tolerant-gpu-pod spec: containers: - name: monitoring-task image: gpu-monitor:latest resources: claims: - name: gpu-resource resourceClaims: - name: gpu-resource source: resourceClaimName: maintenance-gpu-claim # 容忍 GPU 设备污点 tolerations: - key: resource.kubernetes.io/device.maintenance operator: Equal value: scheduled effect: NoSchedule - key: resource.kubernetes.io/device.thermal-throttling operator: Equal value: detected effect: PreferNoSchedule从 concepts/components/scheduler.md 可以确认调度器在筛选阶段会验证设备污点和 Pod 容忍度的匹配关系从而阻止 Pod 被调度到不合适的设备上。3. 管理员访问控制v1.33 通过命名空间标签限制只有特定管理员命名空间才能创建 ResourceClaim防止非管理员误用设备管理功能# 启用 DRA 管理访问的命名空间 apiVersion: v1 kind: Namespace metadata: name: gpu-admin-namespace labels: # v1.33 特性管理员访问标签 resource.kubernetes.io/admin-access: enabled --- # 只有管理员命名空间才能创建的 ResourceClaim apiVersion: resource.k8s.io/v1alpha2 kind: ResourceClaim metadata: name: admin-gpu-claim namespace: gpu-admin-namespace spec: resourceClassName: high-perf-gpu-class # 管理员级别的配置 parameters: # 允许超额分配 overcommit: true # 强制亲和性 requiredNodeAffinity: nodeSelectorTerms: - matchExpressions: - key: gpu.nvidia.com/class operator: In values: [A100, H100]4.6 DRA GPU 监控和调试使用 GPU 监控 Pod 可以采集设备指标通过挂载/dev与/proc/driver/nvidia暴露 Prometheus 指标# GPU 使用情况监控 Pod apiVersion: v1 kind: Pod metadata: name: gpu-monitor spec: containers: - name: nvidia-smi-exporter image: mindprince/nvidia_gpu_prometheus_exporter:0.1 ports: - containerPort: 9445 name: metrics securityContext: capabilities: add: [SYS_ADMIN] volumeMounts: - name: dev mountPath: /dev - name: proc-driver-nvidia mountPath: /proc/driver/nvidia readOnly: true resources: claims: - name: monitor-gpu resourceClaims: - name: monitor-gpu source: resourceClaimName: monitoring-gpu-claim volumes: - name: dev hostPath: path: /dev - name: proc-driver-nvidia hostPath: path: /proc/driver/nvidia五、多种型号的 GPU使用 Node Affinity 调度如果集群 Node 中安装了多种型号的 GPU可以使用Node Affinity将 Pod 调度到指定 GPU 型号的 Node 上。首先在集群初始化时给 Node 打上 GPU 型号的标签# Label your nodes with the accelerator type they have. kubectl label nodes node-with-k80 acceleratornvidia-tesla-k80 kubectl label nodes node-with-p100 acceleratornvidia-tesla-p100然后在创建 Pod 时通过nodeSelector指定 GPU 型号apiVersion: v1 kind: Pod metadata: name: cuda-vector-add spec: restartPolicy: OnFailure containers: - name: cuda-vector-add # https://github.com/kubernetes/kubernetes/blob/v1.7.11/test/images/nvidia-cuda/Dockerfile image: k8s.gcr.io/cuda-vector-add:v0.1 resources: limits: nvidia.com/gpu: 1 nodeSelector: accelerator: nvidia-tesla-p100 # or nvidia-tesla-k80 etc.补充practice/introduction/tensorflow.md 中的 Kubeflow TensorFlow 示例也体现了 GPU 在训练任务中的使用方式例如通过ks param set cnn num_gpus 1为 CNN 训练指定 GPU 数量。六、在容器中使用 CUDA 库NVIDIA CUDA Toolkit 和 cuDNN 等需要预先安装在所有 Node 上。为了访问/usr/lib/nvidia-375需要将 CUDA 库以 hostPath volume 的形式传给容器。下面以一个nvidia-smiJob 为例演示apiVersion: batch/v1 kind: Job metadata: name: nvidia-smi labels: name: nvidia-smi spec: template: metadata: labels: name: nvidia-smi spec: containers: - name: nvidia-smi image: nvidia/cuda command: [nvidia-smi] imagePullPolicy: IfNotPresent resources: limits: alpha.kubernetes.io/nvidia-gpu: 1 volumeMounts: - mountPath: /usr/local/nvidia/bin name: bin - mountPath: /usr/lib/nvidia name: lib volumes: - name: bin hostPath: path: /usr/lib/nvidia-375/bin - name: lib hostPath: path: /usr/lib/nvidia-375 restartPolicy: Never创建并验证$ kubectl create -f job.yaml job nvidia-smi created $ kubectl get job NAME DESIRED SUCCESSFUL AGE nvidia-smi 1 1 14m $ kubectl get pod -a NAME READY STATUS RESTARTS AGE nvidia-smi-kwd2m 0/1 Completed 0 14m $ kubectl logs nvidia-smi-kwd2m Fri Jun 16 19:49:53 2017 ----------------------------------------------------------------------------- | NVIDIA-SMI 375.66 Driver Version: 375.66 | |--------------------------------------------------------------------------- | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | || | 0 Tesla K80 Off | 0000:00:04.0 Off | 0 | | N/A 74C P0 80W / 149W | 0MiB / 11439MiB | 100% Default | --------------------------------------------------------------------------- ----------------------------------------------------------------------------- | Processes: GPU Memory | | GPU PID Type Process name Usage | || | No running processes found | -----------------------------------------------------------------------------七、附录CUDA 安装方法安装 CUDA# Check for CUDA and try to install. if ! dpkg-query -W cuda; then # The 16.04 installer works with 16.10. curl -O http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb dpkg -i ./cuda-repo-ubuntu1604_8.0.61-1_amd64.deb apt-get update apt-get install cuda -y fi安装 cuDNN首先到 NVIDIA 官网注册并下载 cuDNN v5.1然后运行命令安装tar zxvf cudnn-8.0-linux-x64-v5.1.tgz ln -s /usr/local/cuda-8.0 /usr/local/cuda sudo cp -P cuda/include/cudnn.h /usr/local/cuda/include sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64 sudo chmod ar /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*安装完成后可以运行nvidia-smi查看 GPU 设备的状态$ nvidia-smi Fri Jun 16 19:33:35 2017 ----------------------------------------------------------------------------- | NVIDIA-SMI 375.66 Driver Version: 375.66 | |--------------------------------------------------------------------------- | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | || | 0 Tesla K80 Off | 0000:00:04.0 Off | 0 | | N/A 74C P0 80W / 149W | 0MiB / 11439MiB | 100% Default | --------------------------------------------------------------------------- ----------------------------------------------------------------------------- | Processes: GPU Memory | | GPU PID Type Process name Usage | || | No running processes found | -----------------------------------------------------------------------------八、AI/ML 推理工作负载的网关管理对于运行在 GPU 上的 AI/ML 推理服务可以使用Gateway API Inference Extension进行智能路由和负载平衡。它面向生成式 AI 与大语言模型LLM推理场景核心组件为 InferencePool定义运行模型服务器的 Pod 池与 InferenceModel面向用户的模型端点在 concepts/objects/gateway-api.md 中有详细介绍仓库 manifests/gateway-api 目录则提供了 Gateway API v1.3.0 的完整示例配置。8.1 Gateway API Inference Extension 配置# 定义 GPU 推理服务池 apiVersion: gateway.networking.x-k8s.io/v1alpha1 kind: InferencePool metadata: name: llama2-gpu-pool spec: deployment: replicas: 3 template: spec: containers: - name: vllm-server image: vllm/vllm-openai:latest resources: limits: nvidia.com/gpu: 1 memory: 16Gi cpu: 4 requests: memory: 8Gi cpu: 2 env: - name: MODEL_NAME value: meta-llama/Llama-2-7b-chat-hf - name: GPU_MEMORY_UTILIZATION value: 0.9 nodeSelector: accelerator: nvidia-tesla-v100 --- # 定义模型端点 apiVersion: gateway.networking.x-k8s.io/v1alpha1 kind: InferenceModel metadata: name: llama2-7b-chat spec: poolRef: name: llama2-gpu-pool routing: # 优先级路由高优先级请求优先分配 priority: high # 智能负载平衡基于 GPU 利用率 loadBalancing: strategy: gpu-aware metrics: - name: gpu_utilization target: 80 - name: memory_utilization target: 85 # 金丝雀发布 trafficSplit: - weight: 90 version: stable poolRef: name: llama2-gpu-pool - weight: 10 version: canary poolRef: name: llama2-gpu-pool-canary --- # Gateway 配置 apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: ai-inference-gateway spec: gatewayClassName: inference-gateway-class listeners: - name: https port: 443 protocol: HTTPS tls: mode: Terminate certificateRefs: - name: inference-tls-cert --- # HTTPRoute 将请求路由到推理模型 apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: llama2-inference-route spec: parentRefs: - name: ai-inference-gateway hostnames: - api.ai-platform.example.com rules: - matches: - path: type: PathPrefix value: /v1/chat/completions - headers: - name: x-model-name value: llama2-7b-chat backendRefs: - group: gateway.networking.x-k8s.io kind: InferenceModel name: llama2-7b-chat filters: # 基于请求优先级的路由 - type: ExtensionRef extensionRef: group: gateway.networking.x-k8s.io kind: PriorityFilter name: inference-priority8.2 性能优势使用 Gateway API Inference Extension 管理 GPU 推理工作负载可以获得以下能力智能路由基于 GPU 利用率、内存使用情况等实时指标进行路由决策降低延迟特别是在高查询率下延迟显著降低提高 GPU 利用率更有效的资源分配和负载平衡支持模型版本管理安全的金丝雀发布和 A/B 测试请求优先级重要请求可以获得优先处理。8.3 监控 GPU 推理服务通过 Prometheus ServiceMonitor 采集推理服务的 GPU 指标# GPU 推理服务监控 ServiceMonitor apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: inference-gpu-metrics spec: selector: matchLabels: app: inference-model endpoints: - port: metrics interval: 30s path: /metrics九、选型建议与延伸阅读综合以上方案针对不同的集群版本和场景可作如下选择v1.8 ~ v1.25 集群使用 Device Plugin 方式配合 NVIDIA/k8s-device-plugin 与 nvidia-docker或 NVIDIA GPU Operator管理nvidia.com/gpuv1.26 集群新项目建议评估 DRA尤其是需要动态设备分配、设备分区MIG或共享、复杂设备配置、更好的错误处理与恢复的场景异构 GPU 集群结合 Node 标签与nodeSelector/ Node Affinity 调度到指定型号节点AI/ML 推理服务使用 Gateway API Inference Extension 实现模型感知路由、金丝雀发布与 GPU 感知负载平衡。仓库中与本主题相关的延伸资料还包括Device 插件原理、特性开关、DRA 资源管理、调度器的 DRA 支持、Gateway API 以及 Gateway API 示例配置可继续深入阅读。赞分享云原生教程【免费下载链接】kubernetes-handbookKubernetes Handbook Kubernetes指南 https://kubernetes.feisky.xyz项目地址https://gitcode.com/gh_mirrors/kub/kubernetes-handbook点击查看免费下载相关推荐Calico Felix 计算图Calculation Graph节点全景解析从数据存储更新到数据面下发Calico Felix 计算图Calculation Graph节点全景解析从数据存储更新到数据面下发 导读 Felix 是 Calico 项目中负责在云原生教程Kubernetes WG Device Management 2024 年度报告解读DRA 核心进入 BetaGPU 与加速器资源管理提速Kubernetes WG Device Management 2024 年度报告解读DRA 核心进入 BetaGPU 与加速器资源管理提速 本文基于 Ku开源治理文档研发协作探索NVIDIA k8s-device-plugin优化GPU资源管理的利器探索NVIDIA k8s device plugin优化GPU资源管理的利器 在现代云计算环境中特别是深度学习和高性能计算应用中GPU扮演着至关重要的角色云原生后端容器运行时上一篇连接池动态调整的数学原理FlexyPool中的排队论应用下一篇Open Realtime API Embedded完全指南如何在微控制器上集成OpenAI实时交互能力创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考