OpenFaaS Hands-On
curl -sLSf https://cli.openfaas.com | sudo sh
docker login
curl -sLS https://get.arkade.dev | sudo sh
arkade install openfaas
kubectl rollout status -n openfaas deploy/gateway
kubectl get all -n openfaas
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
kubectl port-forward svc/gateway -n openfaas 8080:8080
ssh -L 8080:127.0.0.1:8080 ubuntu@
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
faas-cli store deploy markdown
faas-cli list
Creating a new function
faas-cli template pull
faas-cli new --list
faas-cli new --lang python3 hello-openfaas --prefix=""
./hello-openfaas
./hello-openfaas/handler.py
./hello-openfaas/requirements.txt
./hello-openfaas/__init__.py
./hello-openfaas.yml
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
return "Hello OpenFaaS"
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
faas-cli up -f hello-openfaas.yml
faas-cli invoke hello-openfaas
curl http://localhost:8080/function/hello-openfaas
kubectl logs deployment/hello-openfaas -n openfaas-fn
Managing multiple 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
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
faas-cli template store describe golang-http
faas-cli template store pull golang-http
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:
- requires no code - can be done with CLI programs
- fast for development and testing
- easy to model in code
Cons:
- additional latency - each function goes back to the server
- chatty (more messages)
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:
- functions can make use of each other directly
- low latency since the functions can access each other on the same network
Cons:
- requires a code library for making the HTTP request