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:
- 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.
- 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
- 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
- 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
- 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/
- 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:
- Receive an object event as its first parameter. This parameter includes all the information regarding the event source. In particular, the data key should contain the body of the function request.
- Receive a second object context with general information about the function.
- Return a string/object that will be used as a response for the caller.
- 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:
- kubeless function deploy hello tells Kubeless to register a new function named hello. The function will be accessible over the web using this name. Note that this doesn't need to be the same as the function name used inside the code (we'll specify that a little further along using the --handler option).
- --trigger-http tells Kubeless that the function will be invoked over HTTP. It's also possible to trigger the function in other ways, but that is not covered here.
- --runtime python2.7 tells Kubeless to use Python 2.7 to execute the code. Node is also supported as a runtime, with more to come in the future.
- --handler test.hello tells Kubeless the name of the function to call inside the code module. You can see in the preceding Python code that the function is called hello.
- --from-file /tmp/hello.py tells Kubeless to upload and use the /tmp/hello.py file as the source for the function. It is possible to pass a function in other ways as well.
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
- Now, let's invoke the function, as follows:
$ kubeless function call hello --data 'Hello Serverless!' --namespace kubeless
Hello Serverless!
- 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.