在 AKS 上使用 OpenFaaSUsing OpenFaaS on AKS

OpenFaaS 是一个通过使用容器构建无服务器函数的框架。OpenFaaS is a framework for building serverless functions through the use of containers. 作为一个开源项目,它在社区中大规模采用。As an open source project, it has gained large-scale adoption within the community. 本文档详细介绍了如何在 Azure Kubernetes 服务 (AKS) 群集上安装和使用 OpenFaas。This document details installing and using OpenFaas on an Azure Kubernetes Service (AKS) cluster.


为了完成本文中的步骤,需要具备以下各项。In order to complete the steps within this article, you need the following.

  • 基本了解 Kubernetes。Basic understanding of Kubernetes.
  • 已在开发系统上配置了 Azure Kubernetes 服务 (AKS) 群集和 AKS 凭据。An Azure Kubernetes Service (AKS) cluster and AKS credentials configured on your development system.
  • 已在开发系统上安装 Azure CLI。Azure CLI installed on your development system.
  • 已在系统上安装 Git 命令行工具。Git command-line tools installed on your system.

添加 OpenFaaS helm 图表存储库Add the OpenFaaS helm chart repo

在本地计算机上以管理员权限打开 Azure 本地 Shell。Open Azure local Shell with administrator priviledge on your local computer.

OpenFaaS 保留有自己的 helm 图表,可以通过所有最新的更改来更新内容。OpenFaaS maintains its own helm charts to keep up to date with all the latest changes.

helm repo add openfaas https://openfaas.github.io/faas-netes/
helm repo update

部署 OpenFaaSDeploy OpenFaaS

作为一种良好做法,OpenFaaS 和 OpenFaaS 函数应分别存储在其自己的 Kubernetes 命名空间中。As a good practice, OpenFaaS and OpenFaaS functions should be stored in their own Kubernetes namespace.

为 OpenFaaS 系统和函数创建一个命名空间:Create a namespace for the OpenFaaS system and functions:

kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml

生成 OpenFaaS UI 门户和 REST API 的密码:Generate a password for the OpenFaaS UI Portal and REST API:

# generate a random password
PASSWORD=$(head -c 12 /dev/urandom | shasum| cut -d' ' -f1)

kubectl -n openfaas create secret generic basic-auth \
--from-literal=basic-auth-user=admin \

可以通过 echo $PASSWORD 获取机密的值。You can get the value of the secret with echo $PASSWORD.

我们在此处创建的密码将由 helm 图表用来在 OpenFaaS 网关上启用基本的身份验证,该网关通过云 LoadBalancer 公开给 Internet。The password we create here will be used by the helm chart to enable basic authentication on the OpenFaaS Gateway, which is exposed to the Internet through a cloud LoadBalancer.

克隆的存储库中包括了 OpenFaaS 的一个 Helm chart。A Helm chart for OpenFaaS is included in the cloned repository. 可以使用此图表将 OpenFaaS 部署到 AKS 群集。Use this chart to deploy OpenFaaS into your AKS cluster.

helm upgrade openfaas --install openfaas/openfaas \
    --namespace openfaas  \
    --set basic_auth=true \
    --set functionNamespace=openfaas-fn \
    --set serviceType=LoadBalancer


NAME:   openfaas
LAST DEPLOYED: Wed Feb 28 08:26:11 2018
NAMESPACE: openfaas

==> v1/ConfigMap
NAME                 DATA  AGE
prometheus-config    2     20s
alertmanager-config  1     20s



若要验证 openfaas 是否已启动,请运行:To verify that openfaas has started, run:

kubectl --namespace=openfaas get deployments -l "release=openfaas, app=openfaas"

创建了一个用于访问 OpenFaaS 网关的公共 IP 地址。A public IP address is created for accessing the OpenFaaS gateway. 若要检索此 IP 地址,请使用 kubectl get service 命令。To retrieve this IP address, use the kubectl get service command. 将 IP 地址分配到服务可能需要一分钟时间。It may take a minute for the IP address to be assigned to the service.

kubectl get service -l component=gateway --namespace openfaas


NAME               TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)          AGE
gateway            ClusterIP   <none>         8080/TCP         7m
gateway-external   LoadBalancer   8080:30800/TCP   7m

若要测试 OpenFaaS 系统,请在端口 8080 上浏览到外部 IP 地址,在本例中为 test the OpenFaaS system, browse to the external IP address on port 8080, in this example. 系统会提示你登录。You will be prompted to log in. 若要获取密码,请输入 echo $PASSWORDTo fetch your password, enter echo $PASSWORD.

OpenFaaS UI

最后,安装 OpenFaaS CLI。Finally, install the OpenFaaS CLI. 此示例使用了 brew,有关更多选项,请参阅 OpenFaaS CLI 文档This example used brew, see the OpenFaaS CLI documentation for more options.

brew install faas-cli

$OPENFAAS_URL 设置为上面发现的公共 IP。Set $OPENFAAS_URL to the public IP found above.

使用 Azure CLI 登录:Log in with the Azure CLI:

echo -n $PASSWORD | ./faas-cli login -g $OPENFAAS_URL -u admin --password-stdin

创建第一个函数Create first function

现在,OpenFaaS 已可运行,使用 OpenFaas 门户创建一个函数。Now that OpenFaaS is operational, create a function using the OpenFaas portal.

单击“部署新函数”并搜索 Figlet 。Click on Deploy New Function and search for Figlet. 选择 Figlet 函数,然后单击“部署”。Select the Figlet function, and click Deploy.


使用 curl 来调用该函数。Use curl to invoke the function. 将下例中的 IP 地址替换为 OpenFaas 网关的 IP 地址。Replace the IP address in the following example with that of your OpenFaas gateway.

curl -X POST -d "Hello Azure"


 _   _      _ _            _
| | | | ___| | | ___      / \    _____   _ _ __ ___
| |_| |/ _ \ | |/ _ \    / _ \  |_  / | | | '__/ _ \
|  _  |  __/ | | (_) |  / ___ \  / /| |_| | | |  __/
|_| |_|\___|_|_|\___/  /_/   \_\/___|\__,_|_|  \___|

创建第二个函数Create second function

现在,创建另一个函数。Now create a second function. 此示例将使用 OpenFaaS CLI 进行部署,包括一个自定义容器映像并从 Cosmos DB 检索数据。This example will be deployed using the OpenFaaS CLI and includes a custom container image and retrieving data from a Cosmos DB. 在创建函数之前,需要配置多个项。Several items need to be configured before creating the function.

首先,为 Cosmos DB 创建一个新的资源组。First, create a new resource group for the Cosmos DB.

az group create --name serverless-backing --location chinaeast2

部署一个 MongoDB 类型的 CosmosDB 实例。Deploy a CosmosDB instance of kind MongoDB. 此实例需要一个唯一的名称,请将 openfaas-cosmos 更新为环境中唯一的某个名称。The instance needs a unique name, update openfaas-cosmos to something unique to your environment.

az cosmosdb create --resource-group serverless-backing --name openfaas-cosmos --kind MongoDB

获取 Cosmos 数据库连接字符串并将其存储在一个变量中。Get the Cosmos database connection string and store it in a variable.

--resource-group 参数的值更新为资源组名,将 --name 参数的值更新为 Cosmos DB 的名称。Update the value for the --resource-group argument to the name of your resource group, and the --name argument to the name of your Cosmos DB.

COSMOS=$(az cosmosdb list-connection-strings \
  --resource-group serverless-backing \
  --name openfaas-cosmos \
  --query connectionStrings[0].connectionString \
  --output tsv)

现在,使用测试数据填充 Cosmos 数据库。Now populate the Cosmos DB with test data. 创建一个名为 plans.json 的文件,并将其复制到以下 json 中。Create a file named plans.json and copy in the following json.

    "name" : "two_person",
    "friendlyName" : "Two Person Plan",
    "portionSize" : "1-2 Person",
    "mealsPerWeek" : "3 Unique meals per week",
    "price" : 72,
    "description" : "Our basic plan, delivering 3 meals per week, which will feed 1-2 people.",
    "__v" : 0

使用 mongoimport 工具加载具有数据的 CosmosDB 实例。Use the mongoimport tool to load the CosmosDB instance with data.

如果需要,安装 MongoDB 工具。If needed, install the MongoDB tools. 以下示例使用 brew 安装这些工具,有关其他选项,请参阅 MongoDB 文档The following example installs these tools using brew, see the MongoDB documentation for other options.

brew install mongodb

将数据加载到数据库中。Load the data into the database.

mongoimport --uri=$COSMOS -c plans < plans.json


2018-02-19T14:42:14.313+0000    connected to: localhost
2018-02-19T14:42:14.918+0000    imported 1 document

运行以下命令来创建函数。Run the following command to create the function. -g 参数的值更新为 OpenFaaS 网关地址。Update the value of the -g argument with your OpenFaaS gateway address.

faas-cli deploy -g --image=shanepeckham/openfaascosmos --name=cosmos-query --env=NODE_ENV=$COSMOS

在部署后,应当会看到为函数新创建的 OpenFaaS 终结点。Once deployed, you should see your newly created OpenFaaS endpoint for the function.

Deployed. 202 Accepted.

使用 curl 测试函数。Test the function using curl. 将 IP 地址更新为 OpenFaaS 网关地址。Update the IP address with the OpenFaaS gateway address.

curl -s


[{"ID":"","Name":"two_person","FriendlyName":"","PortionSize":"","MealsPerWeek":"","Price":72,"Description":"Our basic plan, delivering 3 meals per week, which will feed 1-2 people."}]

还可以在 OpenFaaS UI 中测试函数。You can also test the function within the OpenFaaS UI.


后续步骤Next Steps

可以在 OpenFaaS 研讨会中通过一系列动手实验室继续学习。这些实验室涵盖的主题包括:如何创建自己的 GitHub 机器人、如何使用机密、如何查看指标,以及如何进行自动缩放。You can continue to learn with the OpenFaaS workshop through a set of hands-on labs that cover topics such as how to create your own GitHub bot, consuming secrets, viewing metrics, and auto-scaling.