Installing Terraform

To install terraform, follow installation instructions here.

Digital Ocean provider

terraform {
  required_providers {
    digitalocean = {
      source  = "digitalocean/digitalocean"
      version = "2.22.3"
    }
  }
}

Authetication

Authetication can be done either with environment variables or terraform variables.

Windows Powershell

Open Powershell and run the following to add new environment variable.

$ENV:DIGITALOCEAN_ACCESS_TOKEN = 'token'

To set the token as environment variable Persistently

[System.Environment]::SetEnvironmentVariable('DIGITALOCEAN_ACCESS_TOKEN','token')

Bash

export DIGITALOCEAN_ACCESS_TOKEN='token'

To set the token as environment variable Persistently

echo 'export DIGITALOCEAN_ACCESS_TOKEN='token'' >> ~/.profile

Replace token with the actual token.

Using Terraform variables Create var.tf and add following

variable "do_token" {
    type = string
    description = "Digital Ocean API Token"
    sensitive = true
}

And add following to provider.tf

provider "digitalocean" {
  token = var.do_token
}

Variables

Variables and their default values

variableRemarksDefault
do_cluster_nameCluster Namek8s-test
do_regionRegionsfo3
do_pool_nameCluster Pool Namek8s-test-pool
do_node_sizeNode CPu and Memorys-1vcpu-2gb
do_nodepool_countNo’s nodes1
do_pool_ad_nameAdditional Pool Namek8s-test-pool-ad
do_node_ad_sizeNode CPu and Memorys-1vcpu-2gb
do_nodepool_ad_countNode CPu and Memory1
do_nodepool_scaleEnable Autoscalingtrue
do_node_maxMax no’s nodes for autoscale3

Cluster

Cluster module can be found in k8s.tf

k8s version

Get latest k8s version supported by Digital Ocean with digitalocean_kubernetes_versions data source.

data "digitalocean_kubernetes_versions" "get_version" {
}

To use version prefix, add version_prefix = "1.24."

```hcl
data "digitalocean_kubernetes_versions" "get_version" {
    version_prefix = "1.24."
}

Cluster with pool

deploy cluster with version from version above

resource "digitalocean_kubernetes_cluster" "k8s_cluster" {
  name    = var.do_cluster_name
  region  = var.do_region
  version = data.digitalocean_kubernetes_versions.get_version.latest_version

  node_pool {
    name       = var.do_pool_name
    size       = var.do_node_size
    auto_scale = var.do_nodepool_scale
    min_nodes  = var.do_nodepool_count
    max_nodes  = var.do_node_max
  }
}

Aditional pool

resource "digitalocean_kubernetes_node_pool" "k8s_cluster_pool_ad" {
  cluster_id = digitalocean_kubernetes_cluster.k8s_cluster.id

  name       = var.do_pool_ad_name
  size       = var.do_node_ad_size
  auto_scale = var.do_nodepool_scale
  min_nodes  = var.do_nodepool_ad_count
  max_nodes  = var.do_node_max
}

Deploying

Before deploying make sure everything is as per spec by validating with

terraform validate

Plan the depliyment with

terraform plan

Deploy the cluster with

terraform deploy

Append -var "do_token=token" to use different token.

Destroy

To destroy the infrastructure, run

terraform destroy

Append -var "do_token=token" to use different token.

Conclusion

Everything used here is in publicly available repo on GitHub here.Check the official documentation here. Feel free to comment here or drop an email. [Au Revoir](## Conclusion).