You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

K3s中实现GPU分片分配的技术问题求助

问题概述

需要在K3s环境中将单块GPU分片分配给多个Pod(例如设置nvidia.com/gpu: 0.5),当前已完成NVIDIA GPU基础配置,可正常使用整卡运行Pod,但无法实现GPU分片;尝试安装NVIDIA GPU Operator时,Pod卡在初始化阶段。

已完成配置与历史问题修复

此前使用默认config.toml.tmpl配置时,GPU Pod出现如下错误:

Warning FailedCreatePodSandBox 2s (x4 over 41s) kubelet Failed to create pod sandbox: rpc error: code = Unknown desc = failed to create containerd task: failed to start shim: exec: "containerd-shim": executable file not found in $PATH: unknown

通过修改containerd配置解决了整卡使用问题,修改后的关键配置片段如下:

[plugins.cri.containerd.runtimes.runc.options]
  BinaryName = "/usr/bin/nvidia-container-runtime"

[plugins.cri.containerd.runtimes.runc]
  # ---- changed from 'io.containerd.runtime.v1.linux' for GPU support
  runtime_type = "io.containerd.runc.v2"
#  runtime_type = "io.containerd.runtime.v1.linux"
# ---- added for GPU support
[plugins.linux]
  runtime = "nvidia-container-runtime"

重启K3s后可正常使用整卡,但GPU分片功能仍无法实现。

当前环境信息

设备插件容器日志

I1118 07:55:45.920375       1 main.go:256] Retreiving plugins.
I1118 07:55:45.920407       1 factory.go:107] Detected NVML platform: found NVML library
I1118 07:55:45.920424       1 factory.go:107] Detected non-Tegra platform: /sys/devices/soc0/family file not found
I1118 07:55:45.920851       1 server.go:165] Starting GRPC server for 'nvidia.com/gpu'
I1118 07:55:45.921242       1 server.go:117] Starting to serve 'nvidia.com/gpu' on /var/lib/kubelet/device-plugins/nvidia-gpu.sock
I1118 07:55:45.923008       1 server.go:125] Registered device plugin for 'nvidia.com/gpu' with Kubelet

节点详情

NAME         STATUS   ROLES                  AGE    VERSION        INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
devops-s2h   Ready    control-plane,master   4d3h   v1.27.7+k3s2   172.16.11.243   <none>        Ubuntu 22.04.3 LTS   6.2.0-36-generic   containerd://1.7.7-k3s1.27

系统与GPU信息

  • uname -a输出:
    Linux devops-S2H 6.2.0-36-generic #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct  9 15:34:04 UTC 2 x86_64 x86_64 x86_64 GNU/Linux 
    
  • nvidia-smi输出:
    +---------------------------------------------------------------------------------------+
    | NVIDIA-SMI 535.129.03             Driver Version: 535.129.03   CUDA Version: 12.2     |
    |-----------------------------------------+----------------------+----------------------+| GPU  Name                 Persistence-M | Bus-Id        Disp.A | Volatile Uncorr. ECC || Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. ||                                         |                      |               MIG M. ||=========================================+======================+======================||   0  Quadro M4000                   Off | 00000000:07:00.0  On |                  N/A || 47%   42C    P8              14W / 120W |    336MiB /  8192MiB |      0%      Default ||                                         |                      |                  N/A |+-----------------------------------------+----------------------+----------------------+
                                                                                           
    +---------------------------------------------------------------------------------------+
    | Processes:                                                                            ||  GPU   GI   CI        PID   Type   Process name                            GPU Memory ||        ID   ID                                                             Usage      ||=======================================================================================||    0   N/A  N/A      1784      G   /usr/bin/gnome-shell                        116MiB ||    0   N/A  N/A      3292    C+G   ...29837382,8897893935064066579,262144      211MiB |+---------------------------------------------------------------------------------------+
    
解决方案

1. 明确GPU硬件限制

你的Quadro M4000不支持NVIDIA的**MIG(多实例GPU)**功能,而官方nvidia.com/gpu的分片请求依赖MIG或时间切片(Time Slicing):

  • MIG仅支持A100、A30、H100等数据中心级GPU;
  • 时间切片是Kubernetes层面的软共享方案,可通过NVIDIA Device Plugin配置启用,适配消费级GPU。

2. 启用时间切片实现GPU分片

步骤1:创建Device Plugin配置文件

创建nvidia-device-plugin-config.yaml:

version: v1
flags:
  migStrategy: none
sharing:
    timeSlicing:
        resources:
        - name: nvidia.com/gpu
          replicas: 2  # 将1张GPU虚拟为2份,每份对应0.5单位请求

步骤2:配置并重启Device Plugin

将配置文件以ConfigMap形式挂载到Device Plugin的DaemonSet中:

  1. 创建ConfigMap:
    kubectl create configmap nvidia-device-plugin-config --from-file=nvidia-device-plugin-config.yaml
    
  2. 修改现有Device Plugin的DaemonSet,添加挂载:
    # 在spec.template.spec.containers[0]下添加volumeMounts
    volumeMounts:
      - name: config
        mountPath: /etc/nvidia-device-plugin
    # 在spec.template.spec下添加volumes
    volumes:
      - name: config
        configMap:
          name: nvidia-device-plugin-config
    
  3. 重启Device Plugin Pod:
    kubectl rollout restart daemonset nvidia-device-plugin-daemonset -n kube-system
    

步骤3:验证资源

查看节点资源,确认虚拟出的GPU份数:

kubectl describe node devops-s2h

此时会显示nvidia.com/gpu: 2,Pod即可设置nvidia.com/gpu: 0.5请求分片资源。

3. 修复GPU Operator初始化卡滞问题

如果坚持使用GPU Operator,按以下步骤排查:

  1. 查看Operator Pod日志定位具体错误:
    kubectl logs -n gpu-operator <gpu-operator-pod-name>
    
  2. 确认节点满足前置条件:内核版本与NVIDIA驱动兼容、containerd配置正确、NVIDIA工具链完整;
  3. 安装Operator时指定适配K3s的runtime参数:
    helm install gpu-operator nvidia/gpu-operator --namespace gpu-operator --create-namespace --set runtimeClassName=nvidia
    
关键注意事项
  • 时间切片是软件层面的资源共享,无硬件隔离,多个Pod会竞争GPU计算资源;
  • 消费级GPU的时间切片性能表现弱于数据中心GPU,高负载场景下可能出现性能波动;
  • 若需要严格的资源隔离,需更换支持MIG的GPU型号。

内容的提问来源于stack exchange,提问作者Adeelhashmi145

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 04:47:08