在使用k8s部署前,首先需要安装k8s集群环境,这里使用minikube安装单节点的集群环境。
定义jupyterhub部署文件
定义jupyterhub.yaml(查看原始文件)
apiVersion: v1
kind: Pod
metadata:
name: jupyterhub
labels:
name: jupyterhub-pod
context: docker-k8s-lab
app: jupyterhub
spec:
containers:
- name: jupyterhub
image: omegang/jupyterhub2
ports:
- containerPort: 8000
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
command: ["/bin/sh","-c"]
args: ["ls; cat jupyterhub_config.py; export HUB_CONNECT_IP=$(hostname -i); echo $HUB_CONNECT_IP; jupyterhub --no-ssl"]
---
apiVersion: v1
kind: Service
metadata:
name: jupyterhub-service
labels:
name: jupyterhub-pod
context: docker-k8s-lab
app: jupyterhub
spec:
type: NodePort
ports:
- name: hubhttp
port: 8000
targetPort: 8000
nodePort: 30009
- name: httpapi
port: 8081
targetPort: 8081
nodePort: 30008
- name: notebookhttp
port: 8888
targetPort: 8888
nodePort: 30010
selector:
app: jupyterhub
启动服务
启动Pod及Service
kubectl create -f jupyterhub.yaml
第一次部署时间较长,因为需要对镜像进行拉取,因此,在使用命令
kubectl get pods
查看得知jupyterhub的相关pod为running状态时,整个服务才准备OK。
在浏览器访问192.168.73.139:30009
,这里的IP以本地部署的主机IP为准,即可进入jupyterhub的登录界面。
使用本地主机的用户,比如root或其他linux用户登录,此时可能会爆错误。
因为在omega提供的原始镜像omegang/jupyterhub2中,在通过kubespawner调用jupyter notebook server镜像时,需要分配pvc,所以,需要对镜像做稍微修改。
修改错误
此时可以先删除创建的Pod与Service
kubectl delete -f jupyterhub.yaml
对omegang/jupyterhub2镜像进行commit修改,首先需要启动容器
docker run -it omegang/jupyterhub2 /bin/bash
进入容器内,在当前目录,通过vi修改jupyterhub_config.yaml,修改
c.KubeSpawner.user_storage_pvc_ensure = False
将容器提交为镜像(tag为自定义的)
docker commit -m "modify jupyterhub_config.py" 容器ID omegang/jupyterhub2:1.1
修改jupyterhub.yaml
containers:
- name: jupyterhub
image: omegang/jupyterhub2:1.1
此时,可以重新创建Pod及Service
kubectl create -f jupyterhub.yaml
此时,访问登录即可成功。