All major cloud offerings have a Function as a Service (FaaS) tool. AWS offers Lambda, Azure offers Functions, GCP offers Cloud Functions. At Metal Toad we have used FaaS to back APIs, perform ETL, and execute work from Queues.
Function as a Service (FaaS) is an important building block for engineers. It forces engineers to build in small microservice-sized blocks. These blocks can be independently tested for a given input and output. This small modularity means that they can develop features and bug fixes in small chunks that are easy to deploy and QA.
A quick disclaimer — before I jump in to rolling your own — I strongly recommend using one of the cloud-built offerings out there. They are designed and tested for scale and resilience past the point most organizations need. But for multiple reasons, using a cloud offering might not be an option for everyone. For those instances please consider using Kubeless.
To get started I’ll assume the reader has at least a passing understanding of Kubernetes and a working local install. A quick start guide to Kubeless can be found here: https://kubeless.io/docs/quick-start/
I started with that but have updated the below commands to work with python 3.8
Handy commands:
Check Kubeless pods or deployments
$ kubectl get pods -n kubeless
$ kubectl get deployment -n kubeless
First install Kubeless:
$ 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
Create a Python Function to test with:\
def hello(event, context):
print(event)
return event['data']
Then you can deploy your function:
$ kubeless function deploy hello --runtime python3.8 --from-file test.py--handler test.hello
You can then test your function:
$ kubeless function call hello --data 'Hello world!'
Congratulations you now have a function.
A few tips and hints.
If your function requires some dependencies, you can give it a requirements file:
$ kubeless function deploy hello --runtime python3.8 --from-file test.py--handler test.hello --dependencies requirments.txt
This will have the function download required packages when it boots. There is supposed to be a way to use a zip file for code and the dependencies, but I haven’t tested. I suspect that would allow them to pods to come online faster when scaling.
At the time of writing this, Kubeless autoscale class doesn’t appear to be working with the most recent kubernetes version: Autoscaling is not working on version v1.0.8 and kubectl 1.19.6 · Issue #1219 · kubeless/kubeless (github.com)
To get around this you can create your own Kubernetes HPA group:
$ kubectl autoscale deployment <function name> --min=1 --max=10 --cpu-percent=<value%>