Kubernetes Cluster Setup with Terraform and Ansible
Provisioning and managing a production-grade Kubernetes cluster involves multiple tools and processes, each designed to handle specific aspects of the infrastructure lifecycle. Terraform and Ansible are two of the most widely used tools for this purpose. Terraform, an Infrastructure as Code (IaC) tool, excels in provisioning cloud resources in a declarative manner, enabling scalable and repeatable deployments. Ansible, on the other hand, is a configuration management and automation tool, ideal for post-provisioning tasks like deploying applications, configuring software, and managing Kubernetes resources.
You will learn how to combine Terraform and Ansible to create a robust, production-ready Kubernetes environment. Using Terraform, you will provision a Kubernetes cluster on a cloud provider such as AWS (EKS) or Google Cloud (GKE). Once the infrastructure is provisioned, Ansible will be used to configure the cluster, deploy applications, and manage configurations dynamically. This multi-tool approach offers a complete solution for managing Kubernetes clusters in a consistent, automated, and scalable way. By the end of the tutorial, you’ll have a clear understanding of how to use Terraform and Ansible together to handle the full lifecycle of a Kubernetes cluster.
What We’re Building
We are creating a fully functional Kubernetes cluster hosted on a cloud provider such as AWS, GCP, or Azure. Terraform
will handle the infrastructure provisioning, including network resources, compute instances, and the Kubernetes control plane. Ansible will then take over to configure the cluster and manage Kubernetes resources. This approach ensures that the cluster is production-ready, secure, and maintainable.
Tools and Technologies
Terraform: An Infrastructure as Code (IaC) tool that allows you to describe your infrastructure in a declarative language. It automates the provisioning of cloud resources in a consistent and repeatable way.
Ansible: A configuration management and orchestration tool used to automate tasks such as software configuration, application deployment, and infrastructure management.
kubectl: A command-line tool used to interact with Kubernetes clusters.
Cloud Provider Account: For this tutorial, you need an account with a cloud provider like AWS (Amazon Web Services), GCP (Google Cloud Platform), or Azure.
Install Prerequisites
Before you begin, ensure that the required tools are installed on your local machine. These include Terraform, Ansible, and kubectl.
Install Terraform: Terraform is available for most operating systems and can be installed via package managers or by downloading the binary from HashiCorp’s website. On Ubuntu/Debian, you can install Terraform as follows:
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
After installation, verify Terraform is working:
terraform -v
Install Ansible: Ansible can be installed through your system’s package manager:
sudo apt update
sudo apt install ansible
Verify the installation:
ansible --version
Install kubectl: You will need the Kubernetes CLI to interact with the cluster. Install it using the following commands:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
Test the installation:
kubectl version --client
Set Up Cloud Provider Credentials
To provision cloud resources using Terraform, you must authenticate with your cloud provider.
AWS: For AWS, use the AWS CLI to configure your credentials:
aws configure
You will be prompted to enter your:
AWS Access Key ID
AWS Secret Access Key
Default region (e.g., us-west-2)
Output format (e.g., json)
Ensure the IAM user associated with these credentials has the necessary permissions to provision Kubernetes resources, such as access to EC2, IAM, and EKS services.
GCP: If you are using Google Cloud, download and install the Google Cloud SDK:
sudo apt-get install google-cloud-sdk
Authenticate with your account:
gcloud auth login
Set the project and region:
gcloud config set project <PROJECT_ID>
gcloud config set compute/region <REGION>
Azure: For Azure, install the Azure CLI:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Log in to your Azure account:
az login
Create a Working Directory
Organize your project files by creating a directory for your Terraform and Ansible configurations. For example:
mkdir k8s-cluster
cd k8s-cluster
This directory will hold the Terraform .tf files, Ansible playbooks, and related configurations.
Initialize Terraform
Inside the working directory, initialize Terraform to download the required providers and set up the environment for creating the Kubernetes cluster:
terraform init
This step ensures that Terraform is ready to work with your chosen cloud provider.
Verify the Environment
Finally, verify that all tools are correctly installed and configured. Test the cloud CLI tools (aws, gcloud, or az) to ensure they can access your account. Confirm kubectl and terraform are accessible from the terminal. A simple terraform -help or kubectl get nodes (later after provisioning) should confirm that the tools are working.
With the prerequisites installed and the environment ready, you’re now prepared to provision the Kubernetes cluster with Terraform. Next, we will write the Terraform configuration and deploy the cluster to the cloud.