Digital Ocean Kubernetes Cluster w/ DOCTL

If you want to set up a Kubernetes cluster on Digital Ocean there are many ways to do this. You could use their control panel, you could use Terraform, but you could also use their command line tool DOCTL.

FYIS

if you need to authenticate or re-authenticate the doctl command line tool do a

doctl auth init

If you still need to install it use

brew install doctl

on your MacOS or check https://github.com/digitalocean/doctl for other OS versions

You of course also need docker running on your own box and have kubectl command line tool running.

Options

To decide what size, region and version there are you need to know what is on offer. For that you can run doctl k8s options command

Sizes

So to check what VCPU and RAM sizes are on offer we simply run the following command:

doctl k8s options sizes
Slug Name
s-1vcpu-2gb s-1vcpu-2gb
s-2vcpu-2gb s-2vcpu-2gb
s-1vcpu-3gb s-1vcpu-3gb
s-2vcpu-4gb s-2vcpu-4gb
s-4vcpu-8gb s-4vcpu-8gb
m-1vcpu-8gb m-8GB
c-2 c-2-4GiB
g-2vcpu-8gb g-2vcpu-8gb
gd-2vcpu-8gb gd-2vcpu-8gb
m-16gb m-16GB
s-6vcpu-16gb s-6vcpu-16gb
....

NB For many commands there are aliases like for Kubernetes itself:

  • kubernetes,
  • kube,
  • k8s,
  • k

Regions

Now for regions there is a similar check to do:

doctl k8s options regions
Slug Name
nyc1 New York 1
sgp1 Singapore 1
lon1 London 1
nyc3 New York 3
ams3 Amsterdam 3
fra1 Frankfurt 1
tor1 Toronto 1
sfo2 San Francisco 2
blr1 Bangalore 1
sfo3 San Francisco 3

Versions

Only when I wanted to check for versions I initially got an error

doctl k8s options versions
Error: Get https://api.digitalocean.com/v2/kubernetes/options: remote error: tls: bad record MAC

When I tried again with VPN it did work

doctl k8s options versions
Slug Kubernetes Version
1.17.5-do.0 1.17.5
1.16.8-do.0 1.16.8
1.15.11-do.0 1.15.11

Complete Command

Now, if we want a complete command to create a cluster we can first do one quick check

doctl kubernetes cluster create --help

It will show us all the options we can use with this at the start

create a cluster
Usage:
doctl kubernetes cluster create <name> [flags]
Aliases:
create, c
Flags:
--auto-upgrade whether to enable auto-upgrade for the cluster
--count int number of nodes in the default node pool (incompatible with --node-pool) (default 3)
-h, --help help for create
....

Final result we use is the following:

doctl kubernetes cluster create smt1 \
   --region ams3 \
   --version 1.17.5-do.0 \
   --tag smt1 \
   --size s-1vcpu-2gb \
   --count 2 \
   --maintenance-window="tuesday=23:00" \
   --auto-upgrade \
   smt-cluster-01

or more on one line

doctl kubernetes cluster create smt1 --version 1.17.5-do.0 --region ams3 --node-pool "name=smt1-node;size=s-1vcpu-2gb;count=1;min-nodes=1;max-nodes=2;auto-scale=true"

The latter has more options including auto scaling turned on.

props andrewsomething

If you want all on multiple lines you could also do

doctl k8s clusters create smt1 \
--region ams3 \
--version 1.17.5-do.0 \
--count "" \
--size "" \
--node-pool "name=smt-1-node;size=s-1vcpu-2gb;count=1;min-nodes=1;max-nodes=2;auto-scale=true" \
--wait

props petermikitsh

This last version we like best as it is easy to read and has all we need.

Kube Config

Then download the kubeclt config and test

doctl kubernetes cluster kubeconfig save use_your_cluster_name

This downloads the kubeconfig for the cluster, merges it with any existing configuration from ~/.kube/config, and automatically handles the authentication token or certificate – DO Link

Quick test

kubectl --kubeconfig="use_your_kubeconfig.yaml"
Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.