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.
---# see https://k3d.io/v5.6.3/usage/configfile/ for complete configapiVersion: k3d.io/v1alpha5kind: Simpleservers: 1agents: 0image: docker.io/rancher/k3s:v1.30.1-k3s1# ingressports: - port: 80:80 nodeFilters: - server:0# will use host docker registryregistries: create: name: registry.localhost host: "0.0.0.0" hostPort: "5000"
This will specify the version of k3s and create a local docker registry (useful for local tests).
Then execute the following command:
$ # create the cluster$ k3d cluster create --config default.yml$ # wait a bit and you can see the cluster is created$ k3d cluster listNAME SERVERS AGENTS LOADBALANCERk3s-default 1/1 0/0 true$ # or using kubectl$ kubectl get nodesNAME STATUS ROLES AGE VERSIONk3d-k3s-default-server-0 Ready control-plane,master 3d21h v1.30.1+k3s1
Create the nginx-values.yaml which is the helm values to use for the helm chart:
replicaCount: 1
Let’s try it:
$ terraform apply --auto-approve -var-file localhost.tfvarsTerraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + createTerraform will perform the following actions: # helm_release.nginx will be created + resource "helm_release" "nginx" { + atomic = false + chart = "nginx"...
$ terraform apply -auto-approveTerraform used the selected providers to generate the following execution plan. Resourceactions are indicated with the following symbols: + createTerraform will perform the following actions: # helm_release.backend_app will be created + resource "helm_release" "backend_app" { + atomic = false + chart = "../../dist/backend-app-0.1.0.tgz" + cleanup_on_fail = false...Plan: 2 to add, 0 to change, 0 to destroy....helm_release.backend_app: Creation complete after 32s [id=backend-app]
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.:
targetEnv: ${targetEnv}app: name: ${appName}# Other properties...