Terraform Digitalocean Kubernetes

- 400 Words
devops kubernetes

DigitalOcean recently launched managed kubernetes on their cloud which was really interesting to me. Doing some rough math it looked around 30-50% cheaper than the big three (AWS, GCP, Azure). DigitalOcean is considerably simpler than AWS which is great for small teams like ours. In addition I really liked the idea of kubernetes instead of vendor lock in like fargate or elasticbeanstalk because I could more easily switch to another cloud provider or even use two of them in the future.

One of the goals was to automate the infrastructure through code. So I set out to use terraform to do that and I’m really happy with the kubernetes integration.

You can check out the project from github here

Using it you’ll get the following features:

To use it you need to:

  1. Install terraform
  2. git clone https://github.com/dcardamo/terraform-k8s-do
  3. cd terraform-k8s-do/prod
  4. create a file called prod.tfvars with inputs matching vars.tf. Example below
  5. terraform init
  6. terraform apply -var-file=autobots.tfvars
  7. cp kubeconfig.yaml ~/.kube/config

Here is an example prod.tfvars:

cluster_name = "production"
k8s_version = "1.13.4-do.0"
node_size = "s-1vcpu-2gb"
cluster_size = 2
do_zone = "nyc1"
traefik_replicas = 2
do_token = "your_do_token"
lets_encrypt_email = "me@example.com"
lets_encrypt_main_domain = "example.com"

Make sure to backup your tfstate files after each apply. You’ll need those for the next time you run terraform.

DigitalOcean Kubernetes Cluster

kubernetes cluster

DigitalOcean Load Balancer

kubernetes cluster

Congratulations, you have a kubernetes cluster running with infrastructure as code. The setup I’m showing here costs $20 for the worker nodes and $15/mo for the load balancer. Pretty slick price in my opinion.