Deploying a Kubeless function using Serverless templates
Serverless Framework is a framework that helps building serverless applications and deploying them in a consistent manner to different clouds (Google, AWS, Azure, etc.).
At Bitnami we have been working alongside the Serverless team to include Kubeless, the serverless framework for Kubernetes, in that list, helping people, who are already familiar with the Serverless Framework, to deploy their applications on-premise on top of Kubernetes.
In a previous blog post, we introduced the Kubeless plugin for Serverless and explained how we could deploy a function using the framework, which included having to create a serverless.yml file for your particular function.
From Serverless Framework version 1.20 onwards, building and deploying functions to Kubeless has become even easier, with the introduction of Kubeless templates. In this blog post, we will show you how to build and deploy a Python function using templates.
This posts assumes you have Kubeless installed in your Kubernetes cluster. You can follow the installation process for Kubeless in its README.md file.
The first thing we will need to do is to install the Serverless CLI globally:
Once installed, we will create the needed scaffolding for our Python function, using the right template and specifying an optional path for your service:
This will create the needed files in the new-project folder.
Inside the folder you will find the following files: serverless.yml, handler.py and package.json.
serverless.yml is the Serverless Framework description file for your function.
handler.py is the file where you define the Python function to call as a response to the HTTP event. The template creates for you an example hello function for you:
package.json is the npm package definition of our functions with all their dependencies, including the kubeless-serverless plugin.
Let’s try deploying the example function to Kubeless and run it:
We can see that there is a new Service in our cluster with the same name as our function and a Function object:
Now, let’s run the function and get the logs:
You can open up a separate tab in your console and stream all logs for a specific Function using this command:
That’s it! You can see how easy and straightforward is to deploy a Python function in Kubeless using the Serverless Framework templates.
Also for Nodejs functions
There is also a Nodejs template for Kubeless. You can create your scaffolding for your Nodejs application specifying the kubeless-nodejs template:
In version 1.20 of the Serverless Framework, there is an open bug in the kubeless template for node and the runtime name was wrong. Check your serverless.yml file to make sure the name of the runtime version is nodejs6 or modify it to that before deploying the function. This will be fixed in the next version of the Serverless Framework.