OpenFaaS是运行在Docker和Kubernetes之上的无服务器功能框架。在本教程中,您将学习如何执行以下操作:
kubectl
。node --version
以下示例输出显示您的计算机上安装了Node.js:
xxxxxxxxxx
v10.16.3
如果未安装Node.js或您运行的是旧版本,您可以从Downloads佩奇。
有了Kind,您可以使用Docker容器作为节点来运行本地的Kubernetes集群。本节中的步骤是可选的。只有当您没有正在运行的Kubernetes集群时才遵循它们。
openfaas-cluster.yaml
,并复制以下规格:xxxxxxxxxx
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
2.使用kind create cluster
用于创建具有一个控制平面和两个工作节点的Kubernetes群集的命令:
xxxxxxxxxx
kind create cluster --config kind-specs/kind-cluster.yaml
xxxxxxxxxx
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.17.0) ��
✓ Preparing nodes �� �� ��
✓ Writing configuration ��
✓ Starting control-plane ��️
✓ Installing CNI ��
✓ Installing StorageClass ��
✓ Joining worker nodes ��
Set kubectl context to "kind-kind"
You can now use your cluster with:
kubectl cluster-info --context kind-kind
Thanks for using kind! ��
您可以使用Helm、纯YAML文件或其自己的名为的安装程序安装OpenFaaSarkade
这为OpenFaaS的运行提供了一种快速而简单的方法。在本节中,您将使用以下功能部署OpenFaaSarkade
。
arkade
:xxxxxxxxxx
curl -sLS https://dl.get-arkade.dev | sudo sh
xxxxxxxxxx
Downloading package https://github.com/alexellis/arkade/releases/download/0.1.10/arkade-darwin as /Users/andrei/Desktop/openFaaS/faas-hello-world/arkade-darwin
Download complete.
Running with sufficient permissions to attempt to move arkade to /usr/local/bin
New version of arkade installed to /usr/local/bin
Creating alias 'ark' for 'arkade'.
_ _
__ _ _ __| | ____ _ __| | ___
/ _` | '__| |/ / _` |/ _` |/ _ \
| (_| | | | < (_| | (_| | __/
\__,_|_| |_|\_\__,_|\__,_|\___|
Get Kubernetes apps the easy way
Version: 0.1.10
Git Commit: cf96105d37ed97ed644ab56c0660f0d8f4635996
2.现在,安装openfaas
使用:
xxxxxxxxxx
arkade install openfaas
xxxxxxxxxx
Using kubeconfig: /Users/andrei/.kube/config
Using helm3
Node architecture: "amd64"
Client: "x86_64", "Darwin"
2020/03/10 16:20:40 User dir established as: /Users/andrei/.arkade/
https://get.helm.sh/helm-v3.1.1-darwin-amd64.tar.gz
/Users/andrei/.arkade/bin/helm3/darwin-amd64 darwin-amd64/
/Users/andrei/.arkade/bin/helm3/README.md darwin-amd64/README.md
/Users/andrei/.arkade/bin/helm3/LICENSE darwin-amd64/LICENSE
/Users/andrei/.arkade/bin/helm3/helm darwin-amd64/helm
2020/03/10 16:20:43 extracted tarball into /Users/andrei/.arkade/bin/helm3: 3 files, 0 dirs (1.633976582s)
"openfaas" has been added to your repositories
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "ibm-charts" chart repository
...Successfully got an update from the "openfaas" chart repository
...Successfully got an update from the "stable" chart repository
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈ Happy Helming!⎈
VALUES values.yaml
Command: /Users/andrei/.arkade/bin/helm3/helm [upgrade --install openfaas openfaas/openfaas --namespace openfaas --values /var/folders/nz/2gtkncgx56sgrpqvr40qhhrw0000gn/T/charts/openfaas/values.yaml --set gateway.directFunctions=true --set faasnetes.imagePullPolicy=Always --set gateway.replicas=1 --set queueWorker.replicas=1 --set clusterRole=false --set operator.create=false --set openfaasImagePullPolicy=IfNotPresent --set basicAuthPlugin.replicas=1 --set basic_auth=true --set serviceType=NodePort]
Release "openfaas" does not exist. Installing it now.
NAME: openfaas
LAST DEPLOYED: Tue Mar 10 16:21:03 2020
NAMESPACE: openfaas
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
To verify that openfaas has started, run:
kubectl -n openfaas get deployments -l "release=openfaas, app=openfaas"
=======================================================================
= OpenFaaS has been installed. =
=======================================================================
# Get the faas-cli
curl -SLsf https://cli.openfaas.com | sudo sh
# Forward the gateway to your machine
kubectl rollout status -n openfaas deploy/gateway
kubectl port-forward -n openfaas svc/gateway 8080:8080 &
# If basic auth is enabled, you can now log into your gateway:
PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)
echo -n $PASSWORD | faas-cli login --username admin --password-stdin
faas-cli store deploy figlet
faas-cli list
# For Raspberry Pi
faas-cli store list \
--platform armhf
faas-cli store deploy figlet \
--platform armhf
# Find out more at:
# https://github.com/openfaas/faas
Thanks for using arkade!
3.要验证是否已创建展开,请运行kubectl get deployments
指挥部。属性指定命名空间和选择器。-n
和-l
参数如下:
xxxxxxxxxx
kubectl get deployments -n openfaas -l "release=openfaas, app=openfaas"
如果部署尚未就绪,您应该会看到类似以下示例输出的内容:
xxxxxxxxxx
NAME READY UP-TO-DATE AVAILABLE AGE
alertmanager 0/1 1 0 45s
basic-auth-plugin 1/1 1 1 45s
faas-idler 0/1 1 0 45s
gateway 0/1 1 0 45s
nats 1/1 1 1 45s
prometheus 1/1 1 1 45s
queue-worker 1/1 1 1 45s
安装完成后,输出应如下所示:
xxxxxxxxxx
NAME READY UP-TO-DATE AVAILABLE AGE
alertmanager 1/1 1 1 75s
basic-auth-plugin 1/1 1 1 75s
faas-idler 1/1 1 1 75s
gateway 1/1 1 1 75s
nats 1/1 1 1 75s
prometheus 1/1 1 1 75s
queue-worker 1/1 1 1 75s
4.检查gateway
部署:
xxxxxxxxxx
kubectl rollout status -n openfaas deploy/gateway
下面的示例输出显示gateway
部署已成功铺开:
xxxxxxxxxx
deployment "gateway" successfully rolled out
xxxxxxxxxx
Use the kubectl port-forward command to forward all requests made to http://localhost:8080 to the pod running the gateway service:
xxxxxxxxxx
kubectl port-forward -n openfaas svc/gateway 8080:8080 &
xxxxxxxxxx
[1] 78674
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080
请注意,与号(&
)在后台运行该进程。您可以使用jobs
用于显示后台进程状态的命令:
xxxxxxxxxx
jobs
xxxxxxxxxx
[1] + running kubectl port-forward -n openfaas svc/gateway 8080:8080
xxxxxxxxxx
Issue the following command to retrieve your password and save it into an environment variable named PASSWORD:
xxxxxxxxxx
PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)
OpenFaaS提供了一个命令行实用程序,您可以使用它来构建和部署无服务器功能。您可以按照中的步骤进行安装Installation佩奇。
现在OpenFaaS和faas-cli
如果安装了命令行实用程序,则可以使用内置模板引擎创建和部署无服务器功能。OpenFaaS提供两种类型的模板:
stdio
与您的无服务器功能进行通信。请参阅Watchdog页,了解有关OpenFaaS Watchdog如何工作的更多详细信息。在本教程中,您将使用经典模板。
xxxxxxxxxx
faas-cli template store list
xxxxxxxxxx
NAME SOURCE DESCRIPTION
csharp openfaas Classic C# template
dockerfile openfaas Classic Dockerfile template
go openfaas Classic Golang template
java8 openfaas Classic Java 8 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
node10-express openfaas-incubator Node.js 10 powered by express template
ruby-http openfaas-incubator Ruby 2.4 HTTP template
python27-flask openfaas-incubator Python 2.7 Flask template
python3-flask openfaas-incubator Python 3.6 Flask template
python3-http openfaas-incubator Python 3.6 with Flask and HTTP
node8-express openfaas-incubator Node.js 8 powered by express template
golang-http openfaas-incubator Golang HTTP template
golang-middleware openfaas-incubator 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
node10-express-service openfaas-incubator Node.js 10 express.js microservice template
crystal-http koffeinfrei Crystal HTTP template
rust-http openfaas-incubator Rust HTTP template
bash-streaming openfaas-incubator Bash Streaming template
☞请注意,您可以为模板指定替代存储区。以下示例命令列出名为的存储库中的模板andreipope
:
xxxxxxxxxx
faas-cli template store list -u https://raw.githubusercontent.com/andreipope/my-custom-store/master/templates.json
2.在本地下载官方模板:
xxxxxxxxxx
faas-cli template pull
xxxxxxxxxx
Fetch templates from repository: https://github.com/openfaas/templates.git at master
2020/03/11 20:51:22 Attempting to expand templates from https://github.com/openfaas/templates.git
2020/03/11 20:51:25 Fetched 19 template(s) : [csharp csharp-armhf dockerfile go go-armhf java11 java11-vert-x java8 node node-arm64 node-armhf node12 php7 python python-armhf python3 python3-armhf python3-debian ruby] from https://github.com/openfaas/templates.git
☞默认情况下,上述命令从OpenFaaS官方GitHub存储库下载模板。如果要使用自定义存储库,则应指定存储库的URL。以下示例命令从名为的存储库中拉出模板andreipope
:
xxxxxxxxxx
faas-cli template pull https://github.com/andreipope/my-custom-store/
faas-cli new
指定以下内容的命令:appfleet-hello-world
)lang
参数,后跟编程语言模板(node
)。xxxxxxxxxx
faas-cli new appfleet-hello-world --lang node
xxxxxxxxxx
Folder: appfleet-hello-world created.
___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|
Function created in folder: appfleet-hello-world
Stack file written: appfleet-hello-world.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.
此时,您的目录结构应该如下所示:
xxxxxxxxxx
tree . -L 2
xxxxxxxxxx
.
├── appfleet-hello-world
│ ├── handler.js
│ └── package.json
├── appfleet-hello-world.yml
└── template
├── csharp
├── csharp-armhf
├── dockerfile
├── go
├── go-armhf
├── java11
├── java11-vert-x
├── java8
├── node
├── node-arm64
├── node-armhf
├── node12
├── php7
├── python
├── python-armhf
├── python3
├── python3-armhf
├── python3-debian
└── ruby
21 directories, 3 files
注意事项:
appfleet-hello-world/handler.js
文件包含无服务器函数的代码。您可以使用echo
列出此文件内容的命令:xxxxxxxxxx
cat appfleet-hello-world/handler.js
xxxxxxxxxx
"use strict"
module.exports = async (context, callback) => {
return {status: "done"}
}
package.json
文件。自动生成的文件只是一个空shell:xxxxxxxxxx
cat appfleet-hello-world/package.json
xxxxxxxxxx
{
"name": "function",
"version": "1.0.0",
"description": "",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
appfleet-hello-world
函数存储在appfleet-hello-world.yml
文件:xxxxxxxxxx
cat appfleet-hello-world.yml
xxxxxxxxxx
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
appfleet-hello-world:
lang: node
handler: ./appfleet-hello-world
image: appfleet-hello-world:latest
appfleet-hello-world.yml
文件,并在纯文本编辑器中更新image
字段中添加您的Docker Hub用户名。下面的示例将我的用户名(andrepopescu12
)添加到图像字段:xxxxxxxxxx
image: andrepopescu12/appfleet-hello-world:latest
一旦您进行了此更改,appfleet-hello-world.yml
文件应如下所示:
xxxxxxxxxx
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
appfleet-hello-world:
lang: node
handler: ./appfleet-hello-world
image: <YOUR-DOCKER-HUB-ACCOUNT>/appfleet-hello-world:latest
2.构建函数。输入faas-cli build
命令,指定-f
参数与您在上一步中编辑的YAML文件的名称(appfleet-hello-world.yml
):
xxxxxxxxxx
faas-cli build -f appfleet-hello-world.yml
xxxxxxxxxx
[0] > Building appfleet-hello-world.
Clearing temporary build folder: ./build/appfleet-hello-world/
Preparing: ./appfleet-hello-world/ build/appfleet-hello-world/function
Building: andreipopescu12/appfleet-hello-world:latest with node template. Please wait..
Sending build context to Docker daemon 10.24kB
Step 1/24 : FROM openfaas/classic-watchdog:0.18.1 as watchdog
---> 94b5e0bef891
Step 2/24 : FROM node:12.13.0-alpine as ship
---> 69c8cc9212ec
Step 3/24 : COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
---> Using cache
---> ebab4b723c16
Step 4/24 : RUN chmod +x /usr/bin/fwatchdog
---> Using cache
---> 7952724b5872
Step 5/24 : RUN addgroup -S app && adduser app -S -G app
---> Using cache
---> 33c7f04595d2
Step 6/24 : WORKDIR /root/
---> Using cache
---> 77b9dee16c79
Step 7/24 : ENV NPM_CONFIG_LOGLEVEL warn
---> Using cache
---> a3d3c0bb4480
Step 8/24 : RUN mkdir -p /home/app
---> Using cache
---> 65457e03fcb1
Step 9/24 : WORKDIR /home/app
---> Using cache
---> 50ab672e5660
Step 10/24 : COPY package.json ./
---> Using cache
---> 6143e79de873
Step 11/24 : RUN npm i --production
---> Using cache
---> a41566487c6e
Step 12/24 : COPY index.js ./
---> Using cache
---> 566633e78d2c
Step 13/24 : WORKDIR /home/app/function
---> Using cache
---> 04c9de75f170
Step 14/24 : COPY function/*.json ./
---> Using cache
---> 85cf909b646a
Step 15/24 : RUN npm i --production || :
---> Using cache
---> c088cbcad583
Step 16/24 : COPY --chown=app:app function/ .
---> Using cache
---> 192db89e5941
Step 17/24 : WORKDIR /home/app/
---> Using cache
---> ee2b7d7e8bd4
Step 18/24 : RUN chmod +rx -R ./function && chown app:app -R /home/app && chmod 777 /tmp
---> Using cache
---> 81831389293e
Step 19/24 : USER app
---> Using cache
---> ca0cade453f5
Step 20/24 : ENV cgi_headers="true"
---> Using cache
---> afe8d7413349
Step 21/24 : ENV fprocess="node index.js"
---> Using cache
---> 5471cfe85461
Step 22/24 : EXPOSE 8080
---> Using cache
---> caaa8ae11dc7
Step 23/24 : HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
---> Using cache
---> 881b4d2adb92
Step 24/24 : CMD ["fwatchdog"]
---> Using cache
---> 82b586f039df
Successfully built 82b586f039df
Successfully tagged andreipopescu12/appfleet-hello-world:latest
Image: andreipopescu12/appfleet-hello-world:latest built.
[0] < Building appfleet-hello-world done in 2.25s.
[0] Worker done.
Total build time: 2.25s
3.您可以通过以下方式列出您的Docker镜像:
xxxxxxxxxx
docker images
xxxxxxxxxx
REPOSITORY TAG IMAGE ID CREATED SIZE
andreipopescu12/appfleet-hello-world latest 82b586f039df 25 minutes ago 96MB
docker login
命令与--username
后跟您的Docker Hub用户名的标志。以下示例命令以以下身份登录andreipopescu12
:xxxxxxxxxx
docker login --username andreipopescu12
接下来,系统将提示您输入您的Docker Hub密码:
xxxxxxxxxx
Password:
Login Succeeded
2.使用faas-cli push
将您的无服务器功能推送到Docker Hub的命令:
xxxxxxxxxx
faas-cli push -f appfleet-hello-world.yml
xxxxxxxxxx
The push refers to repository [docker.io/andreipopescu12/appfleet-hello-world]
073c41b18852: Pushed
a5c05e98c215: Pushed
f749ad113dce: Pushed
e4f29400b370: Pushed
b7d0eb42e645: Pushed
84fba0eb2756: Pushed
cf2a3f2bc398: Pushed
942d3272b7d4: Pushed
037b653b7d4e: Pushed
966655dc62be: Pushed
08d8e0925a73: Pushed
6ce16b164ed0: Pushed
d76ecd300100: Pushed
77cae8ab23bf: Pushed
latest: digest: sha256:4150d4cf32e7e5ffc8fd15efeed16179bbf166536f1cc7a8c4105d01a4042928 size: 3447
[0] < Pushing appfleet-hello-world [andreipopescu12/appfleet-hello-world:latest] done.
[0] Worker done.
xxxxxxxxxx
echo -n $PASSWORD | faas-cli login --username admin --password-stdin
2.运行faas-cli deploy
用于部署无服务器功能的命令:
xxxxxxxxxx
faas-cli deploy -f appfleet-hello-world.yml
xxxxxxxxxx
Deploying: appfleet-hello-world.
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Handling connection for 8080
Handling connection for 8080
Deployed. 202 Accepted.
URL: http://127.0.0.1:8080/function/appfleet-hello-world
☞OpenFaaS提供了一种基于每秒请求数量的自动伸缩机制,该数量是从普罗米修斯读取的。为简单起见,我们不会在本教程中介绍自动缩放。要进一步加深您的知识,您可以参考Auto-scaling佩奇。
faas-cli list
列出部署到本地OpenFaaS网关的功能的命令:xxxxxxxxxx
faas-cli list
xxxxxxxxxx
Function Invocations Replicas
appfleet-hello-world 0 1
☞请注意,您还可以通过提供网关的url列出部署到不同网关的功能,如下所示:
xxxxxxxxxx
faas-cli list --gateway https://<YOUR-GATEWAT-URL>:<YOUR-GATEWAY-PORT>
4.您可以使用faas-cli describe
方法来检索有关appfleet-hello-world
功能:
xxxxxxxxxx
faas-cli describe appfleet-hello-world
xxxxxxxxxx
Name: appfleet-hello-world
Status: Ready
Replicas: 1
Available replicas: 1
Invocations: 1
Image: andreipopescu12/appfleet-hello-world:latest
Function process: node index.js
URL: http://127.0.0.1:8080/function/appfleet-hello-world
Async URL: http://127.0.0.1:8080/async-function/appfleet-hello-world
Labels: faas_function : appfleet-hello-world
Annotations: prometheus.io.scrape : false
faas-cli invoke
命令,指定:-f
使用描述函数的YAML文件的名称(appfleet-hello-world.yml
)appfleet-hello-world
)xxxxxxxxxx
faas-cli invoke -f appfleet-hello-world.yml appfleet-hello-world
xxxxxxxxxx
Reading from STDIN - hit (Control + D) to stop.
2.类型CTRL+D
。以下示例输出显示您的无服务器功能按预期工作:
xxxxxxxxxx
appfleet
Handling connection for 8080
{"status":"done"}
您在前面几节中创建、部署并随后调用的函数只是一个空shell。在本节中,我们将其更新为:
stdin
npm
安装一些依赖项。以下命令将移动到appfleet-hello-world
目录,然后安装get-stdin
和request
套餐:xxxxxxxxxx
cd appfleet-hello-world && npm i --save get-stdin request
5.替换handler.js
文件包含:
xxxxxxxxxx
"use strict"
const getStdin = require('get-stdin')
const request = require('request');
let handler = (req) => {
request(`http://api.openweathermap.org/data/2.5/weather?q=${req}&?units=metric&APPID=<YOUR-OPENWEATHERMAP-APP-KEY>`, function (error, response, body) {
console.error('error:', error)
console.log('statusCode:', response && response.statusCode)
console.log('body:', JSON.stringify(body))
})
};
getStdin().then(val => {
handler(val);
}).catch(e => {
console.error(e.stack);
});
module.exports = handler
☞若要尝试此功能,请替换<YOUR-OPENWEATHERMAP-API-KEY>
使用您的OpenWeatherMap API密钥。
faas-cli remove
命令删除您在本教程前面部署的函数:xxxxxxxxxx
faas-cli remove appfleet-hello-world
xxxxxxxxxx
Deleting: appfleet-hello-world.
Handling connection for 8080
Removing old function.
7.旧函数移除后,您需要重新构建、推送和部署修改后的函数。与发出三个单独的命令不同,您可以使用openfaas-cli up
命令,如下例所示:
xxxxxxxxxx
faas-cli up -f appfleet-hello-world.yml
xxxxxxxxxx
[0] > Building appfleet-hello-world.
Clearing temporary build folder: ./build/appfleet-hello-world/
Preparing: ./appfleet-hello-world/ build/appfleet-hello-world/function
Building: andreipopescu12/appfleet-hello-world:latest with node template. Please wait..
Sending build context to Docker daemon 43.01kB
Step 1/24 : FROM openfaas/classic-watchdog:0.18.1 as watchdog
---> 94b5e0bef891
Step 2/24 : FROM node:12.13.0-alpine as ship
---> 69c8cc9212ec
Step 3/24 : COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
---> Using cache
---> ebab4b723c16
Step 4/24 : RUN chmod +x /usr/bin/fwatchdog
---> Using cache
---> 7952724b5872
Step 5/24 : RUN addgroup -S app && adduser app -S -G app
---> Using cache
---> 33c7f04595d2
Step 6/24 : WORKDIR /root/
---> Using cache
---> 77b9dee16c79
Step 7/24 : ENV NPM_CONFIG_LOGLEVEL warn
---> Using cache
---> a3d3c0bb4480
Step 8/24 : RUN mkdir -p /home/app
---> Using cache
---> 65457e03fcb1
Step 9/24 : WORKDIR /home/app
---> Using cache
---> 50ab672e5660
Step 10/24 : COPY package.json ./
---> Using cache
---> 6143e79de873
Step 11/24 : RUN npm i --production
---> Using cache
---> a41566487c6e
Step 12/24 : COPY index.js ./
---> Using cache
---> 566633e78d2c
Step 13/24 : WORKDIR /home/app/function
---> Using cache
---> 04c9de75f170
Step 14/24 : COPY function/*.json ./
---> Using cache
---> f5765914bd05
Step 15/24 : RUN npm i --production || :
---> Using cache
---> a300be28c096
Step 16/24 : COPY --chown=app:app function/ .
---> 91cd72d8ad7a
Step 17/24 : WORKDIR /home/app/
---> Running in fce50a76475a
Removing intermediate container fce50a76475a
---> 0ff17b0a9faf
Step 18/24 : RUN chmod +rx -R ./function && chown app:app -R /home/app && chmod 777 /tmp
---> Running in 6d0c4c92fac1
Removing intermediate container 6d0c4c92fac1
---> 1e543bfbf6b0
Step 19/24 : USER app
---> Running in 6d33f5ec237d
Removing intermediate container 6d33f5ec237d
---> cb7cf5dfab12
Step 20/24 : ENV cgi_headers="true"
---> Running in 972c23374934
Removing intermediate container 972c23374934
---> 21c6e8198b21
Step 21/24 : ENV fprocess="node index.js"
---> Running in 3be91f9d5228
Removing intermediate container 3be91f9d5228
---> aafb7a756d38
Step 22/24 : EXPOSE 8080
---> Running in da3183bd88c5
Removing intermediate container da3183bd88c5
---> 5f6fd7e66a95
Step 23/24 : HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
---> Running in a590c91037ae
Removing intermediate container a590c91037ae
---> fbe20c32941f
Step 24/24 : CMD ["fwatchdog"]
---> Running in 59cd231f0576
Removing intermediate container 59cd231f0576
---> 88cd8ac65ade
Successfully built 88cd8ac65ade
Successfully tagged andreipopescu12/appfleet-hello-world:latest
Image: andreipopescu12/appfleet-hello-world:latest built.
[0] < Building appfleet-hello-world done in 13.95s.
[0] Worker done.
Total build time: 13.95s
[0] > Pushing appfleet-hello-world [andreipopescu12/appfleet-hello-world:latest].
The push refers to repository [docker.io/andreipopescu12/appfleet-hello-world]
04643e0c999f: Pushed
db3ccc4403b8: Pushed
24d1d5a62262: Layer already exists
adfa28db7666: Layer already exists
b7d0eb42e645: Layer already exists
84fba0eb2756: Layer already exists
cf2a3f2bc398: Layer already exists
942d3272b7d4: Layer already exists
037b653b7d4e: Layer already exists
966655dc62be: Layer already exists
08d8e0925a73: Layer already exists
6ce16b164ed0: Layer already exists
d76ecd300100: Layer already exists
77cae8ab23bf: Layer already exists
latest: digest: sha256:818d92b10d276d32bcc459e2918cb537051a14025e694eb59a9b3caa0bb4e41c size: 3456
[0] < Pushing appfleet-hello-world [andreipopescu12/appfleet-hello-world:latest] done.
[0] Worker done.
Deploying: appfleet-hello-world.
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Handling connection for 8080
Handling connection for 8080
Deployed. 202 Accepted.
URL: http://127.0.0.1:8080/function/appfleet-hello-world
☞请注意,您可以跳过推送或部署步骤:
xxxxxxxxxx
faas-cli up -f appfleet-hello-world.yml --skip-push
xxxxxxxxxx
faas-cli up -f appfleet-hello-world.yml --skip-deploy
8.要验证更新后的无服务器函数是否正常工作,请按如下方式调用:
xxxxxxxxxx
faas-cli invoke -f appfleet-hello-world.yml appfleet-hello-world
xxxxxxxxxx
Reading from STDIN - hit (Control + D) to stop.
Berlin
Handling connection for 8080
Hello, you are currently in Berlin
statusCode: 200
body: "{\"coord\":{\"lon\":13.41,\"lat\":52.52},\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"description\":\"scattered clouds\",\"icon\":\"03d\"}],\"base\":\"stations\",\"main\":{\"temp\":282.25,\"feels_like\":270.84,\"temp_min\":280.93,\"temp_max\":283.15,\"pressure\":1008,\"humidity\":61},\"visibility\":10000,\"wind\":{\"speed\":13.9,\"deg\":260,\"gust\":19},\"clouds\":{\"all\":40},\"dt\":1584107132,\"sys\":{\"type\":1,\"id\":1275,\"country\":\"DE\",\"sunrise\":1584077086,\"sunset\":1584119213},\"timezone\":3600,\"id\":2950159,\"name\":\"Berlin\",\"cod\":200}"
9.要进行清理,请运行faas-cli remove
命令,并使用您的无服务器函数的名称(appfleet-hello-world
作为论点):
xxxxxxxxxx
faas-cli remove appfleet-hello-world
xxxxxxxxxx
Deleting: appfleet-hello-world.
Handling connection for 8080
Removing old function.
OpenFaaS提供了一个基于Web的用户界面。在本节中,您将了解如何使用它来部署无服务器功能。
echo
检索密码的命令:xxxxxxxxxx
echo $PASSWORD
xxxxxxxxxx
49IoP28G8247MZcj6a1FWUYUx
2.打开浏览器,访问http://localhost:8080。要登录,请使用admin
您在上一步中检索到的用户名和密码。您将被重定向至OpenFaaS主页。选择部署新功能按钮。
docker.io/andreipopescu12/appfleet-hello-world
在Docker镜像输入框appfleet-hello-world
在函数名称输入框。您将被重定向至Invoke Function页面:
如果一切正常,天气预报将显示在响应体字段:
OpenFaaS网关公开以下指标:
在本节中,您将学习如何设置Prometheus和Grafana来跟踪无服务器功能的运行状况。
xxxxxxxxxx
kubectl get deployments -n openfaas -l "release=openfaas, app=openfaas"
xxxxxxxxxx
NAME READY UP-TO-DATE AVAILABLE AGE
alertmanager 1/1 1 1 15m
basic-auth-plugin 1/1 1 1 15m
faas-idler 1/1 1 1 15m
gateway 1/1 1 1 15m
nats 1/1 1 1 15m
prometheus 1/1 1 1 15m
queue-worker 1/1 1 1 15m
2.暴露prometheus
部署时,创建名为的服务对象prometheus-ui
:
xxxxxxxxxx
kubectl expose deployment prometheus -n openfaas --type=NodePort --name=prometheus-ui
xxxxxxxxxx
service/prometheus-ui exposed
☞The--type=NodePort
标志将公开prometheus-ui
每个节点的IP地址上的服务。另外,一个ClusterIP
创建服务。您将使用此命令连接到prometheus-ui
来自群集外部的服务。
3.视察prometheus-ui
服务,请输入以下命令:
xxxxxxxxxx
kubectl get svc prometheus-ui -n openfaas
xxxxxxxxxx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
prometheus-ui NodePort 10.96.129.204 <none> 9090:31369/TCP 8m1s
4.将所有请求转发到http://localhost:9090到运行prometheus-ui
服务:
xxxxxxxxxx
kubectl port-forward -n openfaas svc/prometheus-ui 9090:9090 &
5.现在,您可以将浏览器指向http://localhost:9090,您应该会看到类似于以下屏幕截图的页面:
stefanprodan/faas-grafana:4.6.3
图像。运行以下命令:xxxxxxxxxx
kubectl run grafana -n openfaas --image=stefanprodan/faas-grafana:4.6.3 --port=3000
xxxxxxxxxx
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/grafana created
7.现在,您可以使用以下内容列出您的部署:
xxxxxxxxxx
kubectl get deployments -n openfaas
xxxxxxxxxx
NAME READY UP-TO-DATE AVAILABLE AGE
alertmanager 1/1 1 1 46m
basic-auth-plugin 1/1 1 1 46m
faas-idler 1/1 1 1 46m
gateway 1/1 1 1 46m
grafana 1/1 1 1 107s
nats 1/1 1 1 46m
prometheus 1/1 1 1 46m
queue-worker 1/1 1 1 46m
8.使用以下内容kubectl expose deployment
命令创建一个服务对象,该服务对象公开grafana
部署:
xxxxxxxxxx
kubectl expose deployment grafana -n openfaas --type=NodePort --name=grafana
xxxxxxxxxx
service/grafana exposed
9.使用以下命令检索有关新服务的详细信息:
xxxxxxxxxx
kubectl get service grafana -n openfaas
xxxxxxxxxx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
grafana NodePort 10.96.194.59 <none> 3000:32464/TCP 60s
10.将所有请求转发到http://localhost:3030到运行grafana
服务:
xxxxxxxxxx
kubectl port-forward -n openfaas svc/grafana 3000:3000 &
[3] 3973
Forwarding from 127.0.0.1:3000 -> 3000
Forwarding from [::1]:3000 -> 3000
11.现在您已经设置了端口转发,您可以通过将浏览器指向http://localhost:3000:
admin
和密码admin
。的主页控制面板将显示页面:https://grafana.com/grafana/dashboards/3434
在Grafan a.com仪表板输入框。然后,选择负载按钮:faas
,然后选择进口:将显示一个空仪表板:
faas-cli invoke
命令如下所示:xxxxxxxxxx
faas-cli invoke -f appfleet-hello-world.yml appfleet-hello-world
16.切换回打开Grafana的浏览器窗口。您的仪表板应该会自动更新,外观与以下屏幕截图类似:
我们希望本教程对学习使用OpenFaaS部署无服务器功能的基础知识有帮助。有关Docker和Kubernetes的更多优秀教程,我们建议您访问our blog。
感谢阅读!