Running serverless functions on premises using OpenFaas with Kubernetes

Earlier most of companies had monolithic architecture for their cloud applications (all the things in a single package). Nowadays companies are decomposing their monolithic app’s business functions in to several micro-services (Isolated component that has single responsibility). Thus thereafter serverless concept came with kind of different concept.

Serverless!! really running without a server ?

Nothing can be executed without servers. We need resources to do calculations 😎. Whereas the word “serverless” highlights these key principles

  1. Simplicity
  2. Less maintenance
  3. Inexpensive

Serverless function has an endpoint that triggers a function written in a programming language.We don’t need to worry about the server and execution environment. Cloud provider’s modules will take care of those.

Serverless on premises?

Running a serverless function is very easy. We can simply use AWS Lambda or GCP function or whatever from cloud providers. So how about if we do have some servers of our own. If you are running Kubernetes orchestration engine on servers, it’s very easy using OpenFaas.

OpenFass — OpenFaaS — Serverless Functions Made Simple for Docker & Kubernetes

Open Faas (Function as a service) is a framework for creating serverless functions on Kubernetes or Docker. It’s an open source project released under MIT License and written in Go.

Setting up OpenFaas on Kubernetes

According to my researches Rancher is the best tool to build a kubernetes cluster on premises(on bare metal servers or on virtual machines). Whole process can be done simply by hitting few kubectl commands with your keyboard 🧐.

Clone openfaas for Kubernetes repo

git clone https://github.com/openfaas/faas-netes
cd faas-netes

Download and deploy namespace yaml

kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml

Deploy openfaas services

kubectl apply -f ./yaml

Open rancher dashboard and see newly created workloads

Gateway and prometheus have web interfaces 😀.

OpenFass Architecture

OpenFaas watchdog module converts http messages to stdin then it will be passed to our functions and stdout vice versa. Prometheus is used for analytics. API Gateway handles the routing using client request-to-function mapping strategies. OpenFass runs each function in separate pods. Therefore if we scale up our pods, the replicas of specific function will be increased 😋.

Deploying a function

You can deploy any working binary as a function (Since it will deal with stdin and stdout). As an example let’s see about c#

Download C# Function template, it is having a simple application that gives the stdin content as the output

Create docker image and push to hub

cd csharp
docker build -t <docker-hub-username>/test-csharp .
docker login
docker push <docker-hub-username>/test-csharp

Deploy the function via openfaas dashboard

Once the function is deployed. You will be able to find the url

Open Postman ,simply POST few characters

Motivation for open source projects

Watch the story of Alex Ellis(founder of openfaas) and see how community gather around open source projects.

Happy Faas !! 😄