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.
Created a WorkSpace for all the practicals to be done which would be more manageable.
STEP 1
Created a 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
- 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.
In ec2.ini file, I added aws_access_key_id and aws_ secret_access_key and the region .
- 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 .
- STEP7
Used ping module in adhoc command to check 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
Vars file
- Provisioning an AWS ec2 Instance in that VPC.
Tasks file
Vars file
- Configuring Kubernetes master on one of the instance.
- Configuring Kubernetes slave on two of the instance.
- Launching Wordpress & mysql server on kubernetes slave by using k8s Master node.
STEP 4
Playbook to launch a VPC on AWS and ec2 instance simultaneously.
STEP 5
Playbook to launch kubernetes cluster ie, master-slave model and then launch Wordpress & mysql simultaneously.
VPC, Subnet, Internet Gateway, Route Table and ec2 instance now has been successfully created.
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.