Deploying OpenFaaS functions with Knative

Deploying OpenFaaS functions with Knative
Deploying OpenFaaS functions with Knative

Knative introduces several API objects to help you build functions, deploy them on Kubernetes with auto-scaling automatically enabled and trigger them via events.

So what is a function, how do you build one and how do you deploy it?

If you write Python, you might write:

def hello():
    print("Hello World !")

That’s a function, I know you know. But how do you get that function inside a container so that you can call it?

Packaging a Function

In Kubeless we took the early decision to inject that function in a running Kubernetes Pod using a Kubernetes ConfigMap, dynamically loading the function in an HTTP based function invoker wrapper. I believe it is a very clever idea, but it has limitations, most notably when you have to deal with dependencies of that function and scaling it. Kubeless does address this issue with an internal build system but otherwise, every time you scale you will need to install all the dependencies again in every Pod. Why did we do this? Because we did not want developers to have to write a Dockerfile or a Kubernetes manifest.

However, if you let your developers write a Dockerfile, then a function is really just a container image and what needs to be decided is the function invocation mechanism inside a running container. Pivotal has its way of invoking functions, the Oracle fn project has its way of doing it etc…all the FaaS projects have their own invoker runtime.

OpenFaaS runtime in Knative

OpenFaaS has been very successful and packages functions in Docker containers, making use of a watchdog process as invoker. It listens over HTTP and passes the payload to the function stdin. It is elegant and simple, hence very powerful.

Now that we have knative it also means that any OpenFaaS function can run directly in Knative, you can use the OpenFaaS language templates and derive a knative build template from it. Then you let knative build mechanism create the docker image and push it to a registry using kaniko for unprivileged Docker builds.

With a function packaged, published in a registry you are now ready to deploy it as a Kubernetes deployment with the traffic going to the function monitored by knative to provide the auto-scaling. The OpenFaaS Kubernetes controller, of course, does this, but now you can also do it directly in Knative.

How-To

Check out the GitHub repository https://github.com/triggermesh/openfaas-runtime, in it, you will find a Knative build template and a configuration example to package a Golang Hello World function.

First clone the repo:

cd openfaas-runtime

Create the Build template for Golang

kubectl apply -f ./go/openfaas-go-runtime.yaml

Launch your function using a full-blown Configuration manifest or if you prefer you can use our tm client to do it from the CLI:

tm deploy buildtemplate --from-url https://raw.githubusercontent.com/triggermesh/openfaas-runtime/master/go/openfaas-go-runtime.yaml
tm deploy service hello-go --from-source https://github.com/golang/example --build-template openfaas-go-runtime --build-argument DIRECTORY=hello

And that is it, two CLI commands and your OpenFaaS function runs on Knative (and of course TriggerMesh).

Related Posts

Join our newsletter

Subscribe to TriggerMesh newsletter. You'll get only the most important news about TriggerMesh