The article presents a tutorial on creating a fully automated Kubernetes cluster using Terraform and AWS EC2, aimed at providing a practical, IaC-based solution for Kubernetes practice and experimentation, particularly useful for CKA and CKAD certification preparation.
Abstract
The author discusses the evolution of their approach to setting up a Kubernetes cluster, transitioning from a manual AWS CLI-based method to a fully automated infrastructure-as-code (IaC) solution using Terraform and AWS EC2. This shift was motivated by the need for a more efficient and controllable environment for learning and practicing Kubernetes, especially in preparation for the Certified Kubernetes Administrator (CKA) and Certified Kubernetes Application Developer (CKAD) exams. The article outlines the steps to clone a GitHub repository containing the necessary Terraform code, apply it to create the Kubernetes infrastructure, and configure the cluster. It also provides a walkthrough of the key scripts and Terraform files involved in the process, emphasizing the ease of creating and destroying clusters with this method. The author concludes by inviting readers to try the code and share their experiences, highlighting the improvements over the previous AWS CLI-based approach.
Opinions
The author finds the previous AWS CLI-based cluster setup non-IaC and impractical for long-term use.
There is an urgent need for a 100% IaC solution to facilitate the creation and destruction of Kubernetes clusters with minimal manual intervention, especially for CKA and CKAD certification aspirants.
The author expresses satisfaction with the new Terraform-driven solution, indicating it works perfectly and efficiently.
The article suggests that the provided code is reliable and should work as-is, with an invitation for feedback if any issues are encountered.
The author encourages readers to engage with the content by clapping and sharing, and to follow their work for updates on related topics.
Mastering Kubernetes One Task at a Time— Creating A Kubernetes Cluster with Kubeadm using Terraform and AWS EC2
100% Automated. Ready, Fire, Cluster! A perfect playground to experiment and learn Kubernetes. Practice CKA and CKAD.
Remarkably and predictably, following the AWS CLI code statements, I could spin up a Kubernetes cluster in less than 10 minutes, exactly as promised. That’s the good news. The bad news is that it is non-IaC and manual. As a result, it’s not a practical long-term solution.
As I am now actively preparing for the CKA and CKAD, there is an even urgent motivation for me to find a 100% IaC solution so that I can create and destroy a cluster with “a click of a button,” practice and experiment completely at my liberty with full control on all the nodes.
It took a little bit of work to get my bearing back on Terraform by referring back to my previous works, and by leveraging the prequel work combined with some inspiration from this timely blog, I am happy to announce that I now have a 100% Terraform-driven solution, and it works perfectly.
The code is in GitHub. So let’s clone and get crackin’!
Clone Cluster GitHub Repo and Apply Terraform
Below is the gist code showing from the beginning to the end of the Kubernetes infrastructure creation, necessary package deployment, and cluster configuration by running terraform on the GitHub code repo cloned in a local environment.
As expected, just firing terraform apply does the full job. After the terraform run, the final step is to ssh into the control plane node and confirm the cluster is up and running.
Code Walk-through and Highlights
Below are the normal terraform template files and some shell scripts under the kubeadm-scripts folder. Worth pointing out:
main.tf — Creates the EC2 nodes and does the necessary Security Group and Key Pair setup as necessary. The variable worker_node_count (default 2) in variables.tf file controls the number of worker nodes.
step-01-k8s-packages.sh — This script runs on all nodes via the remote-exec provisioner. See comments in the script to understand each step.
step-02-k8s-cp-init.sh — This script runs on the control plane node only, again via the remote-exec provisioner. The most important outcome of this script is the node join command.
step-03-k8s-join.sh — This is created during run time via the local-exec provisioned as part of the control plane instance creation. It contains the join command to be run on each worker node, obtained by running the kubeadm token create — print-join-command command on the control plane node. The script, once ready, runs on each worker node via the remote-exec provisioner.
Here I’ve presented the code and explained how to create a fully functional Kubernetes cluster with Kubeadm using Terraform and AWS EC2.
Because it is IaC using terraform, the first part of the post shows step-by-step commands to create the infrastructure and configure the Kubernetes cluster, all via terraform templates and run.
The second part highlights the essential components of the code and the mechanism by which the nodes are configured remotely from a local terraform-enabled environment.
Overall this 100% IaC automation of creating a Kubernetes cluster with Kubeadm is a highly efficient and improved solution compared to its predecessor, which did everything using AWS CLI.
The code should work as-is out of the box. If you encounter any issues, please let me know via comments. All the best!
If you benefited from reading the post, please 👏 a few times before parting, and help others by sharing it; I highly appreciate that!
Please follow to stay in touch, track, and be the first to get notified of all future writings on AWS Cloud, Containers, Kubernetes, and Machine Learning. Also, check all my stories on The AWS Way publication.