OpenFaaS Tutorial: Build and Deploy Serverless Java Functions

KarthiKeyan Shanmugam
5 min readOct 19, 2018

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

  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 other services or call it directly from any web or mobile app.

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 a good understanding of what is Serverless and its use cases, let’s 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. It’s 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.
  • Inbuilt Auto-scaling facility as demand increases.
  • Lastly, it’s supported by Open Community and No vendor lock-in.

Key Components

  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
  • Run docker --version to check the version of the docker you're running.

Step #1. Install OpenFaaS

The 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
  1. Clone OpenFaaS from Github and checkout latest release
  1. 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.
  2. Once all the Docker containers are up, Deployment is complete. You can check the status using Docker ps command.
  3. Once the installation is complete, log in to UI portal at http://127.0.0.1:8080

Step #2. Install OpenFaaS CLI

Install the latest version of the CLI type using curl with URL as below

curl -sL https://cli.openfaas.com | sudo sh

Step #3. Create a 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 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 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 an 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

Image — Handler file for Java function

I have not modified the 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 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

Step #5. Deploy function

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!

Additional Resources:

Originally published at https://www.upnxtblog.com on October 19, 2018.

--

--