Using Terraform to Set Up a DigitalOcean Kubernetes Cluster

Terraform is an Infrastructure as Code tool (IaC) which manages resources with configuration files rather than through GUIs or custom scripts. It allows you to build, change, and manage your resources in a safe, consistent, and repeatable way by defining the infrastructure in a declarative way. We will be using Terraform along with Terraform cloud to deploy all of the necessary resources needed to create a DigitalOcean kubernetes cluster.
Terraform Cloud Setup⌗
Let’s start with making sure that Terraform is installed on your local machine, by listing all of the available subcommands available:
terraform -help
If the command is not there, follow the Terraform docs for proper installation
Next, create a Terraform Cloud
account and from within that account, create an Organization by following the official Terraform instructions - create account and organization
Since we will be using a CLI-driven workflow we will need to authenticate by running the terraform login
command. A browser window will automatically open to the Terraform Cloud login screen. Name the token appropriately and click Create API token
.
The Terraform CLI will then prompt you for the token. Paste it into your terminal and press Enter
to complete the authentication process.
Terraform Configuration Files⌗
Create a folder called terraform-doks-example
and inside create a main.tf
file that contains:
provider "digitalocean" {}
resource "digitalocean_vpc" "vpc" {
name = var.name
region = var.region
ip_range = 10.10.10.0/24
}
resource "digitalocean_kubernetes_cluster" "doks" {
name = "cluster-${var.cluster_name}-${var.region}-${var.environment}"
region = var.region
# data source that is added in the data.tf file
version = data.digitalocean_kubernetes_versions.doks.latest_version
# the id of the vpc that we are creating above
vpc_uuid = digitalocean_vpc.vpc.id
tags = var.tags
node_pool {
name = format("%s-nodes", var.cluster_name)
size = "s-1vcpu-2gb"
auto_scale = true
min_nodes = 1
max_nodes = 2
tags = var.node_tags
labels = var.node_labels
}
}
Here we are creating a VPC and a managed Kubernetes cluster.
We will now need a variables.tf
file for the input variables that are referenced in the main.tf
file.
variable "name" {
description = "The name of the VPC."
type = string
}
variable "environment" {
description = "The environment that the resources will be deployed to."
type = string
}
variable "cluster_name" {
description = "The name of the Kubernetes cluster."
type = string
}
variable "region" {
type = string
description = "The region where the Kubernetes cluster will be created."
}
variable "tags" {
description = "A list of tag names to be applied to the Kubernetes cluster."
type = list(any)
}
variable "node_labels" {
description = "List of Kubernetes labels to apply to the nodes"
type = map(any)
}
variable "node_tags" {
description = "The list of instance tags applied to all nodes."
type = list(any)
}
Later, we will be defining these variables in our Terraform Cloud workspace.
Add a file called data.tf
:
data "digitalocean_kubernetes_versions" "doks" {
version_prefix = "1.25."
}
This is a data source that we will use for our managed DigitalOcean Kubernetes cluster. It provides access to the available Kubernetes Service versions. Here we are pinning the cluster to a specific minor version.
Next, create a file called terraform.tf
:
terraform {
cloud {
organization = <name-of-terraform-cloud-organization> # name of Organization that was created at the beginning
workspaces {
name = <name-of-terraform-cloud-workspace>
}
}
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
This specifies the version constraints for the DigitalOcean provider that Terraform will use to manage your DigitalOcean resources. It also includes the cloud
block for our Terraform Cloud integration.
Now, go to your terminal and run terraform init
. This will initialize the provider plugin and Terraform Cloud. Once it is successful, you will see the workspace that you specified in your terraform cloud
block in the Projects and Workspaces folder in Terraform Cloud. Once inside the workspace, go to the Variables
folder.
Add the variables from the variables.tf
file. It should end up looking like this:
Don’t forget to add your DigitalOcean Personal Access Token as a variable!
We can now go ahead and run terraform plan
. At this point, you should be able to see the resources that will be created by Terraform in your terminal.
Now all you have to do is run terraform apply
to create the resources in DigitalOcean! 😎