JupyterHub with k8s

时间:2024-03-02 09:05:00

安装流程主要包括:

  • 准备docker、k8s环境
  • 安装helm
  • 配置pvc持久化
  • 安装jupyterhub

一、安装helm

参考官网:
https://helm.sh/zh/docs/intro/install/
1、下载helm:https://github.com/helm/helm/releases
2、解压即安装:

mv helm /usr/bin/
helm version
version.BuildInfo{Version:"v3.5.1", GitCommit:"32c22239423b3b4ba6706d450bd044baffdcf9e6", GitTreeState:"clean", GoVersion:"go1.15.7"}

二、配置pvc持久化

1、ubuntu18安装nfs

服务器端:sudo apt install nfs-kernel-server
服务器端:sudo apt install portmap(如果安装上一个,可以不用执行此操作,也不用执行步骤2)
客户端:sudo apt install nfs-common
1.2.修改NFS配置文件
1)配置portmap

sudo vim /etc/default/portmap
-I 127.0.0.1

2)配 置/etc/hosts.allow

$ sudo vim /etc/hosts.allow

1.3.配置/etc/exports
NFS挂载目录及权限由/etc/exports文件定义。

$sudo vim /etc/exports
比如我要将将我的home目录中的/home/lin/NFSshare(该目录要给777权限)目录让192.168.66.的IP共享, 则在该文件末尾添加下列语句:
/home/lin/NFSshare  192.168.66.
(rw,sync,no_root_squash)

然后保存退出。
1.4.重启nfs服务

$ sudo /etc/init.d/nfs-kernel-server restart

2、编写yaml文件

:~/jupyterhub# pwd
/root/jupyterhub
:~/jupyterhub# ll
total 24
drwxr-xr-x 2 root root 4096 3月 16 14:56 ./
drwx------ 30 root root 4096 3月 16 14:43 ../
-rw-r--r-- 1 root root 528 3月 16 13:37 config.yaml
-rw-r--r-- 1 root root 162 3月 16 10:41 install.sh
-rw-r--r-- 1 root root 203 3月 15 20:04 jhub-nfs-pvc.yaml
-rw-r--r-- 1 root root 193 3月 16 10:36 jhub-nfs.yaml

:~/jupyterhub# cat jhub-nfs.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 10000Mi
accessModes:
- ReadWriteMany
nfs:
server: 10.10.10.184
path: "/jhub-nfs"

:~/jupyterhub# cat jhub-nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: hub-db-dir
namespace: jhub
spec:
accessModes:
- ReadWriteMany
storageClassName: ""
resources:
requests:
storage: 5000Mi

:~/jupyterhub# kubectl apply -f jhub-nfs.yaml
:~/jupyterhub# kubectl apply -f jhub-nfs-pvc.yaml
:~/jupyterhub# kubectl get pv,pvc -n jhub
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/nfs-pv 10000Mi RWX Retain Bound jhub/hub-db-dir 4h22m
persistentvolume/standard 2Gi RWO Retain Available local-storage 18h

NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/hub-db-dir Bound nfs-pv 10000Mi RWX 4h21m

三、安装jupyterhub

1、准备一个配置文件可参考链接https://github.com/jupyterhub/zero-to-jupyterhub-k8s/blob/master/jupyterhub/values.yaml
准备一个config.yaml的配置文件,用于给用户使用的Helm chart提供values,Helm根据这些values来配置k8s集群的资源。
生成一个代表32个字节的随机十六进制字符串,用作安全令牌。

openssl rand -hex 32

2、创建一个config.yaml
把1生成的random hex添加进去

proxy:
secretToken: "<RANDOM_HEX>"

config.yaml具体配置如下

:~/jupyterhub# cat config.yaml
proxy:
secretToken: "d86bd32afa35a0d26e6af6a0121334fd6f9bfb3ff889ce5cb17bd48750322bdc"
service:
type: NodePort
hub:
uid: 1000
fsGid: 1000
db:
type: sqlite-memory
extraConfig:
myConfig: |
config = \'/etc/jupyter/jupyter_notebook_config.py\'
c.Spawner.cmd = [\'jupyter-labhub\']
singleuser:
uid: 1000
fsGid: 100
defaultUrl: "/lab"
storage:
type: "static"
static:
pvcName: "hub-db-dir"
capacity: 1Gi
extraEnv:
CHOWN_HOME: \'yes\'
memory:
limit:
guarantee: 1G

3、使用helm安装JupyterHub

helm repo add jupyterhub https://jupyterhub.github.io/helm-chart/
helm repo update

会产生如下的输出

Hang tight while we grab the latest from your chart repositories...
...Skip local chart repository
...Successfully got an update from the "stable" chart repository
...Successfully got an update from the "jupyterhub" chart repository
Update Complete. ⎈ Happy Helming!⎈

编写执行脚本

:~/jupyterhub# cat install.sh
helm upgrade --cleanup-on-fail
--install jhub jupyterhub/jupyterhub
--namespace jhub
--create-namespace
--version=0.10.6
--values config.yaml

注意:
RELEASE:Helm release namespace
NAMESPACE:k8s namespace
--version:the version of Helm chart。JupyterHub的版本和Heml chart的版本有对应关系

执行启动脚本

:~/jupyterhub# bash install.sh
在第二步执行过程中,可以利用命令来查看正在创建的pods

四、登录验证jupyterhub

kubectl get pod --namespace jhub
等待JupyterHub proxy pod进入运行状态

NAME READY STATUS RESTARTS AGE
hub-5d4ffd57cf-k68z8 1/1 Running 0 37s
proxy-7cb9bc4cc-9bdlp 1/1 Running 0 37s
找到我们用来进入JupyterHub的IP地址。运行下面的命令,直到proxy-public的EXTERNAL-IP可以获取到。

kubectl get service --namespace jhub
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hub ClusterIP 10.51.243.14 8081/TCP 1m
proxy-api ClusterIP 10.51.247.198 8001/TCP 1m
proxy-public LoadBalancer 10.51.248.230 104.196.41.97 80:31916/TCP 1m
如果proxy-public的IP太长窗口放不开,可以用下面这个命令:

kubectl describe service proxy-public --namespace jhub
使用JupyterHub。从proxy-public的IP访问JupyterHub,

jupyterHub支持多用户,所以您可以随意输入用户名和密码登录即可

其中遇到问题:
问题一:
root@lb-3:~/jupyterhub# kubectl logs -n jhub hub-55684c976f-vkl8f
No config at /etc/jupyterhub/config/values.yaml
Loading /etc/jupyterhub/secret/values.yaml
Loading extra config: myConfig
[I 2021-03-16 03:49:47.672 JupyterHub app:2332] Running JupyterHub version 1.2.2
[I 2021-03-16 03:49:47.672 JupyterHub app:2362] Using Authenticator: dummyauthenticator.dummyauthenticator.DummyAuthenticator
[I 2021-03-16 03:49:47.672 JupyterHub app:2362] Using Spawner: kubespawner.spawner.KubeSpawner-0.14.1
[I 2021-03-16 03:49:47.673 JupyterHub app:2362] Using Proxy: jupyterhub.proxy.ConfigurableHTTPProxy-1.2.2
[I 2021-03-16 03:49:47.673 JupyterHub app:1457] Writing cookie_secret to /srv/jupyterhub/jupyterhub_cookie_secret
[I 2021-03-16 03:49:47.687 alembic.runtime.migration migration:155] Context impl SQLiteImpl.
[I 2021-03-16 03:49:47.687 alembic.runtime.migration migration:158] Will assume non-transactional DDL.
[I 2021-03-16 03:49:47.690 alembic.runtime.migration migration:517] Running stamp_revision -> 4dc2d5a8c53c
[W 2021-03-16 03:49:47.698 JupyterHub app:1687] No admin users, admin interface will be unavailable.
[W 2021-03-16 03:49:47.698 JupyterHub app:1688] Add any administrative users to c.Authenticator.admin_users in config.
[I 2021-03-16 03:49:47.698 JupyterHub app:1717] Not using allowed_users. Any authenticated user will be allowed.
[I 2021-03-16 03:49:47.732 JupyterHub app:2399] Initialized 0 spawners in 0.001 seconds
[I 2021-03-16 03:49:47.733 JupyterHub app:2611] Not starting proxy
[W 2021-03-16 03:49:50.736 JupyterHub proxy:807] api_request to the proxy failed with status code 599, retrying...
[W 2021-03-16 03:49:53.916 JupyterHub proxy:807] api_request to the proxy failed with status code 599, retrying...
[W 2021-03-16 03:49:57.070 JupyterHub proxy:807] api_request to the proxy failed with status code 599, retrying...
[W 2021-03-16 03:50:00.711 JupyterHub proxy:807] api_request to the proxy failed with status code 599, retrying...
[W 2021-03-16 03:50:05.288 JupyterHub proxy:807] api_request to the proxy failed with status code 599, retrying...
[W 2021-03-16 03:50:09.367 JupyterHub proxy:807] api_request to the proxy failed with status code 599, retrying...
[W 2021-03-16 03:50:17.372 JupyterHub proxy:807] api_request to the proxy failed with status code 599, retrying...
[W 2021-03-16 03:50:21.130 JupyterHub proxy:807] api_request to the proxy failed with status code 599, retrying...
[E 2021-03-16 03:50:21.130 JupyterHub app:2844]
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/jupyterhub/app.py", line 2842, in launch_instance_async
await self.start()
File "/usr/local/lib/python3.8/dist-packages/jupyterhub/app.py", line 2615, in start
await self.proxy.get_all_routes()
File "/usr/local/lib/python3.8/dist-packages/jupyterhub/proxy.py", line 854, in get_all_routes
resp = await self.api_request(\'\', client=client)
File "/usr/local/lib/python3.8/dist-packages/jupyterhub/proxy.py", line 818, in api_request
result = await exponential_backoff(
File "/usr/local/lib/python3.8/dist-packages/jupyterhub/utils.py", line 179, in exponential_backoff
raise TimeoutError(fail_message)
TimeoutError: Repeated api_request to proxy path "" failed.
解决:
通过pod定位到calico,calico报错如下

重启calico