OpenFaaS架构——openfaas-cli

879 阅读6分钟

openfaas-cli是openfaas提供的命令行工具,他能够直接和gateway交互,且提供了很多语言的构造模板。相当于openfaas的开发工具。

本文通过一个python的例子来记录openfaas-cli的操作流程。

新建函数模板

执行命令cli new text-parser --lang python --quiet 该命令的含义是创建一个新的python的函数模板,且跳过模板注释。

编写函数体

编辑handler文件,让它返回一个字符串。

构建函数

执行命令cli build -f ./text-parser.yml

[0] > Building text-parser.
Clearing temporary build folder: ./build/text-parser/
Preparing: ./text-parser/ build/text-parser/function
Building: text-parser:latest with python template. Please wait..
Sending build context to Docker daemon  8.192kB
Step 1/29 : FROM openfaas/classic-watchdog:0.18.1 as watchdog
0.18.1: Pulling from openfaas/classic-watchdog
ff01b6ed694a: Pulling fs layer
ff01b6ed694a: Verifying Checksum
ff01b6ed694a: Download complete
ff01b6ed694a: Pull complete
Digest: sha256:35bda3fcffdd89c21f990f240434799901c80627280eca86eec56db71b8bb30d
Status: Downloaded newer image for openfaas/classic-watchdog:0.18.1
 ---> 94b5e0bef891
Step 2/29 : FROM python:2.7-alpine
2.7-alpine: Pulling from library/python
aad63a933944: Pulling fs layer
259d822268fb: Pulling fs layer
10ba96d218d3: Pulling fs layer
44ba9f6a4209: Pulling fs layer
44ba9f6a4209: Waiting
259d822268fb: Download complete
44ba9f6a4209: Verifying Checksum
44ba9f6a4209: Download complete
10ba96d218d3: Retrying in 5 seconds
aad63a933944: Download complete
aad63a933944: Pull complete
259d822268fb: Pull complete
10ba96d218d3: Retrying in 4 seconds
10ba96d218d3: Retrying in 3 seconds
10ba96d218d3: Retrying in 2 seconds
10ba96d218d3: Retrying in 1 second
10ba96d218d3: Verifying Checksum
10ba96d218d3: Download complete
10ba96d218d3: Pull complete
44ba9f6a4209: Pull complete
Digest: sha256:724d0540eb56ffaa6dd770aa13c3bc7dfc829dec561d87cb36b2f5b9ff8a760a
Status: Downloaded newer image for python:2.7-alpine
 ---> 8579e446340f
Step 3/29 : ARG ADDITIONAL_PACKAGE
 ---> Running in 51d699fc9a1b
Removing intermediate container 51d699fc9a1b
 ---> 17f8cd50b251
Step 4/29 : COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
 ---> 834b8ecccc88
Step 5/29 : RUN chmod +x /usr/bin/fwatchdog
 ---> Running in b1ff2a2a3233
Removing intermediate container b1ff2a2a3233
 ---> fc26e5f38dca
Step 6/29 : RUN apk --no-cache add ca-certificates ${ADDITIONAL_PACKAGE}
 ---> Running in 8d7876773504
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
OK: 11 MiB in 32 packages
Removing intermediate container 8d7876773504
 ---> 5bf8b148cc61
Step 7/29 : RUN addgroup -S app && adduser app -S -G app
 ---> Running in 60bd5ec381a1
Removing intermediate container 60bd5ec381a1
 ---> eb735a55e698
Step 8/29 : WORKDIR /home/app/
 ---> Running in 7ca803a53bc2
Removing intermediate container 7ca803a53bc2
 ---> d45361f556ed
Step 9/29 : COPY index.py           .
 ---> f31ca2cc5f53
Step 10/29 : COPY requirements.txt   .
 ---> 27fb6dacffe2
Step 11/29 : RUN chown -R app /home/app &&   mkdir -p /home/app/python && chown -R app /home/app
 ---> Running in d64310d180da
Removing intermediate container d64310d180da
 ---> 147cecde5534
Step 12/29 : USER app
 ---> Running in fce89d455128
Removing intermediate container fce89d455128
 ---> 38f1ab89c7ab
Step 13/29 : ENV PATH=$PATH:/home/app/.local/bin:/home/app/python/bin/
 ---> Running in be182290e546
Removing intermediate container be182290e546
 ---> 80cb0bde8045
Step 14/29 : ENV PYTHONPATH=$PYTHONPATH:/home/app/python
 ---> Running in 2b7df7efea62
Removing intermediate container 2b7df7efea62
 ---> f2585b81b98b
Step 15/29 : RUN pip install -r requirements.txt --target=/home/app/python
 ---> Running in 0beda935cc6f
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Removing intermediate container 0beda935cc6f
 ---> 25f8bef2cd3e
Step 16/29 : RUN mkdir -p function
 ---> Running in 54e4c256fe0f
Removing intermediate container 54e4c256fe0f
 ---> 5287edf63565
Step 17/29 : RUN touch ./function/__init__.py
 ---> Running in 61d2af017939
Removing intermediate container 61d2af017939
 ---> 36a66a2a3fe7
Step 18/29 : WORKDIR /home/app/function/
 ---> Running in 34a24e02df5f
Removing intermediate container 34a24e02df5f
 ---> 9fa5edcf129e
Step 19/29 : COPY function/requirements.txt	.
 ---> 884c9ee630ff
Step 20/29 : RUN pip install -r requirements.txt --target=/home/app/python
 ---> Running in 2afe139960e1
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
WARNING: You are using pip version 20.0.2; however, version 20.2.3 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 2afe139960e1
 ---> 9cf033e63b46
Step 21/29 : WORKDIR /home/app/
 ---> Running in b830f8f259d4
Removing intermediate container b830f8f259d4
 ---> a52d7fb33661
Step 22/29 : USER root
 ---> Running in 1e6eeb130411
Removing intermediate container 1e6eeb130411
 ---> de5273fde695
Step 23/29 : COPY function           function
 ---> 0b0fd2038ada
Step 24/29 : RUN chown -R app:app ./ &&   chmod -R 777 /home/app/python
 ---> Running in d6757d300e4c
Removing intermediate container d6757d300e4c
 ---> f7d82b533e89
Step 25/29 : USER app
 ---> Running in 0c234791af91
Removing intermediate container 0c234791af91
 ---> f2f4227e18bb
Step 26/29 : ENV fprocess="python index.py"
 ---> Running in d0df97d6e72a
Removing intermediate container d0df97d6e72a
 ---> 3f9f69c3ceb6
Step 27/29 : EXPOSE 8080
 ---> Running in cfe61e4cb8be
Removing intermediate container cfe61e4cb8be
 ---> 7f2114295ba5
Step 28/29 : HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
 ---> Running in c890f0cfb318
Removing intermediate container c890f0cfb318
 ---> a0ce61ab2be5
Step 29/29 : CMD ["fwatchdog"]
 ---> Running in 59afd9cff302
Removing intermediate container 59afd9cff302
 ---> 72d8c9f635f2
Successfully built 72d8c9f635f2
Successfully tagged text-parser:latest
Image: text-parser:latest built.
[0] < Building text-parser done in 245.75s.
[0] Worker done.

Total build time: 245.75s

在构建结束后,整个最后的文件夹结构如下所示:

将handler构建到本地Docker镜像仓库中,如下图所示:

这里因为是基于kunernetes的多节点部署,所以得把打包好的镜像推送到公共仓库(这里是推送到了dockerhub),让所有可调度节点都能下载镜像,然后修改text-parser.yml中的镜像地址。

version: 1.0
provider:
  name: openfaas
  gateway: http://10.254.108.206:8080
functions:
  text-parser:
    lang: python
    handler: ./text-parser
    image: [dockerhub仓库名]/text-parser:0.0.1

部署函数

部署函数cli deploy -f ./text-parser.yml

可以看到部署成功后的组件

进行调用测试

开源版本的openfaas-cli虽然原理上支持部署到任意命名空间,但是由于openfaas-cli部署函数是将请求构造成规范的参数然后发往gateway,而gateway中又是固定使用openfaas-fn这个命名空间。如果强行部署到到其他命名空间因为准入问题会导致部署失败。

unable create Deployment: deployments.apps is forbidden: User "system:serviceaccount:openfaas:openfaas-controller" cannot create resource "deployments" in API group "apps" in the namespace "text"

在gateway中,openfaas-fn这个命名空间作为环境变量


functionURLResolver = handlers.FunctionAsHostBaseURLResolver{
			FunctionSuffix:    config.DirectFunctionsSuffix,
			FunctionNamespace: config.Namespace,
		}