如何在Linux服务器上安装和配置JupyterHub?

解读

国内生产环境普遍采用 CentOS 7/8、Ubuntu 20.04/22.04 或国产麒麟、统信 UOS 等服务器系统,面试官想确认候选人是否具备“从零到上线”交付 JupyterHub 的能力:

  1. 能否在受限内网/国产 CPU 下完成依赖安装;
  2. 是否理解多用户隔离、系统安全与性能调优;
  3. 能否把 Hub 接入公司现有身份体系(LDAP/AD、钉钉、飞书、OIDC);
  4. 是否掌握容器化与 K8s 交付方式,以应对弹性扩容与 DevOps 流程。
    回答必须体现“可维护、可审计、可扩展”的运维思维,而非简单 pip install。

知识点

  1. 操作系统层:Systemd 服务、防火墙、SELinux、内核参数调优。
  2. Python 环境:Miniconda/官方 RPM 源、venv、pip 与 conda 混合依赖冲突处理。
  3. JupyterHub 架构:Hub 进程、ConfigurableHTTPProxy、Spawner、Authenticator 插件体系。
  4. 身份认证:PAM、LDAP、GitHub、GenericOAuthenticator、钉钉自建应用 OAuth2。
  5. Spawner 选型:LocalProcessSpawner、SystemdSpawner、DockerSpawner、KubeSpawner。
  6. 隔离与资源:Linux cgroup、systemd slice、Docker --memory/--cpus、Kube ResourceQuota。
  7. 网络与接入:Nginx 反向代理、HTTPS 证书(国密/国际)、WebSocket 支持、子域 wildcard。
  8. 持久化:本地 home、NFS v3/v4、CephFS、JuiceFS、PVC。
  9. 审计与合规:jupyter-telemetry、结构化日志、ELK/ClickHouse、等保 2.0 日志留存。
  10. 高可用:Hub 多副本 + 共享数据库(PostgreSQL/MySQL)、Redis 会话、Kubernetes RollingUpdate。
  11. 国内镜像加速:清华 tuna、中科大、华为云、阿里云 PyPI 镜像,Nodejs 镜像源。
  12. 国产化适配:aarch64、龙芯、鲲鹏 CPU 编译 nodejs、configurable-http-proxy。

答案

以下给出一条在 CentOS 7 内网机“从零到生产”的最小可扩展路径,候选人可据此展开,既展示细节,又保留扩展空间。

步骤 1:操作系统基础
a. 选用 CentOS 7.9 最小化安装,打开 80、443、8000、8081 端口,关闭 SELinux 或设为 permissive(setenforce 0)。
b. 挂载 /home 独立盘(xfs),开启 quota 选项,为后续用户磁盘限额做准备。
c. 创建 jupyter 系统用户,UID/GID 统一规划,避免与 LDAP 冲突。

步骤 2:Python 环境
a. 下载 Miniconda3-py39-Linux-x86_64.sh,安装至 /opt/miniconda,所有后续操作使用 conda 环境而非系统 Python。
b. 配置国内镜像:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
c. 新建环境:
conda create -n jhub python=3.9 nodejs=16 -y
conda activate jhub

步骤 3:安装核心组件
pip install jupyterhub jupyterlab notebook
npm install -g configurable-http-proxy
验证:configurable-http-proxy --version 能输出版本号即 node 环境正常。

步骤 4:最小化配置文件 /etc/jupyterhub/jupyterhub_config.py
c = get_config()
c.JupyterHub.bind_url = 'https://0.0.0.0:8000'
c.JupyterHub.hub_ip = '0.0.0.0'
c.JupyterHub.hub_connect_ip = '10.0.0.10' # 内网真实 IP
c.JupyterHub.port = 8000
c.JupyterHub.cookie_secret_file = '/etc/jupyterhub/cookie_secret'
c.JupyterHub.db_url = 'sqlite:///etc/jupyterhub/jupyterhub.sqlite'
c.Authenticator.admin_users = {'admin'}
c.Spawner.default_url = '/lab'
c.Spawner.notebook_dir = '~/notebook'
c.LocalAuthenticator.create_system_users = True # 自动创建 OS 用户
c.SystemdSpawner.unit_name_template = 'jupyter-{USERNAME}'
c.SystemdSpawner.user_workingdir = '/home/{USERNAME}/notebook'
c.SystemdSpawner.isolate_tmp = True
c.SystemdSpawner.isolate_devices = True
c.SystemdSpawner.disable_user_sudo = True
c.SystemdSpawner.cpu_limit = 2
c.SystemdSpawner.mem_limit = '4G'
(如用 DockerSpawner 则替换为 c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner',并配置 image、network、volume。)

步骤 5:Systemd 托管
创建 /etc/systemd/system/jupyterhub.service
[Unit]
Description=JupyterHub
After=network.target

[Service]
User=root
Environment="PATH=/opt/miniconda/envs/jhub/bin:/usr/local/bin:/usr/bin"
ExecStart=/opt/miniconda/envs/jhub/bin/jupyterhub -f /etc/jupyterhub/jupyterhub_config.py
WorkingDirectory=/etc/jupyterhub
Restart=always

[Install]
WantedBy=multi-user.target
systemctl daemon-reload && systemctl enable --now jupyterhub

步骤 6:HTTPS 与反向代理
a. 国密或国际证书放 /etc/ssl/jhub/,在 jupyterhub_config.py 追加:
c.JupyterHub.ssl_key = '/etc/ssl/jhub/server.key'
c.JupyterHub.ssl_cert = '/etc/ssl/jhub/server.crt'
b. 若前端已有 Nginx,可用 stream 模块或 443 反代,注意 proxy_set_header Upgrade 与 Connection "upgrade",以支持 WebSocket。

步骤 7:身份集成(以 LDAP 为例)
pip install jupyterhub-ldapauthenticator
config 追加:
c.JupyterHub.authenticator_class = 'ldapauthenticator.LDAPAuthenticator'
c.LDAPAuthenticator.server_address = 'ldap.corp.local'
c.LDAPAuthenticator.bind_dn_template = 'uid={username},ou=users,dc=corp,dc=local'
c.LDAPAuthenticator.use_ssl = True
c.LDAPAuthenticator.server_port = 636
c.LDAPAuthenticator.lookup_dn = True
c.LDAPAuthenticator.user_search_base = 'ou=users,dc=corp,dc=local'
c.LDAPAuthenticator.user_attribute = 'uid'
c.LDAPAuthenticator.allowed_groups = ['cn=data,ou=groups,dc=corp,dc=local']

步骤 8:资源与审计
a. 启用 cgroup v1 memory,cpu 控制器,SystemdSpawner 自动写 slice。
b. 安装 jupyter-telemetry 并配置 Kafka 或 FileSink,日志格式 JSON,包含 username、server、CPU、内存峰值,满足等保 2.0 180 天留存。
c. 通过 logrotate 切割 /var/log/jupyterhub/*.log,防止单文件过大。

步骤 9:升级与回滚
conda list --export > /etc/jupyterhub/conda-lock.txt
升级前先在测试环境 conda install --file 验证兼容性;生产使用 Blue-Green,Systemd 的 ExecReload 支持零中断重启 Hub 进程,用户 Notebook 不重启。

步骤 10:国产化适配(加分项)
在 aarch64 服务器上,需自行编译 nodejs(或从华为云镜像获取 aarch64 二进制),再 npm install -g configurable-http-proxy;鲲鹏 CPU 关闭 jemalloc 可避免内存缺页异常。

拓展思考

  1. 多机房高可用:Hub 无状态化,把 db_url 指向 PostgreSQL 流复制集群,Redis 存 session,K8s Ingress 做地域负载均衡;当单机房故障,用户重新登录即可恢复会话。
  2. 弹性资源:使用 KubeSpawner 结合 Cluster-Autoscaler,根据排队用户数自动弹出 Pod,空闲 10 分钟后回收,节省 30% 云成本。
  3. 数据合规:Notebook 产出落地到对象存储(OSS、Ceph RGW),开启 WORM 与多版本,配合数据湖权限(Ranger/IAM),实现“计算与存储分离、权限统一”。
  4. 模型即服务:在 singleuser 镜像里预装 jupyter-server-proxy,把 Streamlit、Gradio、TensorBoard 作为子服务暴露,统一入口,研发无需再申请端口。
  5. 成本可视化:基于 Prometheus + Grafana,采集 Notebook Pod 的 CPU 秒、内存秒、GPU 秒,按部门标签汇总,每月出具“算力账单”,倒逼用户优化代码。