OpenFaaS Hands-On

# Install open Faas CLI on MacOS/Linux
curl -sLSf https://cli.openfaas.com | sudo sh
# Docker login where you install open-faas-cli
docker login
# Assuming you have k8s cluster setup
# Deploy Open Faas in this k8s clsuter.
# arkade installs OpenFaaS using its official helm chart
curl -sLS https://get.arkade.dev | sudo sh arkade install openfaas
# Check the gateway is ready. Expected output is "deployment "gateway" successfully rolled out"
kubectl rollout status -n openfaas deploy/gateway
# View all openfaas artifacts
kubectl get all -n openfaas
Sample output --
NAME READY STATUS RESTARTS AGE pod/alertmanager-5c6f67f7dc-znmml 1/1 Running 0 26d pod/gateway-67c89cf5b-blqdw 2/2 Running 1 (26d ago) 26d pod/nats-c9688fd95-q8qjv 1/1 Running 0 26d pod/prometheus-7db9744469-9qqmb 1/1 Running 0 26d pod/queue-worker-5c4cd75fd8-92d55 1/1 Running 4 (26d ago) 26d NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/alertmanager ClusterIP 10.100.155.173 9093/TCP 26d service/gateway ClusterIP 10.101.221.175 8080/TCP 26d service/gateway-external NodePort 10.110.121.47 8080:31112/TCP 26d service/nats ClusterIP 10.99.56.221 4222/TCP 26d service/prometheus ClusterIP 10.102.143.25 9090/TCP 26d NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/alertmanager 1/1 1 1 26d deployment.apps/gateway 1/1 1 1 26d deployment.apps/nats 1/1 1 1 26d deployment.apps/prometheus 1/1 1 1 26d deployment.apps/queue-worker 1/1 1 1 26d NAME DESIRED CURRENT READY AGE replicaset.apps/alertmanager-5c6f67f7dc 1 1 1 26d replicaset.apps/gateway-67c89cf5b 1 1 1 26d replicaset.apps/nats-c9688fd95 1 1 1 26d replicaset.apps/prometheus-7db9744469 1 1 1 26d replicaset.apps/queue-worker-5c4cd75fd8 1 1 1 26d
# From a different terminal open a tunnel from Kubernetes cluster to local computer so that you can access the OpenFaaS gateway.
# There are other ways to access OpenFaaS gw but I am using this.
kubectl port-forward svc/gateway -n openfaas 8080:8080
# On MacOS terminal create SSH tunnel on bridge adapter of multipaas VM
# I need this command to access open-faas resources from MacOS. Because I have installed open-faas-cli on MacOS. Because I want to use MacOS docker-desktop.
# You may not require this, if you have k8s, docker, faas-cli installed on same VM.
ssh -L 8080:127.0.0.1:8080 ubuntu@
# Access OpenFaas admin UI
gateway URL is: http://127.0.0.1:8080
username - admin
password - kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode
# Wohoo Ready to Rock !!!!
# deploy some sample functions from UI or cli from OpenFaaS store(like docker hub):
faas-cli store deploy markdown faas-cli list
# (I am not doing this) OpenFaaS tracks metrics on functions automatically using Prometheus. The metrics can be turned into a useful dashboard with free and Open Source tools like Grafana
# deploy grafana image in k8s
# expose it as node port
# enable port forwarding

Creating a new function

# There are two ways to create a new function:
# scaffold a function using a built-in or community code template (default)
# take an existing binary and use it as your function (advanced)
# Method 1: Scaffold or generate a new function
# Before creating a new function from a template pull the templates from GitHub:
faas-cli template pull
# this will create a directory called template. Inside it more directories and files for each supported language.
# After that, to find out which languages are available type in:
faas-cli new --list
# Create the a new function names "hello-openfaas" using scaffold.
faas-cli new --lang python3 hello-openfaas --prefix=""
# This will create 1 file called hello-openfaas.yml in current directory and other 3 files in a directory called hello-openfaas.
./hello-openfaas ./hello-openfaas/handler.py ./hello-openfaas/requirements.txt ./hello-openfaas/__init__.py ./hello-openfaas.yml
Here's the contents of the YAML file:
provider: name: openfaas gateway: http://127.0.0.1:8080 functions: hello-openfaas: lang: python3 handler: ./hello-openfaas image: hello-openfaas
The name of the function is represented by the key under functions i.e. hello-openfaas
The language is represented by the lang field
The folder used to build from is called handler, this must be a folder not a file
The Docker image name to be used is under the field image
# This function will just return the input, so it's effectively an echo function.
# Edit the message in handler.py so it returns Hello OpenFaaS instead i.e.
return "Hello OpenFaaS"
# Note: Please make sure that you have logged in to docker registry with docker login command before running this command.
# build, push and deploy the function( you can use separate commands also.)
faas-cli build -f hello-openfaas.yml # create image faas-cli push -f hello-openfaas.yml # push image to docker registry faas-cli deploy -f hello-openfaas.yml # deploy in k8s cluster. Creates a deployment in open-faas-fn namespace
OR
# faas-cli up command combines build, push and deploy commands of faas-cli in a single command.
faas-cli up -f hello-openfaas.yml
# Invoke the function with faas-cli invoke
faas-cli invoke hello-openfaas
# Functions can be invoked via a GET or POST method only.
curl http://localhost:8080/function/hello-openfaas
# View logs
kubectl logs deployment/hello-openfaas -n openfaas-fn

Managing multiple functions

# The YAML file for the CLI allows functions to be grouped together into stacks, this is helpful when working with a set of related functions.
To see how this works generate two functions:
$ faas-cli new --lang python3 first
For the second function use the --append flag:
$ faas-cli new --lang python3 second --append=./first.yml
For convenience let's rename first.yml to example.yml.
$ mv first.yml example.yml
Now look at the file:
provider: name: openfaas gateway: http://127.0.0.1:8080 functions: first: lang: python3 handler: ./first image: first second: lang: python3 handler: ./second image: second
Here are several flags that help when working with a stack of functions:
# Build in parallel faas-cli build -f ./example.yml --parallel=2 # Build / push only one function: faas-cli build -f ./example.yml --filter=second
# build/push and deploy.
faas-cli up example.yml

Custom templates: Template Store

The Template Store is a similar concept to the Function Store, it enables users to collaborate by sharing their templates. The template store also means that you don't have to remember any URLs for making use of your favourite community or project templates.
You can Search and discover templates using the following two commands:
$ faas-cli template store list $ faas-cli template store list -v
# Let's find a Golang template with a HTTP format
faas-cli template store describe golang-http
# Pull the template down:
faas-cli template store pull golang-http
# create a function with this template by typing in:
faas-cli new --lang golang-http

Create Workflows

Chaining functions on the client-side
You can pipe the result of one function into another using curl, the faas-cli or some of your own code. Here's an example:
Pros:
Cons:

Call one function from another

The easiest way to call one function from another is make a call over HTTP via the OpenFaaS API Gateway. This call does not need to know the external domain name or IP address, it can simply refer to the API Gateway as gateway through a DNS entry.
When accessing a service such as the API gateway from a function it's best practice to use an environmental variable to configure the hostname, this is important for two reasons - the name may change and in Kubernetes a suffix is sometimes needed.
Pros:
Cons: