lines of code

Kubeless: Role your own FaaS

Filed under:

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%>

 

Date posted: May 3, 2021

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Metal Toad is an Advanced AWS Consulting Partner. Learn more about our AWS Managed Services

About the Author

Nathan Wilkerson, VP of Engineering

Nathan started building computers, programming and networking with a home IPX network at age 13. Since then he has had a love of all things computer; working in programming, system administration, devops, and Cloud Computing. Over the years he's enriched his knowledge of computers with hands on experience and earning his AWS Certified Solutions Architect – Professional.

Recently, Nathan has transitioned to a Cloud Operations Manager role. He helps clients and internal teams interface with the Cloud Team using the best practices of Kanban to ensure a speedy response and resolution to tickets.

Schedule a Free Consultation

Speak with our team to understand how Metal Toad can help you drive innovation, growth, and success.