Serverless allows developers to build and run applications and services without thinking about the servers actually running the code. Serverless services, or FaaS (Functions-as-a-Service) providers, instrument this concept by allowing developers to upload the code while taking care of deploying running and scaling it.
AWS Lambda was the first one in the market to offer this kind. Serverless can help create an environment that allows DevOps teams to focus on improving code, processes, and upgrade procedures, instead of provisioning, scaling and maintaining servers.
Popular cloud providers that support Function As A Service (FaaS) as follows:
Today we are going to look at Open Source project OpenFaaS for building Serverless functions on top of container platforms. In this tutorial, we are going to deploy OpenFaaS stack and perform the following
If you’re looking for Kubernetes native Serverless framework, check out Kubeless here.
Quick Snapshot
Serverless services or FaaS lets you run code without provisioning or managing servers (but still servers are needed). You pay only for the compute time you consume there is no charge when your code is not running. You can run code for virtually any type of application or backend service all with zero administration. Just upload your code and FaaS provider would take care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger other services or call it directly from any web or mobile app.
Now that we have got a good understanding of what is Serverless and its use cases, let’s check out what is OpenFaaS and its offerings.
OpenFaaS (Functions as a Service) is a framework for building Serverless functions on top of Docker and Kubernetes container platforms. Any process can be packaged as a function enabling you to consume a range of web events without repetitive boiler-plate coding. It’s an open-source project, it has gained large-scale adoption within the community.
Ok, our next step is to deploy OpenFaaS stack and build our first function.
Docker CE 17.05 or later is required
docker run hello-world
docker --version
to check the version of the docker you’re running. The next set of steps is for Docker Swarm. Deployment Steps for Kubernetes is different, I will try to cover later in another article.
docker swarm init
git clone https://github.com/openfaas/faas && \ cd faas && \ ./deploy_stack.sh
As you can see during the installation, deployment scripts will create a username(admin) and password combination. Save the credentials so that we can use it for further steps for login to the UI portal.
Docker ps
command. http://127.0.0.1:8080
Install the latest version of the CLI type using curl with URL as below
curl -sL https://cli.openfaas.com | sudo sh
OpenFaaS CLI has a template engine built-in with which can create new functions in a given programming language. For the list of supported languages, please refer here.
To create new Java 8 Function, use the following command
faas-cli new --lang java8 <<function name>>
1 Stack File (hellojava.yml)
and Folder would have been generated.
hellojava.yml
gateway
– here we can specify a local/remote gateway addressfunctions
– this block defines the functions in our stacklang: java8
– even though Docker is used behind the scenes to package your function. You don’t have to write your own Dockerfile unless you want to but there is also an option to specify your Dockerfile.handler
– this is the folder/path to your handler.java
file and any other source code you needimage
– this is the Docker image name. If you are going to push to the Docker Hub or any other private registry change the prefix from hellojava
to include your Docker Hub accountHandler.java
Image – Handler file for Java function
I have not modified the Handler file and leave as-is for now.
Build the function using below command
faas-cli build -f <<stack file>>
Now you should see the output from the Docker Engine as it builds your function into an image in your local Docker library. You would be able to see the image on docker images
command
Since I’m deploying it on a single Docker Swarm cluster, there is no need to push images to a registry, images will be picked up from the local Docker library. If you’re trying it on multi-node cluster then, edit the stack file to include your repo name and push it to the registry.
For deployment use following command
faas-cli deploy -f <<stack file>>
Congrats! We have successfully created a new function and deployed it.
Test the function from OpenFaaS UI, you should see HTTP Status as 200
and Response body as 'Hello,world!'
Congrats! We have successfully built a new function and invoked it. There is much more to the OpenFaaS platform than what was covered here, but now you would have got a good idea of the basics of building Serverless functions. Do Checkout some of the examples built by the OpenFaaS community.
Like this post? Don’t forget to share it!
There are few things as valuable to a business as well-designed software. Organizations today rely…
The cryptocurrency industry is being reshaped by the fusion of blockchain technology and artificial intelligence…
Introduction Artificial Intelligence (AI) has also found its relevance in graphic design and is quickly…
Imagine a world where the brilliance of Artificial Intelligence (AI) meets the unbreakable security of…
In today’s fast-paced digital landscape, automation is not just a luxury but a necessity for…
The world of casino gaming has leveraged the emerging technology advancements to create immersive and…
This website uses cookies.