现在jupyter已通过k8s安装完成,并通过nodeport暴露出来。
如果不能直接访问这个nodeport(像我在的公司)或是希望能组织好jupyter实例,
那应该如何调通呢?
这里包括两个技术点:
一,jupyter本身需要允许指定ip的访问。
红色部分,继承了docker镜像的cmd,并新增了--NotebookApp.allow_origin='*'参数,不然,nginx跳过去,jypyter不接受。
apiVersion: apps/v1
kind: Deployment
metadata:
name: ai-jupyter
spec:
replicas: 1
selector:
matchLabels:
name: ai-jupyter
template:
metadata:
labels:
name: ai-jupyter
spec:
imagePullSecrets:
- name: xxxx
nodeSelector:
accelerator: nvidia-tesla-k80
containers:
- name: sis-ai-jupyter
image: harbor.xxxx.com.cn/3rd_part/tensorflow:1.14.0-gpu-py3-jupyter
imagePullPolicy: IfNotPresent
command: ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=/tf --ip 0.0.0.0 --no-browser --allow-root --NotebookApp.allow_origin='*'"]
resources:
limits:
nvidia.com/gpu: 2
---
apiVersion: v1
kind: Service
metadata:
name: ai-jupyter
spec:
type: NodePort
ports:
- port: 8888
targetPort: 8888
nodePort: 30302
selector:
name: ai-jupyter
二,nginx需要有websocket的升级处理。
红色部分,对kernels和terminals两个目录的连接,作了ws升级处理。
upstream ai_jupyter {
ip_hash;
server 1.2.3.4:30302;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_set_header Host $host;
proxy_set_header X-FORWARDED-FOR $remote_addr;
proxy_pass http://ai_jupyter;
}
location ~ /api/kernels/ {
proxy_pass http://ai_jupyter;
proxy_set_header Host $host;
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
location ~ /terminals/ {
proxy_pass http://ai_jupyter;
proxy_set_header Host $host;
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
}