OpenFaaS Tutorial: Build and Deploy Serverless Java Functions
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 supports 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 following
- Create a function from a code template
- Build the function as a Docker image
- Deploy the function
- Invoke the function
If you’re looking for Kubernetes native Serverless framework,check out Kubeless here.
How it works
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 from other services or call it directly from any web or mobile app.
- Using FaaS with Cloud services as event sources – Whenever any of the Event sources publish events that cause the function to be invoked.
- On-demand function invocation over HTTPS – In addition to invoking functions using event sources, you can also invoke respective functions over HTTPS. This can be done by defining a custom REST API and endpoint using API Gateway.
- On-demand function invocation (build your own event sources using custom apps) – User applications such as client, mobile, or web applications can publish events and invoke Serverless functions using the Mobile SDKs, such as the AWS Mobile SDK for Android.
- Scheduled events – You can also set up Serverless function to invoke your code on a regular, scheduled basis using the AWS Lambda console. You can specify a fixed rate (number of hours, days, or weeks) or you can specify a cron expression.
Now that we have got good understanding of what is Serverless and its use cases,lets 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.Its an open source project, it has gained large-scale adoption within the community.
Key Advantages :
- Functions can be deployed,invoked via UI portal
- Functions can be written in any language for Linux or Windows and package in Docker/OCI image format
- Runs on existing hardware or public/private cloud – Kubernetes and Docker Swarm native platforms
- Command Line Interface(CLI) available with YAML format for templating and defining functions
- In built Auto-scaling facility as demand increases
- Lastly,its supported by Open Community and No vendor lock-in
- API Gateway provides an external route into the functions and collects Cloud Native metrics through Prometheus.
- Function Watchdog is responsible for starting and monitoring functions in OpenFaaS
- API Gateway will scale functions according to demand by altering the service replica count in the Docker Swarm or Kubernetes API.
Ok,our next step is to deploy OpenFaaS stack and build our first function.
Docker CE 17.05 or later is required
- From the docker site,install the latest version of the docker for your platform.For instructions on Docker installation,check out here.
- Once the installation of docker is over,check the installation by running following command
docker run hello-world
docker --versionto check the version of the docker you’re running.
Step #1. Install OpenFaaS
- Initialize Docker Swarm by running below command
docker swarm init
- Clone OpenFaaS from Github and checkout latest release
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 this credentials so that we can use it for further steps for login to UI portal.
- Once all the Docker containers are up,Deployment is complete.You can check status using
- Once the installation is complete,login to UI portal at
Step #2. Install OpenFaaS
Install latest version of the CLI type using curl with url as below
curl -sL https://cli.openfaas.com | sudo sh
Step #3. Create new function
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 following command
faas-cli new --lang java8 <<function name>>
1 Stack File (hellojava.yml) & Folder would have been generated.
gateway– here we can specify a local / remote gateway address
functions– this block defines the functions in our stack
lang: 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 option to specify your Dockerfile.
handler– this is the folder / path to your handler.java file and any other source code you need
image– 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 account
Image – Handler file for Java function
I have not modified Handler file and leave as-is for now.
Step #4. Build function
Build the function using below command
faas-cli build -f <<stack file>>
Now you should see 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
Step #5. Deploy function
Since I’m deploying it on 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 registry.
For deployment use following command
faas-cli deploy -f <<stack file>>
Congrats! We have successfully created new function and deployed.
Test the function from OpenFaaS UI,you should see HTTP Status as
200 and Response body as
Congrats! We have successfully built 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!