Kubernetes Cluster on Multi-Cloud using Terraform and Ansible


Kubernetes is an open-source container orchestration platform that automates various processes involved in deploying, managing, and scaling containerized applications.

Kubernetes cluster can be set up across on-premise, public, private or hybrid clouds. This is why Kubernetes is an ideal platform for hosting applications that require rapid scaling.

Kubernetes Cluster

A Kubernetes cluster is a set of nodes that runs containerized applications. Kubernetes cluster allows the application to run across multiple machines and environments.

Kubernetes cluster consists of one master node and several worker nodes. These nodes can be physical computers or virtual machines. The master node controls the state of the cluster, and the master node is the one that assigns tasks to the worker node. The worker nodes are the components that run these applications. Worker nodes perform tasks assigned by the master node.

We can use automation tools like Ansible or Puppet to automate the configuration of the K8s cluster. If you want to know how we can configure the K8s cluster using Ansible then you can refer to my Article: Configure Kubernetes cluster using Ansible Role Link is mentioned below: 👇 👇

In this article, I am going to configure the K8s cluster on multi-cloud(AWS & AZURE) using Terraform and Ansible. So let’s first know something about Terraform and Ansible.

Terraform is an open-source infrastructure as a code tool created by HashiCorp. Terraform allows creating infrastructure with code in a simple human-readable language called HCL(HashiCorp Configuration Language). Terraform reads the configuration file and creates an execution plan which can be executed to launch the infrastructure.


Ansible is an open-source IT Configuration Management, Deployment & Orchestration tool. Ansible is very simple to use yet powerful enough to automate complex multi-tier IT application environments. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

For this practical, I will use Red Hat 8 Operating System as a workstation so Terraform and Ansible should be installed on it.


Install yum-config-manager to manage your repositories.

Use yum-config-manager to add the official HashiCorp Linux repository.

Install Terraform.

For the Ansible Controller node Python 2 (version 2.7) or Python 3 (versions 3.5 and higher) should be installed.

Ansible can be installed on Red Hat 8 with pip, the Python package manager.

Authenticating to AWS

AWS provider is used to interacting with many resources supported by AWS. The provider needs to be configured with proper credentials before it can be used. We can store our Access Key and Secret Key in Credentials files which live in ~/.aws/credentials. A simple way to create this file is by installing AWS CLI and running the was configure command.

Now we can use the Provider block to configure Terraform to use the profile defined in the credentials file.

For more information, you can refer to the documentation: 👇 👇

Authenticating to Azure

The most simple way to authenticate terraform to Azure is by using Azure CLI. Firstly, log in to the Azure CLI using with az login command.

Once logged in then we can list the subscriptions if there is more than one subscription with the account with the az account list command.

Now set the subscription which we want terraform to use az account set –subscription =” SUBSCRIPTION_ID”.

Now we can use the Provider block to configure Terraform to use the Default Subscription defined in the Azure CLI

For more information, you can refer to the documentation: 👇 👇

I am going to create a separate terraform file for launching infrastructure for Kubernetes Master node over AWS and Kubernetes Worker Node over Azure. But before this let’s create an ssh key for doing ssh to Master node and Worker Node.

I am creating key.tf file to create ssh key and then save the key locally with teffaform_key.pem filename.

Now I am creating aws.tf file which will create an infrastructure with VPC, Subnet, Internet Gateway, routing table, Security group, and then finally launch the instance for Kubernetes Master node.

Create azure.tf file to create Resource group, Virtual Network, Subnet, Network security group, Network interface and launch Virtual Machines for Kubernetes Worker node.

Now I am going to create another terraform file ansible.tf which will update the inventory of ansible with the IP Address of the Kubernetes Master node and Kubernetes Worker node. Then I have used local-exec provisioned to run the ansible-playbook which will use the Ansible Role to configure these instances and Kubernetes Master node and Kubernetes Worker Node.

playbook.yml file contains the playbook to run the Ansible Role which will configure instances on AWS and Azure as Kubernetes master and worker node.

I already have an Ansible role to configure Kubernetes master and worker node created, you can download and use it from Ansible galaxy. The link is mentioned below: 👇 👇


The above Ansible role is created for configuring the K8s cluster in a private network. But here we are configuring the K8s cluster over multi-cloud so we need to use the Public IP of the master node while starting the Kubernetes master which — control-plane endpoint.

In my case, all terraform files and ansible files are in the same folder which is the working directory. Also terraform will dynamically create ssh key and inventory file in the same directory which will be used by Ansible to configure the Kubernetes cluster over AWS and Azure.

Execute the terraform code:

First, we have to initialize the working directory which contains terraform configuration files so that terraform plugin for the respective provider could be installed.

Now let’s run the terraform plan command to create an execution plan.

The output is saying that there are 25 resources that to be added.

Now have to execute the actions proposed in a terraform plan and for this, we have to use terraform apply command.

Terraform will run the ansible command to run the playbook with the local-exec provisioner. Ans the playbook will run the role to configure k8s master node and worker node.

Once terraform apply command execution is completed then we can see the recap of the Ansible playbook, total resources added by the Terraform and in output, we can see the IP Address of K8s Master node and worker node. Also, the inventory is created by Terraform which Ansible can use.

We can see all the resources which are added by the Terraform and also we can check if our k8s cluster is up or not.

Now I will login to the K8s Master node instance which is running in AWS to check if the Kubernetes cluster is up or not.

Finally, Kubernetes multi-node cluster is up where the Kubernetes master node is running on AWS and Kubernetes worker node is running on Azure. We can deploy our containerized applications that require rapid scaling.

Suppose we want to destroy the complete infrastructure then we can use terraform destroy command to destroy the complete infrastructure which is created by Terraform.

With just one click the complete infrastructure will be destroyed and again we can launch the same with terraform apply command.

You can refer to the complete code on my GitHub.

GitHub Link: https://github.com/adyraj/K8s-cluster-multi-cloud

Thank You for reading!! 😇😇



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditya Raj

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻