jupyterhub的淺淺使用-重點在解決無法登錄
- jupyterhub的淺淺使用-重點在解決無法登錄
- 1、jupyterhub是什么
- 2、創建Dockerfile
- 3、啟動容器
- 3.1、生成配置文件`jupyterhub --generate-config`
- 3.2、運行容器
- 3.3、進入容器配置用戶密碼
- 3.4、訪問`127.0.0.1:8000`并登錄
- 4、后臺創建的用戶無法訪問hub問題
- 5、api使用
- 5.1 在web頁面生成token,并保存
- 5.2 使用token調用api
參考:https://mp.weixin.qq.com/s/jxTqlk0NXFcn-RPonQfsYg
jupyterhub的淺淺使用-重點在解決無法登錄
無意中需要搭建一下jupyterhub,這個之前也沒聽說過,之前只聽說過jupyter notebook,當然也沒咋用過。
1、jupyterhub是什么
- 專為公司、教室和研究實驗室設計的筆記本電腦的多用戶
jupyter notebook
版本 - 我的理解就是:公用一套python環境,支持多用戶
jupyter notebook
。類似和一臺服務器虛擬化成眾多虛擬機一樣的概念。
2、創建Dockerfile
FROM quay.io/jupyterhub/jupyterhub:latest
RUN apt update && apt install vim net-tools lsof -y
RUN pip install jupyterhub-idle-culler jupyter_server jupyterlab
構建 docker build -t jupyterhub:1.0.0 .
3、啟動容器
3.1、生成配置文件jupyterhub --generate-config
然后終端執行下面命令創建jupyterhub_config.py
配置文件:
jupyterhub --generate-config -f /etc/jupyterhub/jupyterhub_config.py
執行完上述操作,在/etc/jupyterhub
路徑下會生成一個名為jupyterhub_config.py
的文件,使用vim或者nano等文本編輯工具打開jupyterhub_config.py
文件
追加配置
import sys
c.Authenticator.allowed_users = {'root','admin'}
c.Authenticator.admin_users = {'root','admin'} # 管理員用戶
# 管理員是否有權在各自計算機上以其他用戶身份登錄,以進行調試,此選項通常用于 JupyterHub 的托管部署,以避免在啟動服務之前手動創建所有用戶
c.JupyterHub.admin_access = True
c.PAMAuthenticator.open_sessions = False # 解決多用戶同時登錄問題。
c.Spawner.args = ['--allow-root'] # 允許root用戶使用
c.LocalAuthenticator.create_system_users = True # 允許創建其他用戶
c.Spawner.notebook_dir = '~' # 設置工作目錄
c.Spawner.default_url = '/lab'c.JupyterHub.extra_log_file = '/etc/jupyterhub/jupyterhub.log' # 指定額外的日志
c.JupyterHub.pid_file='/etc/jupyterhub/jupyterhub.pid' # 指定pid文件位置
c.JupyterHub.db_url='/etc/jupyterhub/jupyterhub.sqlite' # 指定數據庫文件位置
c.JupyterHub.cookie_secret_file='/etc/jupyterhub/jupyterhub_cookie_secret' # 指定cookie_secret文件位置
c.ConfigurableHTTPProxy.pid_file='/etc/jupyterhub/jupyterhub-proxy.pid' # 設置proxy.pid文件位置# 設置用戶一小時內無使用則關閉jupyterlab服務
c.JupyterHub.services = [{'name': 'idle-culler','command': [sys.executable, '-m', 'jupyterhub_idle_culler', '--timeout=1800'],}
]c.JupyterHub.load_roles = [{"name": "list-and-cull", # name the role"services": ["idle-culler", # assign the service to this role],"scopes": [# declare what permissions the service should have"list:users", # list users"read:users:activity", # read user last-activity"admin:servers", # start/stop servers],}
]
3.2、運行容器
docker run -itd --hostname=jupyterhub \
-v /etc/jupyterhub:/etc/jupyterhub \
--name=jupyterhub-new -p 8000:8000 jupyterhub:1.0.0 \
jupyterhub --ip 0.0.0.0 -f /etc/jupyterhub/jupyterhub_config.py
3.3、進入容器配置用戶密碼
默認hub使用的是PAM的認證,需要linux用戶的賬號和密碼。因為容器里面沒有用戶的密碼,所以需要創建
- 創建用戶
useradd -m user_name
- 創建密碼
paaawd user_name
hub支持的認證如下:
3.4、訪問127.0.0.1:8000
并登錄
使用admin或者root登錄成功【注意要操作上一步驟的給root設置密碼,創建admin用戶和密碼】
4、后臺創建的用戶無法訪問hub問題
在linux后臺使用
- 創建用戶
useradd -m user_name
- 創建密碼
paaawd user_name
后在web上無法登錄。
解決:需要在hub的web頁面上創建用戶,然后在容器中使用paaawd user_name
配置密碼,才能在web頁面登錄。猜測這個是DB的原因。
5、api使用
API文檔: https://jupyterhub.readthedocs.io/en/stable/reference/rest-api.html
5.1 在web頁面生成token,并保存
5.2 使用token調用api
注意換成你的token。
- 調用
curl -X GET -H "Authorization: token 8a14793766e74b8ab48669f87d4b31b7" http://127.0.0.1:8000/hub/api/user
- 返回
{"pending": null,"server": "/user/tom1/","admin": false,"groups": [],"roles": ["user"],"last_activity": "2025-05-28T09:52:30.195982Z","name": "tom1","created": "2025-05-28T08:33:38.295913Z","kind": "user","servers": {"": {"name": "","full_name": "tom1/","last_activity": "2025-05-28T09:50:54.605630Z","started": "2025-05-28T08:35:56.168903Z","pending": null,"ready": true,"stopped": false,"url": "/user/tom1/","user_options": {},"progress_url": "/hub/api/users/tom1/server/progress","full_url": null,"full_progress_url": null}},"token_id": "a8","session_id": null,"scopes": ["access:servers!user=tom1","delete:servers!user=tom1","read:servers!user=tom1","read:shares!user=tom1","read:tokens!user=tom1","read:users!user=tom1","read:users:activity!user=tom1","read:users:groups!user=tom1","read:users:name!user=tom1","read:users:shares!user=tom1","servers!user=tom1","tokens!user=tom1","users:activity!user=tom1","users:shares!user=tom1"]
}