How run k8s in Digital Ocean with Terraform

Amalfitano
2 min readSep 15, 2024

Some times you need to test something in Kubernetes as fast as you can, like how to create a volume or how to deploy an app with an ALB. Whatever you want, I usually use next recipe to move fast.

Caveats:

  • Digital Ocean active account
  • Token already created in Digital Ocean.

Hands-on!

This example will run only in local environment and is not production-ready code.

main.tf

terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0" # Use the latest 2.x version
}
}
}

provider "digitalocean" {

token = var.digitalocean_token
}


data "digitalocean_kubernetes_versions" "current" {}

resource "digitalocean_kubernetes_cluster" "my_cluster" {
name = "my-k8s-cluster"
region = "sfo3" # Replace with your preferred region
version = data.digitalocean_kubernetes_versions.current.latest_version

node_pool {
name = "default-pool"
size = "s-2vcpu-2gb" # Replace with your preferred node size
node_count = 1 # The name of Nodes.
}
}

resource "digitalocean_kubernetes_node_pool" "additional_node" {
cluster_id = digitalocean_kubernetes_cluster.my_cluster.id
name = "additional-node-pool"
size = "s-2vcpu-2gb" # Replace with your preferred node size
node_count = 1

tags = ["k8s", "additional-node"]
}

variables.tf

variable "digitalocean_token" {
description = "DigitalOcean API token"
type = string
sensitive = true
}

output.tf

output "cluster_id" {
value = format("doctl kubernetes cluster kubeconfig save %s", digitalocean_kubernetes_cluster.my_cluster.id)
}

terraform.tfvars

digitalocean_token = "here you digital ocean token :)"

Amalfitano
Amalfitano

Responses (1)

Write a response

Thank you.. looking for a simplest K8s in DO.. and found this .. But need source code, and also how to run it