第一篇章、OpenFaas 部署及Helloworld开发
2022-06-22 本文已影响0人
onmeiei
一、kubernetes
kubernetes现在已经成了环境管理及容器调度的实时标准了。openfaas的部署章节,推荐使用kubernetes进行部署和使用。
建议使用kubernetes
kubernetes的搭建可以使用kubeadm进行搭建,比较简单。
此处不赘述了。
二、使用helm进行部署
主要参考以下文档,里面贴心的提供了helm的安装命令。
faas-netes/README.md at master · openfaas/faas-netes (github.com)
只摘取核心命令如下:
- 创建两个kubernetes namespace
- openfaas:用于核心组件
- openfaas-fn:用于functions
kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
- 添加helm仓库
helm repo add openfaas https://openfaas.github.io/faas-netes/
- 更新仓库并部署
helm repo add openfaas https://openfaas.github.io/faas-netes/
部署完毕后,就是等待过程了
$ kubectl -n openfaas get all
NAME READY STATUS RESTARTS AGE
pod/alertmanager-7c68b7b5ff-bvcwr 1/1 Running 0 106m
pod/basic-auth-plugin-7744f57bfd-gxb8f 1/1 Running 0 106m
pod/gateway-6df6975b8d-rdfws 2/2 Running 0 106m
pod/nats-647b476664-dvwqv 1/1 Running 0 106m
pod/prometheus-b9696f886-t2lqz 1/1 Running 0 106m
pod/queue-worker-754898d5c5-rb79v 1/1 Running 1 (104m ago) 106m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/alertmanager ClusterIP 10.12.104.144 <none> 9093/TCP 106m
service/basic-auth-plugin ClusterIP 10.12.93.126 <none> 8080/TCP 106m
service/gateway ClusterIP 10.12.34.140 <none> 8080/TCP 106m
service/gateway-external NodePort 10.12.93.55 <none> 8080:31112/TCP 106m
service/nats ClusterIP 10.12.255.250 <none> 4222/TCP 106m
service/prometheus ClusterIP 10.12.192.19 <none> 9090/TCP 106m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/alertmanager 1/1 1 1 106m
deployment.apps/basic-auth-plugin 1/1 1 1 106m
deployment.apps/gateway 1/1 1 1 106m
deployment.apps/nats 1/1 1 1 106m
deployment.apps/prometheus 1/1 1 1 106m
deployment.apps/queue-worker 1/1 1 1 106m
NAME DESIRED CURRENT READY AGE
replicaset.apps/alertmanager-7c68b7b5ff 1 1 1 106m
replicaset.apps/basic-auth-plugin-7744f57bfd 1 1 1 106m
replicaset.apps/gateway-6df6975b8d 1 1 1 106m
replicaset.apps/nats-647b476664 1 1 1 106m
replicaset.apps/prometheus-b9696f886 1 1 1 106m
replicaset.apps/queue-worker-754898d5c5 1 1 1 106m
$
可以看到已经部署了NodePort service,暴露端口为31112
,可以通过http://<node-ip>:31112
打开gateway的管理界面了。
三、打开gateway管理界面
打开界面之前,先查一下密码。
$ PASSWORD=$(kubectl -n openfaas get secret basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode) && \
echo "OpenFaaS admin password: $PASSWORD"
OpenFaaS admin password: xxxxxxxxxx
记住这个密码,打开gateway界面,用户名为admin,密码为上面查到内容。
可以先从商城下载一个部署尝鲜一下。
空空如也的管理界面
点击
Deploy New Function
搜索env,选中,并点击`deploy`按钮
点击`INVOKE`按钮就可以调用了
接下来就是如何开发和部署自己的function了。
四、开发和部署function
安装cli工具
首先,需要安装一个faas-cli工具,可以自己从github上进行下载【推荐,可以选择最新版本】,也可以使用一下命令进行安装【安装的不是最新版本】。
curl -sSL https://cli.openfaas.com | sudo -E sh
下载之后,放到/usr/local/bin
并添加可执行权限
$ sudo mv faas-cli /usr/local/bin
$ sudo chmod +x /usr/local/bin/faas-cli
登录CLI工具
# 设置OpenFaas gateway的地址
export OPENFAAS_URL=193.169.0.90:31112
# 获取OpenFaas gateway admin的密码
$ PASSWORD=$(kubectl -n openfaas get secret basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode) && echo "OpenFaaS admin password: $PASSWORD"
# 登录faas-cli
$ echo -n $PASSWORD | faas-cli login -g $OPENFAAS_URL -u admin --password-stdin
获取模板列表
OpenFaas提供了各种语言的模板列表,可以通过以下命令获取最新的列表
$ faas-cli template pull
Fetch templates from repository: https://github.com/openfaas/templates.git at
2022/06/22 18:59:24 Attempting to expand templates from https://github.com/openfaas/templates.git
查看模板列表
$ faas-cli template store list
NAME SOURCE DESCRIPTION
csharp openfaas Classic C# template
dockerfile openfaas Classic Dockerfile template
go openfaas Classic Golang template
java11 openfaas Java 11 template
java11-vert-x openfaas Java 11 Vert.x template
node17 openfaas HTTP-based Node 17 template
node16 openfaas HTTP-based Node 16 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
php8 openfaas Classic PHP 8 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
根据模板创建项目
$ faas-cli new helloworld --lang node17
Folder: helloworld created.
___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|
Function created in folder: helloworld
Stack file written: helloworld.yml
Notes:
You have created a new function which uses Node.js 17 and the OpenFaaS
of-watchdog which gives greater control over HTTP responses.
npm i --save can be used to add third-party packages like request or cheerio
npm documentation: https://docs.npmjs.com/
Unit tests are run at build time via "npm run", edit package.json to specify
how you want to execute them.
编辑yaml,设置内部镜像存储
$ cat helloworld.yaml
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:31112
functions:
helloworld:
lang: node17
handler: ./helloworld
image: harbor.helloworld.cn/faas/helloworld:latest
构建并发布
$ faas-cli build -f helloworld.yaml
输出省略。。。
$ faas-cli publish -f helloworld.yaml
输出省略。。。
$ faas-cli deploy -f helloworld.yaml
Deploying: helloworld.
Deployed. 202 Accepted.
URL: http://127.0.0.1:31112/function/helloworld