Deploy OpenFaaS Serverless Framework on Kubernetes

Posted on 75 views

Welcome to this guide on how to deploy OpenFaaS Serverless Framework on Kubernetes. But before we commence on the crux of this tool, I would like to explain a few concepts.

Serverless is a cloud computing design whereby business logic is written as a function that can be executed in a stateless manner. Serverless, as the word goes, does not literally mean that we are executing code without servers, It means that we do not have to provision hardware infrastructure when writing code. This provisioning and management of servers are done by third-party services.

What is FaaS/OpenFaas?

FaaS stands for function-as-a-Service. This is a serverless model that enables one to build, run and manage applications/ serverless functions without the complex infrastructure associated with it i.e building and deploying microservices.

OpenFaas is an open-source framework for building serverless functions with Docker and Kubernetes.

Kubernetes in the mix?

Kubernetes is an open-source portable and extensible platform that is used for automation deployment, scaling e.t.c

The main features of serverless architectures are:

  • No servers, VMs, or containers
  • Instrumentation: the logs and metrics are collected
  • Functions as the building blocks
  • No storage
  • Bring Your Own Code (BYOC): allows one to run any code
  • No cost for idle time, it only incurs costs when servers are running
  • Scalability with the request

Serverless architectures are used in:

  • Webhooks
  • Real-time chat applications
  • Check-out and payment
  • Data processing

Now we are set to begin this guide. By the end of this guide, you should be able to deploy OpenFaaS Serverless Framework on Kubernetes.

Step 1: Install and Set up Kubernetes Cluster (Skip if you already have Kubernetes Cluster)

In this guide, we will install and set up the Kubernetes cluster using Minikube. If you prefer other methods of deploying Kubernetes Cluster checkout our guides in the following links:

Running a Kubernetes Cluster using Minikube

When doing Minikube Kubernetes Cluster, first install snap:

##On RHEL 8/Centos 8/ Rocky Linux 8
sudo yum install epel-release
sudo yum install snapd

##On Debian/Ubuntu
sudo apt install snapd

##On macOS
brew install snap

Create a symbolic link for snap as shown.

sudo ln -s /var/lib/snapd/snap /snap
echo 'export PATH=$PATH:/var/lib/snapd/snap/bin' | sudo tee -a /etc/profile.d/snap.sh
source /etc/profile.d/snap.sh

Start an enable snapd service:

sudo systemctl enable --now snapd.service snapd.socket

Install kubectl using snap

$ sudo snap install kubectl --classic
2021-08-24T18:39:53+03:00 INFO Waiting for automatic snapd restart...
kubectl 1.21.4 from Canonical installed

Install MiniKube on your system.

wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 -O minikube

Change permissions and copy it to the path /usr/local/bin/

sudo chmod 755 minikube
sudo cp minikube /usr/local/bin/

Install minikube as shown.

sudo chmod +x /usr/local/bin/minikube

Check the version of Minikube:

$ minikube  version
minikube version: v1.23.2
commit: 0a0ad764652082477c00d51d2475284b5d39ceed

Run Kubernetes using Minikube

Now run the cluster on your system. Ensure that you have a hypervisor i.e VirtualBox/KVM installed on your system.

Install Virtualbox on your system with the aid of the guides below.

Install KVM hypervisor on your system with aid from the guides below

With KVM/Virtualbox installed, run Minikube specifying the driver to use from your hypervisor as below

  1. With VirtualBox driver

Run the commands below to start Minikube instance using VirtualBox driver:

$ minikube start --driver=virtualbox

2. With KVM driver

Add your user to the libvirt group

sudo usermod -aG libvirt $USER
newgrp libvirt

The run the commands below to start Minikube instance using KVM driver:

$ minikube start --driver=kvm2

Sample Output for Virtualbox:

...........
🔥  Creating virtualbox VM (CPUs=2, Memory=2900MB, Disk=20000MB) ...
🐳  Preparing Kubernetes v1.21.2 on Docker 20.10.6 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: default-storageclass, storage-provisioner
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

Check the installed Kubectl version.

$ kubectl version
Client Version: version.InfoMajor:"1", Minor:"22", GitVersion:"v1.22.2", GitCommit:"8b5a19147530eaac9476b0ab82980b4088bbc1b2", GitTreeState:"clean", BuildDate:"2021-09-15T21:38:50Z", GoVersion:"go1.16.8", Compiler:"gc", Platform:"linux/amd64"
The connection to the server localhost:8080 was refused - did you specify the right host or port?

Step 2: Install arkade

In this guide, we will use arkade to install OpenFaaS. Arkade is an open-source CLI that helps to easily manage apps. It is written in Go language and uses helm charts and kubectl to install applications to the Kubernetes cluster.

Use the below command to install arkade on your machine.

## For MacOS / Linux users 
sudo curl -SLsf https://dl.get-arkade.dev/ | sudo sh

## For Windows users(using Git Bash) 
curl -SLsf https://dl.get-arkade.dev/ | sh

Sample Output:

Downloading package https://github.com/alexellis/arkade/releases/download/0.8.1/arkade as /tmp/arkade
Download complete.

Running with sufficient permissions to attempt to move arkade to /usr/local/bin
New version of arkade installed to /usr/local/bin
Creating alias 'ark' for 'arkade'.
            _             _      
  __ _ _ __| | ____ _  __| | ___ 
 / _` | '__| |/ / _` |/ _` |/ _ \
| (_| | |  |   < (_| | (_| |  __/
 \__,_|_|  |_|\_\__,_|\__,_|\___|

Get Kubernetes apps the easy way

Version: 0.8.1
Git Commit: 00dec04ccf9cb022e07bf2eb4a082f59b2ed5f99

Step 3: Install OpenFaas

With arkade installed on your system, you will easily download OpenFaaS using the command:

arkade install openfaas --basic-auth-password MyStrongPassword --set=faasIdler.dryRun=false

In the command, you have installed OpenFaaS using the official helm chart. You have installed OpenFaaS with your own password “MyStrongPassword” and disabled faasIdler.dryRun. This is required for autoscaling.

Sample output:

............
faas-cli store deploy figlet
faas-cli list

# For Raspberry Pi
faas-cli store list \
 --platform armhf

faas-cli store deploy figlet \
 --platform armhf

# Find out more at:
# https://github.com/openfaas/faas

Thanks for using arkade!

Step 4: Install faas-cli

The faas-cli is required once OpenFaaS is installed on your system. It is used to deploy and test functions with OpenFaas. Install it on your system using the command:

## For Linux and MacOS users
curl -sL https://cli.openfaas.com | sudo sh

## For Windows users with (Git Bash)
curl -sL https://cli.openfaas.com | sh

Now we have successfully set up OpenFaaS, we can now login to it using fast-cli or the Web UI as below.

faas-cli login --username admin --password MyStrongPassword

The default username is admin and the password, we use the one specified earlier MyStrongPassword

Sample Output:

Running with sufficient permissions to attempt to move faas-cli to /usr/local/bin
New version of faas-cli installed to /usr/local/bin
Creating alias 'faas' for 'faas-cli'.
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|

CLI:
 commit:  72816d486cf76c3089b915dfb0b66b85cf096634
 version: 0.13.13

Step 5: Deploy OpenFaas on Kubernetes

Now with the OpenFaaS installation successful, we will now get to the crux of this guide. Deploying the OpenFaaS framework on Kubernetes is done by issuing the below command.

kubectl get deploy --namespace openfaas

Sample Output:

NAME                READY   UP-TO-DATE   AVAILABLE   AGE
alertmanager        0/1     1            0           71s
basic-auth-plugin   1/1     1            1           71s
gateway             0/1     1            0           71s
nats                0/1     1            0           71s
prometheus          0/1     1            0           71s
queue-worker        0/1     1            0           71s

Check the status of the core containers in OpenFaaS using the command:

kubectl rollout status -n openfaas deploy/gateway

Sample Output:

Waiting for deployment "gateway" rollout to finish: 0 of 1 updated replicas are available...
deployment "gateway" successfully rolled out

Then forward all the requests sent to http://localhost:8080 using kubectl port-forward command.

kubectl port-forward -n openfaas svc/gateway 8080:8080

This connection remains open as long as the process is running. If it disconnects due to some reason, re-run it. You can run it in the background using & at the end.

Step 6: Access the OpenFaaS Web UI

Now access the OpenFaas web interface using the URL http://127.0.0.1:8080

How-To-Deploy-OpenFaaS-Serverless-Framework-on-Kubernetes

Login with the credentials; username: admin password: MyStrongPassword. On successful login, you will see this OpenFaas dashboard.

How-To-Deploy-OpenFaaS-Serverless-Framework-on-Kubernetes-1-1024x297

We can now deploy a new function on OpenFaas by clicking on Deploy a new Function.

How-To-Deploy-OpenFaaS-Serverless-Framework-on-Kubernetes-4

There is a long list of functions to deploy. For the purposes of this guide, I will go for a Nodeinfo deployment.

How-To-Deploy-OpenFaaS-Serverless-Framework-on-Kubernetes-5

Select it and the option for deploy becomes active as shown.

How-To-Deploy-OpenFaaS-Serverless-Framework-on-Kubernetes-6

Click deploy and your new function will appear as shown.

How-To-Deploy-OpenFaaS-Serverless-Framework-on-Kubernetes-7-1024x482

Select it and invoke it. The node info will then be displayed. Still, you can use the provided URL to see the node information.

How-To-Deploy-OpenFaaS-Serverless-Framework-on-Kubernetes-8

Conclusion.

Congratulations! We have triumphantly gone through how to Deploy OpenFaaS Serverless Framework on Kubernetes. I hope this guide was informative. In case you had issues with deploying OpenFaaS Serverless Framework on Kubernetes let us know.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.