Deploy OpenFaas to minikube
May 21, 2020 — 2 Min Read
OpenFaas is an open source FaaS platform that allows to deploy event-driven functions and microservices to Kubernetes quickly.
In this tutorial we will see how to deploy it. In particular we will use minikube, which implements a simple local k8s cluster.
Source: OpenFaas Doc
Requirements
This are the tools used, in parenthesis the version used for the tutorial:
- docker (19.03.8);
- helm (3.2.1);
- minikube (1.10.1);
- openfaas cli (0.12.4).
Please refer to the respective documentation on how to install them.
Start minikube
We will use minikube with the docker driver so that the k8s node will be running in a container:
minikube start --driver=docker
Deploy OpenFaas
First thing to do is to create the required namespaces:
$ kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
namespace/openfaas created
namespace/openfaas-fn created
Then we should create credentials for the gateway:
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"
At this point we need to add the repository to be able to download the helm template:
helm repo add openfaas https://openfaas.github.io/faas-netes/
helm repo update
Now we can generate the deployment files using Helm and apply it:
helm template \
openfaas openfaas/openfaas \
--namespace openfaas \
--set basic_auth=true \
--set functionNamespace=openfaas-fn \
--set faasnetes.imagePullPolicy="IfNotPresent" \
--set ingress.enabled=true > openfaas.yaml
kubectl apply -f openfaas.yaml
OpenFaas should be deployed now:
$ kubectl get pods -n openfaas
NAME READY STATUS RESTARTS AGE
alertmanager-9668b5785-2ftwn 1/1 Running 0 10m
basic-auth-plugin-56c84b75b6-khhkx 1/1 Running 0 10m
faas-idler-6c9b9cb54f-fnr89 1/1 Running 0 10m
gateway-6bff97c6cd-twfpv 2/2 Running 0 10m
nats-58d4d5db8d-k22kp 1/1 Running 0 10m
prometheus-8b754b655-lmfft 1/1 Running 0 10m
queue-worker-744c85f647-qkb2t 1/1 Running 0 10m
Next step is to port-forward the gateway to the local machine:
kubectl port-forward svc/gateway -n openfaas 31112:8080 &
so that we can login:
export OPENFAAS_URL=http://127.0.0.1:31112
echo -n $PASSWORD | faas-cli login --password-stdin
and eventually open the web ui at localhost:31112.
Deploy custom function
In order to deploy local function to the cluster, it’s necessary to point your shell to use minikube’s docker deamon:
eval $(minikube docker-env)
This step is necessary as during the deploy OpenFaas needs to pull the function’s image, but the host’s daemon is not available inside the cluster, hence it won’t be possible to pull the image from it.
So now we can build the function (refer to the official documentation to see how to write one):
faas build
This last command will build the image inside the minikube container itself, so the image will be available to OpenFaas when we will perform the deploy with:
faas deploy