How to set up Kubeless

It is pretty easy to set up Kubeless. First, we download Kubeless from the release page, create a namespace, and then, through the YAML manifest found on the release page, we create the functions' custom resource definition and launch a controller. If you are setting up Kubeless on your personal laptop, then we have to use minikube to do this. So, let's see how to set up minikube, as we will be using it for our tutorials.

First, let's set up minikube by going to https://github.com/kubernetes/minikube. Once minikube is installed, we should be able to have a single node Kubernetes cluster inside a virtual machine. We should also be able to execute minikube commands through the Command Prompt. Let's create a cluster and then create Kubeless resources within this cluster. Then, we will create a simple Kubeless function and then deploy and invoke it. We will also set up a dashboard for minikube and see how the controller and function are created and deployed, respectively. Let's look at the how to implement this:

  1. Let's create a minikube local Kubernetes cluster, as shown in the following code:
$ minikube start
Starting local Kubernetes v1.9.0 cluster...
Starting VM...

Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.
  1. Next, create a minikube dashboard with the following command. The command trigger will open up a browser with the dashboard. The dashboard will show us the services, pods, and manager: 
$ minikube dashboard
  1. Now that we have a cluster, let's deploy Kubeless to it, as shown in the following code. There are several Kubeless manifests available for multiple Kubernetes environments (non-RBAC, RBAC, and OpenShift). We will be using the manifest for non-RBAC ( nonrole-based access control): 
$ export RELEASE=$(curl -sk https://api.github.com/repos/kubeless/kubeless/releases/latest | grep tag_name | cut -d '"' -f 4)
$ kubectl create ns kubeless
$ echo $RELEASE
$ kubectl create -f https://github.com/kubeless/kubeless/releases/download/v1.0.0-alpha.8/kubeless-non-rbac-v1.0.0-alpha.8.yaml
serviceaccount "controller-acct" created
customresourcedefinition "functions.kubeless.io" created
customresourcedefinition "httptriggers.kubeless.io" created
customresourcedefinition "cronjobtriggers.kubeless.io" created
configmap "kubeless-config" created
deployment "kubeless-controller-manager" created
  1. Now that we have deployed Kubeless, let's check whether it has deployed properly: 
$ kubectl get pods -n kubeless
NAME READY STATUS RESTARTS AGE
kubeless-controller-manager-c6b69df76-65gsh 1/1 Running 0 2m
$ kubectl get deployment -n kubeless
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubeless-controller-manager 1 1 1 1 3m
$ kubectl get customresourcedefinition
NAME AGE
cronjobtriggers.kubeless.io 3m
functions.kubeless.io 3m
httptriggers.kubeless.io 3m
  1. Next, we need to install the Kubeless CLI locally for deploying, invoking, and deleting the Kubeless functions, as shown in the following code: 
$ export OS=$(uname -s| tr '[:upper:]' '[:lower:]') 
$ curl -OL https://github.com/kubeless/kubeless/releases/download/$RELEASE/kubeless_$OS-amd64.zip
$ unzip kubeless_$OS-amd64.zip
$ sudo mv bundles/kubeless_$OS-amd64/kubeless /usr/local/bin/
  1. Let's create a function and deploy it. The following is a simple Python function that we will deploy and invoke. Create a function named test.py with the following content:
def hello(event, context):
print event
return event['data']

Functions in Kubeless have the same format, regardless of the language of the function or the event source. In general, every function will do the following:

  1. Now, let's deploy the function, and when we refresh the minikube dashboard, we should be able to see the hello function deployed there, as shown in the following code: 
$ kubeless function deploy hello --runtime python2.7 --from-file test.py --handler test.hello --namespace kubeless
INFO[0000] Deploying function...
INFO[0000] Function hello submitted for deployment
INFO[0000] Check the deployment status executing 'kubeless function ls hello'

The following list explains the various elements of the preceding code:

We will see the function custom resource being created through the following commands: 

$ kubectl get functions
NAME AGE
hello 2m

$ kubeless function ls --namespace kubeless
NAME NAMESPACE HANDLER RUNTIME DEPENDENCIES STATUS
hello kubeless test.hello python2.7 1/1 READY
  1. Now, let's invoke the function, as follows: 
$ kubeless function call hello --data 'Hello Serverless!' --namespace kubeless
Hello Serverless!
  1. We can also delete the function, as follows:
$ kubeless function delete hello --namespace kubeless
$ kubeless function ls --namespace kubeless
NAME NAMESPACE HANDLER RUNTIME DEPENDENCIES STATUS

So far, we have installed Kubeless locally, created a simple function, deployed it, invoked it, and undeployed it. In the next section, we will learn how to automate deployment using the Serverless Framework.