This repository contains Terraform code to provision a Kubernetes cluster with 4 nodes on DigitalOcean.
-
Clone the Repository
git clone https://github.com/arthurkretzer/digitalocean-k8s-iac.git cd digitalocean-k8s-iac
-
Configure Variables
Create a env.tfvars file and add the following content:
do_token = "your_digitalocean_token" region = "nyc1" node_size = "s-2vcpu-2gb"
-
Use Terraform
Create the cluster by initializing, planning and applying terraform.
terraform init terraform plan -var-file="env.tfvars" terraform apply -var-file="env.tfvars"
- Cluster ID: Cluster ID for doctl commands if necessary.
- Kubeconfig: Kubernetes configuration file located in ./kubeconfig.yaml for cluster access. It is auto added to your kubeconfig file, or execute manually the doctl command in https://cloud.digitalocean.com/kubernetes/clusters/your-k8s-cluster-id getting started.
- Cluster Endpoint: URL to access the Kubernetes API server.
To destroy the infrastructure and avoid charges:
terraform destroy -var-file=env.tfvars
Note: In some cases, the worker droplets might not be automatically destroyed. If this happens, manually delete them from the DigitalOcean dashboard or use the CLI:
-
Install
doctl
Follow the documentation here.
-
Authenticate with your API token:
doctl auth init
Follow the prompt and enter your DigitalOcean API token.
-
List and delete droplets:
doctl compute droplet list # Identify and delete the droplets for id in $(doctl compute droplet list --format ID --no-header); do doctl compute droplet delete $id --force; done
If you have make installed, you can use:
make clean
This will destroy the k8s cluster and all droplets in your digital ocean account.
This project is licensed under the Apache License v2.0.