This tutorial shows how to use the faas-cli to deploy functions to Knative.
-
Install Knative on any Kubernetes following the Knative Installation Guide. This tutorial works on any architecture that Knative supports, I will use Knative on Arm, Check out my tutorial Knative on Raspberry Pi.
-
Setup Docker-Desktop with experimental to enable buildx this will be used to build multi-architecture images including
arm64
. -
Install FaaS CLI using arkade
arkade get faas-cli
-
Create new app using one of the default template store (ie
csharp
,dockerfile
,go
,java11
,java11-vert-x
,node
,node12
,php7
,python
,python3
,python3-debian
,ruby
) Or runfaas-cli template store list
to see a range of other templates from the community. In this example I'm usingnode12
and setting the flag--prefix
for the container registry and namespace, in my case I am usingdocker.io/csantanapr
faas-cli new --lang node12 hello --prefix docker.io/csantanapr
-
Build and publish the container image leveraging
docker buildx
for two architectures.faas-cli publish -f hello.yml --platforms linux/amd64,linux/arm64
-
Generate the Kubernetes Knative Custom Resource using the value
serving.knative.dev/v1
for the CRD API, the defaul namespace used isopenfaas-fn
you can change this, I'm going to usedefault
faas-cli generate --api=serving.knative.dev/v1 -f hello.yml -n default
-
You can take the standard output redirect to a file, then add additional attributes to the Knative definition, by default the
faas-cli
generates a simple service with the container image uri that we published a previous step. This is a sample output:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: hello namespace: default spec: template: spec: containers: - image: docker.io/csantanapr/hello:latest
You can pipe it to
service.yaml
like thisfaas-cli generate --api=serving.knative.dev/v1 -f hello.yml -n default > service.yaml
-
To deploy the service to your cluster (ie. raspberry pi) configure your
kubectl
target contextkubectx knative-pi
-
You can edit and deploy the Knative yaml, or you can generate and apply in one step, by piping the output to
kubectl
faas-cli generate --api=serving.knative.dev/v1 -f hello.yml -n default | kubectl apply -f -
-
Wait for Knative Service to be Ready
kubectl wait ksvc hello --all --timeout=-1s --for=condition=Ready
-
Get the URL of the new Service
SERVICE_URL=$(kubectl get ksvc hello -o jsonpath='{.status.url}') echo $SERVICE_URL
-
Test the App
curl -d 'Hello World' -H "Content-Type: text/plain" $SERVICE_URL
-
The output is the return value from the source code handler.js now you can go ahead and change the code based on nodejs runtime contract, and publish a new image tag and Update the tag in hello.yml
{"status":"Received input: Hello World"}
-
You can also try
JSON
curl -d '{"msg":"Hello World"}' -H "Content-Type: application/json" $SERVICE_URL
The output
{"status":"Received input: {\"msg\":\"Hello World\"}"}