【1.6】JupyterHub——让Jupyter支持多用户

支持多用户的jupyter

一、安装

1.下载并安装Anaconda3

wget -c https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh
bash Anaconda3-5.2.0-Linux-x86_64.sh

安装提示:

Do you wish the installer to prepend the Anaconda3 install location
to PATH in your /root/.bashrc ? [yes|no]
[no] >>> /data/user/sam/.bashrc

修改环境变量

vim /etc/profile

# Anaconda 3
#export PATH=/data/software/anaconda3/bin:$PATH  #因为现在默认的是anaconda2里面的python2
alias activate3="source /data/software/anaconda3/bin/activate root"
alias deactivate3="source /data/software/anaconda3/bin/deactivate"

source /etc/profile

2.下载安装 jupyterhub

source /data/software/anaconda3/bin/activate root  #启动conda环境,如无提示,后续的操作,都需要在该环境下完成

conda install -c conda-forge jupyterhub

3、创建配置文件

jupyterhub --generate-config
提示:此时的工作路径是/data/software
Writing default config to: jupyterhub_config.py

4、修改配置文件

取消注释,根据注释提示,填写相关信息

c.JupyterHub.ip = 'IP地址'
c.Spawner.ip = '127.0.0.1'
c.JupyterHub.port = 6364
c.PAMAuthenticator.encoding = 'utf8'
c.LocalAuthenticator.create_system_users = True
c.Authenticator.whitelist = {'user1', 'user1', 'user3'}
c.Authenticator.admin_users = {'user1'}
c.LocalAuthenticator.group_whitelist = {'group1'}
c.JupyterHub.statsd_prefix = 'jupyterhub'

组白名单好像会让用户白名单失效?

5、启动JupyterHub

nohup jupyterhub --no-ssl > jupyterhub.log &

(在配置文件目录下执行此命令?)

8、测试访问

用IP+端口测试访问

9、用户管理

用户白名单的用户会自动添加,但无密码,需要修改密码才能登录;

adduser等

二、讨论

2.1.同时支持Python2和python3

Notebook的右上角点new 只看到 python 3 kernel,需要同时支持Python2和python3

查看目前的conda环境中的kernels

[root@pydev pydev]# jupyter-kernelspec list 
Available kernels: 
  python3    /data/software/anaconda3/share/jupyter/kernels/python3

查看是否安装了python2

python2 -m pip --version

然后,在该conda环境下通过Python2关联ipykernel

python2 -m pip install --upgrade ipykernel 
python2 -m pip install ipykernel
python2 -m ipykernel install --user

source activate anaconda2   ##这个是启动py2的conda环境
ipython kernel install

查看该环境下的kernels

jupyter-kernelspec list
Available kernels:
  python2    /root/.local/share/jupyter/kernels/python2
  python3    /data/software/anaconda3/share/jupyter/kernels/python3

重启,打开jupyterhub,就能看到py2和py3的切换了

2.2 同步Linux的环境变量

jupyterhub启动以后,linux的path中新增的部分,发现没法在os.environ[‘PATH’]找到,最后不得已重启jupyterhub。

ps -ef |grep jupy
kill -9 process_id11111

cd /data/sotware # 在配置文件目录下执行
source /data/software/anaconda3/bin/activate root 

nohup jupyterhub --no-ssl > jupyterhub.log &

同时,修改配置文件,限制单个用户的环境变量不要出现

vim /data/software/jupyterhub_config.py

c.Spawner.env_keep = ['PATH', 'PYTHONPATH', 'CONDA_ROOT', 'CONDA_DEFAULT_ENV', 'VIRTUAL_ENV', 'LANG', 'LC_ALL']

##这里是想要单个用户中出现的环境变量
c.Spawner.environment = {}

三、报错

报错1 : configurable-http-proxy没有访问权限

解决办法:

升级: npm install -g configurable-http-proxy

(base) [sam@c01 software]# jupyterhub --no-ssl
[I 2018-12-10 16:35:39.200 JupyterHub app:1667] Using Authenticator: jupyterhub.auth.PAMAuthenticator-0.9.2
[I 2018-12-10 16:35:39.200 JupyterHub app:1667] Using Spawner: jupyterhub.spawner.LocalProcessSpawner-0.9.2
[I 2018-12-10 16:35:39.210 JupyterHub app:1014] Loading cookie_secret from /mnt/nfs/data/software/jupyterhub_cookie_secret
[I 2018-12-10 16:35:39.355 JupyterHub proxy:431] Generating new CONFIGPROXY_AUTH_TOKEN
[W 2018-12-10 16:35:39.361 JupyterHub auth:490] Ignoring username whitelist because group whitelist supplied!
[I 2018-12-10 16:35:39.583 JupyterHub app:1849] Hub API listening on http://127.0.0.1:8081/hub/
[W 2018-12-10 16:35:39.588 JupyterHub proxy:560] Running JupyterHub without SSL.  I hope there is SSL termination happening somewhere else...
[I 2018-12-10 16:35:39.588 JupyterHub proxy:562] Starting proxy @ http://192.168.50.125:6364/
[C 2018-12-10 16:35:39.601 JupyterHub app:1861] Failed to start proxy
    Traceback (most recent call last):
      File "/data/software/anaconda3/lib/python3.6/site-packages/jupyterhub/app.py", line 1859, in start
        await self.proxy.start()
      File "/data/software/anaconda3/lib/python3.6/site-packages/jupyterhub/proxy.py", line 566, in start
        self.proxy_process = Popen(cmd, env=env, start_new_session=True, shell=shell)
      File "/data/software/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
        restore_signals, start_new_session)
      File "/data/software/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
        raise child_exception_type(errno_num, err_msg, err_filename)
    PermissionError: [Errno 13] Permission denied: 'configurable-http-proxy'

(base) [root@c01 software]# npm install -g configurable-http-proxy

/mnt/nfs/data/software/anaconda3/bin/configurable-http-proxy -> /mnt/nfs/data/software/anaconda3/lib/node_modules/configurable-http-proxy/bin/configurable-http-proxy
+ configurable-http-proxy@4.0.1
added 33 packages and updated 7 packages in 11.264s

   ╭─────────────────────────────────────╮
   │                                     │
   │   Update available 5.6.0 → 6.4.1    │
   │       Run npm i npm to update       │
   │                                     │

参考资料

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学