

多用户 JupyterHub 部署及 GitHub 用户认证
source link: https://segmentfault.com/a/1190000039803440
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

多用户 JupyterHub 部署及 GitHub 用户认证
最近工作上,领导希望将分析师在各自电脑上的分析环境和任务脚本进行统一管理,要求我给出一个解决方案。而分析师使用的工具是 Jupyter,我自然而然的想到了官方的 JupyterHub。经过调研,发现 JupyterHub 完全能满足需求,而且对分析师来说学习成本几乎为零。于是我便花了一天的时间部署了一个多用户 JupyterHub 测试环境,并配置好 GitHub 认证。下面和大家分享一下我的部署过程。
01/ 环境
根据 JupyterHub 安装要求,我准备好了如下环境:
- 系统环境:CentOS 7.6
语言环境:Python 3.6,NodeJS 14.16
02/ 安装 JupyterHub 及配置 Nginx 反向代理
JupyterHub 安装非常简单,可以直接使用 pip 进行安装。按如下命令即可进行安装:
npm install -g configurable-http-proxy
python3 -m pip install jupyterhub
安装完成后,创建一个 JupyterHub 运行目录,进入该目录,先生成配置文件,然后启动:
jupyterhub --generate-config
jupyterhub
启动后,在浏览器输入:
http://127.0.0.1:8000
就能访问 JupyterHub 了,此时会自动跳转到登录页。
JupyterHub 支持多种账号身份认证,你甚至可以自己实现一个认证器。我使用的是 GitHub 认证器进行账号认证,后面我再讲如何对接 GitHub 认证对接。
为了让分析师更好的使用 JupyterHub,我申请了一个域名,使用 Nginx 反向代理到 JupyterHub 到 8000 服务上。我使用 Linux 的 Systemd 来启动 JupyterHub 的守护进程,创建一个名为 jupyterhub.service 的 Unit 配置,如下:
[Unit]
Description=The JupyterHub Service
After=syslog.target network.target
[Service]
User=root
Restart=always
WorkingDirectory=/path/to/jupyterhub
PrivateTmp=yes
PrivateDevices=yes
ExecStart=/usr/bin/python3 -m jupyterhub -f jupyterhub_config.py --upgrade-db
[Install]
WantedBy=multi-user.target
配置好 JupyterHub 的 Systemd 服务后,就可以使用如下命令来管理 JupyterHub 服务了。
systemctl start jupyterhub.service
systemctl stop jupyterhub.service
接着就可以配置 Nginx 反向代理了:
# top-level http config for websocket headers
# If Upgrade is defined, Connection = upgrade
# If Upgrade is empty, Connection = close
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# HTTP server to redirect all 80 traffic to SSL/HTTPS
server {
listen 80;
server_name YOUR.DOMAIN.COM;
# Tell all requests to port 80 to be 302 redirected to HTTPS
return 302 https://$host$request_uri;
}
# HTTPS server to handle JupyterHub
server {
listen 443;
ssl on;
server_name YOUR.DOMAIN.COM;
ssl_certificate /etc/letsencrypt/live/YOUR.DOMAIN.COM/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/YOUR.DOMAIN.COM/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security max-age=15768000;
# Managing literal requests to the JupyterHub front end
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# websocket headers
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Scheme $scheme;
proxy_buffering off;
}
# Managing requests to verify letsencrypt host
location ~ /.well-known {
allow all;
}
}
03/ 配置 GitHub 账号认证
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK