In order to setup a GitOps workflow, you need to use an Infrastructure as Code (IaC) tool so you can define infrastructure in files that can be tracked by a source control tool like Git.
Terraform is software that uses declarative configuration files to automate the provisioning of infrastructure resources like compute instances, managed databases, firewalls and Kubernetes Clusters.
In this chapter we will use Terraform to create a DigitalOcean Managed Kubernetes Cluster with a control plane and three nodes, like the diagram below.
Diagram Source: Kubernetes Components Documentation
Go to Github and fork clone the KubeCon EU 2022 Workshop Repo and then change into the directory.
Note Make sure to update the command with your github username.
To clone with SSH:
git clone git@github.com:<GITHUB_USERNAME>/kubecon-2022-doks-workshop.git
cd kubecon-2022-doks-workshop
To clone with HTTPS:
git clone https://github.com/<GITHUB_USERNAME>/kubecon-2022-doks-workshop.git
cd kubecon-2022-doks-workshop
- Create an API token
- Export your token as an environment variable called
DO_TOKEN
.
export DO_TOKEN="<YOUR_DO_TOKEN>"
Note: Since Windows doesn't support enviornment variables, Windows users should keep the token on their clipboard to easily paste.
doctl auth init
doctl account get
You should see output like this:
Email Droplet Limit Email Verified UUID Status
kschlesinger@digitalocean.com 25 true 4ba4b281-ie98-4888-a843-2365cf961232 active
Step 4 - Update the doks.tf file
Look for the comments and check the following:
- Change the datacenter region to one that is geographically close to you
- Ensure you have the slug for latest version of DigitalOcean Kubernetes
Change into the Terraform directory and run the initialize command:
cd terraform
terraform init
If successful, you will see this message:
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Run terraform plan
terraform plan -var do_token=$DO_TOKEN
If the plan looks good, run terraform apply
.
terraform apply -var do_token=$DO_TOKEN
You must respond with yes
to this prompt in order to create a cluster. You will see this question:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
If the apply is successful, it will take a 4-5 minutes for your cluster to provision.
To see the status of your cluster, go to the DigitalOcean Cloud Console and click on the Kubernetes Tab. You will see a progress bar indicating whether or not your cluster is fully provisioned. When your cluster is ready, Terraform will also send you a success message in the terminal.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Once your cluster is ready, download a kubeconfig file with your authentication data.
From the Kubernetes view in the Cloud Console, click on the Overview
tab and go to 2. Connecting to Kubernetes
. There, you will find a doctl
command that will download all the necessary info to your kubeconfig file.
For more in-depth instructions, please see the official DigitalOcean documentation on how to connect to a cluster.
When your cluster is ready, run the command
kubectl get nodes
You should see output similar to this:
NAME STATUS ROLES AGE VERSION
kubecon-node-cbu76 Ready <none> 1m v1.22.8
kubecon-node-cbu7a Ready <none> 1m v1.22.8
kubecon-node-cbu7e Ready <none> 1m v1.22.8
Congratulations! You have created a Kubernetes Cluster with Terraform.