### 1. **准备环境和资源**
- **Singularity 镜像**:确保已经下载或创建了RStudio Server的Singularity镜像,
singularity pull --name singularity-rstudio.simg shub://nickjer/singularity-rstudio
- **Slurm 作业脚本**:编写一个用于提交Slurm作业的脚本,其中指定了作业的资源请求、运行时间、输出日志位置以及启动RStudio Server的命令。
### 2. **编写Slurm作业脚本**
创建一个Slurm作业脚本(如`rstudio-server.sh`),示例如下:
```bash
#!/bin/bash
#SBATCH --job-name=rstudio-server # 设置作业名称
#SBATCH --partition=main # 指定运行的分区
#SBATCH --time=08:00:00 # 设置最大运行时间
#SBATCH --ntasks=1 # 请求1个任务
#SBATCH --cpus-per-task=1 # 每个任务分配1个CPU
#SBATCH --mem=16000 # 分配16GB内存
#SBATCH --output=slurm_%j.out # 输出日志文件
#SBATCH --signal=USR2 # 信号处理
module load singularity # 加载Singularity模块
export RSTUDIO_PASSWORD=$(openssl rand -base64 15) # 生成随机登录密码
readonly PORT=$(python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()') # 获取未使用端口
cat 1>&2 <<END
1. SSH tunnel from your workstation using the following command:
ssh -N -L 8787:${HOSTNAME}:${PORT} ${USER}@${SLURM_SUBMIT_HOST}.$(dnsdomainname)
and point your web browser to http://localhost:8787
2. Log in to RStudio Server using the following credentials:
user: ${USER}
password: ${RSTUDIO_PASSWORD}
When done using RStudio Server, terminate the job by:
1. Exit the RStudio Session ("power" button in the top right corner of the RStudio window)
2. Issue the following command on the login node:
scancel -f ${SLURM_JOB_ID}
END
# 启动RStudio Server
singularity exec $HOME/simg/singularity-rstudio.simg rserver --www-port ${PORT} --auth-none 0 --auth-pam-helper rstudio_auth
```
### 3. **提交Slurm作业**
在命令行中使用 `sbatch` 命令提交作业:
```bash
sbatch rstudio-server.sh
```
### 4. **建立SSH隧道**
提交作业后,查看作业的输出日志(`slurm_<job_id>.out`),获取如何连接到RStudio Server的信息。按照日志中的提示,使用SSH隧道转发来连接RStudio Server:
```bash
ssh -N -L 8787:<node>:<port> <user>@<cluster>
```
- `<node>`:RStudio Server运行的节点名称。
- `<port>`:RStudio Server使用的端口。
- `<user>`:集群用户名。
- `<cluster>`:HPC集群的登录节点。
### 5. **访问RStudio Server**
在本地浏览器中访问 `http://localhost:8787`,使用集群用户名和生成的密码登录RStudio Server。
### 6. **关闭RStudio Server**
完成工作后,可以通过以下步骤关闭RStudio Server:
1. 退出RStudio会话(点击右上角的“电源”按钮)。
2. 在命令行中使用 `scancel` 命令取消作业:
```bash
scancel -f <job_id>
```
### 总结
- **环境准备**:确保Singularity镜像已经准备好。
- **编写脚本**:创建并提交Slurm作业脚本,指定资源、运行时间和启动命令。
- **建立连接**:通过SSH隧道连接到远程的RStudio Server。
- **使用和关闭**:访问RStudio Server完成工作后,正确关闭作业。
这个方法结合了Slurm的作业管理和Singularity的容器技术,适合在HPC集群上运行RStudio Server,特别适用于需要高性能计算资源的用户。
参考tpall/rstudio-slurm: Run Rstudio server in slurm cluster. (github.com)