Jupyterhub安装配置及心得

Jupyter简介

Jupyter是一款基于python的web notebook服务,目前有大多python数据挖掘与机器学习爱好者使用这款服务,其特性其实与Ipytohn Notebook差不多,准确说Ipython Notebook是一款提供增强型交互的功能的shell,而Jupyter除了Ipython的功能,还加入了普通编辑器的通用功能,是一款带代码交互的动态文档web编辑器。

Jupyterhub简介

由于Jupyter只支持单用户的使用场景,作为一个可交互的web服务,只支持单用户模式实在让人难以理解。估计开发者也觉得这个问题太过鸡肋,于是Jupyterhub因有而生。

安装Anaconda

JupyterHub是基于Python的,所以我们需要安装一下Python的相关环境。Anaconda是一个非常成熟的Python包管理工具,因此本文档选用该工具进行基础环境的安装,首先就是安装该工具。

下载安装

  • 下载
    • 这里我使用清华的镜像站点下载,速度很nice,50-60MB/s
      wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda2-2018.12-Linux-x86_64.sh
  • 安装
    bash Anaconda2-2018.12-Linux-x86_64.sh

    根据提示安装即可,安装过程中会询问你是否将anaconda的路径加入到环境变量.bashrc中,默认是no,所以如果在安装的过程中手太快,一键到底了的话,可以通过手动添加的方式进行设置。

将anaconda3加入环境变量

vim ~/.bashrc

在bashrc文件的最后添加

# added by Anaconda3 2018.12 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/opt/anaconda3/bin/conda' shell.bash hook 3> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/opt/anaconda3/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base
    else
        \export PATH="/opt/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<

保存并退出
然后

source ~/.bashrc

检测Anaconda是否安装成功:

conda list

如果提示conda: command not found,请参考是否将Anaconda3加入环境变量,并且更新生效。

Jupyterhub安装

  • 可以使用清华的conda源加速
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge  
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/  
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/  
conda config --set show_channel_urls yes  
conda install -c conda-forge jupyterhub
  • 如果感觉conda安装慢可以使用pip安装
    • pip安装Jupyterhub:
    $ pip3 install jupyterhub notebook -i https://pypi.douban.com/simple/

检测安装是否成功

jupyterhub -h
npm install -g configurable-http-proxy
configurable-http-proxy -h

生成配置文件

jupyterhub --generate-config -f /etc/jupyterhub/jupyterhub_config.py

修改配置

  • 以下是我的配置
c.JupyterHub.ip = '192.168.2.4'
c.JupyterHub.port = 12443
c.Spawner.ip = '127.0.0.1'
c.PAMAuthenticator.encoding = 'utf8'
c.Authenticator.whitelist = {'root','admin', 'tv', 'aiker'}  #默认不能使用root登录,需要修改配置
c.LocalAuthenticator.create_system_users = True
c.Authenticator.admin_users = {'root', 'admin'}
c.JupyterHub.authenticator_class = 'dummyauthenticator.DummyAuthenticator'
c.JupyterHub.statsd_prefix = 'jupyterhub'
#c.NotebookApp.notebook_dir = '/volume1/study/python/' #jupyter 自定义目录使用
c.Spawner.notebook_dir = '/volume1/study/' #jupyterhub自定义目录
c.JupyterHub.statsd_prefix = 'jupyterhub'
c.JupyterHub.ssl_cert = '/usr/syno/etc/certificate/_archive/xYa1nX/fullchain.pem'
c.JupyterHub.ssl_key = '/usr/syno/etc/certificate/_archive/xYa1nX/privkey.pem'
  • 如果需要使用root登录,需要修改配置
    修改源码 $jupyterhub_安装目录/site-packages/jupyterhub/spawner.py,找到如下代码块做修改:
def get_args(self):  
 """Return the arguments to be passed after self.cmd  

 Doesn't expect shell expansion to happen.  
 """  
 args = []  

 if self.ip:  
 args.append('--ip="%s"' % self.ip)  

 if self.port:  
 args.append('--port=%i' % self.port)  
 elif self.server.port:  
 self.log.warning("Setting port from user.server is deprecated as of JupyterHub 0.7.")  
 args.append('--port=%i' % self.server.port)  

 if self.notebook_dir:  
 notebook_dir = self.format_string(self.notebook_dir)  
 args.append('--notebook-dir="%s"' % notebook_dir)  
 if self.default_url:  
 default_url = self.format_string(self.default_url)  
 args.append('--NotebookApp.default_url="%s"' % default_url)  

 if self.debug:  
 args.append('--debug')  
 if self.disable_user_config:  
 args.append('--disable-user-config')  
 args.append('--allow-root') ##添加此行代码,实现root访问与登陆  
 args.extend(self.args)  
 return args

用户认证

Jupyterhub支持多种认证方式:PAM和LDAP,默认使用的是PAM,即与系统用户层使用同一认证管理,用户名与密码与系统配置的相同。

首先用py3安装一个插件:

$ pip3 install jupyterhub-dummyauthenticator -i https://pypi.douban.com/simple/  

然后,如果遇到生成token问题,在配置文件中修改此配置:

c.JupyterHub.authenticator_class = 'dummyauthenticator.DummyAuthenticator'  

具体细节可以参考这个问题。

Kernel

Kernel可以理解为每个notebook运行的后台环境,Jupyterhub以及Jupyter虽然是基于python的application,但这并不意味着其notebook的功能只能为python环境使用,事实上Jupyter的强大就体现在此处,其可以支持任意环境的notebook,目前主流的数据科学环境基本都支持,R, SAS,SPARK等。

Anaconda Kernel安装

conda环境安装特别简单,首先确保anaconda环境已经集成了Ipython,默认是已经集成了。其次在用户级别下执行一下命令即可:

$ $your_anaconda_install_path/bin/ipython kernel install --user --name anaconda

执行完成后,便会在jupyterhub中看到名为anaconda的kernel,还是再重申下,这个命令需要在每个用户级别下local执行。new后便可使用anaconda环境中的所有python lib。

同时支持Python2和python3

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

查看目前的conda环境中的kernels

jupyter-kernelspec list 
Available kernels: 
python3 /root/.local/share/jupyter/kernels/python3

Jupyterhub安装配置及心得_第1张图片

需要安装python2,python2下安装ipython,成功后执行

/opt/anaconda2/bin/ipython kernel install --user --name python2

查看该环境下的kernels

# jupyter-kernelspec list
Available kernels:
  python2    /root/.local/share/jupyter/kernels/python2
  python3    /root/.local/share/jupyter/kernels/python3

重启jupyterhub即可生效,web效果如下:

R Kernel安装

  • 根据需要决定是否安装,不勉强
    安装R Kernel之前需要安装r-irkernel,这步我们使用anaconda来安装:

    $ conda install -c r r-irkernel

安装完毕之后,运行R命令:

IRkernel::installspec()  

执行完成后,便可在jupyterhub中看到名为R的kernel。

启动

 jupyterhub --config=/etc/jupyterhub/jupyterhub_config.py --no-ssl
  • 后台启动:
 nohup jupyterhub --config=/etc/jupyterhub/jupyterhub_config.py --no-ssl > /dev/null 2>&1 &

注意事项:

  • 如果已经有npm,需要注意npm版本>=6.0
  • 注意jupyterhub-console和python模块prompt版本
  • 一定注意配置环境变量
  • 如果使用反向代理,注意可能引起ipykernel不能正确解析

    安装主题

  • 更改主题只对my server生效,不对control面板起作用
    Jupyterhub安装配置及心得_第2张图片
pip install jupyterthemes
jt -l #列出主题
jt -t monokai -T -N -altp -fs 13 -nfs 13 -tfs 13 -ofs 13 #应用monokai主题

执行完命令后刷新web即可。

为jupyterhub启用代理

目的是为了更方便和安全
这里使用nginx代理

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    resolver 192.168.2.1 114.114.114.114;
    set $backend "https://jp.abcgogo.com:12443";

    server_name jp.abcgogo.com;

    ssl_certificate /usr/syno/etc/certificate/ReverseProxy/faea4d05-2458-4ffc-acfe-e4b48e5a04f9/fullchain.pem;

    ssl_certificate_key /usr/syno/etc/certificate/ReverseProxy/faea4d05-2458-4ffc-acfe-e4b48e5a04f9/privkey.pem;

    add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload" always;

    location / {
        proxy_set_header        Host                $http_host;
        proxy_set_header        X-Real-IP           $remote_addr;
        proxy_set_header        X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto   $scheme;
        proxy_intercept_errors  on;
    # WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_read_timeout 120s;
        proxy_next_upstream error;

        proxy_pass $backend;

    }

}

jupyterhub使用了 websocket, 所以需要这样配置

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

如果nginx代理没有配置websocket,cell里面的内容会不能解析,红框里面显示等待。

Jupyterhub安装配置及心得_第3张图片

可以根据情况参考配置:

nginx官网websocket说明

    location / {
        proxy_pass http://jupyterhub IP:port;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Scheme $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_read_timeout 120s;
        proxy_next_upstream error;
    }

检查重启nginx配置:

nginx -t && nginx -s reload

做好dns解析就可以使用域名访问了。

转载于:https://blog.51cto.com/m51cto/2370679

你可能感兴趣的:(Jupyterhub安装配置及心得)