0
点赞
收藏
分享

微信扫一扫

OpenFaaS 入门openfaas cli 配置与使用

unadlib 2022-05-09 阅读 48

本文将探索如何通过openfaas cli (客户端)安装并使用受支持的语言node.js模板之一创建、构建、部署和调用。我们将为工作流程的每个部分使用OpenFaaS CLI。

什么是 OpenFaas?

OpenFaaS 入门openfaas cli 配置与使用_github

OpenFaaS 是一个框架,用于在任何平台(Windows 或 Linux)上将代码、二进制文件或容器打包为无服务器函数。​​访问网站​​

在接下来的几分钟内,我们将:

  • 从代码模板创建函数
  • 将函数构建为 Docker 映像
  • 将镜像推送到 Docker 注册表
  • 部署函数
  • 调用函数

先决条件

在开始之前,您应该使用部署指南在您的笔记本电脑或集群上设置 OpenFaaS:

安装openfaas cli (客户端) 

注意:此处演示为linux系统中安装openfaas cli 如果是在windows系统,请去github中下载windows系统版本的openfaas-cli.exe 文件安装可参考:​​https://github.com/openfaas/faas-cli/releases​​

curl -sL https://cli.openfaas.com | sudo sh

注意:要确保开发机器上能正常访问github

安装成功的界面截图

OpenFaaS 入门openfaas cli 配置与使用_node.js_02

验证openfaas cli 

安装完成后,执行命令faas-cli 确认,如果是在windows 系统中就在cmd 中验证.

[root@localhost ~]# faas-cli
___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|


Manage your OpenFaaS functions from the command line

Usage:
faas-cli [flags]
faas-cli [command]

Available Commands:
auth Obtain a token for your OpenFaaS gateway
build Builds OpenFaaS function containers
cloud OpenFaaS Cloud commands
completion Generates shell auto completion
deploy Deploy OpenFaaS functions
describe Describe an OpenFaaS function
generate Generate Kubernetes CRD YAML file
help Help about any command
invoke Invoke an OpenFaaS function
list List OpenFaaS functions
login Log in to OpenFaaS gateway
logout Log out from OpenFaaS gateway
logs Fetch logs for a functions
namespaces List OpenFaaS namespaces
new Create a new template in the current folder with the name given as name
publish Builds and pushes multi-arch OpenFaaS container images
push Push OpenFaaS functions to remote registry (Docker Hub)
registry-login Generate and save the registry authentication file
remove Remove deployed OpenFaaS functions
secret OpenFaaS secret commands
store OpenFaaS store commands
template OpenFaaS template store and pull commands
up Builds, pushes and deploys OpenFaaS function containers
version Display the clients version information

Flags:
--filter string Wildcard to match with function names in YAML file
-h, --help help for faas-cli
--regex string Regex to match with function names in YAML file
-f, --yaml string Path to YAML file describing function(s)

Use "faas-cli [command] --help" for more information about a command.
[root@localhost ~]#

配置openfaas cli 开发环境

注意:这里必需要操作,否则默认是连接到openfaas 地址为开发机器本机地址。

echo export OPENFAAS_URL=192.168.10.12:31112 >> ~/.bashrc
source ~/.bashrc

配置openfaas cli 开发环境连接至openfaas 平台

登录至openfaas 平台

faas-cli login -u admin -p admin

登录成功,可以执行客户端命令以验证是否正常登录到openfaas 平台中

[root@localhost dev_node]# faas-cli list
Function Invocations Replicas
nodeinfo 7 1

openfaas 平台UI界面展示信息对比

OpenFaaS 入门openfaas cli 配置与使用_docker_03

创建node.js 应用

创建一个新的 Node.js 函数应用

[root@localhost dev_node]# faas-cli new callme --lang node
2022/05/09 17:39:48 No templates found in current directory.
2022/05/09 17:39:48 Attempting to expand templates from https://github.com/openfaas/templates.git
2022/05/09 17:39:51 Fetched 16 template(s) : [csharp dockerfile go java11 java11-vert-x node node12 node12-debian node14 node16 node17 php7 python python3 python3-debian ruby] from https://github.com/openfaas/templates.git
Folder: callme created.
___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|


Function created in folder: callme
Stack file written: callme.yml

Notes:
You have created a new function which uses Node.js 12.13.0 and the OpenFaaS
Classic Watchdog.

npm i --save can be used to add third-party packages like request or cheerio
npm documentation: https://docs.npmjs.com/

For high-throughput services, we recommend you use the node12 template which
uses a different version of the OpenFaaS watchdog.

[root@localhost dev_node]#

检查创建的应用

[root@localhost dev_node]# ll
total 12
drwx------ 2 root root 4096 May 9 17:39 callme
-rw------- 1 root root 157 May 9 17:39 callme.yml
drwxr-xr-x 18 root root 4096 May 9 17:39 template
[root@localhost dev_node]#

构建函数

注意:需要先编辑callme.yml。且YAML文件中将image行设置为您私有的docker 镜像仓库存放.

OpenFaaS 入门openfaas cli 配置与使用_node.js_04

faas-cli build -f callme.yml

推送你的应用代码

faas-cli push -f callme.yml

推送到指定镜像仓库完成后,我们就可以部署和运行该功能了

部署应用

faas-cli deploy -f callme.yml

调用应用

faas-cli invoke -f callme.yml callme

列出应用

faas-cli list

删除应用

faas-cli rm -f callme.yml

上述操作也可去OpenFaaS UI 控制台界面中操作

举报

相关推荐

0 条评论