Ansible Role to Configure k8s Multi Node Cluster over AWS Cloud.

Created a k8s multinode cluster over AWS cloud, then created an Ansible Playbook to launch 3 AWS EC2 Instance, Ansible Playbook to configure Docker over those instances & created Playbook to configure k8s Master, k8s Worker Nodes on the above created EC2 Instances using kubeadm.

Prasantmahato
5 min readApr 18, 2021

--

Created a WorkSpace for all the practicals to be done which would be more manageable.

STEP 1

Created a Configuration File.

Configuration file

So, I want all the IP to be fetched from AWS.

To achieve that I will be using dynamic inventory instead of static inventory.

To create Dynamic Inventory .

  • Step 1

Created a directory named dynamic_inventory

  • Step 2

As, we all know Ansible is created on top of the python, So it has the capability to fetch IP from the python file.

In that folder, I downloaded and created two files.

This command will create a ec2.py dynamic inventory file

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py

This command will create a ec2.ini dynamic inventory file

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

Two executable file in my dynamic inventory folder.
  • Step 3

We need to make those two files executable.

chmod +x ec2.py

chmod +x ec2.ini

  • Step 4

In ec2.py file, I added an interpreter name on the top , and then commented the 172th line.

Changed in ec2.py file

In ec2.ini file, I added aws_access_key_id and aws_ secret_access_key and the region .

Changed in ec2.ini file
  • Step5

Now I provided environmental Variables.

export AWS_ACCESS_KEY_ID=<Your_access_key>

export AWS_SECRET_ACCESS_KEY=<Your_secret_access_key>

export AWS_REGION=<Your_AWS_region>

  • STEP 6

Looking for the all the hosts on AWS Cloud .

IP of all the ec2 instance in my ap-south-1 region.
  • STEP7

Used ping module in adhoc command to check the connectivity.

Checking the connectivity.

Now, We are done with dynamic inventory.

STEP 3

Created the role for multiple used cases step by step as following

  • For Launching a VPC, Igw, Subnet, Route table

Tasks file

Task in Tasks file.

Vars file

All variables in Vars file.
  • Provisioning an AWS ec2 Instance in that VPC.

Tasks file

Provisioning AWS ec2 instance

Vars file

Variables used in Tasks file .
  • Configuring Kubernetes master on one of the instance.
Configuring k8s Master.
  • Configuring Kubernetes slave on two of the instance.
Configuring k8s Slave.
  • Launching Wordpress & mysql server on kubernetes slave by using k8s Master node.
Wordpress and mysql server.

STEP 4

Playbook to launch a VPC on AWS and ec2 instance simultaneously.

Playbook for VPC and ec2-instance.
Running Playbook

STEP 5

Playbook to launch kubernetes cluster ie, master-slave model and then launch Wordpress & mysql simultaneously.

Playbook for kuberntes Cluster, WordPress and mysql.
Running the Playbook.

VPC, Subnet, Internet Gateway, Route Table and ec2 instance now has been successfully created.

VPC, Subnet, Internet Gateway, Route Table and ec2 instance

STEP 6

Next, I have updated the details WordPress required and then providing MySQL details, It brings to the end of my practical and leads to the great used cases to be solved.

Wordpress server

Open for any Queries and suggestions. Click Here

THANKYOU

--

--