Serverless Guides

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 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.

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 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.

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.
  • Inbuilt Auto-scaling facility as demand increases.
  • Lastly, it’s supported by Open Community and No vendor lock-in.

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

    Image – Validate Docker Installation

  • Run docker --version to check the version of the docker you’re running.

    Image – Check Docker version

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

    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
    Image – Deploy OpenFaaS

    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.

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

    Image – Deployment status

  4. Once the installation is complete, log in to UI portal at http://127.0.0.1:8080

    Image – OpenFaaS UI Portal

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

Image – FaaS CLI installation

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

Image – Create New Java Function

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

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

Image – Build function

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

Image – Post build complete,you can see Docker image

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

Image – Deploy the function

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!'

Image – Invoke Function from Portal

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:

Summary
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
Karthik

Allo! My name is Karthik,experienced IT professional.Upnxtblog covers key technology trends that impacts technology industry.This includes Cloud computing,Blockchain,Machine learning & AI,Best mobile apps, Best tools/open source libs etc.,I hope you would love it and you can be sure that each post is fantastic and will be worth your time.

Share
Published by
Karthik

Recent Posts

Finding the Right Time to Build Your Software Instead of Buy

There are few things as valuable to a business as well-designed software. Organizations today rely…

1 week ago

Innovators in Crypto: Prominent AI-Powered Coins

The cryptocurrency industry is being reshaped by the fusion of blockchain technology and artificial intelligence…

3 weeks ago

Top AI Design Tools Every Graphic Designer Should Use in 2024

Introduction Artificial Intelligence (AI) has also found its relevance in graphic design and is quickly…

2 months ago

Transforming Industries: The Integration of AI and Blockchain

Imagine a world where the brilliance of Artificial Intelligence (AI) meets the unbreakable security of…

2 months ago

How Can I Use Automation to Streamline My Digital Marketing Efforts?

In today’s fast-paced digital landscape, automation is not just a luxury but a necessity for…

2 months ago

Top 5 AI Technologies Transforming the Casino Gaming Landscape in 2025

The world of casino gaming has leveraged the emerging technology advancements to create immersive and…

3 months ago

This website uses cookies.