<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Jupyter Enterprise Gateway · Jupyter Blog</title>
    <link>https://jupyter.org/medium-archive/hugo/tags/jupyter-enterprise-gateway/</link>
    <description>The Project Jupyter blog: news, releases, and community stories, archived from blog.jupyter.org.</description>
    <generator>Hugo</generator>
    <lastBuildDate>Tue, 16 Oct 2018 15:03:29 &#43;0000</lastBuildDate>
    <atom:link href="https://jupyter.org/medium-archive/hugo/tags/jupyter-enterprise-gateway/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>On-demand Notebooks with JupyterHub, Jupyter Enterprise Gateway and Kubernetes</title>
      <link>https://jupyter.org/medium-archive/hugo/posts/2018/on-demand-notebooks-with-jupyterhub-jupyter-enterprise-gateway-and-kubernetes/</link>
      <pubDate>Tue, 16 Oct 2018 15:03:29 &#43;0000</pubDate>
      <dc:creator>Luciano Resende</dc:creator>
      <guid>https://jupyter.org/medium-archive/hugo/posts/2018/on-demand-notebooks-with-jupyterhub-jupyter-enterprise-gateway-and-kubernetes/</guid>
      <description>by: Luciano Resende, Kevin Bates, Alan Chin</description>
      <content:encoded>&lt;p&gt;by: &lt;a href=&#34;https://twitter.com/lresende1975&#34;&gt;Luciano Resende&lt;/a&gt;, &lt;a href=&#34;https://twitter.com/kbates4&#34;&gt;Kevin Bates&lt;/a&gt;, Alan Chin&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://jupyter-notebook.readthedocs.io/en/stable/&#34;&gt;&lt;strong&gt;Jupyter Notebook&lt;/strong&gt;&lt;/a&gt; has become the “de facto” platform used by data scientists to build interactive applications and to tackle big data and AI problems.&lt;/p&gt;
&lt;p&gt;With the increased adoption of Machine Learning and AI by enterprises, we have seen more and more requirement to build analytics platforms that provide on-demand notebooks for data scientists and data engineers in general.&lt;/p&gt;
&lt;p&gt;This article describes how to deploy multiple components from the Jupyter Notebook stack to provide an on-demand analytics platform powered by JupyterHub and Jupyter Enterprise Gateway on a Kubernetes cluster.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&#34;https://jupyter.org/medium-archive/hugo/medium-archive/hugo/posts/2018/on-demand-notebooks-with-jupyterhub-jupyter-enterprise-gateway-and-kubernetes/images/001-1_F_jJ1nDSQgBhrkbsEXB93Q.webp&#34; width=&#34;1600&#34; height=&#34;889&#34; fetchpriority=&#34;high&#34; alt=&#34;Image 1 — Deployment Architecture&#34;&gt;
&lt;figcaption&gt;Image 1 — Deployment Architecture&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h2 id=&#34;on-demand-notebooks-infrastructure&#34;&gt;On-Demand Notebooks Infrastructure&lt;/h2&gt;
&lt;p&gt;Below are the main components we are going to use to build our solution, and its high-level description:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://jupyterhub.readthedocs.io/&#34;&gt;JupyterHub&lt;/a&gt; enables the creation of a multi-user Hub which spawns, manages, and proxies multiple instances of the single-user &lt;a href=&#34;https://jupyter-notebook.readthedocs.io/&#34;&gt;Jupyter Notebook&lt;/a&gt; server providing the ‘as a service’ feeling we are looking for.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://jupyter-enterprise-gateway.readthedocs.io/en/latest/&#34;&gt;Jupyter Enterprise Gateway&lt;/a&gt; provides optimal resource allocations by enabling kernels to be launched in its own pod enabling notebook pods to have minimal resources while kernel specific resources are allocated/deallocated accordingly to its lifecycle. It also enables the base image of the kernel to become a choice.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://kubernetes.io/&#34;&gt;Kubernetes&lt;/a&gt; enables easy management of containerized applications and resources with the benefit of Elasticity and multiple other quality of services.&lt;/p&gt;
&lt;h2 id=&#34;jupyterhub-deployment&#34;&gt;JupyterHub Deployment&lt;/h2&gt;
&lt;p&gt;JupyterHub is the entry point for our solution, it will manage user authorization and provisioning of individual Notebook servers for each user.&lt;/p&gt;
&lt;p&gt;JupyterHub configuration is done via a config.yaml, and the following settings are required:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Enable custom notebook configuration (coming from the customized user image).&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;hub:
  extraConfig: |-
    config = &amp;#39;/etc/jupyter/jupyter_notebook_config.py&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;
&lt;li&gt;Define the docker image to be used when instantiating the notebook server for each user&lt;/li&gt;
&lt;li&gt;Define custom environment variables used to connect the Notebook server with Jupyter Enterprise Gateway to enable support for remote kernels&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;singleuser:
  image:
    name: elyra/nb2kg
    tag: dev
  storage:
    dynamic:
      storageClass: nfs-dynamic
  extraEnv:
    KG_URL: &amp;lt;FQDN of Gateway Endpoint&amp;gt;
    KG_HTTP_USER: jovyan
    KERNEL_USERNAME: jovyan
    KG_REQUEST_TIMEOUT: 60
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The complete config.yaml would look like the one below:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;hub:
  db:
    type: sqlite-memory
  extraConfig: |-
    config = &amp;#39;/etc/jupyter/jupyter_notebook_config.py&amp;#39;
    c.Spawner.cmd = [&amp;#39;jupyter-labhub&amp;#39;]
proxy:
  secretToken: &amp;#34;xxx&amp;#34;
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ingress:
  enabled: true
  hosts:
    - &amp;lt;FQDN Kubernetes Master&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;singleuser:
  defaultUrl: &amp;#34;/lab&amp;#34;
  image:
    name: elyra/nb2kg
    tag: 2.0.0.dev0
  storage:
    dynamic:
      storageClass: nfs-dynamic
  extraEnv:
    KG_URL: &amp;lt;FQDN of Gateway Endpoint&amp;gt;
    KG_HTTP_USER: jovyan
    KERNEL_USERNAME: jovyan
    KG_REQUEST_TIMEOUT: 60
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;rbac:
  enabled: true
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;debug:
  enabled: true
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Detailed deployment instructions for JupyterHub can be found at &lt;a href=&#34;https://zero-to-jupyterhub.readthedocs.io/en/stable/&#34;&gt;Zero to JupyterHub for Kubernetes&lt;/a&gt;, but the command below would deploy it into a Kubernetes environment.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;helm upgrade --install --force hub jupyterhub/jupyterhub --namespace hub --version 0.7.0 --values jupyterhub-config.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;custom-jupyterhub-user-image&#34;&gt;Custom JupyterHub user image&lt;/h2&gt;
&lt;p&gt;By default, JupyterHub would deploy a vanilla Notebook Server image which will require that all resources ever used by the image to be allocated when the Kubernetes image is instantiated.&lt;/p&gt;
&lt;p&gt;Our custom image will enable kernels to be started in its own pod, promoting a better resource allocation as resources can be allocated and freed up as needed. This also gives us the flexibility of supporting different frameworks for different notebooks (e.g. a notebook using Python and TensorFlow, while another is using Python and Caffe2).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Dockerfile for &lt;a href=&#34;https://github.com/jupyter/enterprise_gateway/tree/master/etc/docker/nb2kg&#34;&gt;elyra-nb2kg custom image&lt;/a&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;FROM jupyterhub/k8s-singleuser-sample:0.7.0
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;# Do the pip installs as the unprivileged notebook user
USER $NB_USER
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ADD jupyter_notebook_config.py /etc/jupyter/jupyter_notebook_config.py
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;# Install NB2KG
RUN pip install --upgrade nb2kg &amp;amp;&amp;amp; \
    jupyter serverextension enable --py nb2kg --sys-prefix
&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;
&lt;li&gt;Jupyter Notebook custom configuration to override Notebook handlers with the ones from NB2KG that will enable the notebook to connect with the Enterprise Gateway that enables remote kernels.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;from jupyter_core.paths import jupyter_data_dir
import subprocess
import os
import errno
import stat
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;c = get_config()
c.NotebookApp.ip = &amp;#39;*&amp;#39;
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;c.NotebookApp.session_manager_class = &amp;#39;nb2kg.managers.SessionManager&amp;#39;
c.NotebookApp.kernel_manager_class = &amp;#39;nb2kg.managers.RemoteKernelManager&amp;#39;
c.NotebookApp.kernel_spec_manager_class = &amp;#39;nb2kg.managers.RemoteKernelSpecManager&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;# https://github.com/jupyter/notebook/issues/3130
c.FileContentsManager.delete_to_trash = False
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;# Generate a self-signed certificate
if &amp;#39;GEN_CERT&amp;#39; in os.environ:
    dir_name = jupyter_data_dir()
    pem_file = os.path.join(dir_name, &amp;#39;notebook.pem&amp;#39;)
    try:
        os.makedirs(dir_name)
    except OSError as exc:  # Python &amp;gt;2.5
        if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
            pass
        else:
            raise
    # Generate a certificate if one doesn&amp;#39;t exist on disk
    subprocess.check_call([&amp;#39;openssl&amp;#39;, &amp;#39;req&amp;#39;, &amp;#39;-new&amp;#39;,
                           &amp;#39;-newkey&amp;#39;, &amp;#39;rsa:2048&amp;#39;,
                           &amp;#39;-days&amp;#39;, &amp;#39;365&amp;#39;,
                           &amp;#39;-nodes&amp;#39;, &amp;#39;-x509&amp;#39;,
                           &amp;#39;-subj&amp;#39;, &amp;#39;/C=XX/ST=XX/L=XX/O=generated/CN=generated&amp;#39;,
                           &amp;#39;-keyout&amp;#39;, pem_file,
                           &amp;#39;-out&amp;#39;, pem_file])
    # Restrict access to the file
    os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR)
    c.NotebookApp.certfile = pem_file
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note that the document above was generated by &lt;code&gt;jupyter notebook --generate-config&lt;/code&gt; and then updated with the required handlers override:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;c.NotebookApp.session_manager_class = &amp;#39;nb2kg.managers.SessionManager&amp;#39;
c.NotebookApp.kernel_manager_class = &amp;#39;nb2kg.managers.RemoteKernelManager&amp;#39;
c.NotebookApp.kernel_spec_manager_class = &amp;#39;nb2kg.managers.RemoteKernelSpecManager&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;jupyter-enterprise-gateway-deployment&#34;&gt;Jupyter Enterprise Gateway deployment&lt;/h2&gt;
&lt;p&gt;Jupyter Enterprise Gateway enables Jupyter Notebook to launch and manage remote kernels in a distributed cluster, including Kubernetes cluster.&lt;/p&gt;
&lt;p&gt;Enterprise Gateway provides a Kubernetes deployment descriptor that makes it simple to deploy it on a Kubernetes environment with the command below:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;kubectl apply -f https://raw.githubusercontent.com/jupyter-incubator/enterprise_gateway/master/etc/kubernetes/enterprise-gateway.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We also recommend that the kernel images be downloaded on all nodes of the Kubernetes cluster to avoid delays/timeouts when launching kernels for the first time on these nodes.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker pull elyra/enterprise-gateway:dev
docker pull elyra/kernel-py:dev
docker pull elyra/kernel-tf-py:dev
docker pull elyra/kernel-r:dev
docker pull elyra/kernel-scala:dev
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;automated-one-click-deployment-using-ansible&#34;&gt;Automated One-Click Deployment using Ansible&lt;/h2&gt;
&lt;p&gt;If you are eager to get started and try this in a few machines, we have published an&lt;a href=&#34;https://github.com/lresende/ansible-kubernetes-cluster&#34;&gt;&lt;code&gt;ansible script&lt;/code&gt;&lt;/a&gt; that deploys the full set of components described above on vanilla RHEL machines/VMs.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ansible-playbook --verbose setup-kubernetes.yml -c paramiko -i hosts-fyre-kubernetes
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Jupyter Enterprise Gateway provides remote kernel management to Jupyter Notebooks. In a JupyterHub/Kubernetes environment, it enables hub to launch tiny Jupyter Notebook pods and only allocate large kernel resources when these are created as independent pods. This approach also allows for easy sharing of expensive resources as GPUs, etc&lt;/p&gt;
&lt;h2 id=&#34;special-thanks&#34;&gt;Special Thanks&lt;/h2&gt;
&lt;p&gt;Special thanks to &lt;a href=&#34;https://twitter.com/e_sundell&#34;&gt;Erik Sundell&lt;/a&gt; and &lt;a href=&#34;https://twitter.com/minrk&#34;&gt;Min RK&lt;/a&gt; from the JupyterHub team for the support and initial discussions around JupyterHub.&lt;/p&gt;
</content:encoded>
    </item>
    
  </channel>
</rss>
