OpenFaas (Function as a Service) is a framework for building serverless functions on the top of containers (with docker and kubernetes). With the help of open faas, it is easy to turn anything into a serverless function that runs on Linux or windows through Docker or Kubernetes. It provides a built-in functionality such as self-healing infrastructure, auto-scaling and the ability to control every aspect of the cluster.
Basically, OpenFaas is a concept of decomposing our applications into a small unit of work. OpenFaas is based on serverless, and the main benefits of serverless are that we do not have to manage the application infrastructure and on the other hand developers can concentrate on delivering business value. Serverless is perfect for IoT devices, microservice architecture or any other type of application that need to be efficient.
Now here the question arises that what is serverless? What do we mean by serverless? So when we look at serverless, we are talking about a new architectural pattern (a way of building systems).
At very starting we used to build monoliths, three-tier applications.
They were very heavy weight.
They are very slow to deploy.
After monolith, we broke these down into microservices.
They focused on being compostable.
We deployed them with Docker containers.
Is the next step in the evolution.
Functions is a small, discrete, reusable pieces of code that we can deploy.
Serverless functions don’t replace our monolith or microservice, they work best alongside our existing systems building integrations and helping events flow between our ecosystem.
Makes use of our existing services or third party resources.
Executes in few seconds. It based upon AWS Lambda’s default.
As we already discussed above microservices and functions. But if we see more in deep, serverless make it much easier to deploy our applications without having to worry about coordinating multiple parts of what our cloud providers offer.
If we are talking about serverless, it runs functions instead of servers and our cloud provider manages how they run and on the other hand, if we are talking about microservices, it is a development philosophy where applications are built through self-contained components.
Microservices focused architecture can Deployed on a serverless platform, a VM based platform, and a container based platform. This is a way to design applications and split them up. Serverless is even more granular than microservices and also provides a much higher degree of functionality.
Cloud is the Native Stack. Through Watchdog component and a Docker Container, any process can become a serverless function. Serverless doesn’t mean that we have to re-write the code in another programing language. We can run code in any language in which we want and wherever we want.
The Api Gateway is where we define all of our functions. They each get a public route, and it can be accessed from there. Api gateway also helps in scaling the functions according to the demand by changing the service replica count in the Docker Swarm or Kubernetes API.
The Function Watchdog embedded in every container and allows any container to become serverless. It acts as an entry point which enables HTTP requests to forward to the target process via Standard Input (STDIN) and on the other hand response is sent back to the caller by writing to Standard Output (STDOUT) from our application.
Prometheus underpins the whole stack and collects statistics. Now from those statistics, we can build dashboards, and when a specific function had a lot of traffics, it scales up automatically using the docker swarm.
To build and deploy functions to OpenFaas, CLI can be used. It is used for deploying functions easier and scriptable. Our only work is to build the Openfaas functions from a set of language templates such as CSharp, Node.js, Python, Ruby, Go and many more, we have to write only a handler file, and the CLI automatically creates a Docker image. For the API gateway, CLI is a RESTful client.
Fass-cli new :- this will create a new function in the current directory.
Fass-cli build :- this command will build docker images.
Fass-cli push :- this command will push docker images into a registry.
Fass-cli deploy :- this command will deploy the functions into a local or remote OpenFaas gateway.
Fass-cli remove :- this command will remove the functions from a local or remote OpenFaas gateway.
Fass-cli invoke :- this command will invoke the functions and read from standard input for the body of the request.
Fass-cli login :- this command will store basic auth credentials for OpenFass gateway.
Fass-cli logout :- this command will remove basic auth credentials for a given gateway.
Fass-cli store :- this command will allow browsing and deploying OpenFaas store functions.
Example:- for creating a template, build and deploy functions with OpenFaas CLI.
OpenFaas also supports Kubernetes. It is Kubernetes-native and uses deployments and services. We can quickly deploy kubernetes easily for kubernetes.
OpenFaas also supports Asynchronous Processing. Asynchronous Processing supports longer timeouts, can process work whenever resources are available instead of handling immediately. Within a few seconds, it can consume a large batch of work and process it.
With a callback URL, we can also use asynchronous calls.
Now Copy ‘of-watchdog’ to a where the main JAR file exists.
This code will read the data from the file.
javac Main.java && mode=afterburn port=8081 fprocess=”java Main” ./of-watchdog/of-watchdog.
ab -n 1000 -c 20 -p ./demo.txt http://localhost:8081/ This command will do the benchmarking.