Go Serverless with Kubeless!
Go Serverless with Kubeless!
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 no longer need to worry about server maintenance, outages, and scaling bottlenecks.
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. The 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 a service that 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 that 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 great extent.
As the Kubeless framework works on top of Kubernetes, it allows you to easily deploy any function on the Kubernetes cluster.
The framework helps you to deploy Functions into pods within the 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 a Kubernetes cluster, so I have used Minikube for the example below. Once the Minikube cluster is up, install the Kubeless controller in it.
I. Download Minikube and start the 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 a 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 whether 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 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kubeless-controller 1 1 1 1 1m
To check the statefulness:
kubectl get statefulset -n kubeless NAME DESIRED CURRENT AGE 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 that 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 the function, give a description, and call the test function from handler.py file; so below are the changes:
service: test provider: name: kubeless runtime: python2.7 plugins: - serverless-kubeless (The above one is the plugin name as I am using serverless-kubeless plugin) functions: test: 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 the Minikube cluster via Kubeless.
Just check whether the function has been deployed or not successfully.
kubectl get function NAME KIND test Function.v1.k8s.io
7. Expose the function or API.
export K8SAPISERVER=https://192.168.99.100:8443 serverless invoke -f test -l
8. curl https://192.168.99.100:8443
9. If you have changed the function, you can redeploy the function with the following command:
serverless deploy
10. To check the logs of the function, use the following command:
serverless logs -f test
Kubernetes is an open-source platform that enables you to deploy containerized applications, scale them on the go, and reduce hardware requirements. So, Kubernetes was a perfect system for building 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 for an additional API server or API router/gateway, which means you don’t need to worry about the provisioning of servers.