Build advanced applications with FaaS on top of Kubernetes

Build advanced applications with FaaS on top of Kubernetes

Published Mar 11, 2019 in microsoft, azure
Scroll

I’m really excited to share with you some of the learnings I did the past week. Function-as-a-Service (FaaS) has been a recent hot topic in the industry and there have been customer asks to also have Functions running on-premise. Azure Functions provides a new way for you to take advantage of the simplicity and flexibility of the Azure Functions programming model on-premises.


In my previous articles, I have demonstrated Azure Functions capabilities as well as building scalable applications with Kubernetes. In this article, I want to share how you can build advanced applications with Functions on top of Kubernetes. There are plenty of use case we can build with Kubernetes. What’s more exciting than having your own Functions executing on top of Kubernetes which provides scalability and agility.


Kubeless, a Bitnami Project


Let me introduce you to Kubeless, a Kubernetes Native Serverless Framework built by Bitnami. Kubeless is a framework that lets you deploy small bits of code without having to worry about the underlying infrastructure plumbing. It leverages Kubernetes resources to provide auto-scaling, API routing, monitoring, troubleshooting and more. This is a really interesting framework as you can now execute Functions code right off Kubernetes. I was playing with this last week as I was trying to find a solution for my customer building a scalable microservice architecture with Kubernetes, and since they were on Java, there were 2 reasonable and wise options.

  1. Implement and run Java on Azure Functions (either on-premise or on Azure)
  2. Implement and run Java on Kubeless (an awesome approach)

Getting Started

In this article, I will assume that you have your Kubernetes cluster deployed. We will need to

  1. Deploy Kubeless to a Kubernetes Cluster
    I will deploy a Kubeless to Kubernetes with RBAC enabled. You could choose a deployment that is Non-RBAC enabled as well.
$ kubectl create ns kubeless
$ kubectl create -f https://github.com/kubeless/kubeless/releases/download/v1.0.2/kubeless-v1.0.2.yaml 


Depending on your OS architecture, you will need to download the Kubeless CLI so that we can execute that later on to our Kubernetes Cluster.

  1. Download Kubeless CLI from the release page
    In my case, I have downloaded the Windows x64 architecture release.
  2. Extract the content and add the kubeless binary to the system PATH.

Deploy Functions with Kubeless Runtime

Let’s deploy our first Function with Kubeless. Kubeless has support for runtimes in different states. In this example, we will attempt to deploy a Java function with dependency on org.joda.time.LocalTime.

$ kubeless get-server-config
[36mINFO[0m[0000] Current Server Config:
[36mINFO[0m[0000] Supported Runtimes are: ballerina0.981.0, dotnetcore2.0, dotnetcore2.1, go1.10, java1.8, nodejs6, nodejs8, php7.2, python2.7, python3.4, python3.6, python3.7, ruby2.3, ruby2.4, ruby2.5, jvm1.8, nodejs_distroless8, nodejsCE8, vertx1.8

Each runtime is encapsulated in a container image.

Dive into Code

Note: Java functions must include io.kubeless as package and should import both io.kubeless.Event and io.kubeless.Context packages.

Java implementation

Pom dependency

Point: handler argument –handler Foo.foo takes Classname.Methodname format.

$ kubeless function deploy hellojava --runtime java1.8 --handler HelloWorld.foo --from-file .\HelloWorld.java --dependencies .\pom.xml

Call Function

Let’s make sure we have the function deployed.

$ kubectl get functions
NAME        AGE
hellopy       4d
hellojava   4d
hellojs     4d

$ kubeless function ls
NAME            NAMESPACE       HANDLER         RUNTIME         DEPENDENCIES                                                    STATUS
hellopy         default         hello.hello     python2.7                                                                       1/1 READY
hellojava       default         HelloWorld.foo  java1.8         <project xmlns="http://maven.apache.org/POM/4.0.0"              1/1 READY
                                                                xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins...      
                                                                xsi:schemaLocation="http://maven.apache.org/POM...      
                                                                http://maven.apache.org/xsd/maven-4.0.0.xsd">
                                                                  <modelVersion>4.0.0</modelVersion>
                                                                  <artifactId>function</artifactId>
                                                                  <name>function</name>
                                                                  <version>1.0-SNAPSHOT</version>
                                                                  <dependencies>
                                                                     <dependency>
                                                                       <groupId>joda-time</groupId>
                                                                       <artifactId>joda-time</artifactId>
                                                                       <version>2.9.2</version>
                                                                     </dependency>
                                                                      <dependency>
                                                                          <groupId>io.kubeless</groupId>
                                                                          <artifactId>params</artifactId>
                                                                          <version>1.0-SNAPSHOT</version>
                                                                      </dependency>
                                                                  </dependencies>
                                                                  <parent>
                                                                    <groupId>io.kubeless</groupId>
                                                                    <artifactId>kubeless</artifactId>
                                                                    <version>1.0-SNAPSHOT</version>
                                                                  </parent>
                                                                </project>                                              
hellojs         default         hellojs.foo     nodejs6                                                                         1/1 READY

Now, let’s execute our function.

$ kubeless function call hellojava --data "Hi there, Kubeless!"
Hi there, Kubeless! - Current local time is: 05:28:34.932

Clean Up Resources

Now that we have got a basic understanding of using Kubeless with Kubernetes. We can delete the function deployed.

$ kubeless function delete hellojava

Summary

In this article, I shared on how we can set up Kubeless on Kubernetes, writing our first Java function with dependency and deploying our function onto Kubernetes with Kubeless.

Next, I will touch on Kubeless support with ingress controller so that we can expose and secure our function publicly.


Cheers!

High Scalable Stream Processing with Apache Kafka and Azure Kubernetes Service - Part 2

Building a high scalable stream processing pipeline with Kubernetes

Published Mar 3, 2019 in microsoft, azure