Serverless is the new thing at the moment, and the OpenFaaS project is certainly something the team here at Civo.com have taken a shine to. Before we can start working with OpenFasS we need some servers to run our functions on, so this guide gets us a dev-ready Kubernetes cluster running with OpenFasS... within about 5 minutes. And best of all, after setting a couple of environment variables, this will be a entirely hands free deployment using terraform!

Pre-requistes

Terraform

Before we get started, we need to get the terraform client installed locally. You can follow the official docs or on MacOS use homebrew:

$ brew install terraform

Source Files

You should clone the latest copy of the config files from github because these will give some important configuration:

$ git clone https://github.com/DMajrekar/terraform-openfaas.git
$ cd terraform-openfaas

OpenFaaS Cli

You can get the OpenFaaS client using:

$ curl -sL https://cli.openfaas.com | sudo sh

or homebrew:

$ brew install faas-cli

Deployment

To build this environment:

  1. Add the settings from the civo-env.sh file into your shell by executing source civo-env.sh and input your username / password from https://www.civo.com/api Note: You'll need to be logged into your Civo account to see the username/password.
  2. Update the public_key variable with your SSH public key in cluster.tf.

    variable "public_key"   { default = "ssh-rsa ..." }
    
  3. Set the access CIDR in the access_cidr to restrict SSH and OpenFaaS access. Note: Usually your public IP address/32 or 0.0.0.0/0 for everywhere.

    variable "access_cidr"  { default = "0.0.0.0/0" }
    
  4. Initialise terraform - terraform init

  5. Run terraform - terraform apply.

Terraform will output the public IP address of your new kube-master server:

$ terraform apply

...

Apply complete! Resources: 12 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path:

Outputs:
kube-master-ip = xxx.xxx.xxx.xxx

You can now connect via SSH to the master server:

$ ssh ubuntu@xxx.xxx.xxx.xxx

Then you can tail the cloud init output file /var/log/cloud-init-output.log and watch the build. This will take a few minutes to complete.

$ tail -f /var/log/cloud-init-output.log

...
clusterrole "faas-controller" created
clusterrolebinding "faas-controller" created
service "prometheus" created
deployment "prometheus" created
service "alertmanager" created
deployment "alertmanager" created
Cloud-init v. 0.7.8 running 'modules:final' at Tue, 19 Sep 2017 13:42:01 +0000. Up 42.73 seconds.
Cloud-init v. 0.7.8 finished at Tue, 19 Sep 2017 13:45:33 +0000. Datasource DataSourceOpenStack [net,ver=2].  Up 254.27 seconds

Broadcast message from root@kube-master (Tue 2017-09-19 13:45:33 UTC):

The system is going down for reboot at Tue 2017-09-19 13:46:33 UTC!

The slave nodes are installing and registering in the background in parallel to the master. They will all restart after the build is complete, and once the master lets you back in, you can confrim that both Kubernetes is running correctly and that OpenFaaS is running as expected:

ubuntu@kube-master:~$ kubectl get no
NAME            STATUS    AGE       VERSION
kube-master     Ready     4m        v1.7.4
kube-slave-01   Ready     2m        v1.7.4
kube-slave-02   Ready     3m        v1.7.4

ubuntu@kube-master:~$ kubectl get po
NAME                            READY     STATUS    RESTARTS   AGE
alertmanager-4083991118-p1kq0   1/1       Running   0          3m
faas-netesd-393444700-z8vvr     1/1       Running   0          3m
gateway-2414585756-1g0nz        1/1       Running   0          3m
prometheus-2983574867-m8ftp     1/1       Running   0          3m

The OpenFaaS gateway will be available on port 31112 of your new kubernetes cluster.

Up and running with OpenFaaS

Now that you have an OpenFaaS cluster running, let's get a quick echo Function up and running.

The faas-cli command we installed earlier, can be used to deploy the echo function to our cluster:

faas-cli  deploy --image alexellis/faas-nodejs-echo --name echo --gateway http://xxx.xxx.xxx.xxx:31112

After a short wait to allow everything to start, you can invoke the function and get a response.

$ echo "test" | faas-cli invoke --gateway http://xxx.xxx.xxx.xxx:31112
{"nodeVersion":"v6.11.2","input":"test\n"}

or we could get an MD5 check running as a function:

$ faas-cli  deploy --image alexellis2/faas-alpinefunction --name checksum --fprocess md5sum --gateway http://xxx.xxx.xxx.xxx:31112
$ echo "hello" | faas-cli invoke --name checksum --gateway http://xxx.xxx.xxx.xxx:31112
b1946ac92492d2347c6235b4d2611184  -

For more information on OpenFaas, I suggest grabbing a coffee and tucking into:

or playing along with:

Cleanup

You can leave your cluster running with OpenFaaS and keep experimenting, but if you want to clean-up the environment type in:

$ terraform destroy

...

openstack_networking_network_v2.network: Destruction complete

Destroy complete! Resources: 15 destroyed.