【问题标题】:stateful jupyter notebook in kubernetesKubernetes 中有状态的 jupyter 笔记本
【发布时间】:2020-05-11 08:14:20
【问题描述】:

尝试在 Kubernetes 中部署有状态的 jupyter notebook,但无法保存在 notebook 中编写的代码,每当 notebook pod 关闭时,所有代码都会被删除。我尝试使用持久音量,但无法达到预期效果。

更新

将挂载路径更改为“/home/jovyan”,因为 jyputer 将 ipynb 保存在此位置。但是现在在部署 Pod 时出现 PermissionError: [Errno 13] Permission denied: '/home/jovyan/.local'。

kind: Ingress
metadata:
  name: jupyter-ingress
spec:
  backend:
    serviceName: jupyter-notebook-service
    servicePort: 8888

---

kind: Service
apiVersion: v1
metadata:
  name: jupyter-notebook-service
spec:  
  clusterIP: None
  selector:
    app: jupyter-notebook
  ports:
  - protocol: TCP    
    port: 8888
    targetPort: 8888

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: jupyter-notebook
  labels:
    app: jupyter-notebook
spec:
  replicas: 1
  serviceName: "jupyter-notebook-service"
  selector:
    matchLabels:
      app: jupyter-notebook
  template:
    metadata:
      labels:
        app: jupyter-notebook
    spec:
      serviceAccountName: dsx-spark
      volumes:
        - name: jupyter-pv-storage
          persistentVolumeClaim:
            claimName: jupyter-pv-claim 
      containers:
      - name: minimal-notebook
        image: jupyter/pyspark-notebook:latest
        ports:
        - containerPort: 8888
        command: ["start-notebook.sh"]
        args: ["--NotebookApp.token=''"]
        volumeMounts:
          - mountPath: "/home/jovyan"
            name: jupyter-pv-storage

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jupyter-pv-claim
spec:
  storageClassName: jupyter-pv-storage
  accessModes:
  - ReadWriteOnce 
  resources:
    requests:
      storage: 1Gi

---
---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: jupyter-pv-volume
  labels:
    type: local
spec:
  storageClassName: jupyter-pv-storage
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce 
  hostPath:
    path: "/home/jovyan"

---

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: jupyternotebook-pv-storage
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
  labels:
provisioner: kubernetes.io/vsphere-volume
parameters:
    diskformat: zeroedthick
    ```

【问题讨论】:

  • 有什么理由不使用 Hub 来代替?
  • 主要专注于在笔记本中编写 spark 代码,这就是为什么使用图像:jupyter/pyspark-notebook:latest
  • @NitinAshok 我在你的 StatefulSet 中看不到 volumeClaimTemplates。您能否详细说明您在持久卷方面尝试了什么?另外,请查看此doc 并告诉我您是否已这样做。
  • @OhHiMark 我对挂载路径进行了一些更改,但现在在部署 Pod 时出现权限被拒绝错误我更新了代码。请看一看。谢谢
  • @NitinAshok 您需要将该文件夹的权限设置为 777,以便以特定用户身份运行的容器能够对其进行写入。

标签: kubernetes jupyter-notebook


【解决方案1】:

带有 jupyter notebook 的 Pod 是非 root 用户,所以无法挂载,所以我们是 在创建 Pod 之前使用 initContainers 更改 Persistent Volume Claim 的用户/权限。

kind: StatefulSet
metadata:
  name: jupyter-notebook
  labels:
    app: jupyter-notebook
spec:
  replicas: 1
  serviceName: "jupyter-notebook-service"
  selector:
    matchLabels:
      app: jupyter-notebook
  template:
    metadata:
      labels:
        app: jupyter-notebook
    spec:

      serviceAccountName: dsx-spark
      volumes:
        - name: ci-jupyter-storage-def
          persistentVolumeClaim:
            claimName: my-jupyter-pv-claim 
      containers:
      - name: minimal-notebook
        image: jupyter/pyspark-notebook:latest
        ports:
        - containerPort: 8888
        command: ["start-notebook.sh"]
        args: ["--NotebookApp.token=''"]
        volumeMounts:
          - mountPath: "/home/jovyan/work"
            name: ci-jupyter-storage-def

      initContainers:
      - name: jupyter-data-permission-fix
        image: busybox
        command: ["/bin/chmod","-R","777", "/data"]
        volumeMounts:
        - name: ci-jupyter-storage-def
          mountPath: /data```

【讨论】:

  • 点击服务时收到 302 响应 [I 11:24:35.924 NotebookApp] 从 /opt/conda/lib/python3.8/site-packages/jupyterlab 加载的 JupyterLab 扩展 [I 11 :24:35.925 NotebookApp] JupyterLab 应用程序目录是 /opt/conda/share/jupyter/lab [I 11:24:35.927 NotebookApp] 从本地目录服务笔记本:/home/jovyan [I 11:24:35.927 NotebookApp] Jupyter Notebook 6.1.4 运行于:jupyter-notebook-7499c4447b-knblc:8888 使用 Control-C 停止此服务器并关闭所有内核(两次跳过确认)。 302 GET / (10.244.1.149) 0.83ms
【解决方案2】:

正如我在 cmets 中已经提到的,您需要确保:

  1. 给定 Pod 的存储必须由 PersistentVolume Provisioner 基于请求的存储类进行配置,或者由管理员预先配置。 volumeClaimTemplates 将使用 PersistentVolumes 提供 stable storage。当 Pod 或 StatefulSet 被删除时,与 Pod 的 PersistentVolume 声明关联的 PersistentVolume 不会被删除。

  2. 容器正在以有权访问该卷的用户身份运行。可以通过将权限更改为 777 或使用适当的 initContainers 来完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2019-05-10
    相关资源
    最近更新 更多