AWS Open Source Blog
Running FaaS on a Kubernetes Cluster on AWS using Kubeless
Serverless computing allows you to build and run applications and services without provisioning, scaling, or managing any servers. FaaS (Functions as a Service) is the runtime that enables serverless computing by firing off bits of code (functions) as they are needed, freeing the developer from managing infrastructure and enabling the developer to simply write business logic code. With the rise of Kubernetes, several open source FaaS platforms have been created. This two-part post will introduce one such FaaS, Kubeless, and how to get it up and running on a Kubernetes cluster on AWS.
– Arun
Kubeless is an open source Function as a Service (FaaS) solution built on top of Kubernetes. Inspired by AWS Lambda, Kubeless aims to bring the functional programming paradigm to Kubernetes users, by bringing function-based packaging into the container. The technical strength of Kubeless lies in the fact that it is a Kubernetes extension using the Custom Resource Definition API object. Kubeless uses k8s primitives to build a Lambda-like system which enables developers to deploy small units of code as functions without worrying about underlying infrastructure. When you use Kubernetes, using Kubeless to deploy small units of code is as straightforward as creating a Kubernetes resource. While you can use Kubeless to deploy HTTP webhooks, you can also deploy functions that can be triggered by events happening in your cloud, such as a file being uploaded to a storage bucket, a data stream, and so on.
Since Kubeless is built on top of upstream Kubernetes, it will be deployable in Amazon EKS, and any functions deployed can be triggered based on multiple AWS sources.
In this post we’ll show you how to run Kubeless on a Kubernetes cluster on AWS created using kops, and how to deploy a trivial function. In a follow up post, we will show you how to trigger your Kubeless functions based on events published to Kinesis streams.
Create a cluster on AWS with kops
kops is one of the provisioning tools available to create a Kubernetes cluster, with very advanced AWS support. Detailed documentation is available. Here we will only show the main steps, so do make sure to check out the full walkthrough, especially if this is your first time using kops. (For more information, read Manage Kubernetes Clusters on AWS Using Kops.)
Get the pre-reqs and set up your environment, create a kops IAM user with required roles, then create an S3 bucket which will store your cluster configuration, set two environment variables, and make sure you know which zone you are going to use, like so:
You are then ready to create your cluster:
Remember to delete it once you are all done:
Install Helm to Deploy an Ingress Controller
Using an Ingress controller allows us to expose functions to the public internet.
Get the Helm client from the GitHub release page, then create a service account with proper RBAC privileges.
Deploy an nginx Ingress controller with a Load Balancer service:
Once your Ingress controller is running, you can get the public DNS hostname used:
Deploy Kubeless
To deploy on Kubeless on the cluster, you need to create a dedicated namespace and then post a few manifests to the Kubernetes API server. Those manifests will create a custom resource definition to declare a new Function object kind, and will launch the Kubeless controller.
Below are the two command lines that show you how to create the namespaces and how to launch the latest version of Kubeless:
Finally, to be able to use Kubeless from the command line, you will need to install the CLI. You can get it from the Github release page or, if you are an OSX user, you can get it directly from brew.
Deploy a function
Let’s create a simple echo function in Python:
Deploy the function:
Soon the function will be ready and a corresponding pod will be running (note that you can configure autoscaling so that your functions scale based on requests or load).
Create a route to the function. Note that the function will be exposed publicly (adding TLS and authentication is possible but not explained here, please see the full documentation).
Once the function is up, you can call it using the Host header defined in the Ingress object and the public endpoint of the Ingress controller.
Conclusion
Congratulations! If you made it this far, you have a running Kubeless installation in an Kubernetes cluster on AWS. In a follow up post, we will show you the really exciting part: how to trigger your function based on cloud events, focusing on AWS Kinesis.
Sebastien Goasguen is a twenty year open source veteran. A member of the Apache Software Foundation, he worked on Apache CloudStack for several years before diving into the container world. He is the founder of Skippbox, a Kubernetes startup acquired by Bitnami. He is the creator of Kubeless and is its current tech lead and product manager. An avid blogger, he enjoys spreading the word about new cutting edge technologies. Sebastien is the author of the O’Reilly Docker Cookbook and co-author of the Kubernetes Cookbook.
The content and opinions in this post are those of the third-party author and AWS is not responsible for the content or accuracy of this post.