OpenFaaS Tutorial: Build and Deploy Serverless Java Functions

Introduction

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

  1. Create a function from a code template
  2. Build the function as a Docker image
  3. Deploy the function
  4. 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.

AWS Lambda / How it works
Image – AWS Lambda / How it works
Image- Another Sample

Prominent use-cases

  • 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 Introduction

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.

Open FaaS Logo
Image – Open FaaS Logo

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

Key Components

Key components
Image – Key components / Source – OpenFaaS
  1. API Gateway provides an external route into the functions and collects Cloud Native metrics through Prometheus.
  2. Function Watchdog is responsible for starting and monitoring functions in OpenFaaS
  3. 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.

Pre-requisites

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

    Validate Docker Installation
    Image – Validate Docker Installation
  • Run docker --version to check the version of the docker you’re running.

    Check Docker version
    Image – Check Docker version

Step #1. Install OpenFaaS

Next set of steps is for Docker Swarm.Deployment Steps for Kubernetes is different,I will try to cover later in another article.

  1. Initialize Docker Swarm by running below command
    docker swarm init

    Initialize Docker Swarm
    Image – Initialize Docker Swarm
  2. Clone OpenFaaS from Github and checkout latest release
    git clone https://github.com/openfaas/faas && \
      cd faas && \
      ./deploy_stack.sh
    Deploy OpenFaaS
    Image – Deploy OpenFaaS

    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.

  3. Once all the Docker containers are up,Deployment is complete.You can check status using Docker ps command.

    Deployment status
    Image – Deployment status
  4. Once the installation is complete,login to UI portal at http://127.0.0.1:8080

    OpenFaaS UI Portal
    Image – OpenFaaS UI Portal

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
 FaaS CLI installation
Image – FaaS CLI installation

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>>
Create New Java Function
Image – Create New Java Function

1 Stack File (hellojava.yml) & Folder would have been generated.

Stack File
Image – Stack File

hellojava.yml

  • 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

Handler.java

Handler file for Java function

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>>
Build function
Image – Build function

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

Post build complete,you can see Docker image
Image – Post build complete,you can see Docker image

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>>
Deploy the function
Image – Deploy the function

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 'Hello,world!'

Invoke Function from Portal
Image – Invoke Function from Portal

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!

Additional Resources:

Summary
OpenFaaS Tutorial: Build and Deploy Serverless Java Functions
Article Name
OpenFaaS Tutorial: Build and Deploy Serverless Java Functions
Description
OpenFaaS (Functions as a Service) is a framework for building serverless functions on top of Docker and Kubernetes container platforms.
Author
Publisher Name
Upnxtblog
Publisher Logo