Munch Lab

Jupyter on a remote compute node (without tears)

You can run Jupyter notebook in your browser from a compute node on the cluster. This way your analysis runs on the file system where your data is, and you can keep data, code and documentation in one place. It only works using Chrome browser.

One time setup

Configure jupyter on the cluster

If you use Anaconda python 3 on the cluster (you should install that yourself) you can follow the steps below. Start by logging into the cluster. Then paste and execute the following,  one block at a time:

conda install jupyter
cd /home/$USER
mkdir -p .jupyter
cd .jupyter
openssl req -x509 -nodes -days 10000 -newkey rsa:2048 -keyout mykey.key -out mycert.pem

If the dir you are in does not contain a file called jupyter_notebook_config.py, you need to make one by executing this:

jupyter notebook --generate-config

Now configure your jupyter_notebook_config.py file. You do this in two steps. First, you generate a hashed password like this. The password you pick is the one you will use to access jupyter from the browser on your own machine. Execute the following and type a password when prompted.

hashed_pass=`python -c "from __future__ import print_function; from notebook.auth import passwd; res = passwd(); print(res)"`

Second, you need to copy/paste the following command into your terminal. It is one command, so copy the *entire block*  below into the terminal *before* you press enter. This adds the needed configuration to jupyter_notebook_config.py.

cat <<EOF > jupyter_notebook_config.py
c.NotebookApp.certfile = '/home/$USER/.jupyter/mycert.pem'
c.NotebookApp.keyfile = '/home/$USER/.jupyter/mykey.key'
c.NotebookApp.open_browser = False
c.NotebookApp.ip = '*'
c.NotebookApp.port = $UID
c.NotebookApp.password = '$hashed_pass'
EOF

Configure ssh access to the cluster

You also need to set up your ssh connection to the cluster so you can connect securely without typing the password every time. If you have not done this already this is how you do it:

First test if you have these two files on your local machine:

~/.ssh/id_rsa
~/.ssh/id_rsa.pub

if not you generate them like this you generate a pair of authentication keys like this – do not enter a passphrase when prompted:

ssh-keygen -t rsa

Now use ssh to create a directory ~/.ssh on the cluster (assuming your username there is XXX):

ssh XXX@login.genome.au.dk mkdir -p .ssh

Finally append the public key on your local machine to the file .ssh/authorized_keys on the cluster and enter the password one last time:

cat ~/.ssh/id_rsa.pub | ssh X@login.genome.au.dk 'cat >> .ssh/authorized_keys'

From now on you can log into the cluster from your local machine without being prompted for a password.

Start a notebook server

This is a little more involved but I have written a script that automates these steps. You can get it here or clone it:

git clone https://github.com/kaspermunch/slurm_jupyter

It has lots of options, that you can see using the --help option:

python slurm_jupyter --help

However,  to start a Jupyter server under some project (say monkey) you just need to execute the script like this and it will open a window in your Chrome browser:

slurm_jupyter -A monkey

If your username on the cluster (eg. donald) is different from that on your local machine you also need to supply the that:

slurm_jupyter -u donald -A monkey

To specify that you want 24g of memory and 3 cores, that you want Jupyter to run in a conda environment called primates, and that you want Jupyter to run for up to 11 hours before slurm cancels your job, you can execute it like this:

slurm_jupyter -u donald -A monkey -m 10g -c 3 -e primates -t 11:00:00

The first time Chrome opens the connection to the cluster it will give you an error page saying “Your connection is not private”. You then need to click “Advanced” and then “Proceed to localhost (unsafe)”.  Then your file tree on the cluster should appear.

To stop the server just press Ctrl-C and the script will do all the canceling, closing and cleanup for you before it exits.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: