FaaS: Setup Kubeless and Serverless in OpenShift

Serverless is a framework that provides a unique way to build and operate serverless applications with different Cloud providers. We will be using a plugin to extend the Serverless command line tool with a Kubernetes-based solution. The serverless setup will be deployed to a local OpenShift instance.

In the following, we will be setting up a local Minishift instance. The FaaS functionality is provided by the Serverless Framework and Kubeless (as a Serverless plugin).

Kubeless is a Kubernetes-native way to deploy and manage your serverless functions. We will use a provider plugin for Serverless to build serverless applications on top of Kubernetes. Kubeless uses Custom Resource Definitions to represent functions as custom Kubernetes resources. For testing you can use Minishift.

What we will need

Install Minishift

OpenShift is a PaaS platform by RedHat that leverages container technologies such as Docker and Kubernetes. Minishift is a great way to setup a single-node OpenShift instance locally. It is basically a Linux virtual machine configured with OpenShift.

  • You need a machine capable of performing virtualization.
  • If you want to use HyperV, you need a compatible Windows edition (e.g. Windows 10 Pro. Windows 10 Home can’t.)
  • At least 4GB of RAM
  1. Download the latest version of Minishift.
  2. Extract its content to your preferred location (for example C:\minishift)
  3. Add the minishift binary to your PATH environment variable.
  4. Setup the virtualization with HyperV with Powershell in admin mode:
PS> ([adsi]”WinNT://./Hyper-V Administrators,group”).Add(“WinNT://$env:UserDomain/$env:Username,user”)

Next you need to create an external switch your VM will need to connect to the Internet. Name the switch “minishift”. Set up HyperV as default driver:

PS> minishift config set hyperv-virtual-switch "minishift"

A new folder .minishift is created in your user directory (e.g. C:\Users\<username>\.minishift)

  1. Run the minishift start command (this might take quite a while).
Minishift start console output

Once the VM is started, further information on how to access it is displayed. Besides that, minikube ip outputs the IP address to access the OpenShift UI in the browser.

Setup Serverless Framework

First, install the Serverless CLI globally with NPM.

$ npm install serverless -g

Second, create a Serverless account and log in:

$ serverless login

This opens the Serverless dashboard in your browser.

Setup the Kubeless Plugin for Serverless

Before we can install the plugin, we have to create a service. Functions and events are grouped together in services and configured with a serverless.yml file. We create a new service using a template, specifying a unique name, and an optional path. We will be using the python template for this example.

$ serverless create --template kubeless-python --path kubeless-service --name kubeless-service 

Install the Kubeless plugin for Serverless:

$ cd kubeless-service
$ serverless plugin install -n serverless-kubeless 

Configure the OpenShift Cluster for Kubeless

By default, any non-empty username and password can be used to login to the local cluster. To login as administrator, use the system account:

$ oc login -u system:admin -s <server-address>:<port>

In case, you are using several OpenShift clusters, you have to select the correct context first.

$ oc config use-context minishift // in case the context is called minishift

Next, create a new Openshift project:

$ oc new-project kubeless  

Download and install the Kubeless command line tool.

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

Set permissions for the develop user:

$ oc project kubeless
$ oc policy add-role-to-user cluster-admin developer
$ oc policy add-role-to-user admin developer

Working with Serverless

As we have finally setup our local development environment, we will now deploy the default function from our kubeless-service directory.

Go into the created folder and install the NPM dependencies as defined by the package.json file.

$ cd kubeless-service
$ npm install 

The created directory should have the following content.

Content of the kubeless-service directory

The serverless.yml file specifies the resulting service including its provider, any plugins, the runtime, and the function handler.

service: kubeless-service
  name: kubeless
  runtime: python2.7
  - serverless-kubeless
    handler: handler.hello

Let’s deploy the function to the Minishift cluster. For each function in the serverless.yaml file, Kubeless will create a Kubernetes function object, and for each HTTP event, it will create a Kubernetes service.

$ cd kubeless-service
$ serverless deploy

There might be an issue with Kubeless putting a securityContext in the deployment file. For OpenShift, the UID of the function executing unprivileged user is automatically set. To solve this, edit the deployment yaml file and remove the part containing the securityContext.

Invoke your function with

$ serverless invoke -f experiment -l 

Further resources

  • https://github.com/kubeless/kubeless/tree/master/examples
  • https://forum.serverless.com/
  • https://serverless.com/examples/