Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Last active November 17, 2018 06:46
Show Gist options
  • Save viveksyngh/c2acd0dfc707ba12b857c7131b347498 to your computer and use it in GitHub Desktop.
Save viveksyngh/c2acd0dfc707ba12b857c7131b347498 to your computer and use it in GitHub Desktop.
OpenFaaS on Kubernetes

Installation of OpenFaaS on kubernetes

You can install OpenFaaS on kubernetes using two ways

1. Using `faas-netes` repository

2. Using Helm 

Installation using faas-netes repository

Clone faas-netes

$ git clone https://github.com/openfaas/faas-netes.git && cd faas-netes

Create namespaces required for OpenFaaS (openfaas and openfaas-fn)

$ kubectl apply -f namespaces.yml

Apply artifacts required for creating OpenFaaS services

$ kubectl apply -f ./yaml

Remove OpenFaaS from your kubernetes cluster

$ kubectl delete -f ./yaml

Install OpenFaaS on kubernetes using Helm

Helm is a package manager for Kubernetes

Install helm client using brew

$ brew install kubernetes-helm

Create service account and a cluster role binding for Tiller

$ kubectl -n kube-system create sa tiller
$ kubectl create clusterrolebinding tiller-cluster-rule \
    --clusterrole=cluster-admin \
    --serviceaccount=kube-system:tiller

Deploy Tiller on kubernetes cluster

$ helm init --skip-refresh --upgrade --service-account tiller

Create kubernetes namespaces

$ kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml

Create basic-auth credentials

$ password=$(head -c 12 /dev/urandom | shasum | cut -d' ' -f1)
$ kubectl -n openfaas create secret generic basic-auth \
    --from-literal=basic-auth-user=admin \
    --from-literal=basic-auth-password=$password

Install OpenFaaS using Helm

$ helm repo add openfaas https://openfaas.github.io/faas-netes/
$ helm upgrade openfaas --install openfaas/openfaas \
    --namespace openfaas  \
    --set functionNamespace=openfaas-fn \
    --set serviceType=LoadBalancer \
    --set basic_auth=true

Install OpenFaaS on kubernetes with OpenFaaS Operator

Install helm client using brew

$ brew install kubernetes-helm

Create service account and a cluster role binding for Tiller

$ kubectl -n kube-system create sa tiller
$ kubectl create clusterrolebinding tiller-cluster-rule \
    --clusterrole=cluster-admin \
    --serviceaccount=kube-system:tiller

Deploy Tiller on kubernetes cluster

$ helm init --skip-refresh --upgrade --service-account tiller

Create kubernetes namespaces

$ kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml

Delete basic-auth secrets from cluster

Delete already existing basic auth credentials from openfaas namespace

$ kubectl delete secret basic-auth -n openfaas

Create basic-auth credentials

$ password=$(head -c 12 /dev/urandom | shasum | cut -d' ' -f1)
$ kubectl -n openfaas create secret generic basic-auth \
    --from-literal=basic-auth-user=admin \
    --from-literal=basic-auth-password=$password

Install OpenFaaS using Helm

$ helm repo add openfaas https://openfaas.github.io/faas-netes/
$ helm upgrade openfaas --install openfaas/openfaas \
    --namespace openfaas  \
    --set functionNamespace=openfaas-fn \
    --set serviceType=LoadBalancer \
    --set basic_auth=true \
    --set operator.create=true

List components deployed by OpenFaaS

$ kubectl get all -n openfaas

Walk thorugh Gateway UI and Deploy a function

Use kubectl to manage functions

Get all functions

$ kubectl get functions -n openfaas-fn

Annotate a function

$ kubectl annotate functions figlet test=test -n openfaas-fn

Describe a function

$ kubectl describe functions figlet -n openfaas-fn

Delete a function

$ kubectl delete functions figlet -n openfaas-fn

Using faas-cli to manage functions

Login through CLI to access gateway

$ faas-cli login --username=admin --password=$password

Create a new function

$ faas-cli new kube-demo --lang=python --prefix=viveksyngh

Build function

$ faas-cli build -f kube-demo.yml

Push function image

$ faas-cli push -f kube-demo.yml

Deploy function

$ faas-cli deploy -f kube-demo.yml

Invoke function

$ echo -n "Hello World!" | faas-cli invoke kube-demo

Async Invocation

$ faas-cli invoke kube-demo --async -H "X-Callback-Url=http://requestbin.fullcontact.com/122nj181"

Make some changes in the function and deploy it.

$ faas-cli up -f kube-demo.yml

Delete OpenFaaS from kubernetes cluster

$ helm delete --purge openfaas

Autoscaling

Use kubernetes port-forward to see prometheus metrics

$ kubectl port-forward svc/prometheus 9090:9090 -n openfaas

Deploy Grafana to cluster in OpenFaaS Kubernetes namespace:

$ kubectl -n openfaas run \
--image=stefanprodan/faas-grafana:4.6.3 \
--port=3000 \
grafana

Expose Grafana with a NodePort:

$ kubectl -n openfaas expose deployment grafana \
--type=NodePort \
--name=grafana

Port-forward Grafana to access it

$ kubectl port-forward deployment/grafana 3000:3000 -n openfaas

Invoke function in infinite loop

$ while [ true ]; do curl -X POST http://127.0.0.1:8080/function/figlet; done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment