Provisioning Kubernetes Clusters with Terraform / OpenTofu
Codify cluster and node infrastructure with Terraform or OpenTofu modules.
In plain words
IaC is an executable construction drawing: plan shows the proposed demolition and state is the locked inventory against which it is compared. The simple Kubernetes rule is: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
Think of it like…
IaC is an executable construction drawing: plan shows the proposed demolition and state is the locked inventory against which it is compared.
Where you’ve seen it
During a controlled maintenance window, the platform operator runs terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan. Expected evidence: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph. If the run instead shows this symptom, stop and restore the last known state: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another.
Words you’ll hear
- Terraform
- For this lesson, Terraform is the controlling idea: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
- OpenTofu
- OpenTofu is the checkpoint; run
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplanand inspect the owner chain. A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another. - IaC
- IaC is the outcome to preserve; the cost is explicit: Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements.
- Verification evidence
- The evidence command is
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan. It connects the declaration to this behavior: Codify cluster and node infrastructure with Terraform or OpenTofu modules.
Visual diagram
Theory & deep dive
How Terraform reaches the intended state
Codify cluster and node infrastructure with Terraform or OpenTofu modules. Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
The concrete checkpoint is terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan; compare the Terraform owner, OpenTofu state, and IaC outcome instead of treating YAML acceptance as proof.
Run terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan after the change and correlate the result with Events and the owning object. Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
OpenTofu: failure boundary and production cost
A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another. The prerequisite contract is concrete: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements. A safe design limits blast radius, records the last good configuration, and uses terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan as a deterministic rollback checkpoint.
Capture the current state with terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another.
How it works, step by step
Step 1 of 41. Plan the lifecycle boundary
Inventory versions, capacity, state, backups, and ownership in a reversible plan: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
- Goal
- Codify cluster and node infrastructure with Terraform or OpenTofu modules.
- Owner
- Terraform
When to use it & trade-offs
What it is
Codify cluster and node infrastructure with Terraform or OpenTofu modules. Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
Why it matters: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another. The invariant at risk is specific: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph. The quickest fact check before changing the design is terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan.
When to reach for it
- Use Terraform to enforce this invariant: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
- Use OpenTofu evidence from
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplanin reviews and incident timelines. Codify cluster and node infrastructure with Terraform or OpenTofu modules. - Choose the managed cloud or platform service route only when this cost is justified by governance or scale: Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements.
When to avoid or be careful
- Avoid assuming Terraform is complete after YAML submission; the required behavior is concrete: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
- Avoid treating the next symptom as random; check the owner and state with
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplanfirst: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another. - Avoid adopting a heavier platform wrapper before measuring its cost: Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements.
Native, manual, and managed paths for Terraform
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan.Strengths: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
Watch out: Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements. Recovery must also recognize this signal: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another.
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan; the expected invariant is: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.Strengths: terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan gives immediate feedback without first building a platform layer.
Watch out: A manual change can hide drift, and unlike Terraform, it does not make this symptom self-documenting: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another.
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan.Strengths: Platform controls can wrap this exact behavior in policy or dashboards: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
Watch out: The added platform ownership buys value at this cost: Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements.
Interview signals & numbers to know
When this topic comes up
- Interview anchor: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
- Trace Terraform to IaC through OpenTofu; the invariant is: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
- Recognize the symptom: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another.
- Justify the cost: Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements.
- Verify with terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan instead of only inspecting YAML.
Benchmarks & rules of thumb
- 2
- compatible engines discussed: Terraform and OpenTofu
- 1
- reviewed plan artifact should precede production apply
- 0
- state files should be committed as ordinary plaintext code
Common mistakes & follow-ups
Common interview mistakes
- Debugging only the child object misses the Terraform owner contract: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph.
- Ignoring this telltale condition leads to unrelated changes: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another.
- Repeating
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplanwithout an owner for remediation wastes retry cost. Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements.
Likely interviewer follow-ups
- How would API output and runtime evidence prove this behavior: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph?
- Which owner object, logs, and Events would you inspect for this symptom-A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another-?
- At what threshold would this cost become unacceptable: Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements?
terraform init; terraform plan -out=cluster.tfplan; terraform apply cluster.tfplan, name the failure, then justify the cost. Invariant: Terraform or OpenTofu declares cloud, network, managed cluster, node, and policy resources, with plan and state providing a reviewable lifecycle graph. Failure: A plan can replace the control plane for a cluster name or immutable field, credentials can leak through state, and a bootstrap order bug can make two providers depend on one another. Cost: Declarative IaC improves reproducibility, but remote state locking, module versioning, secret backends, imports, and blast-radius layering are platform requirements.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Provisioning Kubernetes Clusters with Terraform / OpenTofu with generative AI interview Q&A and hands-on practice.