You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需Minikube,如何在Kubernetes中访问Jupyter Notebook本地链接?

嘿,我完全懂你的困扰——不用Minikube也绝对能搞定这个问题!下面给你几个实用的方案,都是日常调试或者生产环境里常用的,你按需选:

方案1:端口转发(最直接的临时调试方案)

这是K8s里最常用的本地访问Pod服务的方式,不需要额外配置:

  • 先找到你的Jupyter Pod名称:
    kubectl get pods
    
  • 执行端口转发命令,把Pod内的Jupyter端口(默认是8888)映射到本地的8888端口:
    kubectl port-forward <你的Jupyter Pod名称> 8888:8888
    
  • 打开本地浏览器访问 http://localhost:8888,然后输入Jupyter启动时生成的token就行。如果找不到token,用下面的命令看Pod日志:
    kubectl logs <你的Jupyter Pod名称>
    
    日志里会有类似 http://localhost:8888/?token=xxxxxx 的行,把后面的token复制过来就行。

方案2:NodePort类型Service(适合集群节点可直接访问的场景)

如果你的K8s集群节点IP能被本地电脑访问到(比如私有云或本地物理集群),可以用NodePort把Jupyter服务暴露出来:

  • 创建一个名为jupyter-service.yaml的配置文件,内容如下(注意selector里的app: jupyter要和你的Jupyter Pod的label对应上):
    apiVersion: v1
    kind: Service
    metadata:
      name: jupyter-service
    spec:
      type: NodePort
      selector:
        app: jupyter
      ports:
        - port: 8888
          targetPort: 8888
          nodePort: 30007  # 可选,指定30000-32767之间的端口,不指定的话K8s会自动分配
    
  • 应用这个配置:
    kubectl apply -f jupyter-service.yaml
    
  • 查看分配的NodePort(如果没指定的话):
    kubectl get service jupyter-service
    
  • 最后用集群节点的IP加NodePort访问:http://<节点IP>:<NodePort>,同样用token登录。

方案3:Ingress(适合有Ingress Controller的成熟集群)

如果你的K8s集群已经部署了Ingress Controller(比如NGINX Ingress),可以用Ingress来配置域名访问,更优雅:

  • 先确保你已经有一个指向Jupyter Pod的ClusterIP Service(可以用方案2的配置,把type: NodePort改成type: ClusterIP)。
  • 创建jupyter-ingress.yaml配置文件:
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: jupyter-ingress
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /
    spec:
      rules:
      - host: jupyter.local  # 自定义一个本地域名,后面需要在hosts里映射
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: jupyter-service
                port:
                  number: 8888
    
  • 应用配置:
    kubectl apply -f jupyter-ingress.yaml
    
  • 找到Ingress Controller的服务IP,然后在本地电脑的hosts文件里添加一行:<Ingress Controller IP> jupyter.local。
  • 最后用浏览器访问http://jupyter.local,输入token即可。

方案4:修改Jupyter监听地址(解决容器内仅绑定localhost的问题)

如果你的Jupyter在容器里只绑定了localhost,导致外部访问不了,可以进入Pod修改配置:

  • 先进入Pod的终端:
    kubectl exec -it <你的Jupyter Pod名称> -- /bin/bash
    
  • 重启Jupyter的时候指定监听所有接口:
    jupyter notebook --ip=0.0.0.0 --allow-root --no-browser
    
    (加上--allow-root是如果容器用root用户启动的话需要,--no-browser是阻止容器内启动浏览器)
  • 之后再用方案1的端口转发或者方案2的Service就能本地访问了。

小提醒

  • 如果是云厂商的K8s集群,用NodePort或Ingress的时候要注意开放对应的安全组规则,不然本地访问会被拦截。
  • 生产环境里建议给Jupyter加认证或者限制访问范围,避免安全风险。

内容的提问来源于stack exchange,提问作者Jonathan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:40:45