If you're the type of engineer that doesn't like to use YAML, yet needs to configure Kubernetes manifests, you're in luck! You can use one of the most popular infrastructure-as-code languages, Terraform, to create, manage, and maintain a Kubernetes environment.

In this blog post you're going to learn how to create a Terraform-based Kubernetes manifest and deploy a Deployment to the Kubernetes cluster.

Prerequisites

To be the most effective by following along with this blog post, you should have the following:

  • Beginner-to-intermediate level knowledge of Terraform.
  • Beginner-to-intermediate level knowledge of Kubernetes
  • A Kubernetes cluster either running in the cloud or on-prem

Writing the Terraform Resource

The Terraform resource for a Kubernetes deployment is written like any other Terraform resource. You first start off with a provider that you are targeting and then the resource(s) that you're creating.

The resource will look very similar to what you'd define in a Kubernetes manifest. Things like tags, the container spec, cpu, memory, labels, replicas etc.

Open up VS Code and create a new file called main.tf to start creating the Kubernetes Terraform resource.

With the main.tf file created, paste in the code below. The code below is doing a few things:

  • It is using the kubernetes Terraform provider to call upon Kubernetes resources.
  • The name of the deployment is called go-container
  • The environment is specifying development
  • Two replicas/pods will be created
  • The Docker image being used is golang:latest
  • Memory and CPU limits are being set
provider "kubernetes" {
}

resource "kubernetes_deployment" "gocontainer" {
  metadata {
    name = "go-container"
    labels = {
      environment = "development"
    }
  }

  spec {
    replicas = 2

    selector {
      match_labels = {
        environment = "development"
      }
    }

    template {
      metadata {
        labels = {
         environment = "development"
        }
      }

      spec {
        container {
          image = "golang:latest"
          name  = "goapp"

          resources {
            limits {
              cpu    = "0.5"
              memory = "512Mi"
            }
            requests {
              cpu    = "250m"
              memory = "50Mi"
            }
          }
        }
      }
    }
  }
}

Deploying the Terraform Resource

Now that the Terraform resource is written in the main.tf configuration, it's time to deploy it.

Before deploying, ensure that you have a Kubernetes cluster set in your context.

Initialize the Terraform directory by running terraform init

Run terraform plan to check for any syntax issues or unresolved references.

To create the resource, run terraform apply

Congrats! You have successfully deployment a Kubernetes Deployment as a Terraform resource.

Closing Thoughts

If you're using Terraform in your environment, this is a really neat and different way to manage Kubernetes resources. A constant discussion is always around how to get away from YAML, and this is one approach.

The code for this blog post can be found here.