Kind & Terraform

Deploying Kind using Terraform

Magsther
AWS Tip

--

In this post, we will look at Kind and how to deploy it using Terraform.

  • What is Kind?
  • How to deploy Kind using Terraform
  • Terraform Registry
  • Interacting with a Kubernetes cluster

What is Kind?

kind is a tool for running local Kubernetes clusters using Docker container nodes.

kind was primarily designed for testing Kubernetes itself, but may be used for local development or CI.

There are many tools to pick from minikube, K3s, MicroK8s to mention a few. The tool we will use is Kind, I find it very lightweight.

One of the things that I really like with Kind, is the support of multi-node (including HA) clusters.

Note: To use Kind, you will need to have docker installed.

Deploying Kind using Terraform

Even though we can easily create a new cluster by running:

kind create cluster, in this post we will use Terraform

Using Terraform to manage your Kubernetes resources have benefits like:

  • Unified and consistent workflow
  • Full Lifecycle Management
  • Defines infrastructure resources in human-readable configuration files that you can version, reuse, and share.

Terraform Registry

The best place to start when using Terraform providers is the Terraform Registry.

A provider is a Terraform plugin that allows users to manage an external API. Provider plugins like the AWS provider or the cloud-init provider act as a translation layer that allows Terraform to communicate with many different cloud providers, databases, and services.

Since we want to use Kind, we search for Kind. The Use Provider button shows an example of how to use the provider.

We can use this as a template.

Setting up the structure

On your local computer, create a new folder mkdir kind-demo

In that folder, create a main.tffile. This file will hold the configuration of the Kind cluster.

Our cluster will contain:

  • 1 node with the role control-plane
  • 1 node with the role worker.

Paste the following code inside the main.tf file.

terraform {
required_providers {
kind = {
source = "tehcyx/kind"
version = "0.0.13"
}
}
}

provider "kind" {}

resource "kind_cluster" "default" {
name = "kindcluster"
node_image = "kindest/node:v1.24.0"
wait_for_ready = true

kind_config {
kind = "Cluster"
api_version = "kind.x-k8s.io/v1alpha4"

node {
role = "control-plane"
}

node {
role = "worker"
}
}
}

You can easily add additional nodes, just add them to the code like this:

node {
role = "worker"
}

Initializing Terraform

Once you have saved themain.tf file, run the following command in the terminal:

terraform init

You should see an output similar to this:

Initializing the backend...Initializing provider plugins...
- Finding tehcyx/kind versions matching "0.0.13"...
- Installing tehcyx/kind v0.0.13...
- Installed tehcyx/kind v0.0.13
...
Terraform has been successfully initialized!

Terraform plan

Great, now that you have initialized Terraform, you can run:

terraform plan.

The plan creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure.

Terraform will perform the following actions:

# kind_cluster.default will be created
+ resource "kind_cluster" "default" {
+ client_certificate = (known after apply)
+ client_key = (known after apply)
+ cluster_ca_certificate = (known after apply)
+ completed = (known after apply)
+ endpoint = (known after apply)
+ id = (known after apply)
+ kubeconfig = (known after apply)
+ kubeconfig_path = (known after apply)
+ name = "kindcluster"
+ node_image = "kindest/node:v1.24.0"
+ wait_for_ready = true

+ kind_config {
+ api_version = "kind.x-k8s.io/v1alpha4"
+ kind = "Cluster"

+ node {
+ role = "control-plane"
}
+ node {
+ role = "worker"
}
}
}

Plan: 1 to add, 0 to change, 0 to destroy.

Terraform apply

To apply the changes, run:

terraform apply

This command executes the actions proposed in a Terraform plan.

Once completed, your new Kubernetes cluster is created.

Interacting with the cluster

Time to do some interactions with our newly provisioned cluster.

The kubectl cluster-info display addresses of the master and services.

> Kubernetes control plane is running at https://127.0.0.1:53839

You can then runkubectl get nodes to retrieve information about the nodes in the Kubernetes cluster.

NAME                        STATUS   ROLES           AGE     VERSION
kindcluster-control-plane Ready control-plane 3m48s v1.24.0
kindcluster-worker Ready <none> 3m20s v1.24.0

Next step

Now that you have a running Kubernetes cluster running, I suggest you jump over to Learn Kubernetes Basics and begin deploying an application :)

Conclusion

In this post, we looked at Kubernetes (kind) and how we can deploy it using Terraform.

If you found this useful, please hit that clap button and follow me to get more articles on your feed.

--

--