Multi Cloud Kubernetes Setup Using Ansible and Terraform

Shashi Kant
7 min readJul 16, 2022

In this blog I am going to Create Multi Cloud Kubernetes Setup Using Ansible and Terraform Where User can deploy Kubernetes Environment on Multi Cloud in “Single Click”. For the dynamic inventory process, the most latest approach is used which includes dynamic IP retrieval with an ansible plugin.

Technologies that are integrated for this Project -

  1. Ansible
  2. Kubernetes
  3. TerraForm
  4. Cloud (AWS, GCP, AZURE)

Let’s get started!!

STEP 1 - Setup Environment for AWS Cloud

STEP 1

Install the required software.

STEP 2

Create IAM user and save access key and secert key temporary anywhere.

STEP 3

Export AWS credentials on terminal or for permanent put your credential in /root/.bashrc file.

STEP 4

Change in file aws_instance_tf ➡ aws.tf

Profile Provide IAM username.

ami ➡ Provide ami-id of amazon linux 2.

key_name Provide your keyname.

security_groups Provide Security Groups.

STEP 5

Put your Instance Key in k8s_master_aws folder in .pem format.

STEP 6

Change the value of private_key_file in k8s_master_aws ➡ ansible.cfg file

By Performing Above Steps k8s Environment for AWS Cloud is Setup.

STEP 2 — Setup Environment for GOOGLE Cloud

STEP 1

Install the required software.

STEP 2

Create a service Account IAM & Admin ➡ service accounts and give power to it.

STEP 3

Generate ssh key in your vm.

STEP 4

Copy the content of file my_key.pub from ~/.ssh folder

STEP 5

Paste the content of my_key.pub file in google cloud

Location — Compute Engine ➡ VM Instances ➡ Metadata ➡ SSH Keys ➡ Edit ➡ Add item

STEP 6

Create keys in google cloud json key and p12 key.

IAM & Admin ➡ service accounts choose your service account click on keys click ADD Key and create new key both json and p12.

STEP 7

Copy *.json key in google_instance_tf folder.

STEP 8

Change name of *.json file to service-account.json

STEP 9

Change in file google_instance_tf ➡ google.tf

Project ➡ Give your project name.

Credentials ➡ Give your json file

STEP 10

Change .p12 key in pem format.

Give your key name at place of pkey.pkcs12

STEP 12

Copy your .pem key file in k8s_worker1_google folder.

STEP 13

Change the value of remote_user in k8s_worker1_google ➡ ansible.cfg file

NOTE — Give remote_user value here those you give username in STEP3.

STEP 14

Change in file k8s_worker1_google ➡ gcp.yml.

Projects ➡ Give your project name

By Performing Above Steps k8s Environment for Google Cloud is Setup.

STEP 3 - Setup Environment for AZURE Cloud

STEP 1

Import packages for Azure

Run command ➡ sudo rpm — import https://packages.microsoft.com/keys/microsoft.asc

STEP 2:-

Make repo for Azure

Copy given below content and run together for making Azure repo.

STEP 3.1

Install Azure python module

STEP 3.2

Install Azure CLI Software

STEP 4

Login to the Azure portal

STEP 5

Automate login screen come up on your vm browser and give username password of your account.

STEP 6

After giving username and password you got msg that is shown given below picture

STEP 7

Retriving id’s of Azure account for futher use

STEP 8

SUBSCRIPTION ID

Location — Go to Resource group ➡ choose your resource group ➡ their you will get Subscription id.

STEP 9

CLIENT ID

TENANT ID

SECRET ID

Location — Go to Azure Active Directory ➡ Go to App Registration

Click on New registration and create new registration with any name

Now click on registration those you create in above there you will find both tenant id and client id

Now click on Client Credentials and create new client secret

Value is your secret id those highlight in your color

STEP 10

Assign the Role to the App registration that you create in STEP9

Location — Go to subscription ➡ Select Access control(IAM) ➡ Click on Add role assignment(Preview)

Select Contributor and click on next

Click on select member

Search your App by name and select it

Click on next and then click on assign role

STEP 11

Export AZURE credentials on terminal or for permanent put your credential in /root/.bashrc file.

STEP 12

Create ssh key in Azure Cloud

Go to SSH-keys and create new key their and download it

STEP 13

Copy public key file with .pub extension in azure_instance_tf folder key name can be anyname in my case it is master.pub

Location of .pub file — Go to resource group ➡ choose your resource ➡ their you find your key those you create in step 12 ➡ Click on that key name and copy public key

STEP 14

Change in azure_instance_tf ➡ azure.tf file

resource_group_name ➡ Your resource name

public_key ➡ Your public key name

subnet-id ➡ For Retrieving subnet-id run given below command on your azure cli

Change group name and vnet name in above command

Choose default id that is at number one

STEP 15

Copy .pem(those you download in STEP 12)file in k8s_worker2_azure folder.

STEP 16

Change in k8s_worker2_azure ➡ ansible.cfg file

private_key_file — Give your private key file name those you copy in STEP 15

STEP 17

Change in k8s_worker2_azure ➡ myazure_rm.yml file

include_vm_resource_groups — Your resource group name

By Performing Above Steps k8s Environment for Azure Cloud is Setup.

STEP 4 - Configure Multi Cloud Setup

STEP 1

RUN setup.yml file for configuring

After run setup.yml file completely Multi Cloud Kubernetes Setup Using Ansible and Terraform Deploy Successfully.

STEP 2

Login into the AWS Kubernetes master node and run command that is given below

Multi Cloud k8s Cluster is Ready!!!

Thats all

Thank You for Reading!!!

--

--