Skip to content

Latest commit

 

History

History
239 lines (173 loc) · 9.59 KB

openfaas.md

File metadata and controls

239 lines (173 loc) · 9.59 KB
title description author ms.topic ms.date ms.subservice ms.author ms.custom
Use OpenFaaS on 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
08/29/2023
aks-developer
juda
mvc, devx-track-azurecli

Use OpenFaaS on Azure Kubernetes Service (AKS)

OpenFaaS is a framework that uses containers to build serverless functions. 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.

Before you begin

Add the OpenFaaS helm chart repo

  1. Navigate to Azure Cloud Shell.

  2. Add the OpenFaaS helm chart repo and update to the latest version using the following helm commands.

    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.

  1. Create a namespace for the OpenFaaS system and functions using the kubectl apply command.

    kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
  2. Generate a password for the OpenFaaS UI Portal and REST API using the following commands. The helm chart uses this password to enable basic authentication on the OpenFaaS Gateway, which is exposed to the Internet through a cloud LoadBalancer.

    # 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"
  3. Get the value for your password using the following echo command.

    echo $PASSWORD
  4. Deploy OpenFaaS into your AKS cluster using the helm upgrade command.

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

    Your output should look similar to the following condensed example output:

    NAME: openfaas
    LAST DEPLOYED: Tue Aug 29 08:26:11 2023
    NAMESPACE: openfaas
    STATUS: deployed
    ...
    NOTES:
    To verify that openfaas has started, run:
    
    kubectl --namespace=openfaas get deployments -l "release=openfaas, app=openfaas"
    ...
    
  5. A public IP address is created for accessing the OpenFaaS gateway. Get the IP address using the kubectl get service command.

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

    Your output should look similar to the following example 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
    
  6. Test the OpenFaaS system by browsing to the external IP address on port 8080, http://52.186.64.52:8080 in this example, where you're prompted to log in. The default user is admin and your password can be retrieved using echo $PASSWORD.

    Screenshot of OpenFaaS UI.

  7. Set $OPENFAAS_URL to the URL of the external IP address on port 8080 and log in with the Azure CLI using the following commands.

    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

  1. Navigate to the OpenFaaS system using your OpenFaaS URL.

  2. Create a function using the OpenFaas portal by selecting Deploy A New Function and search for Figlet.

  3. Select the Figlet function, and then select Deploy.

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

  4. Invoke the function using the following curl command. Make sure you replace the IP address in the following example with your OpenFaaS gateway address.

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

    Your output should look similar to the following example output:

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

Create second function

Configure your Azure Cosmos DB instance

  1. Navigate to Azure Cloud Shell.

  2. Create a new resource group for the Azure Cosmos DB instance using the az group create command.

    az group create --name serverless-backing --location eastus
    
  3. Deploy an Azure Cosmos DB instance of kind MongoDB using the az cosmosdb create command. Replace openfaas-cosmos with your own unique instance name.

    az cosmosdb create --resource-group serverless-backing --name openfaas-cosmos --kind MongoDB
    
  4. Get the Azure Cosmos DB database connection string and store it in a variable using the az cosmosdb list command. Make sure you replace the value for the --resource-group argument with the name of your resource group, and the --name argument with the name of your Azure Cosmos DB instance.

    COSMOS=$(az cosmosdb list-connection-strings \
      --resource-group serverless-backing \
      --name openfaas-cosmos \
      --query connectionStrings[0].connectionString \
      --output tsv)
    
  5. Populate the Azure Cosmos DB with test data by creating a file named plans.json and copying 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
    }

Create the function

  1. Install the MongoDB tools. The following example command installs these tools using brew. For more installation options, see the MongoDB documentation.

    brew install mongodb
  2. Load the Azure Cosmos DB instance with data using the mongoimport tool.

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

    Your output should look similar to the following example output:

    2018-02-19T14:42:14.313+0000    connected to: localhost
    2018-02-19T14:42:14.918+0000    imported 1 document
    
  3. Create the function using the faas-cli deploy command. Make sure you 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, your output should look similar to the following example output:

    Deployed. 202 Accepted.
    URL: http://52.186.64.52:8080/function/cosmos-query
    
  4. Test the function using the following curl command. Make sure you update the IP address with the OpenFaaS gateway address.

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

    Your output should look similar to the following example 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."}]
    

    [!NOTE] You can also test the function within the OpenFaaS UI:

    Screenshot of OpenFaas UI.

Next steps

Continue to learn with the OpenFaaS workshop, which includes a set of hands-on labs that cover topics such as how to create your own GitHub bot, consuming secrets, viewing metrics, and autoscaling.