Skip to content

Provisioning a DigitalOcean Cluster

Before running any kb infra do commands, you need two things:

  • doctl installed and authenticated
  • A DigitalOcean API token set in your environment

Run the setup command. It checks whether doctl already has credentials and prompts for doctl auth init if not:

Terminal window
kb infra do setup

A successful run prints: Authenticated as you@example.com (active).

The OpenTofu DigitalOcean provider reads the token from DIGITALOCEAN_TOKEN or DIGITALOCEAN_ACCESS_TOKEN. The plan and apply commands check for this before calling tofu and exit early with instructions if neither variable is set.

Set the token directly:

Terminal window
export DIGITALOCEAN_ACCESS_TOKEN=dop_v1_...

Or pull it from an encrypted secrets file:

Terminal window
export DIGITALOCEAN_ACCESS_TOKEN=$(kb secrets decrypt .env.json | jq -r .DIGITALOCEAN_ACCESS_TOKEN)

Create a token at https://cloud.digitalocean.com/account/api/tokens.

Download the DigitalOcean provider:

Terminal window
kb infra do init

This runs tofu init against tofu/remote/digitalocean/. Only needed on first use or after changing provider versions.

The Terraform configuration accepts variables that control cluster shape. The defaults are:

VariableDefaultDescription
regionsyd1DigitalOcean region slug
node_sizes-1vcpu-2gbDroplet size for worker nodes
node_count3Initial node count
auto_scaletrueEnable autoscaling
min_nodes2Autoscaling lower bound
max_nodes4Autoscaling upper bound
registry_namers-incContainer registry name
maintenance_daysundayMaintenance window day
maintenance_start_time04:00Maintenance window start (UTC)

cluster_name is required and has no default. Pass it as a tofu variable. Create a terraform.tfvars file in tofu/remote/digitalocean/:

cluster_name = "my-cluster"
region = "nyc3"
node_count = 2

Preview what OpenTofu will create:

Terminal window
kb infra do plan

The plan creates three resources: a VPC (<cluster_name>-vpc), a container registry, and the DOKS cluster itself. The cluster version is resolved at plan time from the DigitalOcean API — this avoids state drift caused by the "latest" slug changing between plan and apply.

Create the infrastructure:

Terminal window
kb infra do apply

OpenTofu shows the plan and asks for confirmation. To skip the prompt:

Terminal window
kb infra do apply --auto-approve

Provisioning a new cluster takes several minutes. The cluster runs Kubernetes auto-upgrade and surge-upgrade, so node pool upgrades happen without downtime.

Once apply completes, write the cluster credentials to disk:

Terminal window
kb infra do kubeconfig

This reads the kubeconfig output from the tofu state and writes it to ~/.kube/do-cluster.conf with permissions 600. To write to a different path:

Terminal window
kb infra do kubeconfig --path ~/.kube/my-cluster.conf

Point kubectl at the new config:

Terminal window
export KUBECONFIG=~/.kube/do-cluster.conf
kubectl get nodes

To see all DOKS clusters in your account with node pool specs and monthly pricing:

Terminal window
kb infra do clusters

Output format is human-readable by default. For scripting:

Terminal window
kb infra do clusters --json
kb infra do clusters --yaml

The clusters command calls doctl kubernetes cluster list and doctl compute size list in parallel, then computes per-pool and per-cluster monthly costs.

A single apply provisions:

  • A VPC named <cluster_name>-vpc in your chosen region
  • A container registry (starter tier) with registry integration enabled on the cluster
  • A DOKS cluster with the latest available Kubernetes version, auto-upgrade enabled, and a maintenance window on Sundays at 04:00 UTC

Destroying the cluster with kb infra do destroy --i-am-sure removes all three resources, including any workloads running in the cluster.