Terraform

Building Kubernetes clusters with Terraform

kOps can generate Terraform configurations, and then you can apply them using the terraform plan and terraform apply tools. This is very handy if you are already using Terraform, or if you want to check in the Terraform output into version control.

The gist of it is that, instead of letting kOps apply the changes, you tell kOps what you want, and then kOps spits out what it wants done into a .tf file. You are then responsible for turning those plans into reality.

The Terraform output should be reasonably stable (i.e. the text files should only change where something has actually changed - items should appear in the same order etc). This is extremely useful when using version control as you can diff your changes easily.

Note that if you modify the Terraform files that kOps spits out, it will override your changes with the configuration state defined by its own configs. In other terms, kOps’s own state is the ultimate source of truth (as far as kOps is concerned), and Terraform is a representation of that state for your convenience.

Terraform Version Compatibility

kOps VersionTerraform VersionFeature Flag Notes
>= 1.19>= 0.12.26, >= 0.13HCL2 supported by default
KOPS_FEATURE_FLAGS=Terraform-0.12 is now deprecated
>= 1.18>= 0.12HCL2 supported by default
>= 1.18< 0.12KOPS_FEATURE_FLAGS=-Terraform-0.12
>= 1.17, < 1.23>= 0.12KOPS_FEATURE_FLAGS=TerraformJSON outputs JSON
<= 1.17< 0.12Supported by default

Using Terraform

Set up remote state

You could keep your Terraform state locally, but we strongly recommend saving it on S3 with versioning turned on that bucket. Configure a remote S3 store with a setting like below:

  1. terraform {
  2. backend "s3" {
  3. bucket = "mybucket"
  4. key = "path/to/my/key"
  5. region = "us-east-1"
  6. }
  7. }

Learn more about Terraform state.

Initialize/create a cluster

For example, a complete setup might be:

  1. $ kops create cluster \
  2. --name=kubernetes.mydomain.com \
  3. --state=s3://mycompany.kubernetes \
  4. --dns-zone=kubernetes.mydomain.com \
  5. [... your other options ...]
  6. --out=. \
  7. --target=terraform

The above command will create kOps state on S3 (defined in --state) and output a representation of your configuration into Terraform files. Thereafter you can preview your changes in kubernetes.tf and then use Terraform to create all the resources as shown below:

Additional Terraform .tf files could be added at this stage to customize your deployment, but remember the kOps state should continue to remain the ultimate source of truth for the Kubernetes cluster.

Initialize Terraform to set-up the S3 backend and provider plugins.

  1. $ terraform init

If you’re using Terraform v0.12.26+, the following warning will be displayed and can be safely ignored. It will not be displayed if you’re using Terraform v0.13.0+.

  1. Warning: Provider source not supported in Terraform v0.12
  2. on kubernetes.tf line 665, in terraform:
  3. 665: aws = {
  4. 666: "source" = "hashicorp/aws"
  5. 667: "version" = ">= 2.46.0"
  6. 668: }
  7. A source was declared for provider aws. Terraform v0.12 does not support the
  8. provider source attribute. It will be ignored.

Use Terraform to review and create the cloud infrastructure and Kubernetes cluster.

  1. $ terraform plan
  2. $ terraform apply

Wait for the cluster to initialize. If all goes well, you should have a working Kubernetes cluster!

Editing the cluster

It’s possible to use Terraform to make changes to your infrastructure as defined by kOps. In the example below we’d like to change some cluster configs:

  1. $ kops edit cluster \
  2. --name=kubernetes.mydomain.com \
  3. --state=s3://mycompany.kubernetes
  4. # editor opens, make your changes ...

Then output your changes/edits to kOps cluster state into the Terraform files. Run kops update with --target and --out parameters:

  1. $ kops update cluster \
  2. --name=kubernetes.mydomain.com \
  3. --state=s3://mycompany.kubernetes \
  4. --out=. \
  5. --target=terraform

Then apply your changes after previewing what changes will be applied:

  1. $ terraform plan
  2. $ terraform apply

Ps: You aren’t limited to cluster edits i.e. kops edit cluster. You can also edit instances groups e.g. kops edit instancegroup nodes|bastions etc.

Keep in mind that some changes will require a kops rolling-update to be applied. When in doubt, run the command and check if any nodes needs to be updated. For more information see the caveats section below.

Teardown the cluster

When you eventually terraform destroy the cluster, you should still run kops delete cluster, to remove the kOps cluster specification and any dynamically created Kubernetes resources (ELBs or volumes). To do this, run:

  1. $ terraform plan -destroy
  2. $ terraform destroy
  3. $ kops delete cluster --yes \
  4. --name=kubernetes.mydomain.com \
  5. --state=s3://mycompany.kubernetes

Ps: You don’t have to kops delete cluster if you just want to recreate from scratch. Deleting kOps cluster state means that you’ve have to kops create again.

Caveats

kops rolling-update might be needed after editing the cluster

Changes made with kops edit (like enabling RBAC and / or feature gates) will result in changes to the LaunchTemplate of your cluster nodes. After a terraform apply, they won’t be applied right away since terraform will not launch new instances as part of that.

To see your changes applied to the cluster you’ll also need to run kops rolling-update after running terraform apply. This will ensure that all nodes’ changes have the desired settings configured with kops edit.