Singularity 容器技术
Singularity 是一个开源的容器平台,专为高性能计算(HPC)和企业级计算(EPC)工作负载优化设计。它的目标是提供一种简化、快速且安全的方式来运行应用程序。与传统容器不同,Singularity 允许在集群环境中以非特权用户身份运行容器,同时保证计算任务的可控性和安全性。这样,用户可以在容器内部获得接近 root 的运行权限,而不会破坏宿主机的安全策略,从而在 HPC 集群上高效地使用各种软件环境和工具链。
手册以 AutoDock4 为例演示镜像构建(镜像构建推荐在 login06-07 上执行,这两台登录节点配置更高,构建速度快)。
1. 查询镜像
前往 Docker Hub 搜索所需镜像。
例如 autodock4:
lucioric/autodock42. 构建与存放 SIF 镜像
建议用户在 个人目录下单独建立镜像目录,所有容器镜像统一放这里:
mkdir -p ~/lustre1/singularity_images
cd ~/lustre1/singularity_images构建镜像:
singularity build autodock4.sif docker://lucioric/autodock4autodock4.sif:本地生成的 Singularity 镜像文件。docker://...:从 Docker Hub 拉取对应镜像。拉取镜像:拉取镜像需要翻墙,可联系管理员获取权限。上传镜像:用户也可以将本地构建好的 .sif 镜像上传至集群。示例镜像:示例文件夹~/example/singularity/下提供了 autodock4.sif ,可直接复制使用。
生成后的文件路径为:
~/lustre1/singularity_images/autodock4.sif3. 新建测试目录并准备输入文件
创建 AutoDock 测试目录:
mkdir -p ~/lustre1/singularity_images/autodock_test
cd ~/lustre1/singularity_images/autodock_test拷贝示例输入文件:
cp -rf /lustre1/share/singularity/plk1 .目录结构示例:
~/lustre1/singularity_images/
├── autodock4.sif
└── autodock_test/
├── plk1/ # 容器化运行目录
└── plk1-2/ # 非容器化运行目录4. 本地测试运行
进入测试目录:
cd ~/lustre1/singularity_images/autodock_test/plk1运行容器:
singularity run -B $PWD:$PWD -H $PWD \
~/lustre1/singularity_images/autodock4.sif autodock4 -p 1tce-a.dpf说明:
-B $PWD:$PWD:将当前目录挂载到容器内相同路径;-H $PWD:设置容器内 home 为当前目录,避免路径错乱;~/lustre1/singularity_images/autodock4.sif:统一存放的镜像文件。
运行过程中终端会被占用,任务完成后程序会自动退出并返回命令行。 如需中途停止,可按 Ctrl+C 强制终止。
测试确认无误后,请使用 Slurm 脚本提交正式计算。
WARNING
不要在登录节点长时间运行容器,正式计算必须提交 Slurm 作业!
5. Slurm 脚本提交(容器化运行)
在 ~/lustre1/singularity_images/autodock_test/ 下新建脚本:job-cnlong-c-sin.srp
#!/bin/bash
#SBATCH -J autodock_test_sin # 作业名
#SBATCH -p cn-long # 分区
#SBATCH -N 1 # 节点数
#SBATCH -c 1 # CPU 核心数
#SBATCH -o %x_%j.out # 输出文件
#SBATCH -e %x_%j.err # 错误文件
#SBATCH -A skl_g1 # 账户
#SBATCH --qos=sklcnl # QOS
cd ~/lustre1/singularity_images/autodock_test/plk1
time singularity run -B $PWD:$PWD -H $PWD \
~/lustre1/singularity_images/autodock4.sif autodock4 -p 1tce-a.dpf提交作业:
sbatch job-cnlong-c-sin.srp6. 运行验证与排错
验证容器是否生效:
ls /usr/local/bin/autodock4如果系统没有该文件,说明运行的是容器内版本。
[skl_lcy@login01 autodock_test]# ls /usr/local/bin/autodock4
ls: 无法访问'/usr/local/bin/autodock4': No such file or directory7. 非容器化方式对比
建议新建独立目录 plk1-2 用于非容器化测试
cp -rf /lustre1/share/singularity/plk1 ~/lustre1/singularity_images/autodock_test/plk1-2在 ~/lustre1/singularity_images/autodock_test/ 下新建脚本: job-cnlong-c-nocontainer.srp:
#!/bin/bash
#SBATCH -J autodock_test_nc
#SBATCH -p cn-long
#SBATCH -N 1
#SBATCH -c 1
#SBATCH -o %x_%j.out
#SBATCH -e %x_%j.err
#SBATCH -A skl_g1
#SBATCH --qos=sklcnl
source /appsnew/source/ADFRsuite-1.0.sh
cd ~/lustre1/singularity_images/autodock_test/plk1-2
time autodock4 -p 1tce-a.dpf提交作业:
sbatch job-cnlong-c-nocontainer.srp容器化与非容器化脚本都使用了 time,运行结束后会在 .err 文件中记录耗时。可以用下面命令快速对比:
grep -H -E "real|user|sys" ~/lustre1/singularity_images/autodock_test/*.err | cut -d'/' -f7-输出示例:
autodock_test/autodock_test_nc_67944.err:real 50m24.680s
autodock_test/autodock_test_nc_67944.err:user 50m17.500s
autodock_test/autodock_test_nc_67944.err:sys 0m0.361s
autodock_test/autodock_test_sin_67946.err:real 50m5.471s
autodock_test/autodock_test_sin_67946.err:user 49m58.176s
autodock_test/autodock_test_sin_67946.err:sys 0m0.413s
- real = 实际耗时
- user = CPU 用户态时间
- sys = CPU 内核态时间
结果表明,容器化 与 非容器化 的运行时间差不多。