Deploying a HA MicroK8s Cluster Using Terraform
Published on: Monday, March 11, 2025
Introduction
In today’s fast-paced cloud-native world, high availability isn’t just a luxury—it’s a necessity. Whether you’re running containerized applications for development, testing, or production, ensuring minimal downtime and seamless scalability is crucial. That’s where MicroK8s shines—a lightweight, yet powerful Kubernetes distribution that’s perfect for edge computing and small-scale clusters.
But how do you make MicroK8s highly available? Manually setting up a fault-tolerant cluster can be time-consuming and error-prone. That’s why we turn to Terraform and Ansible - two powerhouse automation tools that help us provision infrastructure and configure nodes effortlessly. In this guide, I’ll walk through how to automate the deployment of a highly available MicroK8s cluster using Terraform for infrastructure as code and Ansible for configuration management. By the end, you’ll have a fully operational HA cluster that’s resilient, scalable, and production-ready.
Let’s dive in and bring automation to your Kubernetes game! 🚀
Overview
We will cover:
- Setting up a highly available MicroK8s cluster using Terraform and Ansible
- Enabling various MicroK8s add-ons for enhanced functionality
- Connecting an external Ceph cluster for persistent storage
- Labeling nodes for specific workloads and applications
- Generating
kubeconfigfiles for seamless cluster access
Each Ansible Playbook is highly customizable. Feel free to look into each playbook and update as necessary.
Prerequisites
You will need:
- At least 3 bare-metal devices. This could be any kind of device from VMs on Cloud Providers to bare-metal systems in your home or datacenter. Arm64 devices are supported as well, so you could use SBCs like various Pi devices, or any other commodity hardware.
- Devices should be running Linux Operating Systems that support Snap Daemon.
- You also need hostnames and ssh configs setup before running this.
Deploying a Highly Available MicroK8s Cluster
In this section we are going to use Terraform's Ansible Provider to deploy a High Availability Kubernetes Cluster.
- Clone the repository and navigate to the ansible-microceph directory.
git clone https://github.com/abasu0713/terrakube.git cd terrakube/ansible-k8s - Lets create a variables file for Terraform and populate it with details about your hosts.
touch variables.auto.tfvarsvariables.auto.tfvarsservers = [ { # Update the hostname as necessary name = "hostname-1", # Update the IP as necessary ip = "" # Update the ssh user ansible will be using as necessary ansible_user = "" labels = { "gateways" = "true" } }, { # Update the hostname as necessary name = "hostname-2", # Update the IP as necessary ip = "" # Update the ssh user ansible will be using as necessary ansible_user = "" labels = { "database" = "true" } }, { # Update the hostname as necessary name = "hostname-3", # Update the IP as necessary ip = "" # Update the ssh user ansible will be using as necessary ansible_user = "" labels = {} }, { # Update the hostname as necessary name = "hostname-4-accelerated", # Update the IP as necessary ip = "" # Update the ssh user ansible will be using as necessary ansible_user = "" labels = { "accelerated" = "true" } } ] # Use create/destroy for the corresponding operation cluster_operation = "create" # Update the list of microk8s addons you want to enable addons = [ "metrics-server", "rook-ceph" ] # If you used `terrakube/ansible-microceph` for setting up the external Ceph cluster, set this to true connect_external_ceph = true - Now let's run the deployment.
terraform init terraform fmt . terraform plan --out plan.txt terraform apply
Once the deployment completes you should have a fully functional Kubernetes cluster.
If you used terrakube/ansible-microceph for setting up the external Ceph cluster, you should
additionally have an external ceph cluster connected to your Kubernetes cluster for persistent storage.
If you have kubectl installed locally you can start using it since the kubeconfig file is generated for you.

Conclusion
With this we have simplified deploying a highly available MicroK8s cluster using Terraform and Ansible. This automation allows you to easily scale your cluster and add new nodes and label them as needed.
You can also customize the deployment by enabling or disabling specific MicroK8s add-ons and labeling nodes for specific workloads.
By connecting an external Ceph cluster, you can achieve persistent storage for your applications.