Install Client

 1# most simple 
 2arkade get doctl
 3
 4# normal way
 5curl -OL https://github.com/digitalocean/doctl/releases/download/v1.104.0/doctl-1.104.0-linux-amd64.tar.gz
 6tar xf doctl-1.104.0-linux-amd64.tar.gz
 7mv doctl /usr/local/bin
 8
 9# Auto-Completion ZSH
10 doctl completion zsh > $ZSH/completions/_doctl

Basics

  • find possible droplet
1doctl compute region list
2doctl compute size list
3doctl compute image list-distribution
4doctl compute image list --public
  • Auth
1doctl auth init --context test
2doctl auth list
3doctl auth switch --context test2
  • Create Project
1doctl projects create --name rkub --environment staging --purpose "stage rkub with github workflows"
  • Create VM
1doctl compute ssh-key list
2doctl compute droplet create test --region fra1 --image rockylinux-9-x64 --size s-1vcpu-1gb --ssh-keys <fingerprint>
3doctl compute droplet delete test -f

with Terraform

 1export DO_PAT="dop_v1_xxxxxxxxxxxxxxxx"
 2doctl auth init --context rkub
 3
 4# inside a dir with a tf file 
 5terraform init
 6terraform validate
 7terraform plan -var "do_token=${DO_PAT}"
 8terraform apply -var "do_token=${DO_PAT}" -auto-approve
 9
10# clean apply
11terraform plan -out=infra.tfplan -var "do_token=${DO_PAT}"
12terraform apply infra.tfplan
13
14# Control
15terraform show terraform.tfstate
16
17# Destroy
18terraform plan -destroy -out=terraform.tfplan -var "do_token=${DO_PAT}"
19terraform apply terraform.tfplan
  • Connect to Droplet with private ssh key ssh root@$(terraform output -json ip_address_workers | jq -r ‘.[0]’) -i .key

  • Example of terraform:

  1###
  2### Provider part
  3###
  4terraform {
  5  required_providers {
  6    digitalocean = {
  7      source = "digitalocean/digitalocean"
  8      version = "~> 2.0"
  9    }
 10  }
 11}
 12
 13provider "digitalocean" {
 14  token = var.do_token
 15}
 16
 17data "digitalocean_ssh_key" "terraform" {
 18  name = "terraform"
 19}
 20
 21###
 22### VPC
 23###
 24resource "digitalocean_vpc" "rkub-project-network" {
 25  name     = "rkub-project-network"
 26  region   = "fra1"
 27  ip_range = "10.10.10.0/24"
 28}
 29
 30###
 31### Droplet INSTANCES
 32###
 33
 34# Droplet Instance for RKE2 Cluster - Manager
 35resource "digitalocean_droplet" "controllers" {
 36    count = 1
 37    image = var.do_system
 38    name = "controller${count.index}"
 39    region = "fra1"
 40    size = var.do_instance_size
 41    tags   = [
 42      "rke2_ansible_test_on_${var.do_system}_${var.GITHUB_RUN_ID}_controllers",
 43      ]
 44    vpc_uuid = digitalocean_vpc.rkub-project-network.id
 45    ssh_keys = [
 46      data.digitalocean_ssh_key.terraform.id
 47    ]
 48
 49  connection {
 50    host = self.ipv4_address
 51    user = "root"
 52    type = "ssh"
 53    private_key = file(pathexpand(".key"))
 54    timeout = "2m"
 55  }
 56
 57  provisioner "remote-exec" {
 58    inline = [
 59      "export PATH=$PATH:/usr/bin",
 60      "cat /etc/os-release",
 61    ]
 62  }
 63}
 64
 65output "ip_address_controllers" {
 66  value = digitalocean_droplet.controllers[*].ipv4_address
 67  description = "The public IP address of your rke2 controllers."
 68}
 69
 70
 71# Droplet Instance for RKE2 Cluster - Workers
 72resource "digitalocean_droplet" "workers" {
 73    count = 2
 74    image = var.do_system
 75    name = "worker${count.index}"
 76    region = "fra1"
 77    size = var.do_instance_size
 78    tags   = [
 79      "rke2_ansible_test_on_${var.do_system}_${var.GITHUB_RUN_ID}_workers",
 80      ]
 81    vpc_uuid = digitalocean_vpc.rkub-project-network.id
 82    ssh_keys = [
 83      data.digitalocean_ssh_key.terraform.id
 84    ]
 85
 86  connection {
 87    host = self.ipv4_address
 88    user = "root"
 89    type = "ssh"
 90    private_key = file(pathexpand(".key"))
 91    timeout = "2m"
 92  }
 93
 94  provisioner "remote-exec" {
 95    inline = [
 96      "export PATH=$PATH:/usr/bin",
 97      "cat /etc/os-release",
 98    ]
 99  }
100}
101
102output "ip_address_workers" {
103  value = digitalocean_droplet.workers[*].ipv4_address
104  description = "The public IP address of your rke2 workers."
105}
106
107###
108### Project
109###
110
111resource "digitalocean_project" "rkub" {
112  name        = "Rkub-${var.GITHUB_RUN_ID}"
113  description = "A CI project to test the Rkub development from github."
114  purpose     = "Cluster k8s"
115  environment = "Staging"
116  resources = flatten([digitalocean_droplet.controllers.*.urn, digitalocean_droplet.workers.*.urn])
117}