Kubernetes Magic: Crafting Clusters the Easy Way with Terraform and Multipass

Santiago Miguez Deagustini
Globant
Published in
7 min readDec 1, 2023
Photo by william william on Unsplash

In this article, I will share my journey toward preparing for the Certified Kubernetes Administrator (CKA) exam. I faced a significant challenge in building a local Kubernetes (K8s) cluster for hands-on practice and exploration. As a macOS M1 user, I was constrained by the lack of viable virtualization options. However, my search led me to a unique solution involving Terraform and Multipass. This article will detail how leveraging those tools provided a seamless approach to creating a local Kubernetes cluster. This solution not only overcame the limitations of the macOS M1 architecture but also proved to be an invaluable resource for my CKA exam preparation.

Background

As mentioned before, I am in the process of preparing for the CKA exam. This certification program ensures that CKAs possess the necessary skills, knowledge, and competency to fulfill the responsibilities of Kubernetes administrators. To effectively practice and explore the various topics covered in the exam curriculum, I require a fast and reliable method to create a local Kubernetes cluster.

During my research on Google, I came across an intriguing repository that detailed how to create a local Kubernetes cluster using Terraform and Multipass. Multipass, developed by Canonical, is designed to orchestrate Ubuntu VMs. As a macOS M1 user, I encountered limitations with virtualization software for VMs. For instance, popular options like VirtualBox do not support this architecture, making it impractical for my needs. However, I found the solution using Terraform and Multipass an excellent alternative.

By leveraging Terraform’s capabilities and the orchestration tools provided by Multipass, I can swiftly and efficiently create a local Kubernetes cluster. This allows me to practice and gain hands-on experience with the various aspects of Kubernetes administration. The combination of Infrastructure as Code, Terraform, and Multipass provides a seamless and reliable solution for my exam preparation.

In conclusion, using Terraform and Multipass to create a local Kubernetes cluster offers me a practical and efficient way to study for the CKA exam. It overcomes the limitations faced by macOS M1 users and provides the necessary tools to explore the intricacies of Kubernetes administration.

Original repository

The repository I refer to is terraform-kubernetes-multipass, described as “Building a DEVELOPMENT Kubernetes cluster locally with Terraform and Multipass”, belonging to Michele Sciabarra. It was a good starting point for what I wanted to achieve because it takes care of deploying the VMs, configuring them, and creating a fully functional K8s cluster. Its latest commit is outdated as some Terraform providers are no longer available, for example, the provider template_file. Also, I wanted to add more functionalities, like creating a High Availability solution to allow multiple control-plane nodes. As a result, I forked the original repository and committed all necessary changes to this repository.

What is under the hood?

As previously mentioned, this project combines a couple of software and tools that allow us to create and recreate a local K8s cluster in a few minutes by just running Terraform commands. Let us take a look at these programs:

  • A VM orchestration software, Multipass, gives us an easy yet powerful way to deploy Ubuntu VMs and allows us to initialize them using a cloud-init file. Its installation is simple and doesn’t need any customization to make it work. Multipass works on multiple platforms, like Linux, Windows, and macOS, including Mac M1, M2, or Intel-based processors.
  • The cloud-init file, which is the industry standard multi-distribution method for cross-platform cloud instance initialization, contains all necessary dependencies that will be installed in the VMs dedicated for Kubernetes (Docker, kubelet, kubectl, CRI-O) or the VM for HAProxy.
  • The kubeadm tool, which performs the actions necessary to get a minimum viable, secure cluster up and running in a user-friendly way, is responsible for creating our Kubernetes cluster. It will start its execution on one of the VMs dedicated to the control plane and join the rest of the VM into the cluster, depending on its role.
  • Additionally, we will use the HAProxy software to enable us to choose between a single control plane node or a highly available cluster, where three nodes are created, stacking the control plane and the etcd key-value store.
  • Terraform automates the deployment of all VMs through Multipass and allows the customization of the cluster’s architecture.
  • Finally, the kubectl tool to verify the cluster status.

Prerequisites

Our first step will be installing all the necessary tools to execute Terraform and create Ubuntu’s VMs. From a terminal, run the following commands to install:

  • The git tool, which allows us to clone the repository:
$ brew install git
  • The Terraform tool:
$ brew tap hashicorp/tap
$ brew install hashicorp/tap/terraform
  • The Multipass software: Follow the steps described on the official website.
  • The kubectl tool that will help to validate the installed cluster:
$ brew install kubectl

Deploying with Terraform

Before starting to work on the deployment, how about trying to understand the structure and some critical files to better understand how this project works? Let’s begin by cloning the repository, running the following command on a terminal:

$ git clone https://github.com/smiguez85/terraform-kubernetes-multipass.git

The next picture shows this repository structure:

Directories and files in the repository

Here are key files and a brief explanation of their use:

  • multipass.tf: basic Terraform file to call and execute modules inside the multipass folder.
  • data.tf: it offers all the information to create the VMs by running the Multipass tool.
  • haproxy.tf: it will create the HAProxy instance and take the first control plane instance IP to generate a working HAProxy configuration file, regardless of the number of control plane nodes chosen.
  • haproxy_final.tf: when three control planes are created, it will create a new HAProxy configuration, including their IPs, allowing balancing the API traffic to these nodes.
  • master.tf: it will create the instance and initialize the Kubernetes control plane, allowing all other nodes to join the cluster, either as a control plane or worker node.
  • more_masters.tfIf the value of the variable masters is three, it will create two additional nodes. This Terraform file works with haproxy_final.tf.
  • template.tf: here is where the cloud-init files are created, depending on the purpose of the VM.
  • variables.tf: contains the value of the different variables that control the instances’ specifications, the number of desired nodes for the control plane and workers, and the Kubernetes version to be used.
  • workers.tf: it will create the instance/s and join it/them to the K8s cluster.
  • script folder: it contains configuration templates modified by the variable.tf file and two auxiliary scripts.
  • reset.sh: it allows us to delete all the resources created by Terraform.

Once we have defined whether to use the default values (two CPUs, 2GB memory, 10GB disk, one control plane, and three workers) or make some adjustments, we can execute Terraform as follows.

Please note that it is important to consider resource allocation carefully for projects deployed in Kubernetes that require a considerable number of resources.

  • Initialize a working directory containing Terraform configuration files.
$ cd terraform-kubernetes-multipass
$ terraform init

The following picture shows the terraform init output:

Terraform init output
  • Terraform creates an execution plan and takes the indicated actions provided by the corresponding files, like creating the VMs and their corresponding configuration.
$ terraform apply -auto-approve

The following picture shows the terraform apply output:

Terraform apply output

As depicted in the previous image, Terraform has just created all resources. Some parts of the output are suppressed, as it could take more than 5 minutes to finish, depending on the number of instances.

Furthermore, Terraform generates a kube-config file in the .kube folder in your HOME directory. Its name is kube-multipass to avoid messing around with an existing version of the kube config. You can choose between replacing the current content or adding it.

If you want to delete all previously created resources, simply run the following command:

$ bash reset.sh

Final Step

Now that Terraform has finished creating all resources, we must verify if the cluster is running. First, check if all instances are created in Multipass:

$ multipass list

The following picture shows the multipass listoutput:

Multipass list output

Subsequently, we are ready to verify the nodes using kubectl commands:

$ cp .kube/config-multipass .kube/config
$ kubectl get nodes
Kubectl output

Excellent! We have a working local K8s cluster! Now you are free to play with it!

Conclusion

This project will save you significant time by streamlining the process of creating and recreating a local Kubernetes cluster for various purposes. Whether you are studying for a certification exam, testing new products, or experimenting with different features, this project offers an efficient solution. The efficiency is achieved through the synergy of Infrastructure as Code with Terraform. By leveraging Terraform’s capabilities, you can easily define and manage the infrastructure required for your Kubernetes cluster.

Furthermore, the project utilizes Multipass, which provides simple yet powerful orchestration tools within a virtual machine. This allows for seamless provisioning and management of the virtual machines that make up your Kubernetes cluster.

In addition to these features, the implementation of HAProxy adds another layer of functionality. With HAProxy, you can explore redundancy in the control plane and its services. This enables you to ensure high availability and fault tolerance for your Kubernetes cluster.

Overall, this project offers a comprehensive solution for creating and managing local Kubernetes clusters. It combines the power of Infrastructure such as Code, Terraform, Multipass, and HAProxy to simplify the process and enhance your productivity.

References

--

--