TMC : Create vSphere with Tanzu clusters using Terraform

Reading Time: 3 mins

Overview

Terraform provider with Tanzu Mission Control gives operations teams the ability to be infrastructure agnostic and build a code pipeline that accommodates a variety of infrastructure back ends. With Terraform, you can attach any conformant Kubernetes cluster to Tanzu Mission Control, therefore providing increased DevOps velocity by offering an additional route to consistent deployments and management of Kubernetes. In this post, I will explain the steps to create workload cluster using Terraform on vSphere with Tanzu.

Pre reqs

  • For this demo, I have registered management cluster (vSphere with Tanzu) to TMC.

Install terraform

  • Install steps are given here, you can follow the same based on bootstrap machine:
Install steps for ubuntu
# Install HashiCorp's package repository

$ sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl

# Add the HashiCorp GPG key

$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

# Add the official HashiCorp Linux repository

$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

# Update to add the repository, and install the Terraform CLI.

$ sudo apt-get update && sudo apt-get install terraform
  • Check Terraform Version:
terraform --version
  • Create a directory with name as terraform-demo:
# Create a directory

mkdir terraform-demo


cd terraform-demo
  • Create files with below content in directory terraform-demo and provide endpoint, vmw_cloud_api_token as shown in screenshot: 
Create file (provider.tf) with below content in directory terraform-demo
// Tanzu Mission Control terraform provider initialization

terraform {
required_providers {
tanzu-mission-control = {
source = "vmware/tanzu-mission-control"
version = "1.0.1"
}
}
}

// Basic details needed to configure Tanzu Mission Control provider
provider "tanzu-mission-control" {
endpoint = "<org name>.tmc.cloud.vmware.com" // Required, provide the org name
vmw_cloud_api_token = "<APi Token>" // Required, provide the API Token
}

variable "SESSION_NAMESPACE" {}
TMC Terraform
endpoint and api token ref
Create file (create-cg.tf) with below content in directory terraform-demo
// Create cluster group
resource "tanzu-mission-control_cluster_group" "create_cluster_group" {
name = "${var.SESSION_NAMESPACE}-cg"
}
Create file (tkgs-create-cluster.tf) with below content in directory terraform-demo
resource "tanzu-mission-control_cluster" "create_tkgs_workload" {
management_cluster_name = "partnerse-demo-mgmt" // Required, change me
provisioner_name = "partnerse-demo-tkgs" // Required, change me
name = "${var.SESSION_NAMESPACE}-tf"

meta {
labels = { "key" : "test" }
}

spec {
cluster_group = "default"
tkg_service_vsphere {
settings {
network {
pods {
cidr_blocks = [
"172.20.0.0/16",
]
}
services {
cidr_blocks = [
"10.96.0.0/16",
]
}
}
}

distribution {
version = "v1.21.6+vmware.1-tkg.1.b3d708a"
}

topology {
control_plane {
class = "best-effort-small"
storage_class = "tanzu"
high_availability = false
}
node_pools {
spec {
worker_node_count = "1"
tkg_service_vsphere {
class = "best-effort-small"
storage_class = "tanzu"
}
}
info {
name = "default-nodepool"
}
}
}
}
}
ready_wait_timeout = "10m"
}
  • Initialize the providers
terraform init
For ref
  • Export the variable
export TF_VAR_SESSION_NAMESPACE=capv-cluster

Create cluster group

terraform apply -target tanzu-mission-control_cluster_group.create_cluster_group -auto-approve
For ref
  • List the resources state
$ terraform state list
tanzu-mission-control_cluster_group.create_cluster_group

Create cluster

terraform apply -target tanzu-mission-control_cluster.create_tkgs_workload -auto-approve
For ref

Validate

  • Navigate to TMC Console > Clusters > capv-cluster-tf to check the status of cluster creation, which generally takes 5-10 mins to complete.
For ref
  • Navigate to TMC Console > Cluster groups > capv-cluster-cg should be in the list. 

Delete cluster

  • Once completely deployed, execute below commands to delete the resources.
terraform destroy -target tanzu-mission-control_cluster.create_tkgs_workload -auto-approve
For ref

Delete cluster group

terraform destroy -target tanzu-mission-control_cluster_group.create_cluster_group -auto-approve
For ref