Files
clear-linux-documentation/tutorials/openfaas.html
2024-11-04 18:56:31 +00:00

481 lines
35 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>OpenFaaS* &#8212; Documentation for Clear Linux* project</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="../_static/bizstyle.css?v=5283bb3d" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
<script src="../_static/documentation_options.js?v=5929fcd5"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../_static/copybutton.js?v=a56c686a"></script>
<script src="../_static/bizstyle.js"></script>
<link rel="canonical" href="https://clearlinux.github.io/clear-linux-documentation/tutorials/openfaas.html" />
<link rel="icon" href="../_static/favicon.ico"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="PHP and PHP-FPM" href="php.html" />
<link rel="prev" title="NVIDIA* CUDA Toolkit" href="nvidia-cuda.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="php.html" title="PHP and PHP-FPM"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="nvidia-cuda.html" title="NVIDIA* CUDA Toolkit"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Documentation for Clear Linux* project</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Tutorials</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">OpenFaaS*</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="openfaas">
<span id="id1"></span><h1>OpenFaaS*<a class="headerlink" href="#openfaas" title="Link to this heading"></a></h1>
<p>This tutorial shows how to set up OpenFaaS running on top of a Kubernetes*
cluster on Clear Linux OS, obtain Clear Linux OS based OpenFaaS templates, and develop an example
function.</p>
<nav class="contents local" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#background" id="id5">Background</a></p></li>
<li><p><a class="reference internal" href="#prerequisites" id="id6">Prerequisites</a></p></li>
<li><p><a class="reference internal" href="#deploy-openfaas" id="id7">Deploy OpenFaaS</a></p></li>
<li><p><a class="reference internal" href="#openfaas-templates" id="id8">OpenFaaS templates</a></p></li>
<li><p><a class="reference internal" href="#example-develop-a-function" id="id9">Example: Develop a function</a></p></li>
</ul>
</nav>
<section id="background">
<h2><a class="toc-backref" href="#id5" role="doc-backlink">Background</a><a class="headerlink" href="#background" title="Link to this heading"></a></h2>
<p>Functions as a Service (FaaS) is a framework for building serverless functions
that are ephemeral, automatically scalable, and focused pieces of code running
within containers to allow developers to focus on application code rather than
infrastructure nuances.</p>
<p>Many cloud service providers have ready-to-use FaaS offerings which offer a
high degree of convenience for developers and granular billing based on
per-second usage.</p>
<p>If you want an on-premise or self-hosted serverless capability to avoid vendor
lock-in or simply want more development, OpenFaaS is currently the most
popular solution in the space based on the number of Github stars on the
project.</p>
</section>
<section id="prerequisites">
<h2><a class="toc-backref" href="#id6" role="doc-backlink">Prerequisites</a><a class="headerlink" href="#prerequisites" title="Link to this heading"></a></h2>
<p>For simplicity, this tutorial assumes you have a Kubernetes single node
cluster with only master node running Clear Linux OS.</p>
<ul class="simple">
<li><p>For detailed instructions on how to install Clear Linux OS, see the <a class="reference internal" href="../get-started/index.html#get-started"><span class="std std-ref">getting
started</span></a> section.</p></li>
<li><p>For a detailed guide on how to set up Kubernetes, see the documentation on
<a class="reference internal" href="kubernetes.html#kubernetes"><span class="std std-ref">Kubernetes</span></a>.</p></li>
</ul>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Please note that in this example the <a class="reference external" href="https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/#control-plane-node-isolation">master node was tainted</a>
to be able to be scheduled, which means containers are able to be deployed
to the master node.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>kubectl<span class="w"> </span>taint<span class="w"> </span>nodes<span class="w"> </span>--all<span class="w"> </span>node-role.kubernetes.io/master-
</pre></div>
</div>
</div>
</section>
<section id="deploy-openfaas">
<h2><a class="toc-backref" href="#id7" role="doc-backlink">Deploy OpenFaaS</a><a class="headerlink" href="#deploy-openfaas" title="Link to this heading"></a></h2>
<ol class="arabic">
<li><p>Install the official command line tool for using OpenFaas, <em>faas-cli</em>, by
installing the <em>faas-cli</em> bundle.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>swupd<span class="w"> </span>bundle-add<span class="w"> </span>faas-cli
</pre></div>
</div>
</li>
<li><p>Download the <a class="reference external" href="https://github.com/openfaas/faas-netes">faas-netes</a>, the
OpenFaaS provider templates that enable Kubernetes for OpenFaaS.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>git<span class="w"> </span>clone<span class="w"> </span>https://github.com/openfaas/faas-netes.git
</pre></div>
</div>
</li>
<li><p>Set variables for the OpenFaaS admin user and password.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>For simplicity, this tutorial uses a basic authentication with an
unecrypted username and password. For production environments, see the
OpenFaaS documentation on <a class="reference external" href="https://docs.openfaas.com/architecture/production/#advanced-kubernetes-configuration">Deploying OpenFaas in Production</a>.</p>
</div>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nb">export</span><span class="w"> </span><span class="nv">FAAS_USER</span><span class="o">=</span>admin
<span class="nb">export</span><span class="w"> </span><span class="nv">FAAS_PASSWD</span><span class="o">=</span>clearlinux
</pre></div>
</div>
</li>
<li><p>Deploy the OpenFaaS stack on Kubernetes using <strong class="command">kubectl</strong>.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>kubectl<span class="w"> </span>apply<span class="w"> </span>-f<span class="w"> </span>faas-netes/namespaces.yml
</pre></div>
</div>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>kubectl<span class="w"> </span>-n<span class="w"> </span>openfaas<span class="w"> </span>create<span class="w"> </span>secret<span class="w"> </span>generic<span class="w"> </span>basic-auth<span class="w"> </span><span class="se">\</span>
--from-literal<span class="o">=</span>basic-auth-user<span class="o">=</span><span class="nv">$FAAS_USER</span><span class="w"> </span><span class="se">\</span>
--from-literal<span class="o">=</span>basic-auth-password<span class="o">=</span><span class="s2">&quot;</span><span class="nv">$FAAS_PASSWD</span><span class="s2">&quot;</span>
</pre></div>
</div>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>kubectl<span class="w"> </span>apply<span class="w"> </span>-f<span class="w"> </span>faas-netes/yaml/
</pre></div>
</div>
<p>Wait for the OpenFaaS pods and services to get ready. This involves
downloading container images from the Internet and may take some time
depending on your Internet connection. You can enter the commands below to
have the terminal wait until services are ready to use.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>kubectl<span class="w"> </span><span class="nb">wait</span><span class="w"> </span>--for<span class="o">=</span><span class="nv">condition</span><span class="o">=</span>available<span class="w"> </span>--timeout<span class="o">=</span>600s<span class="w"> </span>deployment/gateway<span class="w"> </span>-n<span class="w"> </span>openfaas
kubectl<span class="w"> </span><span class="nb">wait</span><span class="w"> </span>--for<span class="o">=</span><span class="nv">condition</span><span class="o">=</span>available<span class="w"> </span>--timeout<span class="o">=</span>600s<span class="w"> </span>deployment/faas-idler<span class="w"> </span>-n<span class="w"> </span>openfaas
</pre></div>
</div>
</li>
<li><p>Login to the OpenFaaS instance. <em>31112</em> is the default port.</p>
<ul>
<li><p>You can login over the command-line:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nb">export</span><span class="w"> </span><span class="nv">OPENFAAS_URL</span><span class="o">=</span>http://127.0.0.1:31112
<span class="nb">echo</span><span class="w"> </span>-n<span class="w"> </span><span class="nv">$FAAS_PASSWD</span><span class="w"> </span><span class="p">|</span><span class="w"> </span>faas-cli<span class="w"> </span>login<span class="w"> </span>--password-stdin
</pre></div>
</div>
</li>
<li><p>You can also login to the OpenFaaS web interface by navigating to
<strong>http://${master_node_IP}:31112</strong></p></li>
</ul>
<figure class="align-center" id="id2">
<a class="reference internal image-reference" href="../_images/openfaas-login.png"><img alt="OpenFaaS web interface login page" src="../_images/openfaas-login.png" style="width: 624.0px; height: 259.0px;" />
</a>
<figcaption>
<p><span class="caption-text">Figure 1: OpenFaaS web interface login page</span><a class="headerlink" href="#id2" title="Link to this image"></a></p>
</figcaption>
</figure>
</li>
</ol>
</section>
<section id="openfaas-templates">
<h2><a class="toc-backref" href="#id8" role="doc-backlink">OpenFaaS templates</a><a class="headerlink" href="#openfaas-templates" title="Link to this heading"></a></h2>
<p>OpenFaaS templates, though not necessary, abstract configurations for
running functions in common programming languages. Templates allows developers
to better focus on writing the code for their function.</p>
<p>OpenFaaS has dozens of <a class="reference external" href="https://github.com/openfaas/templates">templates in the official store</a>. There are also Clear Linux OS-based templates
available for download.</p>
<ol class="arabic">
<li><p>You can list all the official templates in the store using
<strong class="command">faas-cli</strong>.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>faas-cli<span class="w"> </span>template<span class="w"> </span>store<span class="w"> </span>list
NAME<span class="w"> </span>SOURCE<span class="w"> </span>DESCRIPTION
csharp<span class="w"> </span>openfaas<span class="w"> </span>Classic<span class="w"> </span>C#<span class="w"> </span>template
dockerfile<span class="w"> </span>openfaas<span class="w"> </span>Classic<span class="w"> </span>Dockerfile<span class="w"> </span>template
go<span class="w"> </span>openfaas<span class="w"> </span>Classic<span class="w"> </span>Golang<span class="w"> </span>template
java8<span class="w"> </span>openfaas<span class="w"> </span>Classic<span class="w"> </span>Java<span class="w"> </span><span class="m">8</span><span class="w"> </span>template
node<span class="w"> </span>openfaas<span class="w"> </span>Classic<span class="w"> </span>NodeJS<span class="w"> </span><span class="m">8</span><span class="w"> </span>template
php7<span class="w"> </span>openfaas<span class="w"> </span>Classic<span class="w"> </span>PHP<span class="w"> </span><span class="m">7</span><span class="w"> </span>template
python<span class="w"> </span>openfaas<span class="w"> </span>Classic<span class="w"> </span>Python<span class="w"> </span><span class="m">2</span>.7<span class="w"> </span>template
python3<span class="w"> </span>openfaas<span class="w"> </span>Classic<span class="w"> </span>Python<span class="w"> </span><span class="m">3</span>.6<span class="w"> </span>template
...
</pre></div>
</div>
</li>
<li><p>Create and enter a workspace.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>mkdir<span class="w"> </span>~/faas-example
<span class="nb">cd</span><span class="w"> </span>~/faas-example
</pre></div>
</div>
</li>
<li><p>Download the Clear Linux OS-based OpenFaaS templates which are stored in the
<a class="reference external" href="https://github.com/clearlinux/dockerfiles">https://github.com/clearlinux/dockerfiles</a> repository and copy them into your
working directory.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>git<span class="w"> </span>clone<span class="w"> </span>https://github.com/clearlinux/dockerfiles.git
cp<span class="w"> </span>-r<span class="w"> </span>dockerfiles/FaaS/OpenFaaS/template/<span class="w"> </span>.
</pre></div>
</div>
</li>
<li><p>After the Clear Linux OS based templates have been retrieved, they will show up in
the same repository and available to use locally.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>faas-cli<span class="w"> </span>new<span class="w"> </span>--list
Languages<span class="w"> </span>available<span class="w"> </span>as<span class="w"> </span>templates:
-<span class="w"> </span>dockerfile-clearlinux
-<span class="w"> </span>python3-clearlinux
</pre></div>
</div>
</li>
</ol>
<p>OpenFaaS is ready to use at this point. See the <a class="reference external" href="https://docs.openfaas.com">OpenFaaS documentation</a> to learn more about deploying functions.</p>
</section>
<section id="example-develop-a-function">
<h2><a class="toc-backref" href="#id9" role="doc-backlink">Example: Develop a function</a><a class="headerlink" href="#example-develop-a-function" title="Link to this heading"></a></h2>
<p>In this example, well imagine a FaaS solution where: a user provides a URL to
a pictures, which invokes a function to do image classification and outputs
the result.</p>
<p>We will use the <a class="reference external" href="https://software.intel.com/en-us/openvino-toolkit/">OpenVINO™ toolkit - Deep Learning Deployment Toolkit (DLDT)</a> to do the image
inference. As inference development is not the focus of this example, we will
just use the built-in sample “<a class="reference external" href="https://docs.openvinotoolkit.org/latest/_inference_engine_samples_classification_sample_async_README.html">classification_sample_async</a>
for this function.</p>
<p>Well use the <em>python3-clearlinux</em> template as a base and customize it by:</p>
<ul class="simple">
<li><p>Adding additional Clear Linux OS bundles (<em>bundles.txt</em>)</p></li>
<li><p>Adding additional required python packages (<em>requirements.txt</em>)</p></li>
<li><p>Adding a script to download and convert DLDT models (<em>helper_script.sh</em>)</p></li>
<li><p>Finally, well develop the python function to be run (<em>handler.py</em>)</p></li>
</ul>
<p>More ways to customize the Clear Linux OS based OpenFaaS templates can be found in the
<a class="reference external" href="https://github.com/clearlinux/dockerfiles/tree/master/FaaS/OpenFaaS/template#python3-clearlinux">README on GitHub</a>.</p>
<ol class="arabic">
<li><p>Enter the previously created working directory.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nb">cd</span><span class="w"> </span>~/faas-example
</pre></div>
</div>
</li>
<li><p>Create a new function skeleton</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>faas-cli<span class="w"> </span>new<span class="w"> </span>--lang<span class="w"> </span>python3-clearlinux<span class="w"> </span>classification-sample<span class="w"> </span>--prefix<span class="o">=</span><span class="s2">&quot;&lt;your docker name&gt;&quot;</span>
</pre></div>
</div>
<p>This will create the directory structure below:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">tree .</span>
<span class="go"> ├── classification-sample</span>
<span class="go"> │ ├── bundles.txt</span>
<span class="go"> │ ├── handler.py</span>
<span class="go"> │ ├── helper_script.sh</span>
<span class="go"> │ ├── __init__.py</span>
<span class="go"> │ └── requirements.txt</span>
<span class="go"> ├── classification-sample.yml</span>
</pre></div>
</div>
</li>
<li><p>Add the required Clear Linux OS bundles to the <code class="file docutils literal notranslate"><span class="pre">bundles.txt</span></code> file.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;computer-vision-openvino&quot;</span><span class="w"> </span>&gt;&gt;<span class="w"> </span>classification-sample/bundles.txt
</pre></div>
</div>
</li>
<li><p>Add the required python packages to the <code class="file docutils literal notranslate"><span class="pre">requirements.txt</span></code> file.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;glob3&quot;</span><span class="w"> </span>&gt;&gt;<span class="w"> </span>classification-sample/requirements.txt
<span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;urllib3&quot;</span><span class="w"> </span>&gt;&gt;<span class="w"> </span>classification-sample/requirements.txt
<span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;networkx==2.3&quot;</span><span class="w"> </span>&gt;&gt;<span class="w"> </span>classification-sample/requirements.txt
</pre></div>
</div>
</li>
<li><p>OpenCV has a <a class="reference external" href="https://github.com/opencv/open_model_zoo/tree/master/tools/downloader">model downloader and other automation tools</a>
to help downloading models and converting them into different formats.
Customize the OpenFaaS template to use the model-downloader in the
<code class="file docutils literal notranslate"><span class="pre">helper_script.sh</span></code> file. The <code class="file docutils literal notranslate"><span class="pre">helper_script.sh</span></code> file script
gets executed during the build process.</p>
<div class="literal-block-wrapper docutils container" id="helper-script-sh">
<div class="code-block-caption"><span class="caption-text">helper_script.sh</span><a class="headerlink" href="#helper-script-sh" title="Link to this code"></a></div>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>cat<span class="w"> </span>classification-sample/helper_script.sh
<span class="c1">#!/bin/bash</span>
<span class="c1"># Download and convert models</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">MODEL_DIR</span><span class="o">=</span><span class="s2">&quot;/models&quot;</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">MO_PATH</span><span class="o">=</span><span class="s2">&quot;/usr/share/openvino/model-optimizer/mo.py&quot;</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">MODEL_NAME</span><span class="o">=</span><span class="s2">&quot;resnet-50-int8-tf-0001&quot;</span>
<span class="c1"># Download and convert models</span>
model-downloader<span class="w"> </span>--name<span class="w"> </span><span class="nv">$MODEL_NAME</span><span class="w"> </span>-o<span class="w"> </span><span class="nv">$MODEL_DIR</span>
model-converter<span class="w"> </span>--name<span class="w"> </span><span class="nv">$MODEL_NAME</span><span class="w"> </span>-d<span class="w"> </span><span class="nv">$MODEL_DIR</span><span class="w"> </span>-o<span class="w"> </span><span class="nv">$MODEL_DIR</span><span class="w"> </span>--mo<span class="w"> </span><span class="nv">$MO_PATH</span>
</pre></div>
</div>
</div>
</li>
<li><p>With the requirements added to the template. Write a python in the
<code class="file docutils literal notranslate"><span class="pre">handler.py</span></code> file. This function will parse the input picture URL,
find the model path, and call “classification_sample_async” to do image
classification.</p>
<div class="literal-block-wrapper docutils container" id="handler-py">
<div class="code-block-caption"><span class="caption-text">handler.py</span><a class="headerlink" href="#handler-py" title="Link to this code"></a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/python3</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">glob</span>
<span class="kn">import</span> <span class="nn">urllib.request</span>
<span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">urlparse</span>
<span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="n">splitext</span>
<span class="n">ALLOWED_IMAGE_TYPE</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;.bmp&quot;</span><span class="p">,</span> <span class="s2">&quot;.BMP&quot;</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">get_ext</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
<span class="n">parsed</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="n">_</span><span class="p">,</span> <span class="n">ext</span> <span class="o">=</span> <span class="n">splitext</span><span class="p">(</span><span class="n">parsed</span><span class="o">.</span><span class="n">path</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ext</span>
<span class="k">def</span> <span class="nf">get_image_from_url</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;get image and save to local path&quot;&quot;&quot;</span>
<span class="n">ext</span> <span class="o">=</span> <span class="n">get_ext</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="n">local_file_path</span> <span class="o">=</span> <span class="s2">&quot;/tmp/image&quot;</span> <span class="o">+</span> <span class="n">ext</span>
<span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlretrieve</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">local_file_path</span><span class="p">)</span>
<span class="k">return</span> <span class="n">local_file_path</span>
<span class="k">def</span> <span class="nf">find_model_path</span><span class="p">():</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot; return model xml path &quot;&quot;&quot;</span>
<span class="n">model_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s1">&#39;MODEL_DIR&#39;</span><span class="p">,</span> <span class="s1">&#39;/models&#39;</span><span class="p">)</span>
<span class="n">model_name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s1">&#39;MODEL_NAME&#39;</span><span class="p">,</span> <span class="s1">&#39;resnet-50-int8-tf-0001&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;.xml&quot;</span>
<span class="n">precision</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s1">&#39;MODEL_PRECISION&#39;</span><span class="p">,</span> <span class="s1">&#39;FP32&#39;</span><span class="p">)</span>
<span class="n">pattern</span> <span class="o">=</span> <span class="n">model_dir</span> <span class="o">+</span> <span class="s1">&#39;/**/&#39;</span> <span class="o">+</span> <span class="n">precision</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">model_name</span>
<span class="n">paths</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">paths</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;No &quot;</span> <span class="o">+</span> <span class="n">model_name</span> <span class="o">+</span> <span class="s2">&quot; found&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="k">return</span> <span class="n">paths</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">do_classification</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">model_path</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot; Use dldt sample classification_sample_async &quot;&quot;&quot;</span>
<span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;classification_sample_async -i &quot;</span> <span class="o">+</span> <span class="n">image</span> <span class="o">+</span> <span class="s2">&quot; -m &quot;</span> <span class="o">+</span> <span class="n">model_path</span>
<span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">handle</span><span class="p">(</span><span class="n">req</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;handle a request to the function</span>
<span class="sd"> Args:</span>
<span class="sd"> req (str): request body</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">req</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Request body is missing.&quot;</span><span class="p">)</span>
<span class="k">return</span>
<span class="n">model_path</span> <span class="o">=</span> <span class="n">find_model_path</span><span class="p">()</span>
<span class="k">if</span> <span class="n">model_path</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span>
<span class="k">if</span> <span class="n">get_ext</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">ALLOWED_IMAGE_TYPE</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Only &quot;</span> <span class="o">+</span> <span class="n">ALLOWED_IMAGE_TYPE</span> <span class="o">+</span> <span class="s2">&quot; images are allowed.&quot;</span><span class="p">)</span>
<span class="k">return</span>
<span class="n">file_path</span> <span class="o">=</span> <span class="n">get_image_from_url</span><span class="p">(</span><span class="n">req</span><span class="p">)</span>
<span class="n">do_classification</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="n">model_path</span><span class="p">)</span>
</pre></div>
</div>
</div>
</li>
<li><p>Build and deploy the function to the OpenFaaS instance.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>faas-cli<span class="w"> </span>build<span class="w"> </span>--build-arg<span class="w"> </span><span class="nv">http_proxy</span><span class="o">=</span><span class="nv">$http_proxy</span><span class="w"> </span>--build-arg<span class="w"> </span><span class="nv">https_proxy</span><span class="o">=</span><span class="nv">$https_proxy</span><span class="w"> </span>-f<span class="w"> </span>classification-sample.yml
faas-cli<span class="w"> </span>deploy<span class="w"> </span>--env<span class="o">=</span><span class="nv">http_proxy</span><span class="o">=</span><span class="nv">$http_proxy</span><span class="w"> </span>--env<span class="o">=</span><span class="nv">https_proxy</span><span class="o">=</span><span class="nv">$https_proxy</span><span class="w"> </span>-f<span class="w"> </span>classification-sample.yml
</pre></div>
</div>
</li>
<li><p>Finally, test the function by going to the OpenFaaS web interface at
<strong>http://${master_node_IP}:31112</strong> and Invoking the <em>classification-sample</em>
function with a URL to any BMP image. The result should show the what the
image has been identified as and probability.</p>
<figure class="align-center" id="id3">
<a class="reference internal image-reference" href="../_images/openfaas-invoke-function.png"><img alt="OpenFaaS web interface invoke function" src="../_images/openfaas-invoke-function.png" style="width: 624.0px; height: 324.0px;" />
</a>
<figcaption>
<p><span class="caption-text">Figure 2: OpenFaaS web interface invoke function</span><a class="headerlink" href="#id3" title="Link to this image"></a></p>
</figcaption>
</figure>
<figure class="align-center" id="id4">
<a class="reference internal image-reference" href="../_images/openfaas-function-output.png"><img alt="OpenFaaS web interface function output" src="../_images/openfaas-function-output.png" style="width: 624.0px; height: 304.0px;" />
</a>
<figcaption>
<p><span class="caption-text">Figure 3: OpenFaaS web interface invoke function</span><a class="headerlink" href="#id4" title="Link to this image"></a></p>
</figcaption>
</figure>
</li>
</ol>
<p><em>Intel, OpenVINO, and the Intel logo are trademarks of Intel Corporation or its subsidiaries.</em></p>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../index.html">
<img class="logo" src="../_static/clearlinux.png" alt="Logo of Clear Linux* Project Docs"/>
</a></p>
<div>
<h3><a href="../index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">OpenFaaS*</a><ul>
<li><a class="reference internal" href="#background">Background</a></li>
<li><a class="reference internal" href="#prerequisites">Prerequisites</a></li>
<li><a class="reference internal" href="#deploy-openfaas">Deploy OpenFaaS</a></li>
<li><a class="reference internal" href="#openfaas-templates">OpenFaaS templates</a></li>
<li><a class="reference internal" href="#example-develop-a-function">Example: Develop a function</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="nvidia-cuda.html"
title="previous chapter">NVIDIA* CUDA Toolkit</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="php.html"
title="next chapter">PHP and PHP-FPM</a></p>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/tutorials/openfaas.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="php.html" title="PHP and PHP-FPM"
>next</a> |</li>
<li class="right" >
<a href="nvidia-cuda.html" title="NVIDIA* CUDA Toolkit"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Documentation for Clear Linux* project</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="index.html" >Tutorials</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">OpenFaaS*</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022 Intel Corporation. All Rights Reserved..
Last updated on Nov 04, 2024.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
</div>
</body>
</html>