Spinning Up a DigitalOcean Kubernetes Cluster
Kubernetes is powerful , setting it up doesn't have to be painful. If
you want a fast, simple way to get a cluster running, DigitalOcean
(DOKS) is a solid choice. Managed control plane, simple tools,
transparent pricing.
In this guide you'll learn how to create a DigitalOcean Kubernetes
cluster two ways:
- using the DigitalOcean CLI (
doctl
) - using Terraform
We'll assume you start from scratch, no tools configured, no
Terraform providers set up.
Prerequisites
Before you begin, you'll need:
- a DigitalOcean account
- a personal access token (API key) from the DigitalOcean API dashboard
- a working terminal (macOS, Linux, or Windows WSL)
- basic familiarity with Kubernetes and Terraform
⚙️ Option 1: Create a Cluster with the doctl
CLI
Step 1: Install doctl
You can install doctl
using Homebrew (macOS/Linux):
brew install doctl
Alternatively, download the binary for your OS from the DigitalOcean
GitHub releases.
Verify:
doctl version
Step 2: Authenticate with your API token
Get your token from DigitalOcean's dashboard under API → Tokens &
Keys.
Then:
doctl auth init
This will prompt you to paste your token. After that, doctl
can manage
your DOKS clusters.
Step 3: Create a Kubernetes Cluster
Let's create a 3‑node cluster, with mid‑tier nodes, firewall options,
etc. Similar to the Civo example with ports for HTTP, HTTPS, and
Kubernetes API.
doctl kubernetes cluster create do-cluster \
--region nyc1 \
--version latest \
--node-pool "name=default;size=s-2vcpu-4gb;count=3" \
--wait
Explanation:
--region
: where you want the cluster (likenyc1
)--version
: Kubernetes version (you can uselatest
or pin a
specific version)--node-pool "name=default;size=s-2vcpu-4gb;count=3"
: three nodes,
each with 2 vCPU, 4GB RAM--wait
: wait until the cluster is ready.
If you want a smaller, single‑node cluster for testing:
doctl kubernetes cluster create do-small-cluster \
--region nyc1 \
--version latest \
--node-pool "name=single;size=s-2vcpu-4gb;count=1" \
--wait
Step 4: Download the kubeconfig & switch context
Once the cluster is ready:
doctl kubernetes cluster kubeconfig save do-small-cluster
kubectl config use-context do-nyc1-do-small-cluster
Then you can verify:
kubectl get nodes
Option 2: Create a Cluster with Terraform
Using Terraform gives you repeatability, versioning, and ease of
cleanup.
Step 1: Install Terraform
If you don't already have it:
brew tap hashicorp/tap
brew install terraform
Or use tfenv
. Then verify:
terraform version
Step 2: Set up the Terraform provider for DigitalOcean
Create a directory, e.g. doks-k8s
:
mkdir doks-k8s && cd doks-k8s
touch main.tf
In main.tf
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
local = {
source = "hashicorp/local"
version = "~> 2.0"
}
}
required_version = ">= 0.13"
}
provider "digitalocean" {
token = var.digitalocean_token
}
data "digitalocean_kubernetes_versions" "doks" {
# optionally filter by prefix
# version_prefix = "1.28."
}
variable "region" {
default = "nyc1"
}
variable "digitalocean_token" {
}
resource "digitalocean_kubernetes_cluster" "demo" {
name = "demo-cluster"
region = var.region
version = data.digitalocean_kubernetes_versions.doks.latest_version
node_pool {
name = "default-pool"
size = "s-2vcpu-4gb"
node_count = 3
tags = ["web", "frontend"]
labels = {
env = "dev"
}
}
}
resource "local_file" "kubeconfig" {
filename = "${path.module}/${digitalocean_kubernetes_cluster.demo.name}-kubeconfig"
content = digitalocean_kubernetes_cluster.demo.kube_config[0].raw_config
}
You'll need to define inputs (variables) for region
,digitalocean_token
, etc. Optionally you can add tags, labels, auto‑scaling, etc.
Step 3: Initialize, Plan, Apply
terraform init
terraform plan -var digitalocean_token=...
terraform apply
When asked, confirm. Terraform will create the VPC, Kubernetes cluster,
node pools, etc.
To use the cluster:
export KUBECONFIG="${path.module}/${digitalocean_kubernetes_cluster.demo.name}-kubeconfig"
kubectl get nodes
Find this in a github repository at: https://github.com/markcallen/digitalocean-example
Cleanup
To delete the cluster and all associated resources:
With Terraform:
terraform destroy -var digitalocean_token=...
With CLI:
doctl kubernetes cluster delete do-cluster --force
Final Thoughts
DigitalOcean makes spinning up Kubernetes clusters fairly painless. Use
the CLI when you want speed; Terraform when you need repeatability and
infrastructure as code. Whether you're experimenting, doing CI/CD, or
learning, DOKS gives you a clean, managed environment with minimal
friction.