Skip to content

Instantly share code, notes, and snippets.

@alexellis
Last active February 7, 2020 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexellis/3b285ca0952d4cef48ca5dbff7e4e288 to your computer and use it in GitHub Desktop.
Save alexellis/3b285ca0952d4cef48ca5dbff7e4e288 to your computer and use it in GitHub Desktop.
single-install-multi-namespace-openfaas.md

Update: see this note: https://gist.github.com/alexellis/3b285ca0952d4cef48ca5dbff7e4e288#gistcomment-3169271

Alpha test multiple namespace support in OpenFaaS

You can already install the OpenFaaS control-plane multiple times to create segregated environments.

Today, we're introducing a new option - single control-plane, multiple namespaces.

There are pros/cons to both solutions and it's still on you to decide what fits best.

Let's go, you'll need 5-10 mins

Test it out with these instructions:

  • Get Kubernetes with kind, k3s, k3d, or minikube

    https://k3s.io

    https://github.com/kubernetes-sigs/kind

    https://github.com/rancher/k3d

    https://kubernetes.io/docs/setup/learning-environment/minikube/

  • Add Tiller

    kubectl -n kube-system create sa tiller \
    && kubectl create clusterrolebinding tiller \
    --clusterrole cluster-admin \
    --serviceaccount=kube-system:tiller
    
    helm init --skip-refresh --upgrade --service-account tiller
    
  • Install OpenFaaS with helm

    echo Installing with helm 👑
    
    helm repo add openfaas https://openfaas.github.io/faas-netes/
    
    kubectl apply -f \
       https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
    
    # generate a random password
    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"
    
    helm upgrade openfaas --install openfaas/openfaas \
        --namespace openfaas  \
        --set basic_auth=true \
        --set functionNamespace=openfaas-fn \
        --set clusterRole=true # needed for multiple namespace access
    
  • Create an alternative namespace

    kubectl create ns fn
    
  • Port-forward

    kubectl port-forward -n openfaas svc/prometheus 9090:9090 &
    kubectl port-forward -n openfaas svc/gateway 8080:8080 &
    
  • Get the password

    PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)
    
    export AUTH=http://admin:$PASSWORD@127.0.0.1:8080
    
    echo -n $PASSWORD | faas-cli login --username admin --password-stdin
    
  • Deploy two functions in two namespaces

    # Create function cows in the default namespace
    faas-cli store deploy cows # openfaas-fn
    
    # Create a function nodeinfo in the fn namespace
    curl -d '{"service":"nodeinfo","namespace":"fn","image":"functions/nodeinfo:latest","labels":{"com.openfaas.scale.min":"2","com.openfaas.scale.max":"5"},"environment":{"output":"verbose","debug":"true"}}' -X POST  $AUTH/system/functions
    
  • View functions deployed:

    # View default
    curl -s $AUTH/system/function/cows | jq
    curl -s $AUTH/system/function/cows?namespace=openfaas-fn | jq
    
    # view "fn" namespace
    curl -s $AUTH/system/function/nodeinfo?namespace=fn | jq
    
    # View all functiions in a namespace
    curl -s $AUTH/system/functions?namespace=fn | jq
    curl -s $AUTH/system/functions?namespace=openfaas-fn | jq
    
  • Invoke both

    curl 127.0.0.1:8080/function/cows
    
    curl 127.0.0.1:8080/function/cows.openfaas-fn
    
    curl 127.0.0.1:8080/function/nodeinfo.fn
    
  • Scale to zero and back

    kubectl scale deploy/nodeinfo -n fn --replicas=0
    curl 127.0.0.1:8080/function/nodeinfo.fn
    
  • Auto-scale

    hey -c 5 -z 2m http://127.0.0.1:8080/function/nodeinfo.fn &
    

    Now monitor with the OpenFaaS API:

    curl -s $AUTH/system/functions | jq
    
    curl -s $AUTH/system/function/nodeinfo?namespace=fn | jq
    
  • View Prometheus

    # View some metrics
    http://127.0.0.1:9090/graph?g0.range_input=1h&g0.expr=rate(gateway_function_invocation_total%5B1m%5D)&g0.tab=0
    
    # See alerts
    http://127.0.0.1:9090/alerts
    

How did it go?

@rajibmitra
Copy link

what is hey here ?
hey -c 5 -z 2m http://127.0.0.1:8080/function/nodeinfo.fn &
I can see the Prometheus from browser.
you should add one uninstall / delete everything tag
which will remove all the namespace and resources.

@alexellis
Copy link
Author

@alexellis
Copy link
Author

You can run kubectl delete ns to clean up

@dplusic
Copy link

dplusic commented Feb 7, 2020

It seems that kubectl annotate namespace fn openfaas="1" is needed after kubectl create ns fn

@alexellis
Copy link
Author

Hi @dpulsic I would recommend you head over to the OpenFaaS slack now and the docs since this feature has been released, this gist is no longer relevant.

https://slack.openfaas.io

https://docs.openfaas.com -> https://docs.openfaas.com/reference/namespaces/

Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment