Slurm REST API 使用
草稿: 本文因某种原因尚未公开。
专栏文章
Slurm REST API 部署和使用示例
OpenAPI
API 服务搭建,参考:容器部署 Swagger API 服务
- https://github.com/SchedMD/slurm/blob/slurm-20-11-8-1/src/slurmrestd/plugins/openapi/v0.0.36/openapi.json
- https://github.com/SchedMD/slurm/blob/slurm-20-11-8-1/src/slurmrestd/plugins/openapi/dbv0.0.36/openapi.json
调用关系
本质上,Slurm Rest API 是将 HTTP 的 json/yaml 转化为 Slurm RPC 请求
_____ slurmctld
/
Client --(REST API)-- slurmrestd --(Slurm RCP)--
\_____ slurmdbdslurm-slurmrestd 部署
从 20.02 开始新增 slurm-slurmrestd 服务提供 REST API 服务,基于 JWT 技术认证,参考
默认提供两种模式:
- 基于 Socket 的 Inet Mode
- 基于 TCP 的 Daemon/Listen Mode, Listen on given set of IP:PORTs for client connections.
认证支持:
- rest_auth/jwt
- http header
- rest_auth/local
- proxy auth
- Apache: mod_proxy_http
- NGINX: auth_request
安装
yum install -y slurm-slurmrestd配置
- 基于
Listen Mode指定端口,修改/usr/lib/systemd/system/slurmrestd.service,把如下行的注释去掉
ExecStart=/usr/sbin/slurmrestd -vv -f /etc/slurm/slurmrest.conf $SLURMRESTD_OPTIONS 0.0.0.0:6820- 指定 API 版本
[root@sms ~]# cat /etc/sysconfig/slurmrestd
SLURMRESTD_OPENAPI_PLUGINS=openapi/v0.0.36更多配置参数,参考:slurmrestd --help
- 配置
/etc/slurm/slurmrest.conf
include /etc/slurm/slurm.conf
AuthType=auth/jwt- 配置
/etc/slurm/slurm.conf
AuthAltTypes=auth/jwt
AuthAltParameters=jwt_key=/etc/slurm/jwt_hs256.key- 配置 /etc/slurm/slurmdbd.conf
AuthAltTypes=auth/jwt
AuthAltParameters=jwt_key=/etc/slurm/jwt_hs256.key- 配置 /var/spool/slurm/ctld/jwt_hs256.key
# 若不配置 AuthAltParameters,默认的位置
dd if=/dev/random of=/var/spool/slurm/ctld/jwt_hs256.key bs=32 count=1
# 官方给的方法
dd if=/dev/random of=/var/spool/slurm/statesave/jwt_hs256.key bs=32 count=1
cp /var/spool/slurm/statesave/jwt_hs256.key /etc/slurm/jwt_hs256.key
chown slurm:slurm /etc/slurm/jwt_hs256.key
chmod 0600 /etc/slurm/jwt_hs256.key
chown root:root /etc/slurm
chmod 0755 /etc/slurm启动服务
- 服务模式
systemctl daemon-reload
systemctl enable slurmrestd.service
systemctl start slurmrestd.service- debug 模式
/usr/sbin/slurmrestd -vvvv -f /etc/slurm/slurmrest.conf 0.0.0.0:6820- API 相关查询
$ slurmrestd -a list
slurmrestd: Possible REST authentication plugins:
slurmrestd: rest_auth/jwt
slurmrestd: rest_auth/local- API schema
$ slurmrestd -s list
slurmrestd: Possible OpenAPI plugins:
slurmrestd: openapi/dbv0.0.36
slurmrestd: openapi/v0.0.35
slurmrestd: openapi/v0.0.36Auth
JWT
使用 JWT 认证,需要在 http header 中添加如下:
- X-SLURM-USER-NAME
- X-SLURM-USER-TOKEN
获取 token:
scontrol token username=$USER [lifespan=$LIFESPAN]调用 API:
curl -H "X-SLURM-USER-NAME:$USER" -H "X-SLURM-USER-TOKEN:${SLURM_JWT}" http://localhost:6820/slurm/v0.0.36/jobs- demo
local
通过 socket 通信?
- ping
- diag
- 提交作业
$ cat job.json
{
"job": {
"tasks": 1,
"name": "test",
"partition": "normal",
"tasks_per_core": 1,
"tasks_per_socket": 1,
"sockets_per_node": 1,
"nodes": 1,
"account": "root",
"distribution": "distribution",
"current_working_directory": "/tmp/",
"environment": {
"PATH": "/bin:/usr/bin/:/usr/local/bin/",
"LD_LIBRARY_PATH": "/lib/:/lib64/:/usr/local/lib"
}
},
"script": "#!/bin/bash\nsrun hostname",
"argv": ""
}
$ curl -s -H X-SLURM-USER-NAME:$(whoami) -H X-SLURM-USER-TOKEN:$SLURM_JWT -X POST 'http://rest/slurm/v0.0.36/job/submit' -H "Content-Type: application/json" -d @job.json
{"errors":[],"job_id":3,"step_id":"BATCH","job_submit_user_msg":""}
$ cat << 'EOF' > job.json
{
"job": {
"account": "test",
"ntasks": 20,
"name": "test18.1",
"nodes": [
2,
4
],
"current_working_directory": "/tmp/",
"user_id": 1000,
"group_id": 1000,
"environment": {
"PATH": "/bin:/usr/bin/:/usr/local/bin/",
"LD_LIBRARY_PATH": "/lib/:/lib64/:/usr/local/lib"
}
},
"get_user_environment": true,
"script": "#!/bin/bash\nsbatch <path>abc.sh"
}
EOF
$ curl -s -H X-SLURM-USER-NAME:$(whoami) -H X-SLURM-USER-TOKEN:$SLURM_JWT -X POST 'http://rest/slurm/v0.0.38/job/submit' -H "Content-Type: application/json" -d @job.json缺点
- 用户权限限制欠缺,所有任务都是以 root 账户提交
demo
$ export `scontrol token username=$USER`
$ curl -s -H X-SLURM-USER-NAME:${USER} -H X-SLURM-USER-TOKEN:${SLURM_JWT} -X GET http://localhost:6820/slurm/v0.0.36/ping
$ curl -s -H X-SLURM-USER-NAME:${USER} -H X-SLURM-USER-TOKEN:${SLURM_JWT} -X GET http://localhost:6820/slurm/v0.0.36/jobs说明:
- token 默认有效期为 1800s,可以通过 lifespan 指定有效期
FAQ
jwt 失效后问题
- 错误信息
[root@sms ~]# sinfo
slurm_load_partitions: Zero Bytes were transmitted or received- 解决方式
unset SLURM_JWT然后在执行申请 token 的命令即可
参考
专栏文章
- Slurm 开发
- Slurm rpmbuild
- Slurm 相关术语
- Slurm QoS 使用
- Slurm Job
- Slurm Partition 使用
- Slurm REST API 使用(当前)
- Slurm scontrol 命令
- Slurm sacct/sacctmgr 命令
- Slurm 命令使用说明
- Slurm sreport 命令
- Slurm FaQ
- Slurm 安装部署
最近更新
最新评论