Skip to content
Snippets Groups Projects
TestDrive.md 9.65 KiB

Functions as a Service - TestDrive

FaaS is a framework for building serverless functions on Docker Swarm Mode with first class metrics. Any UNIX process can be packaged as a function in FaaS enabling you to consume a range of web events without repetitive boiler-plate coding.

Please support the project and put a Star on the repo.

Overview

A Docker stack file with a number of sample functions is provided so that you can get up and running within minutes. You can also clone the code to hack on it or package your own functions.

The guide makes use of a free testing/cloud service, but if you want to try it on your own laptop just follow the guide in the README file on Github. There is also a blog post that goes into the background of the project.

  • So let's head over to http://play-with-docker.com/ and start a new session.

  • Click "Add new Instance" to create a Docker host, more can be added later.

This one-shot script clones the code, initialises Docker swarm mode and then deploys the FaaS sample stack.

# docker swarm init --advertise-addr eth0 && \
  git clone https://github.com/alexellis/faas && \
  cd faas && \
  git checkout 0.6.6-beta && \
  ./deploy_stack.sh && \
  docker service ls

The shell script makes use of a v3 docker-compose.yml file

If you are not testing on play-with-docker then remove --advertise-addr eth0 from first line of the script.

  • Now that everything's deployed take note of the two DNS entries at the top of the screen.

Sample functions

Some of the sample functions are:

  • Markdown to HTML renderer (markdownrender) - takes .MD input and produces HTML (Golang)
  • Docker Hub Stats function (hubstats) - queries the count of images for a user on the Docker Hub (Golang)
  • Node Info (nodeinfo) function - gives you the OS architecture and detailled info about the CPUS (Node.js)
  • Webhook stasher function (webhookstash) - saves webhook body into container's filesystem - even binaries (Golang)

Newly added sample functions (not included in the sample stack)

Invoke the sample functions with curl or Postman:

Head over to the Github and Star the project, or read on to see the input/output from the sample functions.

Working with the sample functions

You can access the sample functions via the command line with a HTTP POST request or by using the built-in UI portal.

UI portal:

The UI portal is accessible on: http://localhost:8080/ - it show a list of functions deployed on your swarm and allows you to test them out.

View screenshot:

You can find out which services are deployed like this:

# docker stack ls
NAME  SERVICES
func  3

# docker stack ps func
ID            NAME               IMAGE                                  NODE  DESIRED STATE  CURRENT STATE         
rhzej73haufd  func_gateway.1     alexellis2/faas-gateway:latest         moby  Running        Running 26 minutes ago
fssz6unq3e74  func_hubstats.1    alexellis2/faas-dockerhubstats:latest  moby  Running        Running 27 minutes ago
nnlzo6u3pilg  func_prometheus.1  quay.io/prometheus/prometheus:latest   moby  Running        Running 27 minutes ago
  • Head over to http://localhost:9090 for your Prometheus metrics

  • A saved Prometheus view is available here: metrics overview

  • Your functions can be accessed via the gateway UI or read on for curl

Building functions from templates and the CLI

The following guides show how to use the CLI and code templates to build functions.

Using a template means you only have to write a handler file in your chosen programming language such as:

  • Ruby
  • Node.js
  • Python
  • CSharp
  • Or propose a template for another programming languae

Guides:

Packaging a custom Docker image

Read the developer guide:

The original blog post also walks through creating a function:

Add new functions to FaaS at runtime

Option 1: via the FaaS CLI

The FaaS CLI can be used to build functions very quickly though the use of templates. See more details on the FaaS CLI here.

Option 2: via FaaS UI portal

To attach a function at runtime you can use the "Create New Function" button on the portal UI at http://localhost:8080/

Creating a function via the UI:

Option Usage
Image The name of the image you want to use for the function. A good starting point is functions/alpine
Service Name Describe the name of your service. The Service Name format is: [a-zA-Z_0-9]
fProcess The process to invoke for each function call. This must be a UNIX binary and accept input via STDIN and output via STDOUT.
Network The network func_functions is the default network.

Once the create button is clicked, faas will provision a new Docker Swarm service. The newly created function will shortly be available in the list of functions on the left hand side of the UI.

Option 3: Through docker-compose.yml stack file

Edit the docker-compose stack file, then run ./deploy_stack.sh - this will only update changed/added services, not existing ones.

Option 4: Programatically through a HTTP POST to the API Gateway

A HTTP post can also be sent via curl etc to the endpoint used by the UI (HTTP post to /system/functions)

// CreateFunctionRequest create a function in the swarm.
type CreateFunctionRequest struct {
	Service    string `json:"service"`
	Image      string `json:"image"`
	Network    string `json:"network"`
	EnvProcess string `json:"envProcess"`
}

Example:

For a quote-of-the-day type of application:

curl localhost:8080/system/functions -d '
{"service": "oblique", "image": "vielmetti/faas-oblique", "envProcess": "/usr/bin/oblique", "network": "func_functions"}'

For a hashing algorithm:

curl localhost:8080/system/functions -d '
{"service": "stronghash", "image": "functions/alpine", "envProcess": "sha512sum", "network": "func_functions"}'

Delete a function at runtime