MT GPU Operator 使用指南
MT GPU Operator 是摩尔线程提供的 Kubernetes GPU 管理组件,用于在 Kubernetes 集群中管理和使用摩尔线程 GPU 设备。本文档介绍 GPU Operator 的基本操作、配置和维护方法。
在 Pod 中使用 GPU 或 sGPU
在 Pod 中使用 GPU
申请 GPU 资源时,只需在 Pod 的 resources.limits 中声明 mthreads.com/gpu 资源即可。以下示例创建一个使用普通 GPU 的 Pod:
$ cat << EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: opencl-test
spec:
restartPolicy: OnFailure
nodeSelector:
kubernetes.io/arch: amd64
containers:
- name: opencl-test
image: "ubuntu:20.04"
imagePullPolicy: Always
command: ["sleep"]
args: ["3600"]
resources:
limits:
mthreads.com/gpu: 1
EOF
在 Pod 中使用 sGPU
要在 Kubernetes 中使用 sGPU,首先需要确保 GPU Operator 安装时已启用 sGPU 相关功能。通过开启 GPU scheduler 组件和 GPU Webhook 组件来支持 sGPU。
在 sGPU 上运行工作负载时,您需要在 Pod 规范中指定 sGPU 资源。以下是与普通 GPU 的资源配置差异:
spec:
containers:
- name: mtpytorch
image: ubuntu:20.04
command:
- "sleep"
args:
- "infinity"
resources:
limits:
- mthreads.com/gpu: 1
+ mthreads.com/sgpu-core: 1
+ mthreads.com/sgpu-memory: 2
sGPU 资源说明
sGPU 使用两种独立的资源类型,可以更精确地控制容器对 GPU 资源的使用。多个 sGPU 容器在共享 1 个 GPU 时,显存独占,但计算资源是根据权重分时复用,不做强隔离。
mthreads.com/sgpu-core:表示计算资源的时间片权重mthreads.com/sgpu-memory:显存数量,以 512 MiB 为单位,设为 2 表示使用 1 GiB 显存
验证 GPU 是否可用
进入容器,安装 OpenCL 相关软件库,然后使用 mthreads-gmi 验证容器内是否可见 GPU:
$ kubectl exec -ti opencl-test bash
root@opencl-test:/# apt-get update && apt-get -y upgrade && apt-get install -y ocl-icd-libopencl1 opencl-headers clinfo
...
root@opencl-test:/# mthreads-gmi
Mon May 19 07:37:31 2025
---------------------------------------------------------------
mthreads-gmi:2.0.0 Driver Version:3.0.0
---------------------------------------------------------------
ID Name |PCIe |%GPU Mem
Device Type |Pcie Lane Width |Temp MPC Capable
| ECC Mode
+-------------------------------------------------------------+
0 MTT S4000 |00000000:01:00.0 |0% 7MiB(49152MiB)
Physical |16x(16x) |88C YES
| N/A
---------------------------------------------------------------
--------------------------------------------------------------
Processes:
ID PID Process name GPU Memory
Usage
+-------------------------------------------------------------+
No running processes found
--------------------------------------------------------------
检查组件
检查 ClusterPolicy 的状态是否就绪:
$ kubectl get clusterpolicy mt-gpu-cluster-policy -o yaml
...
status:
namespace: mt-gpu-operator
state: ready
如果 status 没有出现在 ClusterPolicy 中,这意味着一些组件还没有准备好,可检查 mt-controller-manager Pod 日志,或查询 Operator 各 Pod 的状态。
日志和命令输出会显示哪些组件还没有准备好,以下示例表明个别组件状态是有问题的:container-toolkit、operator-validator、gpu-feature-discovery、device-plugin 和 gpu-exporter。可以向支持人员报告以下结果和有问题的组件状态:
$ kubectl -n mt-gpu-operator get po -o wide
NAME READY STATUS RESTARTS AGE
kured-f94df 1/1 Running 0 62s
kured-k442c 1/1 Running 0 62s
kured-lgwt4 1/1 Running 0 62s
kured-xhd8d 1/1 Running 0 62s
mt-aiops-c988dc879-4pnst 1/1 Running 0 62s
mt-container-toolkit-daemonset-24w6f 1/1 Running 0 62s
mt-container-toolkit-daemonset-rtvr5 0/1 Init:Error 3 (43s ago) 62s
mt-controller-manager-7bbfbfc44c-zl9tr 2/2 Running 0 106s
mt-device-plugin-56hxn 0/1 Init:0/1 0 60s
mt-device-plugin-nqnnn 1/1 Running 0 61s
mt-driver-toolkit-89lsm 1/1 Running 0 62s
mt-driver-toolkit-nwv92 1/1 Running 0 62s
mt-gpu-exporter-fwqww 1/1 Running 0 60s
mt-gpu-exporter-jf26z 0/1 Init:0/1 0 60s
mt-gpu-feature-discovery-djh8g 0/1 Init:0/1 0 61s
mt-gpu-feature-discovery-p2csq 1/1 Running 0 61s
mt-gpu-webhook-5b58696885-2fqpg 1/1 Running 0 62s
mt-gpu-webhook-gen-secret-d9pl2 0/1 Completed 0 62s
mt-gpushare-scheduler-578d5595f9-h59rv 1/1 Running 0 62s
mt-operator-validator-mnlpt 0/1 Init:0/2 0 61s
mt-operator-validator-zzkgh 1/1 Running 0 61s
nfd-master-847694d746-5xw54 1/1 Running 0 63s
nfd-worker-bjg8v 1/1 Running 0 62s
nfd-worker-g8pdl 1/1 Running 0 62s
nfd-worker-vm2zx 1/1 Running 0 62s
nfd-worker-wl5j4 1/1 Running 0 62s
node-problem-detector-hq4lr 1/1 Running 0 62s
node-problem-detector-hztjz 1/1 Running 0 61s
node-problem-detector-j77rg 1/1 Running 0 61s
node-problem-detector-q7fmx 1/1 Running 0 62s
自定义 GPU 资源名
GPU 默认资源名为 mthreads.com/gpu,如用户有自定义需要,可通过以下步骤进行修改。
1. 确认旧资源已释放
确保没有 Pod 使用旧资源名:
mthreads.com/gpu
否则迁移可能无法完成。
2. 配置新资源名
执行:
kubectl edit clusterpolicy gpu-cluster-policy
在 spec.universalGPUDeviceController 下配置:
gpuResourceName: example.com/MTT-S5000-80G
gpuResourceNameMigration:
enabled: true
legacyResourceNames:
- mthreads.com/gpu
timeout: 30m
字段说明:
-
gpuResourceName:本轮期望注册给 kubelet 的整卡 GPU 资源名;为空时等价于mthreads.com/gpu。 -
legacyResourceNames:需要迁移和清理的旧整卡 GPU 资源名。 -
enabled:启用旧整卡 GPU 资源名清理。 -
timeout:资源名迁移的超时时间。
资源名规则:
-
格式:
<DNS前缀>/<资源名称>,且只能包含一个/ -
DNS 前缀:最长 253 字符,仅允许小写字母、数字、
-、. -
资源名称:最长 63 字符,允许大小写字母、数字、
-、_、.
注意:不要在
args中重复配置--gpu-resource-name。
3. 等待并确认迁移完成
迁移通常需要约 7 分钟。
执行:
kubectl get clusterpolicy gpu-cluster-policy \
-o jsonpath='{.status.universalGPUDeviceController.gpuResourceNameMigration.phase}'
输出 Done 即表示迁移完成。
随后检查 Node,确认已注册新的 GPU 资源名:
kubectl describe node <node-name>
4. 更新业务 YAML
后续 GPU 工作负载统一使用新资源名:
resources:
requests:
example.com/MTT-S5000-80G: 1
limits:
example.com/MTT-S5000-80G: 1
注意:不要再提交使用
mthreads.com/gpu的新工作负载。
5. 注意事项
-
无需停止 kubelet
-
不要手工删除 checkpoint 或 socket
-
不要手动修改 Node Status
-
首次迁移时,
legacyResourceNames填写原默认资源名mthreads.com/gpu
驱动生命周期管理
用户可以通过配置 mthreads_v1alpha4_clusterconfig 文件(YAML 安装方式)或 mt-gpu-operator-custom-resources/values.yaml 文件(Helm 安装方式)来指定下载的驱动,该文件中承载的是 ClusterConfig CR。该 CR 的用途:
-
指定需要安装哪些驱动程序。
-
指定集群中 GPU 访问模式的比例。GPU 访问模式包括 GPU、sGPU、passthrough。