本文最后编辑于 前,其中的内容可能需要更新。
faasd简介 FaaS
这个词相信大家有所耳闻,而OpenFaaS
就是一款相当流行的FaaS框架,提供了多个版本,可以在kubernetes上运行,也能在其他部署了容器运行时的地方运行,faasd
是他的轻量级的serverless,方便到可以在你的树莓派上体验serverless。
部署faasd 官方提供了脚本,实际上一键执行即可,一般不会有什么问题,如果你使用CentOS7
这种比较老旧的系统的话,推荐你先升级内核再进行部署。升级内核的方法我说过很多次,不再赘述。
1 2 3 4 # 没git的机器记得安装git git clone https://github.com/openfaas/faasd --depth=1 cd faasd ./hack/install.sh
没错 这就部署完成了,是不是非常简单。
过一会他会弹出一些信息,你可以获取到你的用户名和密码,存放在/var/lib/faasd/secrets/
下。
目前来看试了几次都没出什么问题,唯一可能造成影响的问题应该是网络问题,国内访问github不太稳定,而很多资源需要在github拉取,这个的话可以挂代理也可以自己手动去部署。
部署完成后,访问对应服务器的8080端口就可以看见openfaas的ui了。
查看容器相关信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ ctr --namespace openfaas task list TASK PID STATUS nats 1198 RUNNING prometheus 1329 RUNNING basic-auth-plugin 1427 RUNNING gateway 1533 RUNNING queue-worker 1639 RUNNING $ ctr --namespace openfaas container list CONTAINER IMAGE RUNTIME basic-auth-plugin ghcr.io/openfaas/basic-auth:0.21.0 io.containerd.runc.v2 gateway ghcr.io/openfaas/gateway:0.21.0 io.containerd.runc.v2 nats docker.io/library/nats-streaming:0.22.0 io.containerd.runc.v2 prometheus docker.io/prom/prometheus:v2.14.0 io.containerd.runc.v2 queue-worker ghcr.io/openfaas/queue-worker:0.12.2 io.containerd.runc.v2
基本使用 faas-cli工具 faasd部署完成后,就可以使用faas-cli
工具了。
这个工具也可以在其他机器上使用,连接远程的openfaas(需要先执行export OPENFAAS_URL=http://IP:PORT
)
因为web-ui比较简单,所以这边就不提了。
使用前需要先进行登陆操作:
1 2 3 4 faas-cli login --username=admin --password=`cat /var/lib/faasd/secrets/basic-auth-password` WARNING! Using --password is insecure, consider using: cat ~/faas_pass.txt | faas-cli login -u user --password-stdin Calling the OpenFaaS server to validate the credentials... credentials saved for admin http://127.0.0.1:8080
简单部署一个服务测试一下:
1 2 3 4 $ faas-cli deploy --image=alexellis/faas-url-ping --name=url-ping # 部署faas-cli命令提供的测试命令 Deployed. 200 OK. URL: http://127.0.0.1:8080/function/url-ping
可以看到很快就部署ok
1 2 3 4 5 curl http://127.0.0.1:8080/function/url-ping Handle this -> Give me a URL and I'll ping it for you. # 测试的这个镜像接收一个url 返回解析结果 # 此处不再演示
动手部署一个自己的faas服务 当然不可能自己从头开始自己做,可以看看提供的模板:
部分模板不是官方的,如我们现在准备用的rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 $ faas-cli template store list # 列出模板列表,可能需要科学上网 NAME SOURCE DESCRIPTION csharp openfaas Classic C# template dockerfile openfaas Classic Dockerfile template go openfaas Classic Golang template java8 openfaas Java 8 template java11 openfaas Java 11 template java11-vert-x openfaas Java 11 Vert.x template node14 openfaas HTTP-based Node 14 template node12 openfaas HTTP-based Node 12 template node openfaas Classic NodeJS 8 template php7 openfaas Classic PHP 7 template python openfaas Classic Python 2.7 template python3 openfaas Classic Python 3.6 template python3-dlrs intel Deep Learning Reference Stack v0.4 for ML workloads ruby openfaas Classic Ruby 2.5 template ruby-http openfaas Ruby 2.4 HTTP template python27-flask openfaas Python 2.7 Flask template python3-flask openfaas Python 3.7 Flask template python3-flask-debian openfaas Python 3.7 Flask template based on Debian python3-http openfaas Python 3.7 with Flask and HTTP python3-http-debian openfaas Python 3.7 with Flask and HTTP based on Debian golang-http openfaas Golang HTTP template golang-middleware openfaas Golang Middleware template python3-debian openfaas Python 3 Debian template powershell-template openfaas-incubator Powershell Core Ubuntu:16.04 template powershell-http-template openfaas-incubator Powershell Core HTTP Ubuntu:16.04 template rust booyaa Rust template crystal tpei Crystal template csharp-httprequest distantcam C# HTTP template csharp-kestrel burtonr C# Kestrel HTTP template vertx-native pmlopes Eclipse Vert.x native image template swift affix Swift 4.2 Template lua53 affix Lua 5.3 Template vala affix Vala Template vala-http affix Non-Forking Vala Template quarkus-native pmlopes Quarkus.io native image template perl-alpine tmiklas Perl language template based on Alpine image crystal-http koffeinfrei Crystal HTTP template rust-http openfaas-incubator Rust HTTP template bash-streaming openfaas-incubator Bash Streaming template cobol devries COBOL Template
openfaas的模板列表类似于helm
的镜像仓库,它提供各种模板
拉取也比较简单:
1 2 3 4 5 faas-cli template store pull rust # 从库拉取模板 Fetch templates from repository: https://github.com/openfaas-incubator/openfaas-rust-template at 2021/12/22 12:33:30 Attempting to expand templates from https://github.com/openfaas-incubator/openfaas-rust-template 2021/12/22 12:33:31 Fetched 1 template(s) : [rust] from https://github.com/openfaas-incubator/openfaas-rust-template
拉取完成后可以看到当前目录下有一个template
文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 tree . . └── template └── rust ├── Dockerfile ├── function │ ├── Cargo.toml │ └── src │ └── lib.rs ├── main │ ├── Cargo.toml │ └── src │ └── main.rs └── template.yml 6 directories, 6 files
创建faas-cli新项目:
1 2 3 4 5 6 7 8 9 10 11 12 faas-cli new test --lang rust --prefix $YOUR_REGISTRY_URL Folder: test created. ___ _____ ____ / _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___| | | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \ | |_| | |_) | __/ | | | _| (_| | (_| |___) | \___/| .__/ \___|_| |_|_| \__,_|\__,_|____/ |_| Function created in folder: test Stack file written: test.yml
此时目录结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 tree . . ├── template │ └── rust │ ├── Dockerfile │ ├── function │ │ ├── Cargo.toml │ │ └── src │ │ └── lib.rs │ ├── main │ │ ├── Cargo.toml │ │ └── src │ │ └── main.rs │ └── template.yml ├── test │ ├── Cargo.toml │ └── src │ └── lib.rs └── test.yml 8 directories, 9 files
为了做区分,稍微修改一下rust函数返回的内容(test/lib.rs),这个函数会在我们在body
中发送的内容前面添加hello 并且返回。
1 2 3 4 pub fn handle (req : String ) -> String { let s = format! ("hello {}" ,req); s.to_string() }
如果你是在其他机器上远程连到faasd,并且机器上有docker
,那么可以直接使用faas-cli build -f test.yaml
来构建并推送镜像,这边由于是同一台机器,所以采用kaniko
来构建,标准的构建上下文通过faas-cli build --shrinkwrap -f test.yml
生成:
PS: 这边为了省力使用了阿里云的镜像仓库,也可以使用自建的私有仓库,参考此处
1 2 3 4 5 6 7 8 9 10 11 12 13 14 faas-cli build --shrinkwrap -f test.yml #生成构建上下文 [0] > Building test. Clearing temporary build folder: ./build/test/ Preparing: ./test/ build/test/function Building: registry.cn-hangzhou.aliyuncs.com/scc-image/test:latest with rust template. Please wait.. test shrink-wrapped to ./build/test/ [0] < Building test done in 0.00s. [0] Worker done. Total build time: 0.00s # # 此处使用官方的kaniko镜像,如果网络有一些问题的话docker hub也有很多同步的镜像,可以使用dockerhub提供的镜像。如果想要定制镜像可以修改build下的test /Dockerfile nerdctl run -v $PWD/build/test:/workspace -v ~/.docker/config.json:/kaniko/config.json --env DOCKER_CONFIG=/kaniko gcr.io/kaniko-project/executor:latest -d $YOUR_REGISTRY_URL/test:latest --force
推送完成后手动部署
1 2 3 4 5 6 7 8 9 cp -r ~/.docker /var/lib/faasd faas-cli deploy -f test.yml Deploying: test. Function test already exists, attempting rolling-update. Deployed. 200 OK. URL: http://127.0.0.1:8080/function/test
简单测试:
1 2 curl http://192.168.188.128:8080/function/test -d 123123 hello 123123
OK,非常完美
参考链接 https://docs.openfaas.com
https://github.com/openfaas/faas-cli