Jupyter Enterprise Gateway

repository·main·Indexed 20 days ago

https://github.com/jupyter-server/enterprise_gateway

A web server for spawning and communicating with remote Jupyter kernels within distributed clusters such as Kubernetes, Docker Swarm, or Apache Spark. It acts as a bridge between the Jupyter interface and remote compute resources, supporting the distribution of Python, R, and Toree kernels. The project includes utilities like the Kernel Image Puller (KIP) for automatic node provisioning and specialized Docker images for YARN clusters and IPython kernels.

Tokens
65.8K
Snippets
133
Records
233
Agent score
70%

What's inside Jupyter Enterprise Gateway

  1. What is included in the demo-base Docker image

    main

    The demo-base image is a Debian-based Miniconda environment designed for demonstrating remote kernel capabilities with Jupyter Enterprise Gateway. It includes:

    • Base OS/Python: continuumio/miniconda3:24.1.2-0 (Python 3.11)
    • Big Data Stack: Hadoop 3.3.1 and Apache Spark 3.2.1 (controlled by SPARK_VERSION in the top-level Makefile)
    • Java: OpenJDK 11 runtime (openjdk-11-jdk-headless)
    • R Support: Miniconda with R packages (r-devtools, r-stringr, r-argparse)
    • Kernels: Apache Toree 0.5.0-incubating and ipykernel<7 (pinned to ensure compatibility with the Enterprise Gateway process-proxy model)
    • Users: jovyan service user (UID 1000) with password-less SSH, plus system users elyra, bob, and alice.
    • Storage: HDFS home directories are automatically created for each system user at container startup.
  2. Overview of the Enterprise Gateway Docker Image

    main

    This Docker image enables Jupyter Enterprise Gateway support within Kubernetes or Docker Swarm clusters. It is built on jupyter/minimal-notebook:2023-03-13 and includes Apache Spark 3.2.1.

    Key Features:

    • Supports launching and distributing Python, R, and Toree kernels across a managed cluster.
    • Includes a pinned version of ipykernel<7 to ensure kernel idle/busy transitions function correctly under the process-proxy model.

    Note: If you require a YARN-based image, use elyra/enterprise-gateway-demo instead.

  3. What is the Kernel Image Puller (KIP)?

    main

    The Kernel Image Puller (KIP) is a utility image designed to work within Kubernetes or Docker Swarm clusters. Its primary responsibility is to contact a configured Jupyter Enterprise Gateway instance and automatically pull the required kernel-based images to every node in the cluster.

    Key Benefits:

    • Automatic Node Provisioning: When adding new nodes to a cluster, KIP ensures kernel images are automatically populated on those nodes.
    • Dynamic Kernelspec Support: When you configure new kernelspecs that use different images, KIP ensures those images are pulled to all cluster nodes automatically.
  4. Overview of Jupyter Enterprise Gateway

    main

    Jupyter Enterprise Gateway is a web server that provides headless access to Jupyter kernels in a distributed enterprise environment. It allows Jupyter Notebook to launch remote kernels in clusters such as:

    • Apache Spark (managed by YARN)
    • IBM Spectrum Conductor
    • Kubernetes
    • Docker Swarm

    It provides out-of-the-box support for the following kernels:

    • Python (using IPython kernel)
    • R (using IRkernel)
    • Scala (using Apache Toree kernel)

    Key Capabilities:

    • Remote Kernel Support: Kernels can be local to the Gateway, distributed via round-robin across cluster nodes, or managed by a resource manager.
    • Security: Provides secure communication from the client through the Gateway to the kernels.
    • Multi-tenancy: Supports multi-tenant environments and persistent kernel sessions.
    • Extensibility: While it supports major orchestrators out of the box, others can be added via an extensible framework.

    Note: Jupyter Enterprise Gateway is not a replacement for JupyterHub. Use JupyterHub to manage multiple Jupyter Notebook deployments.

  5. Use the Spark-enabled IPython kernel image

    main

    This Docker image provides an IPython kernel (with debugger support) designed to be launched via Jupyter Enterprise Gateway within a Kubernetes cluster. It is built on top of elyra/kernel-py and includes Apache Spark and the OpenJDK 8 runtime (openjdk-8-jdk).

    Key Features:

    • IPython kernel support including debugger capabilities.
    • Pre-installed data science libraries.
    • Spark on Kubernetes support directly from within a Jupyter Notebook.
    • Bundled Apache Spark (currently version 3.2.1).

    Warning: Using this kernel within Spark inside a Docker Swarm configuration may not yield expected results; it is optimized for Kubernetes.

  6. Use the Spark R kernel with Jupyter Enterprise Gateway

    main

    This Docker image provides an IRkernel (R kernel) that can be launched via Jupyter Enterprise Gateway within a Kubernetes cluster. It integrates Apache Spark, allowing you to run Spark on Kubernetes directly from a Jupyter Notebook.

    Key Features:

    • IRkernel support.
    • Spark on Kubernetes support from within Jupyter.
    • Bundled OpenJDK 8 runtime (openjdk-8-jdk) required by Apache Spark.

    Important Notes:

    • The image is built on elyra/kernel-r.
    • The bundled Apache Spark version is currently 3.2.1 (controlled by SPARK_VERSION in the top-level Makefile).
    • Compatibility Warning: Using this kernel within Spark in a Docker Swarm configuration may not yield expected results; it is designed for Kubernetes.
  7. Use the Scala (Apache Toree) kernel with Enterprise Gateway

    main

    This Docker image enables Scala support via the Apache Toree kernel when launched through Jupyter Enterprise Gateway in a Kubernetes or Docker Swarm cluster. It is built on top of elyra/spark (currently using Spark version 3.2.1).

    Capabilities:

    • Scala (Toree) kernel support.
    • Spark on Kubernetes support directly from within a Jupyter Notebook.

    Note: Using this kernel within Spark in a Docker Swarm configuration may not yield expected results.

  8. Developer use cases for Jupyter Enterprise Gateway

    main

    Jupyter Enterprise Gateway is designed for developers who need to extend its capabilities or integrate remote kernel functionality into their own applications. Common developer tasks include:

    • Implementing new Resource Managers: Authoring a new ProcessProxy class to support different resource managers (e.g., Kubernetes, Docker Swarm) and leverage their specific features.
    • Extending Jupyter applications: Modifying applications like nbclient to use a KernelManager that leverages remote kernels spawned via Enterprise Gateway.
    • Platform Integration: Integrating remote kernel launching capabilities into existing compute platforms.
    • Custom Kernel Launchers: Implementing kernel launchers for non-Python environments (e.g., a Go kernel) to run remotely in clusters like Kubernetes.
    • Custom Container Images: Extending or creating custom kernel container images to support specific data science workflows.
    • Kernel-as-a-Service: Authoring custom Kernel-as-a-Service applications.
  9. Use the Enterprise Gateway REST API to build applications

    main

    The Enterprise Gateway REST API allows developers to author new applications that interact with the gateway. While several endpoints exist, most application logic should focus on two primary endpoints:

    • /api/kernelspecs: Used to retrieve a list of available kernel specifications.
    • /api/kernels: Used to start, stop, interrupt, and restart kernels.

    Note that while the /api/sessions endpoint can be used to manage a kernel's lifecycle, it is often unnecessary for standard application flows. For instance, Jupyter Notebook and JupyterLab use /api/kernelspecs to discover kernels and /api/kernels for lifecycle management, while keeping the 'session' state on the client side.

  10. What is Jupyter Enterprise Gateway?

    main

    Jupyter Enterprise Gateway is a headless web server that provides a Kernel as a Service model. It allows remote notebooks to launch kernels on behalf of users within managed-cluster environments (such as Kubernetes, Hadoop YARN, or Docker Swarm).

    Unlike the default Jupyter framework which runs kernels locally on the web server, Enterprise Gateway distributes kernels across a compute cluster. This enables:

    • Resource Optimization: Better management of limited resources like GPUs and large memory.
    • Scalability: Support for a large number of simultaneous users and active kernels.
    • High Availability: Support for Disaster Recovery scenarios where a new Gateway server can be spun up to service existing remote kernels.
    • Granular Security: Improved security controls for enterprise, scientific, and academic implementations.
  11. Handle interrupt and shutdown requests in a kernel launcher

    main

    A kernel launcher must listen on its communication port for specific JSON messages sent by Enterprise Gateway to manage the kernel lifecycle:

    Interrupt Signals

    To interrupt current processing (equivalent to SIGINT), Enterprise Gateway sends a signal event:

    • Format: {"signum": n}
    • Common values: n=2 (for SIGINT).
    • Polling: Enterprise Gateway sends {"signum": 0} to perform a poll() check to see if the process is still alive.

    Shutdown Requests

    When the process proxy is performing final cleanup and needs the launcher to exit, it sends a shutdown request:

    • Format: {"shutdown": 1}
    • Action: Upon receiving this, the launcher should stop listening on the communication socket and exit.
  12. How kernel launch works from Jupyter Lab to Enterprise Gateway

    main

    When Jupyter Lab is configured to use Enterprise Gateway, the kernel launch process follows a specific sequence of interactions across multiple components. This process involves two main phases:

    1. Kernelspec Retrieval: Jupyter Lab requests available kernelspecs from Jupyter Server via GET api/kernelspecs. Jupyter Server proxies this request to Enterprise Gateway, which returns the specifications to allow Jupyter Lab to identify valid kernels.

    2. Kernel Initialization and Session Creation:

      • Jupyter Lab initiates a session via POST api/sessions to Jupyter Server.
      • Jupyter Server forwards a kernel creation request via POST api/kernels to Enterprise Gateway.
      • Enterprise Gateway uses a ProcessProxy to call launch_process(), which triggers the actual kernel launch.
      • A ResourceManager confirms the startup and provides state and host information.
      • Once the kernel is running, connection information is passed back through the proxy to Enterprise Gateway.
      • Enterprise Gateway establishes TCP socket connections (requests and handshakes) directly with the Kernel.
      • Finally, the session and kernel responses are propagated back to Jupyter Lab.
    3. Kernel Information Retrieval: After launch, Jupyter Lab uses a WebSocket connection (ws GET api/kernels) to communicate with the kernel. Enterprise Gateway handles the kernel_info_request and kernel_info_reply messages to facilitate the WebSocket upgrade between the client and the kernel.

        sequenceDiagram
            participant JupyterLab
            participant JupyterServer
            participant EnterpriseGateway
            participant ProcessProxy
            participant Kernel
            participant ResourceManager
            Note left of JupyterLab: fetch kernelspecs
            JupyterLab->>JupyterServer: https GET api/kernelspecs
            JupyterServer->>EnterpriseGateway: https GET api/kernelspecs
            EnterpriseGateway-->>JupyterServer: api/kernelspecs response
            JupyterServer-->>JupyterLab: api/kernelspecs response
    
            Note left of JupyterLab: kernel initialization
            JupyterLab->>JupyterServer: https POST api/sessions
            JupyterServer->>EnterpriseGateway: https POST api/kernels
            EnterpriseGateway->>ProcessProxy: launch_process()
            ProcessProxy->>Kernel: launch kernel
            ProcessProxy->>ResourceManager: confirm startup
            Kernel-->>ProcessProxy: connection info
            ResourceManager-->>ProcessProxy: state & host info
            ProcessProxy-->>EnterpriseGateway: complete connection info
            EnterpriseGateway->>Kernel: TCP socket requests
            Kernel-->>EnterpriseGateway: TCP socket handshakes
            EnterpriseGateway-->>JupyterServer: api/kernels response
            JupyterServer-->>JupyterLab: api/sessions response
    
            JupyterLab->>JupyterServer: ws GET api/kernels
            JupyterServer->>EnterpriseGateway: ws GET api/kernels
            EnterpriseGateway->>Kernel: kernel_info_request message
            Kernel-->>EnterpriseGateway: kernel_info_reply message
            EnterpriseGateway-->>JupyterServer: websocket upgrade response
            JupyterServer-->>JupyterLab: websocket upgrade response