Go Serverless with Kubeless!

Aashish Chetwani
Posted by Aashish Chetwani on September 22, 2017

In the past few years, cloud computing has been a game changer in a number of ways and the emergence of serverless computing/architecture is just another example of what cloud tech has been able to achieve.

So, what is serverless?

The phrase "serverless" doesn’t mean servers are no longer involved; it simply means that developers don't need to worry about server maintenance, outages, and scaling bottlenecks anymore.

Serverless is getting lots of attention lately. It is positioned as the next evolution for building distributed applications, going beyond container-based systems and letting developers build application workflows based on triggers and events.

There are a lot of frameworks around serverless and some of them are open source. Most popular ones include IronFunctions, Gestalt, OpenLambda, Fission, Apache OpenWhisk, etc. I will be talking about a Kubernetes-native serverless framework - kubeless.

What is Kubeless?

It is an open source serverless framework running on the top of Kubernetes. It allows you to deploy a small bit of code without having to worry about underlying infrastructure. It uses Kubernetes resources to provide auto-scaling, routing, monitoring, and troubleshooting.

You just need to create the function and it allows you to deploy functions dynamically inside a container at runtime on the top of Kubernetes and it can be exposed via HTTP or by triggering an event.

Events are managed by Apache Kafka while HTTP triggers are exposed with Kubernetes services. This integration brings functions and events into your Kubernetes cluster and allows you to bring them logically together in service which can run on top of pods.

In the above architecture, a single master and a node cluster are built with multiple functions deployed inside multiple pods using kubeless. You don't need to worry about architecture and you can simply deploy the function inside the pods.

What is a Function?

It is a single unit of deployment or it’s merely a piece of code similar to microservices which can be used to perform actions like CRUD operations on the database or processing a file in a database. It is triggered by events and provides a good view of modularity and allows reusability to a high extent.

As kubeless framework works on the top of Kubernetes, it allows you to easily deploy any function on Kubernetes cluster.

The framework helps you to deploy Functions into pods within Kubernetes cluster.

What are Events?

Events are used to trigger the function. So, in Kubernetes if you want to list the pods or any services, you can use the following events:

Kubectl get pods (It will list all the pods in the namespace.)
Kubecl get services (It will list all the services in the namespace.)

Some other examples may include :

HTTP endpoint for API gateway( e.g. rest API), Scheduled timer

Installation and Deployment Steps:

kubeless runs on Kubernetes cluster, so for the below example, I have used Minikube. Once the Minikube cluster is up, install kubeless controller in it.

I. Download Minikube and start Minikube cluster.
The command for this is:

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
    -> Start Minikube cluster with the help of the following command 
       minikube start

Now you have running single-node cluster on your local machine, Minikube also configures 'kubectl' for you

II. To check whether the pod is up and running, use the following command:

kubectl get pod

III. Create the namespace and services:

#Before creating the namespace expose the release:
-> export RELEASE=v0.1.0
#Create the namespace:
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
    -> Start Minikube cluster with the help of the following command 
       minikube start

To check everything is created or not, use the following command:
To check whether pods are running or not:

  -> kubectl get pods -n kubeless

    NAME                               	  READY   STATUS     RESTARTS   AGE
    kafka-0                            	  1/1     Running    2      	1m
    kubeless-controller-1046320385-48z61  1/1     Running    0      	1m
    zoo-0                              	  1/1     Running    0      	1m

Make sure all pods are running and pass the health check of 1/1.

To check the deployment:

-> kubectl get deployment -n kubeless

  kubeless-controller 	1     	  1         1     	 1           1m

To check the statefulness:

kubectl get statefulset -n kubeless
    kafka 	1     	  1         1m
    zoo   	1     	  1   	    1m

To get the third party resource:

kubectl get thirdpartyresource

    NAME          	DESCRIPTION                          	 VERSION(S)
    function.k8s.io     Kubernetes Native Serverless Framework   v1

IV. Once the Kubeless installation is done, install node.js for serverless architecture because serverless is a part of node.js cli.

Note: Serverless runs on Node v4 or higher.

-> npm install -g serverless

check whether serverless architecture is installed or not, check the version of serverless.

V. Now try out an example from kubeless-serverless.

I am showing the working example which is available on git, but you can also create your own functions and deploy it on Kubernetes cluster with the help of the kubeless framework.

Clone this repo first:

1. git clone https://github.com/serverless/serverless-kubeless

2. I am going to use python function from the above examples, so move to kubeless-serverless/examples/get-python/ location. I have created a function which displays the list of array values; you can create your function also.

Open handler.py and add this function
def test():
list_element = [1,5,8,12,78,12]
for i in range(len(list_element)):
print list_element[i]

3. Edit the serverless.yml file in the function section. Change the name of function, give description and call test function from handler.py file; so below are the changes:

service: test
  name: kubeless
  runtime: python2.7
  - serverless-kubeless
(The above one is the plugin name as I am using serverless-kubeless plugin)
  description: 'print the array'
  handler: handler.test
(The above is the function name, you can edit the handler file and create your own
function inside the handler file, I have created function call test, so I am using test)  

4. Download the dependencies:

npm install

5. Deploy function:

serverless deploy

6. The function will be deployed to Minikube cluster via kubeless.
Just check whether the function has been deployed or not successfully.

kubectl get function
test   Function.v1.k8s.io

7. Expose the function or API.

serverless invoke -f test -l

8. curl

9. If you have changed the function, you can redeploy the function with the following command:

serverless deploy

10. To check the logs of function, use the following command:

serverless logs -f test

Kubernetes is an open source platform which enables you to deploy containerized applications, scale them on the go and reduce the hardware requirement. So, Kubernetes was a perfect system to build a serverless solution and kubeless didn’t disappoint us. With kubeless, you can create apps in minutes which will allow us to concentrate more on the development part rather than the architecture. And as kubeless uses Kubernetes primitive, so there is no need of additional API server or API router/gateway, which means you don’t need to worry about provisioning of servers.

Topics: Cloud, DevOps, & Containers, Minikube, Serverless, Kubernetes, Container, Docker, Kubeless

Leave Comment

Subscribe Email

    Post By Topic

    See all