4 min read

OpenFaaS announced on the 5th of September 2018 that they have released support for stateless microservices in OpenFaaS 0.9.0. They assert that managing FaaS functions and microservices will now be easier. A stateless microservice can be deployed as if it were a FaaS Function and managed by a FaaS framework or Platform such as OpenFaaS. Hence, no special routes, flags or filters are needed in the OpenFaaS CLI, Gateway API or UI.

Source: OpenFaaS

The upgrade came as a follow-up to two requests from the microservices community.

One of the users at Wireline.io raised a feature request to enhance the HTTP route functionality of functions and write functions to run on both, AWS Lambda and on OpenFaaS, without any additional changes. Then came the request from the CEO of GitLab, Sid Sijbrandi who wanted to learn more about Serverless and how it could benefit Gitlab. He was apprehensive whether OpenFaaS could be used to manage both, FaaS Functions and the microservices his team was more familiar (eg. Sinatra apps). He wanted to know more about scaling to zero when idle.

To address these requests, the OpenFaaS blog has given its viewers an example of deploying a Ruby and Sinatra guestbook backed by MySQL deployed to OpenFaaS with Kubernetes.

This is how the task can be done-

Users have to start of by creating the Sinatra stateless microservices. They can then go on to create a hello-world service by supplying their own Dockerfile and executing the following commands

$ mkdir -p sinatra-for-OpenFaaS/ \
 && cd sinatra-for-OpenFaaS/
$ faas-cli new --prefix=alexellis2 --lang dockerfile frank-says

They need to replace alexellis2 with their Docker Hub account or another Docker registry. This has to be followed by creating a Gemfile and the main.rb file:

./frank-says/main.rb:

require 'sinatra'

set :port, 8080
set :bind, '0.0.0.0'

open('/tmp/.lock', 'w') { |f|
 f.puts "Service started"
}

get '/' do
 'Frank has entered the building'
end

get '/logout' do
 'Frank has left the building'
End

 

Things to note on OpenFaaS workloads while doing this-

  • Bind to TCP port 8080
  • Write a file /tmp/.lock when ready to receive traffic

The Dockerfile will add a non-root user, add the Ruby source and Gemfile then installs the Sinatra gem. Finally, it will add a healthcheck on a 5-second interval and set the start-up command.

Users can now deploy the example using the OpenFaaS CLI.

  • Login with account details
$ docker login
  • Run the up command which is an alias for build, push and deploy.
$ faas-cli up --yaml frank-says.yml

Deploying: frank-says.

Deployed. 200 OK.
URL: http://127.0.0.1:8080/function/frank-says

To Deploy the Sinatra guestbook with MySQL, they need to execute-

$ git clone https://github.com/OpenFaaS-incubator/OpenFaaS-sinatra-guestbook \
 && cd OpenFaaS-sinatra-guestbook

Configure MySQL database details in ./sql.yml.

$ cp sql.example.yml sql.yml

Finally deploy the guestbook:

$ faas-cli up
http://127.0.0.1:8080/function/guestbook

The  URL given by the command above should be used to access the microservice.

Now, Sign the guest book using the UI and then reset the MySQL table at any time by posting to /function/guestbook/reset.

Source: OpenFaaS

The guestbook code stores its state in a MySQL table. A key property of FaaS functions and stateless microservices is that it can be restarted at any time without losing data.

For a detailed implementation of the guestbook example, head over to the OpenFaaS Blog post

How to Enable Zero-Scale?

To enable scaling to zero simply follow the documentation

Next, users have to add a label to their stack.yml file to tell OpenFaaS that your function is eligible for zero-scaling:

labels:
     com.OpenFaaS.scale.zero: true

Finally, redeploy the guestbook with faascli up. The faas-idler will now scale the function to zero replicas as soon as it is detected as idle. The default idle period is set at 5 minutes, which can be configured at deployment time.

OpenFaaS has also deployed a stateless microservice written in Ruby that will scale to zero when idle and back again in time to serve traffic. It can be managed in exactly the same way as OpenFaaS existing FaaS functions.

Thus, we have seen how the support for stateless microservices has made it easier for users to manage their microservices easily.

Head over to the OpenFaaS blog for a detailed explanation of deploying a simple hello-world Sinatra service and to gain more insights about the upgrade.

Read Next

6 Ways to blow up your Microservices!

Google, IBM, RedHat and others launch Istio 1.0 service mesh for microservices

Welcome Express Gateway 1.11.0, a microservices API Gateway on Express.js