others-serverless kubeless tutorial for beginners hello world

In this tutorial , I would demo how to setup kubeless and make a hello-world function with kubeless.

Let’s begin.

#1 Assumption

You should have used kubernetes, and have a working kubernetes custer.

To verify this, just run this in your computer:

➜  kubectl cluster-info

Kubernetes master is running at ...

#2 Install kubeless

Now let’s install kubeless controllers in kubernetes:

According to this document, you can install kubeless with RBAC enabled by this command:

➜  kubectl create ns kubeless
➜  kubectl create -f https://github.com/kubeless/kubeless/releases/download/v1.0.6/kubeless-v1.0.6.yaml 

To verify your installation, just run this command:

➜  kubectl get deployment -n kubeless

NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
kubeless-controller-manager   1/1     1            1           41h

#3 Install kubeless CLI

Now you should install kubeless CLI in your computer, which is used to connect to the remote kubeless service in kubernetes.

For MAC or Linux users:

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/

Verify the kubeless CLI installation:

➜  kubeless --help
Serverless framework for Kubernetes

  kubeless [command]

#4 Deploy a python hello world function to kubeless

Define a python hello world kubeless function:

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:

Receives an object event as their first parameter. This parameter includes all the information regarding the event source. In particular, the key ‘data’ should contain the body of the function request. Receives a second object context with general information about the function. Returns a string/object that will be used as response for the caller.

save it as hello.py.

Run this command to deploy the function in kubeless:

➜  kubeless function deploy hello --runtime python2.7 --from-file hello.py --handler hello.hello --namespace kubeless

The explanation of the function deploy command:

hello: This is the name of the function we want to deploy. –runtime python2.7: This is the runtime we want to use to run our function. Available runtimes can be found executing kubeless get-server-config. –from-file test.py: This is the file containing the function code. It is supported to specify a zip file as far as it doesn’t exceed the maximum size for an etcd entry (1 MB). –handler test.hello: This specifies the file and the exposed function that will be used when receiving requests. In this example we are using the function hello from the file test.py.

Verify the function is installed:

➜  kubeless kubeless function ls -n kubeless
hello   kubeless       hello.hello  python2.7                   READY

Or you can just use kubectl to view the function in kubernetes:

➜  kubectl get functions -n kubeless
hello   14s

Now call the function:

➜  kubeless kubeless function call hello --data "hello world" -n kubeless
hello world

You can delete the function like this:

➜  kubeless function delete hello -n kubeless

#5 Deploy a java helloworld function in kubeless

Java functions must use io.kubeless as package and should import both io.kubeless.Event and io.kubeless.Context packages. Function should be made part of a public class and should have a function signature that takes Event and Context as inputs and produces String output.

Let’s define a Java function like this:

package io.kubeless;

import io.kubeless.Event;
import io.kubeless.Context;

public class Foo {
    public String foo(io.kubeless.Event event, io.kubeless.Context context) {
        return "Hello world!";

Save the file as Foo.java in current working directory(It’s unnecessary to create sub folders as io/kubeless).

Once you have Java function meeting the requirements it can be deployed with Kubeless as below. Where handler part –handler Foo.foo takes Classname.Methodname format.

➜  kubeless function deploy javahello --runtime java1.8 --handler Foo.foo --from-file Foo.java -n kubeless

Now call the java function in kubeless:

➜  kubeless function call javahello -n kubeless

You can delete the function like this:

➜  kubeless function delete javahello -n kubeless