Set up environment on MacOS
brew install go docker-machine-driver-hyperkit faas-cli kubernetes-cli kubernetes-helm
brew cask install minikube docker
docker-machine-driver-hyperkit can replace by virtualbox. Use
brew cask install virtualbox
to install it.
docker-machine-driver-hyperkit and docker both have docker-machine. It may cause some error when homebrew create symbolic link. This error can be ignored since docker-machine will always point to docker’s when starting docker for mac.
Start minikube
Use virtualbox (Default)
minikube start
Use hyperkit
minikube start --vm-driver=hyperkit
Or use
minikube config set vm-driver hyperkit
to modify default value.
Deploy OpenFaaS to minikube
- Create a service account for Helm’s server component (tiller):
kubectl -n kube-system create sa tiller && kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller
- Install tiller which is Helm’s server-side component:
helm init --skip-refresh --upgrade --service-account tiller
- Create namespaces for OpenFaaS core components and OpenFaaS Functions:
kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
- Add the OpenFaaS helm repository:
helm repo add openfaas https://openfaas.github.io/faas-netes/
- Update all the charts for helm:
helm repo update
- Create a password (Remember this for login to OpenFaaS dashboard):
export PASSWORD=...
Random password example:export PASSWORD=$(head -c 12 /dev/urandom | shasum| cut -d' ' -f1)
- Create a secret for the password:
kubectl -n openfaas create secret generic basic-auth --from-literal=basic-auth-user=admin --from-literal=basic-auth-password="$PASSWORD"
- Install OpenFaaS using the chart:
helm upgrade openfaas --install openfaas/openfaas --namespace openfaas --set functionNamespace=openfaas-fn --set basic_auth=true
- Finally once all the Pods are started you can login using the faas-cli:
echo -n $PASSWORD | faas-cli login -g http://$(minikube ip):31112 -u admin --password-stdin
- You can use
echo http://$(minikube ip):31112
to find dashboard URL and open it by browser (username: admin)
Use faas-cli
Generate a Go function
faas-cli new go-fn --lang go
This command will generate a go-fn directory contains a simple template function response like this “Hello, Go. You said: (request body)” and a go-fn.yml file in working directory.
Build function
- Create a Docker Hub account
- Add your Docker Hub username to go-fn.yml file’s image tag
1 | functions: |
- Build docker image:
faas-cli build -f go-fn.yml
- Push to Docker Hub:
faas-cli push -f go-fn.yml
You can find go-fn in your Docker Hub repository.
Deploy function
faas-cli deploy -f go-fn.yml --gateway http://$(minikube ip):31112
Then you can see this function in the dashboard. Invoke it by using dashboard UI or using faas-cli.
echo test | faas-cli invoke go-fn --gateway http://$(minikube ip):31112
Build, push and deploy can use just one command to finish these step
faas-cli up -f go-fn.yml --gateway http://$(minikube ip):31112
Develop with third-party package using Go module (Go 1.11+)
- Init go module in go-fn directory:
go mod init PROJECT_NAME
- Import some third-party package in handler.go
example:
1 | package function |
- Create vendor directory:
go mod vendor
(OpenFaaS use vendor to manage dependency) - Build and deploy this new function:
faas-cli up -f go-fn.yml --gateway http://$(minikube ip):31112
How to use this example repository
- Deploy OpenFaaS
- Clone this repository
- Create vendor in go-fn directory
- Modify yml file’s Docker Hub username in image tag
- Deploy function