摩尔线程云原生套件 v2.0.0 安装指南
容器运行时套件
支持的平台
支持的 Linux 发行版
| 操作系统名称及版本 | amd64 / x86_64 | arm64 / aarch64 |
|---|---|---|
| Ubuntu 20.04 | Y | N/A |
| Kylin V10 SP1 | N/A | Y |
| UnionTech OS Desktop 20 Pro | N/A | Y |
| Debian 12 | Y | N/A |
支持的容器运行时
| 容器运行时名称及版本 | amd64 / x86_64 | arm64 / aarch64 |
|---|---|---|
| Docker 20.10 | Y | Y |
| containerd 1.5 | Y | Y |
安装指南
先决条件
在开始之前,请确保已为您的 Linux 发行版安装了最新的摩尔线程显卡驱动。您可以通过以下命令来进行验证:
❯ mthreads-gmi
Fri May 16 10:41:32 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 S80 |00000000:01:00.0 |0% 516MiB(16384MiB)
Physical |16x(16x) |60C YES
| N/A
---------------------------------------------------------------
---------------------------------------------------------------
Processes:
ID PID Process name GPU Memory
Usage
+-------------------------------------------------------------+
No running processes found
---------------------------------------------------------------
安装 Docker CE
sudo apt install docker.io
更多安装信息请参考官方文档。
安装摩尔线程容器运行时套件
使用 dpkg 包管理工具进行安装:
sudo dpkg -i mtml_2.0.0.deb mt-container-toolkit_2.0.0.deb
绑定摩尔线程容器运行时到 Docker,设置默认的容器运行时为 mthreads 并重启 Docker daemon:
$ (cd /usr/bin/musa && sudo ./docker setup $PWD)
您可以通过如下命令验证上述步骤是否成功:
❯ docker run --rm --env MTHREADS_VISIBLE_DEVICES=all ubuntu:22.04 mthreads-gmi
Fri May 16 02:42:24 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 S80 |00000000:01:00.0 |0% 516MiB(16384MiB)
Physical |16x(16x) |60C YES
| N/A
---------------------------------------------------------------
---------------------------------------------------------------
Processes:
ID PID Process name GPU Memory
Usage
+-------------------------------------------------------------+
No running processes found
---------------------------------------------------------------
可以看到在容器内部已经能够获取到摩尔线程 GPU 设备。
卸载摩尔线程容器运行时套件
恢复 Docker 默认的容器运行时并重启 Docker daemon:
$ (cd /usr/bin/musa && sudo ./docker cleanup $PWD)
通过 dpkg 包管理工具卸载摩尔线程容器 运行时套件:
$ sudo dpkg -r mt-container-toolkit mtml sgpu-dkms
# 完全卸载并清除配置文件
$ sudo dpkg -P mt-container-toolkit mtml sgpu-dkms
使用指南
摩尔线程容器运行时可以使用以下环境变量对容器中的 GPU 设备进行设置:
MTHREADS_VISIBLE_DEVICES设置挂载到容器中的摩尔线程 GPU 设备。MTHREADS_DRIVER_CAPABILITIES设置容器中用户态驱动程序的能力。
GPU 枚举
通过设置 MTHREADS_VISIBLE_DEVICES 环境变量可以指定特定的 GPU 设备到容器中。
MTHREADS_VISIBLE_DEVICES 可以设置的值如下:
| 可设置的值 | 描述 |
|---|---|
0,1,2, 或者 438e3949-f6f2-f7b4-c904-8ffb9d6fa9d3 | 通过 GPU 的设备编号或者 GPU UUID 指定。 |
all | 所有的 GPU 设备均可见。 |
void, empty 或者 unset | mthreads-container-runtime 的行为将和 runc[1] 保持一致。 |
示例:
使用全部 GPU:
❯ docker run --rm -e MTHREADS_VISIBLE_DEVICES=all ubuntu:20.04 mthreads-gmi
Mon Jul 17 07:49:07 2023
---------------------------------------------------------------
mthreads-gmi:2.0.0 Driver Version:3.0.0
---------------------------------------------------------------
ID Name |PCIe |%GPU Mem
Device Type |Pcie Lane Width |Temp MPC Capable
+-------------------------------------------------------------+
0 MTT S2000 |00000000:03:00.0 |0% 4MiB(16384MiB)
Physical |8x(8x) |51C NO
+-------------------------------------------------------------+
1 MTT S2000 |00000000:04:00.0 |0% 4MiB(16384MiB)
Physical |8x(8x) |50C NO
---------------------------------------------------------------
---------------------------------------------------------------
Processes:
ID PID Process name GPU Memory
Usage
+-------------------------------------------------------------+
No running processes found
---------------------------------------------------------------
使用设备号指定 GPU:
❯ docker run --rm -e MTHREADS_VISIBLE_DEVICES=1 ubuntu:20.04 mthreads-gmi
Mon Jul 17 07:49:36 2023
---------------------------------------------------------------
mthreads-gmi:2.0.0 Driver Version:3.0.0
---------------------------------------------------------------
ID Name |PCIe |%GPU Mem
Device Type |Pcie Lane Width |Temp MPC Capable
+-------------------------------------------------------------+
0 MTT S2000 |00000000:04:00.0 |0% 4MiB(16384MiB)
Physical |8x(8x) |50C NO
---------------------------------------------------------------
---------------------------------------------------------------
Processes:
ID PID Process name GPU Memory
Usage
+-------------------------------------------------------------+
No running processes found
---------------------------------------------------------------
使用 GPU UUID 指定 GPU:
❯ mthreads-gmi --query | grep UUID
GPU UUID : 7e8581f5-6722-035b-9b7f-07b8ed270fd0
GPU UUID : 2716ae75-44e4-231b-df26-f7c2c4abb678
❯ docker run --rm -e MTHREADS_VISIBLE_DEVICES=2716ae75-44e4-231b-df26-f7c2c4abb678 ubuntu:20.04 mthreads-gmi
Mon Jul 17 07:50:33 2023
---------------------------------------------------------------
mthreads-gmi:2.0.0 Driver Version:3.0.0
---------------------------------------------------------------
ID Name |PCIe |%GPU Mem
Device Type |Pcie Lane Width |Temp MPC Capable
+-------------------------------------------------------------+
0 MTT S2000 |00000000:04:00.0 |0% 4MiB(16384MiB)
Physical |8x(8x) |50C NO
---------------------------------------------------------------
---------------------------------------------------------------
Processes:
ID PID Process name GPU Memory
Usage
+-------------------------------------------------------------+
No running processes found
---------------------------------------------------------------
驱动能力
MTHREADS_DRIVER_CAPABILITIES 用于控制容器中用户态驱动程序的能力。
MTHREADS_DRIVER_CAPABILITIES 可以设置的值如下:
| 可设置的值 | 描述 |
|---|---|
compute,video 或者 graphics,utility | 可以传入多个驱动能力,以 “,” 为分割符。 |
all | GPU 所有的能力。 |
| empty 或者 unset | 设置默认值: compute, utility。 |
void | 不挂载任何驱动能力。 |
支持设置的驱动能力列表。
| 驱动能力 | 描述 |
|---|---|
compute | 使用 MUSA 和 OpenCL 的应用。 |
graphics | 使用 OpenGL 和 Vulkan 的应用。 |
utility | 使用 mthreads-gmi 和 MTML。 |
video | 使用编解码能力的应用。 |
示例:
❯ docker run --rm \
-e MTHREADS_VISIBLE_DEVICES=all \
-e MTHREADS_DRIVER_CAPABILITIES=compute,utility \
ubuntu:20.04 mthreads-gmi
Mon Jul 17 07:55:29 2023
---------------------------------------------------------------
mthreads-gmi:2.0.0 Driver Version:3.0.0
---------------------------------------------------------------
ID Name |PCIe |%GPU Mem
Device Type |Pcie Lane Width |Temp MPC Capable
+-------------------------------------------------------------+
0 MTT S2000 |00000000:03:00.0 |0% 4MiB(16384MiB)
Physical |8x(8x) |51C NO
+-------------------------------------------------------------+
1 MTT S2000 |00000000:04:00.0 |0% 4MiB(16384MiB)
Physical |8x(8x) |50C NO
---------------------------------------------------------------
---------------------------------------------------------------
Processes:
ID PID Process name GPU Memory
Usage
+-------------------------------------------------------------+
No running processes found
---------------------------------------------------------------
Dockerfiles
构建容器镜像时在 Dockerfile 添加环境变量,运行容器时就无需再指定它们。
ENV MTHREADS_VISIBLE_DEVICES all
ENV MTHREADS_DRIVER_CAPABILITIES compute,utility
MT GPU Operator
先决条件
在安装 MT GPU Operator 前, 您应该确保 Kubernetes 集群满足一些先决条件:
-
节点必须配置容器引擎,例如 Docker CE/EE、containerd 或 cri-o。安装 Docker 请参见安装 Docker Engine。
-
节点功能发现 (NFD) 是每个节点上 GPU Operator 的依赖项。默认情况下,NFD master 和 worker 由 GPU Operator 自动部署。如果在部署 GPU Operator 之前,NFD 已在集群中运行,则可以将 GPU Operator 配置为不安装 NFD。
-
要在 Kubernetes 1.13 和 1.14 中进行监控,请启用 kubelet
KubeletPodResources[a] 功能门。从 Kubernetes 1.15 开始,默认启用KubeletPodResources。 -
要在 Kubernetes 中收集 GPU 使用数据,必须在集群中部署 Prometheus。
$ export VERSION=$(curl -s https://api.github.com/repos/prometheus-operator/prometheus-operator/releases | grep tag_name | grep -v -- '-rc' | grep -v -- '-alpha' | sort -r | head -1 | awk -F': ' '{print $2}' | sed 's/,//' | xargs)$ echo $VERSION$ kubectl create -f https://github.com/prometheus-operator/prometheus-operator/releases/download/${VERSION}/bundle.yaml -
要自动安装 MT 驱动程序,必须在集群内的每个节点上手动安装以下软件包:
-
LightDM 是一个跨桌面显示管理器,可以使用以下命令进行安装:
$ sudo apt install -y lightdm -
libdrm2是内核直接渲染管理器 (DRM) 服务的用户空间接口,可以使用以下命令进行安装。$ sudo apt install -y libdrm2注意:所需的最低版本是
2.4.99。
-
-
GPU 调度器是基于 Scheduling Framework 构建的。如果要启用此功能,请确保 Kubernetes 的版本大于
1.19. -
为了确保 sGPU DEB 包的成功安装,需要首先在集群内的每个节点上安装所需的依赖包
dkms。-
dkms可以使用以下命令安装:$ sudo apt install -y dkms注意:所需的最低版本是
1.95。
-
要启用 Kubelet Pod Resources 功能门,请运行以下命令:
echo -e "KUBELET_EXTRA_ARGS=--feature-gates=KubeletPodResources=true" | sudo tee /etc/default/kubelet
安装指南
先将部署包下载,然后解压:
$ unzip mt-gpu-operator-2.0.0.zip
$ rm mt-gpu-operator-2.0.0.zip
$ cd operator_2.0.0
$ tree
.
├── change_image_repo.sh
├── end-user-cn.md
├── mt-gpu-operator-2.0.0.tgz
├── mt-gpu-operator-custom-resources-2.0.0.tgz
├── mt-gpu-operator.yaml
├── mthreads_v1alpha4_clusterconfig.yaml
├── mthreads_v1beta2_clusterpolicy.yaml
└── sync_image.sh
0 directories, 8 files
$ tar -xvzf mt-gpu-operator-2.0.0.tgz
$ tar -xvzf mt-gpu-operator-custom-resources-2.0.0.tgz
$ tree
.
├── change_image_repo.sh
├── end-user-cn.md
├── mt-gpu-operator
│ ├── Chart.yaml
│ ├── crds
│ │ ├── biosconfigs.mthreads.com-crd.yaml
│ │ ├── clusterconfigs.mthreads.com-crd.yaml
│ │ ├── clusterpolicies.mthreads.com-crd.yaml
│ │ └── nodeconfigs.mthreads.com-crd.yaml
│ ├── templates
│ │ ├── _helpers.tpl
│ │ ├── mt-controller-manager-cm.yaml
│ │ ├── mt-controller-manager-deployment.yaml
│ │ ├── mt-controller-manager-metrics-service-svc.yaml
│ │ ├── mt-controller-manager-sa.yaml
│ │ ├── mt-gpu-operator-cleanup-crd-job.yaml
│ │ ├── mt-gpu-operator-namespace.yaml
│ │ ├── mt-gpu-operator-upgrade-crd-job.yaml
│ │ ├── mt-leader-election-rolebinding-rb.yaml
│ │ ├── mt-leader-election-role-role.yaml
│ │ ├── mt-manager-rolebinding-crb.yaml
│ │ ├── mt-manager-role-cr.yaml
│ │ ├── mt-metrics-reader-cr.yaml
│ │ ├── mt-mutating-webhook-configuration-mutatingwebhookconfiguration.yaml
│ │ ├── mt-proxy-rolebinding-crb.yaml
│ │ ├── mt-proxy-role-cr.yaml
│ │ ├── mt-published-device-cm.yaml
│ │ ├── mt-validating-webhook-configuration-validatingwebhookconfiguration.yaml
│ │ └── mt-webhook-service-svc.yaml
│ └── values.yaml
├── mt-gpu-operator-2.0.0.tgz
├── mt-gpu-operator-custom-resources
│ ├── Chart.yaml
│ ├── templates
│ │ ├── _helpers.tpl
│ │ ├── mt-gpu-cluster-config-clusterconfig.yaml
│ │ └── mt-gpu-cluster-policy-clusterpolicy.yaml
│ └── values.yaml
├── mt-gpu-operator-custom-resources-2.0.0.tgz
├── mt-gpu-operator.yaml
├── mthreads_v1alpha4_clusterconfig.yaml
├── mthreads_v1beta2_clusterpolicy.yaml
└── sync_image.sh
5 directories, 38 files
镜像使用
同步到用户镜像仓库
部署包中提供镜像同步脚本,用户可以将 registry.mthreads.com 上的镜像同步到自己的内网 harbor 中:
$ ./sync_image.sh registry.mthreads.com/mcconline <your.registry.com/repo>
部署包中提供了脚本 change_image_repo.sh 来为用户修改部署包下所有文件中的镜像仓库地址,使用方式如下:
$ ./change_image_repo.sh registry.mthreads.com/mcconline <your.registry.com/repo>
使用 Helm 安装
MT GPU Operator Helm charts 提供了多个可自定义的选项,可以根据您的环境进行配置。
Chart 自定义选项
使用 Helm chart 时可以使用以下选项。这些选项可以与 --set 一起使用。
-
mt-gpu-operator
参数 描述 默认值 namespaceMT GPU Operator 部署在哪个命名空间下。 mt-gpu-operator -
mt-gpu-operator-custom-resources
参数 描述 默认值 mtGpuClusterPolicyClusterpolicy.nfd.enabled将节点功能发现插件部署为 DaemonSet。 如果 NFD 已在集群中运行,请将此变量设置为 false。truemtGpuClusterPolicyClusterpolicy.driverToolkit.enabled默认情况下,GPU Operator 将 MT 驱动程序部署为系统上的容器。在预装 MT 驱动程序的系统上使用 GPU Operator 时,应将此值设置为 false。truemtGpuClusterPolicyClusterpolicy.containerToolkit.enabled默认情况下,GPU Operator 将 MT Container Toolkit 部署为系统上的容器。在预装 MT Container Toolkit 的系统上使用 GPU Operator 时,请将此值设置为 false。truemtGpuClusterPolicyClusterpolicy.kured.enabled将 kured 插件部署为 DaemonSet。如果 kured 已在集群中运行,则将此变量设置为 false。truemtGpuClusterPolicyClusterpolicy.nodeProblemDetector.enabled将节点问题检测器 (NPD) 插件部署为 DaemonSet。如果 NPD 已在集群中运行,请将此变量设置为 false。truemtGpuClusterPolicyClusterpolicy.aiOps.enabled默认情况下,GPU Operator 将 MT AIOps 部署为系统上的容器。如果不需要此组件,请将此值设置为 false。truemtGpuClusterPolicyClusterpolicy.gpuScheduler.enabled默认情况下,GPU Operator 不部署 GPU Scheduler。如果您想使用 sGPU,请将此变量设置为 true。falsemtGpuClusterPolicyClusterpolicy.gpuWebhook.enabled默认情况下,GPU Operator 不部署 GPU Webhook。如果您想使用 sGPU,请将此变量设置为 true。falsemtGpuClusterPolicyClusterpolicy.kubevirt.enabled默认情况下,GPU Operator 部署 KubeVirt 补丁以启用 vGPU 和 GPU 直通。如果要启用虚拟机工作负载,请将此值设置为 true。falsemtGpuClusterPolicyClusterpolicy.dcgm.enabled默认情况下,GPU Operator 会部署 DCGM 组件,该组件是一套 GPU 管理和监控的核心组件。 如果不需要该功能,请将此值设置为 false。truemtGpuClusterPolicyClusterpolicy.dcgmExporter.enabled默认情况下,GPU Operator 会部署 DCGM Exporter 组件,该组件是基于 DCGM 的 Prometheus Exporter。 如果不需要该功能,请将此值设置为 false。true
常见部署场景
本节介绍使用 Helm chart 安装 MT GPU Operator 时的一些常见部署方案。
- 自定义驱动安装
自定义驱动程序工具包配置,用户最好将驱动下载到设备节点本地目录
/usr/data下,然后参考如何指定驱动并配置和本地获取包然后修改 mt-gpu-operator-custom-resources/values.yaml。
$ helm install --wait mt-gpu-operator mt-gpu-operator
$ helm install --wait mt-gpu-operator-custom-resources mt-gpu-operator-custom-resources
- 如果机器上预装了驱动程序和 MT 容器工具包
本示例节点上已预安装 MT 驱动程序和 MT Container Toolkit。
在安装 MT GPU Operator 之前,请确保集群中的容器运行时已配置良好。
Docker:
$ cat /etc/docker/daemon.json | grep default-runtime
"default-runtime": "mthreads",
containerd:
$ cat /etc/containerd/config.toml | grep default_runtime_name
default_runtime_name = "mthreads"
使用以下选项安装 MT GPU Operator。
$ helm install --wait mt-gpu-operator mt-gpu-operator
$ helm install --wait mt-gpu-operator-custom-resources mt-gpu-operator-custom-resources \
--set mtGpuClusterPolicyClusterpolicy.driverToolkit.enabled=false \
--set mtGpuClusterPolicyClusterpolicy.containerToolkit.enabled=false
-
自定义 sGPU 配置
默认情况下,sGPU 相关功能处于禁用状态。如果你想在 k8s 中使用 sGPU,你需要开启 GPU scheduler 组件和 GPU Webhook 组件,使用如下命令安装 mt-gpu-operator 和 mt-gpu-operator-custom-resources:
$ helm install --wait mt-gpu-operator mt-gpu-operator
$ helm install --wait mt-gpu-operator-custom-resources mt-gpu-operator-custom-resources \
--set mtGpuClusterPolicyClusterpolicy.gpuScheduler.enabled=true \
--set mtGpuClusterPolicyClusterpolicy.gpuWebhook.enabled=true
验证 GPU Operator 安装
安装 Helm Chart 后,检查 Pod 的状态以确保所有容器都在运行并且验证已完成。
$ kubectl get pods -n mt-gpu-operator
NAME READY STATUS RESTARTS AGE
kured-xzgk9 1/1 Running 0 28m
mt-aiops-784789fd44-6cwdf 1/1 Running 0 17m
mt-container-toolkit-daemonset-28466 1/1 Running 0 35s
mt-controller-manager-77674d7677-sgx99 2/2 Running 0 29m
mt-dcgm-8gwtd 1/1 Running 0 32s
mt-dcgm-exporter-zbrb4 1/1 Running 1 35s
mt-driver-toolkit-wxcsz 1/1 Running 0 28m
mt-gpu-exporter-jsjwk 1/1 Running 0 33s
mt-gpu-feature-discovery-zrlhn 1/1 Running 0 34s
mt-node-status-exporter-kdgb4 1/1 Running 0 33s
mt-operator-validator-qt4tg 1/1 Running 0 32s
mt-universal-gpu-device-controller-bx29w 1/1 Running 0 34s
nfd-master-8699f4456b-jszpc 1/1 Running 0 28m
nfd-worker-7k9g8 1/1 Running 0 28m
nfd-worker-htsbc 1/1 Running 0 28m
nfd-worker-szsjd 1/1 Running 0 28m
node-problem-detector-26tgc 1/1 Running 0 28m
node-problem-detector-ntsx7 1/1 Running 0 28m
node-problem-detector-vsjnx 1/1 Running 0 28m
使用 YAML 部署 MT GPU Operator
以下是如何使用 yaml 文件安装 MT GPU Operator 当前官方版本的示例。
$ kubectl apply -f mt-gpu-operator.yaml
等待 mt-controller-manager pod ready:
$ kubectl get po -n mt-gpu-operator
NAME READY STATUS RESTARTS AGE
mt-controller-manager-68fc5978f5-c2rpf 2/2 Running 0 24s
根据需要修改 mthreads_v1beta2_clusterpolicy 文件,然后创建该 CR:
$ kubectl apply -f mthreads_v1beta2_clusterpolicy.yaml
修改 mthreads_v1alpha4_clusterconfig.yaml 文件,指定驱动等配置:
$ kubectl apply -f mthreads_v1alpha4_clusterconfig.yaml
检查 MT GPU Operator 管理的组件的状态是否都是 Running:
$ kubectl get po -n mt-gpu-operator
NAME READY STATUS RESTARTS AGE
kured-xzgk9 1/1 Running 0 28m
mt-aiops-784789fd44-6cwdf 1/1 Running 0 17m
mt-container-toolkit-daemonset-28466 1/1 Running 0 35s
mt-controller-manager-77674d7677-sgx99 2/2 Running 0 29m
mt-dcgm-8gwtd 1/1 Running 0 32s
mt-dcgm-exporter-zbrb4 1/1 Running 0 35s
mt-driver-toolkit-wxcsz 1/1 Running 0 28m
mt-gpu-exporter-jsjwk 1/1 Running 0 33s
mt-gpu-feature-discovery-zrlhn 1/1 Running 0 34s
mt-node-status-exporter-kdgb4 1/1 Running 0 33s
mt-operator-validator-qt4tg 1/1 Running 0 32s
mt-universal-gpu-device-controller-bx29w 1/1 Running 0 34s
nfd-master-8699f4456b-jszpc 1/1 Running 0 28m
nfd-worker-7k9g8 1/1 Running 0 28m
nfd-worker-htsbc 1/1 Running 0 28m
nfd-worker-szsjd 1/1 Running 0 28m
node-problem-detector-26tgc 1/1 Running 0 28m
node-problem-detector-ntsx7 1/1 Running 0 28m
node-problem-detector-vsjnx 1/1 Running 0 28m
通过 ClusterConfig 快速安装驱动
- 用户在安装完 mt-gpu-operator 和 mt-gpu-operator-custom-resources 两个 chart 后,需要手动配置为集群配置驱动。
- 用戶需要在每个 gpu 节点上的
/usr/data目录(如该目录不存在,就创建出来)下放置 ddk 和 mtml 两个 deb 包。 - 修改 ClusterConfig CR 对象, ClusterConfig 对象用于控制集群哪些节点安装哪些驱动。
$ kubectl edit clusterconfig gpu-cluster-config
ClusterConfig 按如下配置:
apiVersion: mthreads.com/v1alpha4
kind: ClusterConfig
metadata:
name: gpu-cluster-config
spec:
upgradePolicy:
autoUpgrade: true
allocateStrategy: all
drain:
deleteEmptyDir: false
enable: false
force: false
timeoutSeconds: 300
maxParallelUpgrades: 1
podDeletion:
deleteEmptyDir: false
force: false
timeoutSeconds: 300
waitForCompletion:
timeoutSeconds: 0
podSelector: ""
nodes:
- nodePoolName: node-pool-1
drivers:
- kind: musa
local:
filePath: /usr/data/musa.deb
- kind: libmtml.so
local:
filePath: /usr/data/mtml.deb
clusterGpuTargetRatio:
gpu: 100%
workspaceDir: /app/data
sgpuSpec:
"max_inst": "16"
"policy": "0"
"overcommit_ratio": "1.1"
"time_slice": "1"
selector:
matchExpressions:
- key: kubernetes.io/arch #这是节点上的label,用户根据需要配置label key
operator: In
values:
- amd64
等待 NodeConfig 对象 InstallDone:
$ kubectl get nodeconfig
NAME DRIVER UPGRADE STATE NODE POOL INSTALLATION TIMEOUT
worker-1 InstallDone node-pool-1
worker-2 InstallDone node-pool-1
worker-3 InstallDone node-pool-1
worker-4 InstallDone node-pool-1
InstallDone 的节点上可以看到 gpu 资源:
$ kubectl describe node worker-4
...
Capacity:
mthreads.com/gpu: 8
...
运行示例 GPU 应用程序
使用普通 GPU
此示例运行一个简单的 CUDA 示例,它将两个向量相加:
$ 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
进入 opencl-test 容器,安装 opencl 相关软件库,然后使用 clinfo 验证:
$ 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
---------------------------------------------------------------
使用 sGPU
要在 sGPU 上运行相同的工作负载,您可以将以下差异应用于 Pod 规范。
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: 1
虚拟机使用 GPU
部署 KubeVirt
注意:目前测试过的版本是 V1.5.2, 其他版本需要自行测试。 KubeVirt 可以使用 KubeVirt Operator 进行安装,该 Operator 管理所有 KubeVirt 核心组件的生命周期。
- 使用
kubectl部署 KubeVirt Operator:export VERSION=$(curl -s https://api.github.com/repos/kubevirt/kubevirt/releases | grep tag_name | grep -v -- '-rc' | grep -v -- '-alpha' | sort -r | head -1 | awk -F': ' '{print $2}' | sed 's/,//' | xargs)echo $VERSIONkubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-operator.yaml - 使用
kubectl部署 KubeVirt 自定义资源定义:kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-cr.yaml
验证 KubeVirt 组件
默认情况下,KubeVirt 部署 7 个 Pod、3 个服务、1 个守护进程集、3 个部署应用程序、3 个副本集。
- 检查部署情况:
kubectl get kubevirt.kubevirt.io/kubevirt -n kubevirt -o=jsonpath="{.status.phase}"
- 检查组件:
kubectl get all -n kubevirt
开启 kubevirt
在 clusterpolicy 对象中将 kuebevirt 开启:
apiVersion: mthreads.com/v1beta2
kind: ClusterPolicy
metadata:
name: gpu-cluster-policy
spec:
kubevirt:
enabled: true
创建 VMIs
现在,您可以继续使用两种类型的 GPU 创建虚拟机实例 (VMI):vGPU 和 GPU 直通。
创建具有摩尔线程 GPU 直通的 VMI
此示例运行具有摩尔线程 GPU 直通功能的 Ubuntu 20.04(focal)VMI。
apiVersion: kubevirt.io/v1
kind: VirtualMachineInstance
metadata:
name: focal
spec:
domain:
clock:
timer:
hpet:
present: false
hyperv: {}
pit:
tickPolicy: delay
rtc:
tickPolicy: catchup
utc: {}
cpu:
cores: 8
sockets: 2
threads: 1
features:
acpi: {}
apic: {}
hyperv:
relaxed: {}
spinlocks:
spinlocks: 8191
vapic: {}
machine:
type: q35
resources:
requests:
memory: 4G
firmware:
bootloader:
efi:
secureBoot: false
devices:
gpus:
- deviceName: mthreads.com/MTT-S2000
name: gpu1
virtualGPUOptions:
display:
enabled: false
ramFB:
enabled: true
disks:
- name: vmdisk
disk:
bus: virtio
inputs:
- type: tablet
bus: usb
name: tablet
volumes:
- name: vmdisk
containerDisk:
image: sh-harbor.mthreads.com/cloud/vmdisks/focal
imagePullPolicy: IfNotPresent
通过应用前面的 YAML 文件,您可以启动并运行 VMI:
kubectl apply -f focal.yaml
kubectl get vmi
在 Kubernetes 集群中预热 VM 镜像
kube-fledged 是一个 Kubernetes operator,用于直接在 Kubernetes 集群的工作节点上创建和管理容器镜像的缓存。为了快速启动 VMI,我们可以使用 kube-fledged 来预热 VM 镜像。
部署 kube-fledged
export KUBEFLEDGED_NAMESPACE=kube-fledged
kubectl create namespace ${KUBEFLEDGED_NAMESPACE}
helm repo add kubefledged-charts https://senthilrch.github.io/kubefledged-charts/
helm repo update
gpg --keyserver keyserver.ubuntu.com --recv-keys 92D793FA3A6460ED
gpg --export >~/.gnupg/pubring.gpg
helm install --verify kube-fledged kubefledged-charts/kube-fledged -n ${KUBEFLEDGED_NAMESPACE} --wait
通过应用以下 YAML 文件,您可以创建 VM 磁盘缓存:
apiVersion: kubefledged.io/v1alpha2
kind: ImageCache
metadata:
name: vmdisks
namespace: mt-gpu-operator
labels:
app: kubefledged
kubefledged: imagecache
spec:
cacheSpec:
- images:
- sh-harbor.mthreads.com/cloud/vmdisks/focal:latest
nodeSelector:
kubernetes.io/arch: amd64
卸载 MT GPU Operator
使用 Helm 卸载 MT GPU Operator
可以通过运行以下命令卸载 MT GPU Operator Custom resource 的 chart:
$ helm uninstall mt-gpu-operator-custom-resources
然后执行以下命令卸载 MT GPU Operator 的 chart:
$ helm uninstall mt-gpu-operator
运行 helm uninstall 不会删除 CRD。您可以使用以下命令删除 CRD:
$ kubectl delete --ignore-not-found=true crd clusterconfigs.mthreads.com clusterpolicies.mthreads.com nodeconfigs.mthreads.com
使用 YAML 卸载
$ kubectl delete -f mthreads_v1alpha4_clusterconfig.yaml
$ kubectl delete -f mthreads_v1beta2_clusterpolicy.yaml
$ kubectl delete -f mt-gpu-operator.yaml
如何指定驱动并配置
用户可以通过配置 mthreads_v1alpha4_clusterconfig 文件来指定下载的驱动,该文件中承载的是 clusterconfig CR,该 CR 的用途:
- 指定需要安装哪些驱动程序。
- 指定集群 中 GPU 访问模式的比例。 GPU 访问模式包括 GPU、可扩展 GPU (sGPU)、虚拟 GPU (vGPU) 和直通 GPU。
基本配置
您可以直接使用以下配置,无需修改。以下 ClusterConfig 对象表示集群中的节点需要安装的 GPU 驱动和 sGPU 驱动。balanced-gpu 表示 sGPU、GPU、passthrough GPU 访问模式各占 33% 来配置集群中的 GPU 设备。
apiVersion: mthreads.com/v1alpha4
kind: ClusterConfig
metadata:
name: gpu-cluster-config
spec:
upgradePolicy:
autoUpgrade: false
allocateStrategy: all
drain:
deleteEmptyDir: false
enable: false
force: false
timeoutSeconds: 300
maxParallelUpgrades: 1
podDeletion:
deleteEmptyDir: false
force: false
timeoutSeconds: 300
waitForCompletion:
timeoutSeconds: 0
podSelector: ""
nodes:
- nodePoolName: node-pool-1
drivers:
- kind: musa
oss:
endpoint: oss.mthreads.com
id: string
secret: string
useSSL: false
bucketName: product-release
objectName: develop/20230607/musa_2023.06.07-D+4358+dkms+glvnd+Ubuntu_amd64.deb
md5sum: string
- kind: libmtml.so
oss:
endpoint: oss.mthreads.com
id: string
secret: string
useSSL: false
bucketName: release-ci
objectName: mtml/release_1.5/a9d889431_mtml_1.5.0-linux-R_amd64.deb
md5sum: string
- kind: sgpu
oss:
endpoint: oss.mthreads.com
id: string
secret: string
useSSL: false
bucketName: release-ci
objectName: cloud/sgpu/master/20230607/sgpu-dkms_1.1.1_amd64.deb
md5sum: string
clusterGpuTargetRatio:
sgpu: 100%
workspaceDir: /tmp
sgpuSpec:
"max_inst": "16"
"policy": "0"
"overcommit_ratio": "1.1"
"time_slice": "1"
驱动升级策略
在 mt-driver-toolkit 升级驱动过程中,需要考虑到每次驱动容器重启时,都必须卸 载驱动内核模块,然后再次加载。因此,升级驱动程序时需要执行具体操作,包括以下步骤:
- 如果 mtgpu 模块处于活动状态(/sys/module/mtgpu/refcnt 的值不为 0),则只能通过重新启动节点来卸载驱动程序。
- 如果 mtgpu 模块未使用,driver-toolkit 会卸载旧的 mtgpu 驱动程序内核模块,这将禁止所有客户端使用 GPU 驱动程序。
- 加载更新的 mtgpu 驱动程序内核模块。
- Container-toolkit 将设置节点容器运行时。
- 启用 GPU 驱动程序的客户端。
驱动升级配置选项
ClusterConfig 中的以下字段可用于配置 clusterconfig 控制器:
autoUpgrade负责启用或禁用驱动程序升级策略。默认值为false。当设置为false时,将忽略所有其他选项,并且即使更新了 ClusterConfig 对象中的drivers、clusterGpuTargetRatio或selector字段,也不会启动升级过程。maxParallelUpgrades是可以并行升级的节点数。 0 表示无限。默认值为 1。allocateStrategy指定哪个操作会触发为节点重新分配 GPU 角色,这个重新分配操作也会导致节点上的驱动重新安装。all:指示当字段(clusterGpuTargetRatio或drivers或selector或sgpuSpec或vgpuMedvWeight)更改时将采取的重新分配操作。onClusterGpuTargetRatioUpdate表示只有clusterGpuTargetRatio