目录
背景:
前提条件:
一、docker环境初始化配置
1. 安装nvidia-docker2
2. 安装docker-compose工具
3. 获取GPU UUID
4. 修改docker runtime为nvidia,指定机器的UUID
二、docker-swarm 环境安装
1. 初始化swarm管理节点
2. 加入工作节点
3. 查看集群节点
三、拷贝基础镜像及部署文件
1. Docker 镜像
2. docker-compose.yml
四、部署应用
1. 部署服务
2. ssh免密验证
实现不同宿主机上的容器可以互通,并且可以免密ssh登陆
机器具备docker、NVIDIA显卡、NVIDIA驱动等相关安装包和硬件设施,nvidia-smi 可以正常显示出机器的GPU显卡
Docker 版本必须 > 1.12
# 执行如下命令
apt install nvidia-docker2 -y
将安装包拷贝到环境,只需要在docker swarm leader节点安装
# 下载docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
mv docker-compose-linux-x86_64 docker-compose
chmod +x docker-compose
mv docker-compose /usr/bin/docker-compose
nvidia-smi -a | grep UUID
修改文件成如下内容,注意其中的GPU UUID需要根据实际的uuid来填写,同时注意格式。
(base) root@nm-zhipu-a100-develop01:~# cat /etc/docker/daemon.json
{
"runtimes": {
"nvidia": {
"path": "nvidia-container-runtime",
"runtimeArgs": []
}
},
"default-runtime": "nvidia",
"node-generic-resources": [
"NVIDIA-GPU=GPU-528afedf-02cf-7380-e22b-e2c05c1be4ec",
"NVIDIA-GPU=GPU-5c46d5eb-0996-a5ac-cb7a-29f274d046a4",
"NVIDIA-GPU=GPU-f57f6d1b-ea13-1480-6321-7a26181b838d",
"NVIDIA-GPU=GPU-75acc759-bb26-da0b-21c5-a31dca12e93f",
"NVIDIA-GPU=GPU-7e4da326-2d9e-3530-9f46-673474746e0b",
"NVIDIA-GPU=GPU-76f183b8-b750-73bc-b438-404a16ae7b78",
"NVIDIA-GPU=GPU-34823444-9a29-0776-f483-c802c6057039",
"NVIDIA-GPU=GPU-a474810c-7356-b242-d0d8-5e83714655de"
],
"default-shm-size": "10G"
}
# 将如下文件中的swarm-resource 这行注释打开
/etc/nvidia-container-runtime/config.toml
swarm-resource = "DOCKER_RESOURCE_GPU"
# 重启docker
systemctl restart docker
# docker swarm leader角色执行
# 初始化一个新的swarm,并且当前的机器变为swarm manager,执行命令后会返回join相关的指令
docker swarm init --advertise-addr 192.168.249.11 #根据实际机器IP填写
类似下图的一条的命令
# worker节点执行,如果有多个worker节点则都需要执行
docker swarm join --token SWMTKN-1-51nvltfqyxlfo0m5f5unzufhs03gmtcs8uoues6zgln0vzhy2c-5agl9x8f9nwrwagy7ud8k6oti 192.168.249.11:2377
# 查看swarm节点信息
docker node ls
如果要在客户方build,需要拷贝Dockerfile文件,并且保证客户环境可以下载依赖的基础镜像和相关的依赖包
Dockerfile
FROM nvcr.io/nvidia/pytorch:23.07-py3
# ADD /data0/nfs/share/pangguoqing/glm66b/data/ /workspace/data
COPY . /workspace
COPY ssh /root/.ssh/
RUN apt-get update && apt-get install -y --no-install-recommends ssh && \
apt-get install -y net-tools && \
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/pdsh/pdsh-2.29.tar.bz2 && \
tar jxvf pdsh-2.29.tar.bz2 && \
cd pdsh-2.29 && \
./configure --with-ssh --with-rsh --with-mrsh--with-mqshell --with-qshell --with-dshgroups--with-machines=/etc/pdsh/machines --without-pam && \
make && \
make install
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install deepspeed && \
pip install sentencepiece
EXPOSE 22
ENV CUDA_DEVICE_MAX_CONNECTIONS="1"
WORKDIR /workspace
ENTRYPOINT service ssh restart && bash
如果有多个worker节点,则需要在文件中增加对应的配置,从0开始往下顺延
挂载点修改
version: '3.7'
services:
main:
image: wind/glm66b-train:base-v2
hostname: main
init: true
tty: true
deploy:
replicas: 1 # 定 义 在 Swarm 中 运 行 的 副 本 数
restart_policy:
condition: on-failure
environment:
- NVIDIA_VISIBLE_DEVICES=all
volumes:
- /data0/nfs/share/pangguoqing/glm66b/data/:/workspace/data/
networks:
- my_overlay_network
worker01:
image: wind/glm66b-train:base-v2
hostname: worker-0
init: true
tty: true
deploy:
replicas: 1
restart_policy:
condition: on-failure
environment:
- NVIDIA_VISIBLE_DEVICES=all
volumes:
- /data0/nfs/share/pangguoqing/glm66b/data/:/workspace/data/
networks:
- my_overlay_network
networks:
my_overlay_network:
driver: overlay
# 部署
docker stack deploy -c docker-compose.yml wande。#根据实际名称修改
# 显示部署的服务
docker stack services wande
# 查看服务容器状态
docker stack ps wande
登陆任意一个容器,通过ssh root@主机名即可登陆
ssh root@worker01
参考文档:
docker service create | Docker Docs
https://www.reddit.com/r/docker/comments/mh36w1/using_nvidia_gpu_with_docker_swarm_started_by/