Jupyterhub安装配置及心得

Jupyter简介

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

Jupyterhub简介

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

安装Anaconda

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

下载安装

  • 下载
    • 这里我使用清华的镜像站点下载,速度很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,因此若是在安装的过程当中手太快,一键到底了的话,能够经过手动添加的方式进行设置。git

将anaconda3加入环境变量

vim ~/.bashrcgithub

在bashrc文件的最后添加web

# 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 <<<

保存并退出
而后shell

source ~/.bashrc

检测Anaconda是否安装成功:npm

conda list

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

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安装配置及心得

须要安装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效果以下:

Jupyterhub安装配置及心得

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安装配置及心得Jupyterhub安装配置及心得
pip install jupyterthemes
jt -l #列出主题
jt -t monokai -T -N -altp -fs 13 -nfs 13 -tfs 13 -ofs 13 #应用monokai主题

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

Jupyterhub安装配置及心得

为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安装配置及心得

能够根据状况参考配置:

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解析就可使用域名访问了。
Jupyterhub安装配置及心得

相关文章
相关标签/搜索