Kubeless - Function as a Service with Kubernetes - Hello World example

What is Kubeless ?

You might have heard of serverless compute services from Amazon AWS ( Lambda ) and Google ( Google Cloud Functions ) , where you can create a function with some code and execute it using a URL from outside without worrying about underlying infrastructure for code execution.

Kubeless offers function as a service similar to above concepts on top of Kubernetes. Using kubeless , a Kubenetes-native serverless framework, you can create functions of any supported programming languages, deploy them using Kubeless and call them using a URL or command.

How to Install Kubeless ?

Its a two step process.

Step 1 : Create Kubeless namespace and create a functions Custom Resource Definition and launch a controller by executing below commands

$ export RELEASE=$(curl -s https://api.github.com/repos/kubeless/kubeless/releases/latest | grep tag_name | cut -d '"' -f 4)
$ kubectl create ns kubeless
$ kubectl create -f https://github.com/kubeless/kubeless/releases/download/$RELEASE/kubeless-$RELEASE.yaml

Step 2 : Install Kubeless CLI

$ 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/

Example of Kubeless function

Function can be written in any of the supported languages by Kubeless. To see the list of supported Runtimes, use following command.

$ kubeless get-server-config
INFO[0000] Current Server Config:
INFO[0000] Supported Runtimes are: python2.7, python3.4, python3.6, nodejs6, nodejs8, ruby2.3, ruby2.4, ruby2.5, php7.2, go1.10, dotnetcore2.0, java1.8, ballerina0.981.0

Function example using Java

// HelloWorld.java

package io.kubeless;

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

public class HelloWorld {
    public String sayHello(io.kubeless.Event event, io.kubeless.Context context) {
        return event.Data;
    }
}

Deploy the function

kubeless function deploy hello --runtime java1.8 --handler HelloWorld.sayHello --from-file HelloWorld.java

Check status of Function Deployment

$ kubeless function ls
NAME       NAMESPACE       HANDLER     RUNTIME    DEPENDENCIES    STATUS
hello      default         Foo.foo     java1.8                    1/1 READY

Calling the Function

# Using kubeless command
$ kubeless function call hello --data 'CodeForEach'
Hello CodeForEach !

# Using localhost proxy URL
$ kubectl proxy -p 8080 &
$ curl -L --data 'CodeForEach' localhost:8080/api/v1/namespaces/default/services/hello:http-function-port/proxy/
Hello CodeForEach !

# Using ingress to expose outside
$ kubeless trigger http create hello --function-name hello
$ kubectl get ing
NAME       HOSTS                         ADDRESS   PORTS   AGE
hello      hello.10.80.72.96.nip.io                80      8s
 

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

www.000webhost.com