Kubeless: A Deep Dive into Serverless Kubernetes Frameworks (1)

Background

The serverless architecture allows developers to focus more on business expansion and innovation, without worrying about traditional server purchase, hardware maintenance, network topology, and resource resizing.

Introduction to Kubeless

Kubeless is a typical representative of these opensource serverless products. According to the official definition, Kubeless is a Kubernetes-native serverless framework that allows you to build advanced applications with FaaS on top of Kubernetes (K8s). According to the categorization of CNCF, Kubeless is a platform product.

Mechanism Explanation

This section takes Kubeless as an example to show you the basic capabilities that a serverless product must have, and how to implement them by using existing K8s features. These features include:

Agile Building

According to CNCF, the lifecycle of a function is presented in the following figure. Users only need to submit the source code and function specifications. The function building and development are generally completed by the platform. Therefore, the ability to quickly build executable functions based on source code submitted by users is an essential capability of a serverless product.

kubeless function deploy hello --runtime python2.7 \
--from-file test.py \
--handler test.hello

Function Resources and K8s Operators

A Kubeless function is a custom K8s object, precisely a K8s operator. The mechanism of K8s operators is provided as follows:

Function Composition

After a Kubeless function-controller detects an ADD event of a function, it triggers the corresponding handler to build the function. A function consists of multiple K8s objects, such as ConfigMap, Service, Deployment, and Pod, as shown in the following figure:

ConfigMap

The ConfigMap object of a function is used to describe the source code and dependencies of the function.

apiVersion: v1
data:
handler: test.hello
# Third-party Python library that the function depends on
requirements.txt: |
kubernetes==2.0.0
# Function source code
test.py: |
def hello(event, context):
print event
return event['data']
kind: ConfigMap
metadata:
labels:
created-by: kubeless
function: hello
# The ConfigMap name
name: hello
namespace: default
...

Service

The service object of a function is used to describe the access method of the function. The service is associated with pods that execute the function logic. The service type is ClusterIP.

apiVersion: v1
kind: Service
metadata:
labels:
created-by: kubeless
function: hello
# The service name
name: hello
namespace: default
...
spec:
clusterIP: 10.109.2.217
ports:
- name: http-function-port
port: 8080
protocol: TCP
targetPort: 8080
selector:
created-by: kubeless
function: hello
# The service type
type: ClusterIP
...

Deployment

The Deployment object of a function is used to deploy pods that execute the function logic. You can use this object to specify the number of replicas of the function.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
created-by: kubeless
function: hello
name: hello
namespace: default
...
spec:
# Specify the number of replicas of the function
replicas: 1
...

Pod

The pod object of a function contains one or more containers that execute the function logic.

...
volumeMounts:
- mountPath: /kubeless
name: hello
- mountPath: /src
name: hello-deps
volumes:
- emptyDir: {}
name: hello
- configMap:
defaultMode: 420
name: hello
...

Summary

1. Kubeless comprehensively uses various components of Kubernetes and the dynamic loading capabilities of different languages to build functions from the source code and to implement these functions.

Flexible Triggering

A mature serverless product must possess flexible triggering capabilities to cope with diversified characteristics of event sources, and be able to conveniently integrate new event sources. CNCF divides function triggering methods into the following categories. For more information, see Function Invocation Types.

HTTP Trigger

If you want to trigger the execution of a function by sending HTTP requests, you need to create an HTTP trigger for the function. Kubeless uses the K8s ingress mechanism to implement an HTTP trigger. Kubeless creates a CRD named httptriggers.kubeless.io to represent the HTTP trigger object. In addition, Kubeless contains a CRD controller named http-trigger-controller. It continuously monitors ADD, UPDATE, and DELETE events of HTTP triggers and Kubeless functions, and execute the corresponding operations upon detection of such events.

kubeless trigger http create http-hello --function-name hello --gateway nginx --path echo --hostname example.com
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
# The name of the ingress object, which was specified when you create the HTTP trigger.
name: http-hello
...
spec:
rules:
- host: example.com
http:
paths:
- backend:
# Direct the request to the service that has been created by Kubeless for the hello function. The type of this service is ClusterIP.
serviceName: hello
servicePort: 8080
path: /echo
curl --data '{"Another": "Echo"}' \
--header "Host: example.com" \
--header "Content-Type:application/json" \
example.com/echo
# Function return
{"Another": "Echo"}

Cronjob Trigger

If you wish to trigger the function execution regularly, you need to create a cronjob trigger for the function. K8s supports using CronJob to regularly run jobs. Kubeless uses this feature to implement the Cronjob trigger. Kubeless creates a CRD named cronjobtriggers.kubeless.io to represent the Cronjob trigger object. In addition, Kubeless has a CRD controller named cronjob-trigger-controller. It continuously monitors ADD, UPDATE, and DELETE events of Cronjob triggers and Kubeless functions, and execute the corresponding operations upon detection of such events.

kubeless trigger cronjob create scheduled-invoke-hello --function=hello --schedule="*/1 * * * *"
apiVersion: batch/v1beta1
kind: CronJob
metadata:
# The CronJob name, which was specified when you create the CronJob trigger.
name: scheduled-invoke-hello
...
spec:
# The CronJob execution schedule, which was specified when you create the CronJob trigger.
schedule: */1 * * * *
...
jobTemplate:
spec:
activeDeadlineSeconds: 180
template:
spec:
containers:
- args:
- curl
- -Lv
# HTTP headers, including some other information, such as event-id, event-time, event-type, and event-namespace.
- ' -H "event-id: xxx" -H "event-time: yyy" -H "event-type: application/json" -H "event-namespace: cronjobtrigger.kubeless.io"'
# Kubeless will create a service of the ClusterIP type for the function
# The endpoint can be spelled based on the name and namespace of the service
- http://hello.default.svc.cluster.local:8080
image: kubeless/unzip
name: trigger
restartPolicy: Never
...

Custom Trigger

If default triggers provided by Kubeless cannot meet your business needs, you can customize new triggers. You can perform the following procedures to build a new trigger:

i. This controller needs to constantly monitor CRUD operations on the source event trigger and the function, and properly deal with these operations. For example, if the controller detects an event that deletes the function, the controller needs to delete the trigger that is bound with the function.
ii. When an event occurs, trigger the execution of the associated function.

Summary

1. Kubeless provides some basic and commonly used triggers, and supports integration of custom triggers for other event sources.

Auto-Scaling

K8s implements automatic horizontal scaling of pods by using Horizontal Pod Autoscaler (HPA). Kubeless functions are deployed through K8s deployment, so they can use HPA to implement auto-scaling.

Measurement Data Retrieval

The first step of auto-scaling is to allow HPA to obtain a sufficient amount of measurement data. Currently, Kubeless functions support auto-scaling based on two measurement metrics: CPU usage and queries per second (QPS). The following figure shows how HPA obtains these two kinds of measurement data.

Usage of measurement data

After understanding how HPA retrieves the measurement data, you may wonder how HPA performs auto-scaling of functions based on such data.

kubeless autoscale create hello --metric=cpu --min=1 --max=3 --value=70
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2alpha1
metadata:
name: hello
namespace: default
labels:
created-by: kubeless
function: hello
spec:
scaleTargetRef:
kind: Deployment
name: hello
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 70
TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target)
kubeless autoscale create hello --metric=qps --min=1 --max=5 --value=2k
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2alpha1
metadata:
name: hello
namespace: default
labels:
created-by: kubeless
function: hello
spec:
scaleTargetRef:
kind: Deployment
name: hello
minReplicas: 1
maxReplicas: 5
metrics:
- type: Object
object:
metricName: function_calls
target:
apiVersion: autoscaling/v2beta1
kind: Service
name: hello
targetValue: 2k
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2alpha1
metadata:
name: hello-cpu-and-memory
namespace: default
labels:
created-by: kubeless
function: hello
spec:
scaleTargetRef:
kind: Deployment
name: hello
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 50
- type: Resource
resource:
name: memory
targetAverageValue: 200Mi

Auto-Scaling Policy

An ideal auto-scaling policy should be able to properly address the following scenarios:

Summary

1. The auto-scaling feature provided by Kubeless is a simple encapsulation of K8s HPA, and avoids directly exposing complex details of creating the HPA.

Conclusion

Kubeless provides a relatively complete serverless solution based on K8s. However, it still has some room to improve in comparison with some commercial serverless products.

Reference

https://www.alibabacloud.com/blog/kubeless-a-deep-dive-into-serverless-kubernetes-frameworks-1_594901?spm=a2c41.13014902.0.0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com