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

Background

Introduction to Kubeless

Mechanism Explanation

Agile Building

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

Function Resources and K8s Operators

Function Composition

ConfigMap

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

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

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

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

Summary

Flexible Triggering

HTTP Trigger

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

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

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

Auto-Scaling

Measurement Data Retrieval

Usage of measurement 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

Summary

Conclusion

Reference

--

--

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