Skip to content

Files

Latest commit

9ead540 · Jan 15, 2022

History

History
253 lines (175 loc) · 8.22 KB

openfaas.md

File metadata and controls

253 lines (175 loc) · 8.22 KB
title description author ms.topic ms.date ms.author ms.custom
Use OpenFaaS with Azure Kubernetes Service (AKS)
Learn how to deploy and use OpenFaaS on an Azure Kubernetes Service (AKS) cluster to build serverless functions with containers.
justindavies
conceptual
03/05/2018
juda
mvc, devx-track-azurecli

Using OpenFaaS on AKS

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. This document details installing and using OpenFaas on an Azure Kubernetes Service (AKS) cluster.

Prerequisites

In order to complete the steps within this article, you need the following.

  • Basic understanding of Kubernetes.
  • An Azure Kubernetes Service (AKS) cluster and AKS credentials configured on your development system.
  • Azure CLI installed on your development system.
  • Git command-line tools installed on your system.

Add the OpenFaaS helm chart repo

Go to https://shell.azure.com to open Azure Cloud Shell in your browser.

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

Deploy OpenFaaS

As a good practice, OpenFaaS and OpenFaaS functions should be stored in their own Kubernetes namespace.

Create a namespace for the OpenFaaS system and functions:

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

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 \
--from-literal=basic-auth-password="$PASSWORD"

You can get the value of the secret with echo $PASSWORD.

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.

A Helm chart for OpenFaaS is included in the cloned repository. 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

Output:

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

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

{snip}

NOTES:
To verify that openfaas has started, run:

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

A public IP address is created for accessing the OpenFaaS gateway. To retrieve this IP address, use the kubectl get service command. It may take a minute for the IP address to be assigned to the service.

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

Output.

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

To test the OpenFaaS system, browse to the external IP address on port 8080, http://52.186.64.52:8080 in this example. You will be prompted to log in. To fetch your password, enter echo $PASSWORD.

OpenFaaS UI

Finally, install the OpenFaaS CLI. This example used brew, see the OpenFaaS CLI documentation for more options.

brew install faas-cli

Set $OPENFAAS_URL to the public IP found above.

Log in with the Azure CLI:

export OPENFAAS_URL=http://52.186.64.52:8080
echo -n $PASSWORD | ./faas-cli login -g $OPENFAAS_URL -u admin --password-stdin

Create first function

Now that OpenFaaS is operational, create a function using the OpenFaas portal.

Click on Deploy New Function and search for Figlet. Select the Figlet function, and click Deploy.

Screenshot shows the Deploy A New Function dialog box with the text figlet on the search line.

Use curl to invoke the function. Replace the IP address in the following example with that of your OpenFaas gateway.

curl -X POST http://52.186.64.52:8080/function/figlet -d "Hello Azure"

Output:

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

Create second function

Now create a second function. 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.

First, create a new resource group for the Cosmos DB.

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

Deploy a CosmosDB instance of kind MongoDB. 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

Get the Cosmos database connection string and store it in a variable.

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)

Now populate the Cosmos DB with test data. 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
}

Use the mongoimport tool to load the CosmosDB instance with data.

If needed, install the MongoDB tools. 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

Output:

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. Update the value of the -g argument with your OpenFaaS gateway address.

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

Once deployed, you should see your newly created OpenFaaS endpoint for the function.

Deployed. 202 Accepted.
URL: http://52.186.64.52:8080/function/cosmos-query

Test the function using curl. Update the IP address with the OpenFaaS gateway address.

curl -s http://52.186.64.52:8080/function/cosmos-query

Output:

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

You can also test the function within the OpenFaaS UI.

alt text

Next Steps

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.