aliyun cloud linux 3.x vpc内自动安装nvidia驱动

env

  • Alibaba Cloud Linux 3.2104 U12
  • GPU:NVIDIA A10

1.安装aliyun cloud linux 3.x

在阿里云上采购ecs,安装aliyun cloud linux 3.x

cat /etc/os-release
a996e08e440bc727cb9efbac65a88151.png

2.脚本化安装nvidia

2.1查看驱动相关版本

dd1923211e7dc9491d81d12fa6a00428.png

建议在云上,查看相关支持版本。

2.2脚本安装

ecs nvidia-driver

install-nvidia.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/sh

#Please input version to install
DRIVER_VERSION="570.133.20"
CUDA_VERSION="12.8.1"
CUDNN_VERSION="9.8.0.87"

#install or no install
IS_INSTALL_eRDMA="FALSE"
IS_INSTALL_RDMA="FALSE"
INSTALL_DIR="/root/auto_install"

#using .run to install driver and cuda
auto_install_script="auto_install_v4.0.sh"

script_download_url=$(curl http://100.100.100.200/latest/meta-data/source-address | head -1)"/opsx/ecs/linux/binary/script/${auto_install_script}"
echo $script_download_url

rm -rf $INSTALL_DIR
mkdir -p $INSTALL_DIR
cd $INSTALL_DIR && wget -t 10 --timeout=10 $script_download_url && bash ${INSTALL_DIR}/${auto_install_script} $DRIVER_VERSION $CUDA_VERSION $CUDNN_VERSION $IS_INSTALL_RDMA $IS_INSTALL_eRDMA

2.3安装过程日志

# 安装参数检查
CHECKING AUTO INSTALL, DRIVER_VERSION=570.133.20 CUDA_VERSION=12.8.1 CUDNN_VERSION=9.8.0.87 , INSTALL RDMA=FALSE, INSTALL eRDMA=FALSE,  PLEASE WAIT ......

The script automatically downloads and installs a NVIDIA GPU driver and CUDA, CUDNN library. if you choose install RDMA or ERDMA, RDMA or ERDMA software will install.
if you choose install perseus, perseus environment will install as well.

# 步骤
1. The installation takes 15 to 20 minutes, depending on the intranet bandwidth and the quantity of vCPU cores of the instance. Please do not operate the GPU or install any GPU-related software until the GPU driver is installed successfully.

2. After the GPU is installed successfully, the instance will restarts automatically.

CUDA-12.8.1 downloading, it takes 3 minutes or more. Remaining installation time 14 - 19 minutes!
5256100K| #################################################################################################### | 100% 

Driver-570.133.20 installing, it tasks 1 to 3 minutes. Remaining installation time 11 to 15 minutes!
| #################################################################################################### | 100% 

CUDA-12.8.1 installing, it tasks 2 to 5 minutes. Remaining installation time 9 to 12 minutes!
| #################################################################################################### | 100% 

cuDNN-9.8.0.87 installing, it takes about 10 seconds. Remaining installation time 6 to 9 minutes!
| #################################################################################################### | 100% 

Receive Connection closed
Connection websocket closed
...

# 提示安装成功,自动重启系统
安装结果提示ALL INSTALL OK  

3. install docker-ce

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
set -euo pipefail

echo "=== Alibaba Cloud Linux 3.x Docker‑CE 一键安装 (VPC 网络) ==="
echo "开始时间:$(date '+%Y-%m-%d %H:%M:%S')"

# 1. 添加 Docker‑CE 仓库
echo "[1/5] 添加 Docker‑CE repo"
dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

echo "[2/5] 替换为 VPC 内网镜像地址"
sed -i 's#https://mirrors.aliyun.com#http://mirrors.cloud.aliyuncs.com#g' /etc/yum.repos.d/docker-ce.repo

# 2. 安装 Docker‑CE 及相关组件
echo "[3/5] 安装 Docker"
dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 3. 系统性能调优
echo "[4/5] 配置内核与网络优化参数"
SYSCTL_FILE="/etc/sysctl.d/51-container-optimized.conf"
if [[ -f "$SYSCTL_FILE" ]]; then
echo " => $SYSCTL_FILE 已存在,跳过写入"
else
cat <<'EOF' > "$SYSCTL_FILE"
# container‑Optimized Default Parameters
kernel.softlockup_panic = 1
kernel.pid_max = 4194303
kernel.softlockup_all_cpu_backtrace = 1
net.ipv4.neigh.default.gc_thresh3 = 8192
net.ipv4.neigh.default.gc_thresh2 = 1024
net.ipv4.tcp_wmem = 4096 12582912 16777216
net.ipv4.tcp_rmem = 4096 12582912 16777216
net.ipv4.ip_forward = 1
net.ipv4.tcp_max_syn_backlog = 8096
net.core.netdev_max_backlog = 16384
net.bridge.bridge-nf-call-iptables = 1
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.somaxconn = 32768
fs.file-max = 2097152
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 16384
fs.inotify.max_user_watches = 524288
vm.max_map_count = 262144
user.max_user_namespaces = 0
EOF
sysctl --system
fi

# 4. 优化 daemon.json 配置
echo "[5/5] 写入 daemon.json 优化配置"
mkdir -p /etc/docker
cat <<'EOF' > /etc/docker/daemon.json
{
"bip": "172.32.200.1/23",
"fixed-cidr": "172.32.200.1/23",
"default-address-pools": [
{
"base": "172.32.210.1/21",
"size": 28
}
],
"registry-mirrors": ["https://jnxt8d8b.mirror.aliyuncs.com"],
"insecure-registries": ["127.0.0.1"],
"max-concurrent-downloads": 10,
"log-driver": "json-file",
"log-opts": {
"max-size": "1024m",
"max-file": "3"
}
}
EOF

# 启动并启用 Docker
echo "启动并设置 Docker 开机自启"
systemctl enable --now docker

echo "Docker 安装完成,当前 Docker 信息:"
docker info

echo "结束时间:$(date '+%Y-%m-%d %H:%M:%S')"
echo "=== 完成 ==="

4.install nvidia-container-toolkit

install_nvidia_container_toolkit.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash
# ============================================================
# NVIDIA Container Toolkit 安装脚本 (USTC镜像源)
# 适用于 RockyLinux / CentOS Stream / RHEL / Fedora
# 作者: 🔥焰
# ============================================================

set -e

echo "[INFO] 开始安装 nvidia-container-toolkit ..."

# 检查是否为root
if [ "$EUID" -ne 0 ]; then
echo "[ERROR] 请使用 root 权限执行此脚本!"
exit 1
fi

# 步骤 1: 配置 USTC 镜像源 repo
echo "[INFO] 配置 USTC 镜像源..."
curl -s -L https://mirrors.ustc.edu.cn/libnvidia-container/stable/rpm/nvidia-container-toolkit.repo | \
sed 's#nvidia.github.io/libnvidia-container/stable/#mirrors.ustc.edu.cn/libnvidia-container/stable/#g' | \
sed 's#nvidia.github.io/libnvidia-container/experimental/#mirrors.ustc.edu.cn/libnvidia-container/experimental/#g' | \
sed 's#https://nvidia.github.io/libnvidia-container/gpgkey#https://mirrors.ustc.edu.cn/libnvidia-container/gpgkey#g' \
> /etc/yum.repos.d/nvidia-container-toolkit.repo

# 步骤 2: 刷新缓存并安装
echo "[INFO] 更新缓存并安装..."
dnf makecache -y
dnf install -y nvidia-container-toolkit

# 步骤 3: 配置 Docker Runtime
echo "[INFO] 配置 Docker runtime..."
nvidia-ctk runtime configure --runtime=docker

# 步骤 4: 重启 Docker 服务
echo "[INFO] 重启 Docker 服务..."
systemctl daemon-reload
systemctl restart docker

# 步骤 5: 验证安装
echo "[INFO] 验证 nvidia-container-toolkit 安装..."
if command -v nvidia-container-runtime &>/dev/null; then
echo "[OK] nvidia-container-runtime 已安装: $(nvidia-container-runtime --version 2>/dev/null | head -n1)"
else
echo "[WARN] 未检测到 nvidia-container-runtime,请检查安装日志。"
fi

echo "[DONE] NVIDIA Container Toolkit 安装完成 🎉"

5.deploy service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
services:
ocr:
image: xxxxx
container_name: ocr_server_predict-10006
ports:
- "5001:5001"
volumes:
- /etc/localtime:/etc/localtime
deploy:
resources:
reservations:
devices:
- driver: nvidia
capabilities: [gpu]
environment:
shm_size: 8g
command: /bin/bash docker/run.sh
stdin_open: true
tty: true
restart: always