• 在OpenShift 4下部署Jupyter Notebook


    最近因为工作需要研究一下AI/ML在OpenShift 4下的应用场景,很自然的想到了数据科学家常用到的建模和机器学习的平台Jupter Notebook,查阅了网上一些材料,基本发现都是基于OpenShift 3.11来进行部署的,近期还是需要在OpenShift 4上实践一下。

    如果对Jupyter Notebook的功能比较好奇想尝试一下,可以直接去官网

    https://jupyter.org/try ,尝试完毕需要环境搭建就继续下面的步骤。

    我们的环境是OpenShift 4.8.15版本,首先找到一个Jupyter notebook的GitHub

    https://github.com/jupyter-on-openshift/jupyter-notebooks

    jupyter项目下有多个目录提供了预装的很多镜像,包括如下内容:

    • jupyter/minimal-notebook: 基础镜像,支持python 3
    • jupyter/scipy-notebook: 基于minimal-notebook,添加了数据分析和可视化的包,包括numpy,matplotlib和scipy等模块
    • jupyter/tensorflow-notebook: 基于scipy-notebook构建,添加了Tensorflow需要的包。
    • jupyter/datascience-notebook: 基于scipy-notebook构建,添加了Julia和R的支持。
    • jupyter/pyspark-notebook: 基于scipy-notebook构建,添加了Spark和Hadoop集群的支持。
    • jupyter/all-spark-notebook:基于pyspark-notebook构建,添加了Scala和R的支持。
    • Jupyter/r-notebook:支持R语言的基础镜像。

    1.部署一个最简单的minimal Notebook

    基于现有镜像构建镜像流Image Stream

    oc new-project myproject
    
    oc create -f https://raw.githubusercontent.com/jupyter-on-openshift/jupyter-notebooks/master/image-streams/s2i-minimal-notebook.json

    部署应用并且开放路由

    oc new-app s2i-minimal-notebook:3.6 --name minimal-notebook     --env JUPYTER_NOTEBOOK_PASSWORD=mypassword
    
    
    oc create route edge minimal-notebook --service minimal-notebook     --insecure-policy Redirect
    
    
    [root@bastion ~]# oc get route/minimal-notebook
    NAME               HOST/PORT                                                             PATH   SERVICES           PORT       TERMINATION     WILDCARD
    minimal-notebook   minimal-notebook-myproject.apps.cluster-59da.sandbox842.opentlc.com          minimal-notebook   8080-tcp   edge/Redirect   None

    基于URL访问,登录完成后就是一个基本界面了。

    如果不需要用提供的构建好的镜像,需要自己完成构建,那就运行

    oc create -f https://raw.githubusercontent.com/jupyter-on-openshift/jupyter-notebooks/master/build-configs/s2i-minimal-notebook.json

    这个语句会创建Build Config,并完成构建,最终结果是在Image Stream中构建一个Image。

    因为minimal-notebook只是最基本的python 3的功能,很多包和函数缺乏,如果需要安装可以通过console,但因为是容器的关系,实例挂了又回到初始状态,因此尝试构建一个scipy-notebook. 

    2.构建一个scipy-notebook

    构建镜像

    oc create -f https://raw.githubusercontent.com/jupyter-on-openshift/jupyter-notebooks/master/build-configs/s2i-scipy-notebook.json

    基于如下命令可以查看Build Config运行状况和日志信息

    oc logs --follow bc/s2i-scipy-notebook-py36

    发现运行到中间出错了。

    查找问题,发现构建过程中OOM的错误

    [root@bastion ~]# oc get build
    NAME                        TYPE     FROM          STATUS                       STARTED       DURATION
    s2i-scipy-notebook-py36-1   Source   Git@3caf4f2   Failed (OutOfMemoryKilled)   4 hours ago   3m17s
    s2i-scipy-notebook-py36-2   Source   Git@3caf4f2   Failed (OutOfMemoryKilled)   4 hours ago   2m54s

    调整Build Config的内存配置。(从1G调整到2G)

    运行构建后成功,浏览发现有2个Image Stream了

    [root@bastion ~]# oc get is
    NAME                   IMAGE REPOSITORY                                                                  TAGS      UPDATED
    s2i-minimal-notebook   image-registry.openshift-image-registry.svc:5000/myproject/s2i-minimal-notebook   3.5,3.6   8 hours ago
    s2i-scipy-notebook     image-registry.openshift-image-registry.svc:5000/myproject/s2i-scipy-notebook     3.6       4 hours ago

    构建应用

    oc new-app s2i-scipy-notebook:3.6 --name scipy-notebook   --env JUPYTER_NOTEBOOK_PASSWORD=mypassword
    
    oc create route edge scipy-notebook --service scipy-notebook    --insecure-policy Redirect

    查看URL

    [root@bastion ~]# oc get route
    NAME               HOST/PORT                                                             PATH   SERVICES           PORT       TERMINATION     WILDCARD
    minimal-notebook   minimal-notebook-myproject.apps.cluster-59da.sandbox842.opentlc.com          minimal-notebook   8080-tcp   edge/Redirect   None
    scipy-notebook     scipy-notebook-myproject.apps.cluster-59da.sandbox842.opentlc.com            scipy-notebook     8080-tcp   edge/Redirect   None

    进入页面访问,输入例子程序

    import sys
    import time
    
    from IPython.display import display, clear_output
    for i in range(10):
        time.sleep(0.25)
        clear_output(wait=True)
        print(i)
        sys.stdout.flush()
    
    %matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    from scipy.special import jn
    x = np.linspace(0,5)
    f, ax = plt.subplots()
    ax.set_title("Bessel functions")
    
    for n in range(1,10):
        time.sleep(1)
        ax.plot(x, jn(x,n))
        clear_output(wait=True)
        display(f)
    
    # close the figure at the end, so we don't get a duplicate
    # of the last plot
    plt.close()

    运行查看结果

    这是一个简单的Jupyter Notebook在OpenShift 4上的构建过程,在OpenShift 4中针对AI/ML更多的是推出了OpenDataHub框架。详细信息可以参考

    https://opendatahub.io/

  • 相关阅读:
    Merge Two Sorted Lists
    Palindrome Number
    Plus One
    Reverse Integer
    Read N Characters Given Read4
    Given two strings S and T, determine if they are both one edit distance apart
    Longest Palindromic Substring
    Missing Ranges
    Java 不被看好前景堪忧?可能是想多了!
    每天数十亿次请求的应用经验分享,值得参考!
  • 原文地址:https://www.cnblogs.com/ericnie/p/15467067.html
Copyright © 2020-2023  润新知