manage local kubernetes with terraform
🎯 Objective
- launch kubernetes locally
- manage application deployment with terraform
Why deploy with terraform?
Quote
While you could use
kubectl
or similar CLI-based tools to manage your Kubernetes resources, using Terraform has the following benefits:
- Unified Workflow - If you are already provisioning Kubernetes clusters with Terraform, use the same configuration language to deploy your applications into your cluster.
- Full Lifecycle Management - Terraform doesn’t only create resources, it updates, and deletes tracked resources without requiring you to inspect the API to identify those resources.
- Graph of Relationships - Terraform understands dependency relationships between resources. For example, if a Persistent Volume Claim claims space from a particular Persistent Volume, Terraform won’t attempt to create the claim if it fails to create the volume.
src: Manage Kubernetes resources via Terraform | Terraform | HashiCorp Developer
Also because we are using terraform at work, and the ops are planning to use it to manage the kubernetes cluster.
Launch local kubernetes
I used k3d as my local kubernetes. But there are several other alternatives you can check out yourself.
.Installation
With nix
It’s as simple as (also install kubectl so you can interact with your kubernetes cluster):
Launch
Create a
default.yml
with the following content:This will specify the version of k3s and create a local docker registry (useful for local tests). Then execute the following command:
Link to original
Getting started with terraform
Installation
With nix
It’s as simple as:
Link to original
Configure terraform kubernetes provider
Create a kubernetes.tf
that defined the kubernetes cluster to connect to:
Create a localhost.tfvars
which will contains the variable of the localhost environment:
Tip
Even better, we can reference the kubernetes provider with a local file:
Then, no need to manually get the host, client certificate, …
Then execute the commands:
You can test the integration by deploying a Nginx. First, create a nginx.tf
with the following content:
The execute the following:
You will see the Pods are deployed correctly (if everything went well):
To remove the Pods, execute the following:
Add helm provider
Create a versions.yml
with the following:
Tip
You can find the provider versions from:
Don’t forget to remove the provider from kubernetes.tf
, otherwise, you will have two kubernetes providers.
Then execute the following to download the helm provider:
Then update nginx.tf
with the following:
Create the nginx-values.yaml
which is the helm values to use for the helm chart:
Let’s try it:
Deploy local application to kubernetes using terraform and helm
Create a local helm chart using the following:
Make your changes and when you are ready, generate the tarball using the following command:
Create a src/backend-app/h-values.yml
that will contain the overrided values, e.g.
Create a src/terraform/backend-app.tf
with the following:
And apply your changes:
Use terraform templating feature to dynamically generate helm values
Yes, we will “template” (not a verb, but you get it) a templating tool… It’s kind of going deeper on the templating world… At one point, it will be quite hard to know what was wrongly interpolated from all those layer of templates…
Anyway, we want to use terraform variables in the helm values file so we can dynamically generate them. Let’s create a h-values.yml.tpl
and change its content, e.g.:
Then let’s edit the backend-app.tf
:
As you can see, the targetEnv
in the h-values.yml
will be replace by terraform workspace name (which is demo
).
So by using terraform templating feature, we can also use other terraform features/plugins, like sops.