容器镜像

关于 Kubeflow 笔记本的容器镜像

Kubeflow 笔记本原生支持三种类型的笔记本:JupyterLabRStudioVisual Studio Code (code-server),但任何基于 Web 的 IDE 都应可用。笔记本服务器作为容器在 Kubernetes Pod 中运行,这意味着 IDE 的类型(以及安装的软件包)取决于您为服务器选择的 Docker 镜像。

官方镜像

Kubeflow 提供了一些示例容器镜像,以帮助您开始使用 Kubeflow 笔记本。

此图表显示了这些镜像之间的关系(注意:节点是可点击的链接,指向相应的 Dockerfile)

%%{init: {'theme':'forest'}}%%
graph TD
  Base[Base] --> Jupyter[Jupyter]
  Base --> Code-Server[code-server]
  Base --> RStudio[RStudio]
  
  Jupyter --> PyTorch[PyTorch]
  Jupyter --> SciPy[SciPy]
  Jupyter --> TensorFlow[TensorFlow]
  
  Code-Server --> Code-Server-Conda-Python[Conda Python]
  RStudio --> Tidyverse[Tidyverse]

  PyTorch --> PyTorchFull[PyTorch Full]
  TensorFlow --> TensorFlowFull[TensorFlow Full]

  Jupyter --> PyTorchCuda[PyTorch CUDA]
  Jupyter --> TensorFlowCuda[TensorFlow CUDA]
  Jupyter --> PyTorchGaudi[PyTorch Gaudi]

  PyTorchCuda --> PyTorchCudaFull[PyTorch CUDA Full]
  TensorFlowCuda --> TensorFlowCudaFull[TensorFlow CUDA Full]
  PyTorchGaudi --> PyTorchGaudiFull[PyTorch Gaudi Full]

  click Base "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/base"
  click Jupyter "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter"
  click Code-Server "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/codeserver"
  click RStudio "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/rstudio"
  click PyTorch "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-pytorch"
  click SciPy "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-scipy"
  click TensorFlow "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-tensorflow"
  click Code-Server-Conda-Python "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/codeserver-python"
  click Tidyverse "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/rstudio-tidyverse"
  click PyTorchFull "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-pytorch-full"
  click TensorFlowFull "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-tensorflow-full"
  click PyTorchCuda "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-pytorch-cuda"
  click TensorFlowCuda "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-tensorflow-cuda"
  click PyTorchCudaFull "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-pytorch-cuda-full"
  click TensorFlowCudaFull "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-tensorflow-cuda-full"
  click PyTorchGaudi "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-pytorch-gaudi"
  click PyTorchGaudiFull "https://github.com/kubeflow/kubeflow/tree/master/components/example-notebook-servers/jupyter-pytorch-gaudi-full"

基础镜像

这些镜像为 Kubeflow 笔记本容器提供了通用的起点。

Dockerfile容器注册表备注
./baseghcr.io/kubeflow/kubeflow/notebook-servers/base通用基础镜像
./codeserverghcr.io/kubeflow/kubeflow/notebook-servers/codeservercode-server (Visual Studio Code)
./jupyterghcr.io/kubeflow/kubeflow/notebook-servers/jupyterJupyterLab
./rstudioghcr.io/kubeflow/kubeflow/notebook-servers/rstudioRStudio

Kubeflow 镜像

这些镜像在基础镜像的基础上扩展了实际常用的软件包。

Dockerfile容器注册表备注
./codeserver-pythonghcr.io/kubeflow/kubeflow/notebook-servers/codeserver-pythoncode-server + Conda Python
./rstudio-tidyverseghcr.io/kubeflow/kubeflow/notebook-servers/rstudio-tidyverseRStudio + Tidyverse
./jupyter-pytorchghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-pytorchJupyterLab + PyTorch
./jupyter-pytorch-fullghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-pytorch-fullJupyterLab + PyTorch + 常用软件包
./jupyter-pytorch-cudaghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-pytorch-cudaJupyterLab + PyTorch + CUDA
./jupyter-pytorch-cuda-fullghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-pytorch-cuda-fullJupyterLab + PyTorch + CUDA + 常用软件包
./jupyter-scipyghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-scipyJupyterLab + 常用软件包
./jupyter-tensorflowghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-tensorflowJupyterLab + TensorFlow
./jupyter-tensorflow-fullghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-tensorflow-fullJupyterLab + TensorFlow + 常用软件包
./jupyter-tensorflow-cudaghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-tensorflow-cudaJupyterLab + TensorFlow + CUDA
./jupyter-tensorflow-cuda-fullghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-tensorflow-cuda-fullJupyterLab + TensorFlow + CUDA + 常用软件包
./jupyter-pytorch-gaudighcr.io/kubeflow/kubeflow/notebook-servers/jupyter-pytorch-gaudiJupyterLab + PyTorch + Gaudi
./jupyter-pytorch-gaudi-fullghcr.io/kubeflow/kubeflow/notebook-servers/jupyter-pytorch-gaudi-fullJupyterLab + PyTorch + Gaudi + 常用软件包

软件包安装

用户在启动 Kubeflow 笔记本后安装的软件包仅在 Pod 的生命周期内有效(除非安装到 PVC 支持的目录中)。

为确保软件包在 Pod 重启后得以保留,用户需要采取以下任一措施:

  1. 构建包含这些软件包的自定义镜像,或者
  2. 确保将其安装到 PVC 支持的目录中

自定义镜像

您可以构建自己的自定义镜像,以便在 Kubeflow 笔记本中使用。

确保您的自定义镜像满足要求的最简单方法是扩展我们的基础镜像之一。

镜像要求

容器镜像若要在 Kubeflow 笔记本中使用,必须满足以下条件:

  • 在端口 8888 上暴露 HTTP 接口
    • Kubeflow 在运行时设置环境变量 NB_PREFIX,其中包含我们期望容器监听的 URL 路径
    • Kubeflow 使用 IFrames,因此请确保您的应用程序在 HTTP 响应头中设置 Access-Control-Allow-Origin: *
  • 以名为 jovyan 的用户身份运行
    • jovyan 的主目录应为 /home/jovyan
    • jovyan 的 UID 应为 1000
  • 在空 PVC 挂载到 /home/jovyan 时成功启动
    • Kubeflow 将 PVC 挂载到 /home/jovyan,以在 Pod 重启时保留状态

安装 Python 软件包

您可以扩展其中一个镜像,并安装 Kubeflow 笔记本用户可能需要的任何 pipconda 软件包。作为指导,请参考 ./jupyter-pytorch-full/Dockerfile 中的 pip install ... 示例,以及 ./rstudio-tidyverse/Dockerfile 中的 conda install ... 示例。

常见的错误原因之一是用户运行 pip install --user ...,导致主目录(由 PVC 支持)包含一个与 /opt/conda/... 中包含的软件包不同或不兼容的版本。

安装 Linux 软件包

您可以扩展其中一个镜像,并安装 Kubeflow 笔记本用户可能需要的任何 apt-get 软件包。请确保在运行 apt-get 之前在 Dockerfile 中切换到 root 用户,并在之后切换回 $NB_USER 用户。

配置 S6 Overlay

某些用例可能需要在笔记本服务器容器启动期间运行自定义脚本,或者高级用户可能想要在容器内部添加额外的服务(例如,Apache 或 NGINX Web 服务器)。为了简化此操作,我们使用了 s6-overlay

s6-overlaytini 等其他初始化系统不同。tini 是为处理容器中作为 PID 1 运行的单个进程而创建的,而 s6-overlay 旨在管理多个进程,并允许镜像的创建者确定哪些进程失败时应静默重启,哪些失败时应导致容器退出。

创建脚本

需要在容器启动期间运行的脚本可以放置在 /etc/cont-init.d/ 中,并按字母数字升序执行。

一个启动脚本的示例可以在 ./rstudio/s6/cont-init.d/02-rstudio-env-fix 中找到。此脚本使用 with-contenv 助手,以便环境变量(传递给容器)在脚本中可用。此脚本的目的是在 Pod 启动时将任何 KUBERNETES_* 环境变量快照到 Renviron.site 中,因为没有这些变量 kubectl 将无法工作。

创建服务

需要由 s6-overlay 监控的额外服务应放置在 /etc/services.d/ 下各自的文件夹中,其中包含一个名为 run 的脚本,以及一个可选的完成脚本 finish

一个服务示例可以在 .jupyter/s6/services.d/jupyterlabrun 脚本中找到,该脚本用于启动 JupyterLab 本身。有关 runfinish 脚本的更多信息,请参阅 s6-overlay 文档

以 Root 身份运行服务

有时您可能需要以 root 用户身份运行服务,为此,您可以更改 Dockerfile,在末尾添加 USER root,然后使用 s6-setuidgid$NB_USER 用户身份运行面向用户的服务。

我们的示例镜像以 $NB_USER 用户身份(而非 root 用户)运行 s6-overlay,这意味着任何与 s6-overlay 相关的文件或脚本必须由 $NB_USER 用户拥有才能成功运行。

下一步

  • 在启动笔记本服务器时指定您的容器镜像以使用它。(请参阅快速入门指南。)

反馈

此页面有帮助吗?