Deploying OpenFaaS on Digital Ocean with Terraform
Everything functional
- tags
- openfaas
- kubernetes
- terraform
- helm
Contents
We are going to look at how to use Terraform to deploy a Kubernetes cluster on Digital Ocean, add a managed postgres database, and redis and OpenFaaS in kubernetes. This will show how to use Terraform to manage the configuration and how we can access both cloud and kubernetes managed services from OpenFaaS functions.
We are going to use the digitalocean, kubernetes, and helm terraform providers.
The plan
- Provision a
digitalocean_kubernetes_cluster
- Provision a
digitalocean_database_cluster
- Provision 2
kubernetes_namespace
foropenfaas
andopenfaas-fn
- Provision a
helm_release
foropenfaas
- Provision a
helm_release
forredis
- Provision 2
kubernetes_secret
to point to the databases - Deploy an OpenFaaS function that reads those secrets and talks to the database.
Let's go.
Install the software
I'm using Debian, your mileage may vary.
OpenFaaS
|
|
Terraform
|
|
kubectl
|
|
helm
This is optional since we are using terraform, but here for reference.
|
|
Terraform
Providers
First we need to define out providers, which we will do in providers.tf
:
|
|
Then run
|
|
To load them locally.
Also, you should define your do_token perhaps in an environment
variable TF_VAR_do_token
.
Digital Ocean Resources
Define digitalocean.tf
:
|
|
We can spin these up using terraform apply
. This takes about 6
minutes for me.
Kubernetes
Now we can add our kubernetes namespaces. In another file called kubernetes.tf
:
|
|
We'll need to run terraform init
again since we added a provider, and
then we can terraform apply
.
Helm
|
|
Once you have this file, do terraform init
and then terraform apply
and both OpenFaaS and Redis should be deployed to your cluster.
Secrets
|
|
Verifying the deployment
Setup kubectl
|
|
If you have your TF_VAR_do_token
setup correctly, it should create a
valid config
file.
Test this with
|
|
Kubernetes control plane is running at https://39cef8c8-ca33-40f1-9454-3373707a22ef.k8s.ondigitalocean.com CoreDNS is running at https://39cef8c8-ca33-40f1-9454-3373707a22ef.k8s.ondigitalocean.com/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Verifying OpenFaaS
We can then verify the deploy with:
|
|
NAME | READY | UP-TO-DATE | AVAILABLE | AGE |
alertmanager | 1/1 | 1 | 1 | 19m |
basic-auth-plugin | 1/1 | 1 | 1 | 19m |
gateway | 1/1 | 1 | 1 | 19m |
nats | 1/1 | 1 | 1 | 19m |
prometheus | 1/1 | 1 | 1 | 19m |
queue-worker | 1/1 | 1 | 1 | 19m |
Connecting to OpenFaaS
Setup the proxy
In a new window, lets setup port forwarding from your local machine to connect to the openfaas gateway in kubernetes.
|
|
Get the OpenFaaS login credentials and login
|
|
Calling the OpenFaaS server to validate the credentials... credentials saved for admin http://127.0.0.1:8080
And now we can list out our deployed functions:
|
|
Function | Invocations | Replicas |
Not a whole lot there yet.
Testing out deploying a function
|
|
Deployed. 202 Accepted. URL: http://127.0.0.1:8080/function/nodeinfo Hostname: nodeinfo-8545846564-wpqm6 Arch: x64 CPUs: 2 Total mem: 1995MB Platform: linux Uptime: 361
Writing a OpenFaaS function that talks to Redis
Get the template running
Lets create our first function. We need to pull the templates locally, so lets do that with:
|
|
Fetch templates from repository: https://github.com/openfaas/templates.git at
And create our function, I'm going to use ruby.
|
|
Change the image
in rubyredis.yml
to be your Docker hub user name, and
then lets deploy it:
|
|
And if that's successful, we can invoke it with:
|
|
Hello world from the Ruby template
Adding redis
Now that we have it working, lets add redis to the picture.
First we need to add the secret to the rubyredis.yml
file, so that it
references the secret we defined above in terraform:
|
|
In the Gemfile
add the redis
gem:
|
|
Now we need to change handler.rb
to conenct to the redis service on
the cluster, which is redis-master.default
(default is the namespace
that it's in) with the password that we load from
/var/openfass/secrets/password
.
|
|
We can then redeploy using
|
|
And we can invoke it now using
|
|
Each time you run this you should see the result increment.
Writing a OpenFaaS function that talk to Postgres
Start a remplate
|
|
Then lets tweak the rubypostgres.yml
file to add the secret (and
docker username!)
|
|
Then we need to add the 'pg' gem:
|
|
Then in the handler
|
|
Now we build it:
|
|
And invoke:
|
|
PID | User | Query 76 | postgres | <insufficient privilege> 69 | postgres | <insufficient privilege> 65 | | <insufficient privilege> 67 | postgres | <insufficient privilege> 72 | postgres | <insufficient privilege> 78 | _dodb | <insufficient privilege> 22113 | doadmin | SELECT * FROM pg_stat_activity 63 | | <insufficient privilege> 62 | | <insufficient privilege> 64 | | <insufficient privilege>
Conclusion
When you are done, you can use terraform destroy
to remove everything.
Don't do that for production!!!
Terraform is pretty nifty in that it lets you spin up the whole environment easily, and OpenFaaS is a very nice way to work with functions easily. Kubernetes is a bit daunting but once it's up and running gives you a great way to scale things up and down.
We setup the cluster and on it deployed OpenFaaS as well as redis. We showed how to connect to redis from OpenFaaS, as well as how to connection to a managed postgres install using an OpenFaaS function.
References
- https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/#install-using-native-package-management
- https://ponderosa.io/blog/kubernetes/2019/03/13/terraform-cluster-create/
- https://github.com/openfaas/faas-netes/blob/master/chart/openfaas/README.md
- https://github.com/openfaas/workshop/blob/master/lab1b.md#run-on-digitaloceans-kubernetes-service
- https://github.com/christi3k/oscon-2019-deploying-with-terraform/blob/master/02-open-faas.md
Previously
Next