FaaS : Serverless Computing

Shubham Gupta
kodeyoga
Published in
6 min readMar 29, 2021

Kubeless an Open-Source way to achieve Serverless Computing

Serverless Computing: Kubeless, AWS Lambda, Google Cloud Function, Azure Function

Working as a developer we have this challenge of maintaining infrastructure resources for our application or we need to have coordination with the DevOps team for resource utilization for our applications. It’s always been a point of discussion between Developers and DevOps where DevOps always asking to reduce the resource required by the services on the other hand developer trying to justify his requirements of resources for the application.

FaaS: Function as a Service

Here comes the Function as a Service (FaaS) to rescue both Developers and DevOps. FaaS is a Cloud Computing Service which helps developers to develop code, execute code and manage application without the complexity of building and maintaining infrastructure. Developing an application in this manner is like developing and deploying applications in a Serverless architecture.

Let’s have a brief intro to Serverless Computing.

Serverless Computing

Serverless Computing is a cloud computing model in which the cloud provider allocates application resources and maintaining servers based on the application resource requirements on behalf of the developers. In Serverless Computing developers are kept abstracted from low-level infrastructure details and server management decisions.

In Serverless Computing, no computing resources will be allocated to the app if the app is not in use or in an idle state. So, the actual pricing will be based on the computing resources utilized by the app. “Serverless” could mislead in the sense that servers are still used by cloud service providers to execute code for developers. However, being a developer of such applications we are not concerned about the configuration, management, maintenance, or scaling of infrastructure.

Several Cloud service providers provide serverless computing as FaaS (Function as a Service) platform which just executes the code without maintaining any state. You might have heard about the popular offering of AWS known as AWS Lambda it’s an example of FaaS. On the same other cloud providers also have their own version for offering FaaS as a platform like Google Could Functions, IBM Cloud Functions, and Microsoft Azure Functions.

FaaS Usecases

Let’s cover some use-cases where FaaS could be helpful

  • Scheduled Operation: Time based batch processing
  • Data Processing (ETL Jobs)
  • Backend API’s
  • Chatbots
  • IT Automation

In all the above use-cases we can have different triggers (can be time-based or event-based) which will let the FaaS provider know to execute and allocate infra resource to the application created by the developer and serve the result to the end-user and again reclaim the resources allocated after completion of the operation.

PaaS Vs FaaS

Platform as a Service, application hosting services is similar to FaaS in terms of abstracting the infrastructure details from the developer. On the contrary, applications deployed using PaaS have at least an instance running to serve external requests. Scaling is achieved by bringing more instances of the application for which the developer will be charged accordingly.

Whereas in FaaS, it doesn’t require any instance of the application running. While an initial request may take longer (up to several seconds) as compare to an already hosted application, but the subsequent requests will be handled in milliseconds. And being a developer you pay only for the execution time of the function.

Cost Benefit

Using FaaS as a platform for deploying the application will be cost-efficient. As the developer will not have to pay just for running the application when it’s not in use. Due to this with FaaS, all server computation and resources will only be required when the application is serving requests. It’s like requesting Resources on-demand, by this you’ll pay for the duration the application was running and not for the idle state.

Kubeless

Kubeless

Kubeless is an open-source Function as a Service (FaaS) platform that runs on top of Kubernetes. Kubeless allows us to deploy small units of code (functions) without having a worry about the infrastructure. It’s a Kubernetes-native serverless framework that uses underlying k8s cluster infrastructure.

Kubeless includes support for Java, Python, Node.js, Ruby, and many more languages. Kubeless also supports events-triggers using Kafka and HTTP REST events. Kubeless uses a Custom Resource Definition to be able to create functions as custom Kubernetes resources. It then runs an in-cluster controller that watches these custom resources and launches runtimes on-demand. The controller dynamically injects the function’s code into the runtimes and makes them available over HTTP or via a PubSub mechanism.

Let’s have a hands-on example for installing and deploying a function using Kubeless on the k8s Minikube cluster.

Prerequisites to be installed before deploying a Kubeless function are Kubernetes CLI, Minikube for local setup. Let’s gets started

  1. Installing Kubeless and setting up environment
$ minikube start$ export RELEASE=$(curl -s https://api.github.com/repos/kubeless/kubeless/releases/latest | grep tag_name | cut -d '"' -f 4)
$ kubectl create ns kubeless
$ kubectl create -f https://github.com/kubeless/kubeless/releases/download/$RELEASE/kubeless-$RELEASE.yaml

Kubernetes objects created by Kubeless

$ kubectl get pods -n kubeless
NAME READY STATUS RESTARTS AGE
kubeless-controller-manager-5d9bf7f5c6-vl4d7 3/3 Running 3 12m
$ kubectl get deployment -n kubeless
NAME READY UP-TO-DATE AVAILABLE AGE
kubeless-controller-manager 1/1 1 1 13m
$ kubectl get customresourcedefinition
NAME CREATED AT
cronjobtriggers.kubeless.io 2021-03-20T16:17:54Z
functions.kubeless.io 2021-03-20T16:17:54Z
httptriggers.kubeless.io 2021-03-20T16:17:54Z

Installing kubeless CLI using export for Linux and Mac

$ export OS=$(uname -s| tr '[:upper:]' '[:lower:]')
$ curl -OL https://github.com/kubeless/kubeless/releases/download/$RELEASE/kubeless_$OS-amd64.zip && \
unzip kubeless_$OS-amd64.zip && \
sudo mv bundles/kubeless_$OS-amd64/kubeless /usr/local/bin/

2. Deploying a Sample Java Function: DemoJava.java

package io.kubeless;import io.kubeless.Event;
import io.kubeless.Context;
public class Demo {
public String hello(io.kubeless.Event event, io.kubeless.Context context) {
return "Hello java world!";
}
}

3. Deploy DemoJava function

$ kubeless function deploy demo-java --runtime java1.8 --handler Demo.hello --from-file DemoJava.java
INFO[0000] Deploying function...
INFO[0000] Function demo-java submitted for deployment
INFO[0000] Check the deployment status executing 'kubeless function ls demo-java'

4. Checking deployed function

$ kubectl get functions
NAME AGE
demo-java 8s
$ kubeless function ls
NAME NAMESPACE HANDLER RUNTIME DEPENDENCIES STATUS
demo-java default Demo.hello java1.8 1/1 READY

5. Checking the pod created for DemoJava function

$ kubectl get pods -l function=demo-java                                                                                                           
NAME READY STATUS RESTARTS AGE
demo-java-f647f84f7-wbbxl 1/1 Running 0 3m57s

6. Run function using CLI

$ kubeless function call demo-java
Hello java world!

Using http

$ kubectl proxy -p 8080 &
[1] 11470
Starting to serve on 127.0.0.1:8080
$ curl -L --data '{"Java": "Kube API"}' --header "Content-Type:application/json" localhost:8080/api/v1/namespaces/default/services/demo-java:http-function-port/proxy/
Hello java world!%

7. Debugging DemoJava Function

$ kubeless function describe demo-java
Name: demo-java
Namespace: default
Handler: Demo.hello
Runtime: java1.8
Label: {"created-by":"kubeless","function":"demo-java"}
Envvar: null
Memory: 0
Dependencies:
$ kubeless function logs demo-java
0 [pool-1-thread-6] INFO io.kubeless.Handler - Response: Hello java world!
355357 [pool-1-thread-19] INFO io.kubeless.Handler - Response: Hello java world!
583797 [pool-1-thread-28] INFO io.kubeless.Handler - Response: Hello java world!

8. Clean-Up: Deleting the deployed function

$ kubeless function delete demo-java

Final Thoughts

FaaS is the next phase in a paradigm shift of how solutions will be designed after Monolithic and Microservices Architecture Patterns. There could be several reasons that serverless computing may not fit your use-cases. But, if you have some use-cases where you need some compute based on the resource utilization, just give it a thought :smile:.

Sources:

  1. https://en.wikipedia.org/wiki/Serverless_computing
  2. https://en.wikipedia.org/wiki/Function_as_a_service
  3. https://medium.com/@BoweiHan/an-introduction-to-serverless-and-faas-functions-as-a-service-fb5cec0417b2
  4. https://kubernetes.io/docs/tasks/tools/
  5. https://minikube.sigs.k8s.io/docs/start/
  6. https://github.com/eldada/kubeless-demo
  7. https://qvik.com/news/serverless-faas-computing-costs/

Shubham Gupta is Senior Consultant(KodeYogi) at KodeYoga, and provide value consulting in Full stack development, Cloud Computing, Reactive programming following best practices to convert clients’ vision into reality. He is passionate about containers. In his free time he likes to do Photography. You can follow him on Twitter, Instagram or you can send him an email to shubham.gupta@kodeyoga.com

--

--