Created by Akanksha


Kubernetes is an Orchestration tool which manages service containers with the help of unit named as POD. Main Idea of the tool goes like this — In Production Environment Our Service must reach client with no downtime, and load balancing feature is an integral part of Kubernetes. The PODS are managed in either single Node or Multi-node Kubernetes Cluster. In Cluster each node contains multiple components like kubelet, etcd, scheduler, selector, service, flunnel, kubeadm etc that registers Slave-nodes with master-node.

Kubernetes is an Orchestration Tool that monitor and manage the Containers. It was designed by Google in 2014 and is a Open-Source tool. Kubernetes is established for deployment, maintenance and scaling of containers to maintain a particular state and provide Continuous support to the environment by various features that include Pods, Labels, Selectors, Controller, Replication Controller, Deployment Controller, Replica Set and Services. Kubernetes uses declarative approach for Orchestration hence uses declarative language i.e. YAML or ….. (Refer my Previous Blog on Kubernetes)

Automation is there in the world of IT from decades but Ansible is mainly designed for “Configuration Management”, the tool come to market with High Scalability and ability to kick Start the business, without giving high wages to employees and just Making Scripts to keep on configuring each node started by the Load Balancers. Now the question arises what all Ansible is providing as automation, Can we rely on it for all the Steps of Configuration Management ?……(Refer my Previous Blog on Ansible)

AWS (Amazon Web Services) provide a great level of services due to which it is the most preferred public cloud in world. According to recent Gartner report AWS Rank 1st in the World of Providing Resources and Services with greatest Availability and Security. AWS provides a set of fully managed services that you can use to build and run serverless applications. Serverless applications don’t require provisioning, maintaining, and administering servers for backend components such as compute, databases, storage, stream processing, message queueing, and more….(Refer my blog on AWS)

Let’s Brief our Plan before Proceeding further:

As written in the Heading that the Task of Setting up Cluster is going to be automated for which we are going to use Ansible Roles. Now, There would be part by Part Description of the following:

1 ▪ What Exactly Ansible Role is?
2 ▪ How Kubernetes Master Role be written using Ansible Role?
3 ▪ How Kubernetes Slave/Worker Role be written using Ansible Role?
4 ▪ How AWS EC2 instance Creation Role be written using Ansible Role?
5 ▪ Finally we will Configure whole setup of Kubernetes Multi-node cluster over AWS by running Setup.yml file in Ansible from our local System.

Note: It’s combined task of Mine and Raktim Midya so for any queries you can reach us.

My Local Machine Configurations:
> RHEL 8 VM on top of VBox Manager with 2 CPU, 4GB RAM.
> Ansible version 2.10.4 installed.
> Proper Network connectivity using Bridge Adapter.

Step 1: Create Ansible Configuration file:

Ansible being Agentless Automation Tool need the inventory file at the Controller node which i have mentioned to be our local System. Inventory file could be created either globally inside controller node in the path (/etc/ansible/ansible.cfg) or could be created at the workspace where we are going to run our playbooks/roles.

Create Workspace for this Project:

Configuration file:

For explanations of the above file Visit!

Step 2: Next we will create three Roles i.e. Kubernetes Master Role, Kubernetes Slave/Worker Role and AWS EC2 instance Creation Role.

1 ▪ What Exactly Ansible Role is?
An Ansible role has a defined directory structure with seven main standard directories. Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.
The command ansible-galaxy automatically create a repository comprising all those file name. (Click For further details)

2 ▪ How Kubernetes Master Role be written using Ansible Role?
There will be some plays that will start and download the required services written below:

Step 3: For Kubernetes Master Node Setup role describing How to configure in tasks folder of kube_master role. Also the vars folder contaning some of the variables values:

As we can see in above code there are many keys and their values. Here we are first installing Docker, Kubeadm and ip-tables which are pre-requisite software at master node. then further changing container service and initializing Kubernetes master then flunnel for tunneling and connection between Slave and Master.

3 ▪ How Kubernetes Slave/Worker Role be written using Ansible Role?
There will be some plays that will start and download the required services written below:

Step 4: Now lets write plays for kube_slave role inside the tasks folder and than mentioning the variable values in the vars folder:

Same as Kubernetes Master, Slave pre-requisites are the three software i.e. Docker, Kubeadm and ip-tables. We have to have updates for the ip table for which we have used /etc/sysctl.d/k8s.conf file in slave.

Registering / Joining of Slave node to master node could only be done via the key that is provided by the mater after the whole setup and initialization. We need to copy the key in slave nodes. For this purpose we have used tokens here.

4 ▪ How AWS EC2 instance Creation Role be written using Ansible Role?
AWS is a Public Cloud Provider that uses 3 modes of interaction — CLI/WebUI/API. Ansible connect AWS through API. We need some of the credentials and some API based package which are mentioned below:

Step 5: For launching instances in AWS EC2 we have the following aws-ec2 role. For this we need some credentials for login into aws account. Then we will see how the roles are written:

Making credential.yml vault file:

Your IAM account Access_key and Secret_key are required here so create one IAM and describe the key here in vault file. Purpose of using vault file instead of normal test file is the security of our credentials as ansible vault encrypt the content using AES256 Encryption Algorithm.

Another thing needed is ec2-key-pairs which we need to further login and do work in the launched instances there on our AWS account. For this Go to EC2
-> Key-pairs -> Craeate Key-pair -> Give name to key (key) -> save in .PEM format -> Craete -> Download the key-pair.

You need to change the key in read-only mode, for that use following command:

In aws-ec2 role, plays will be written in tasks folder in main.yml file:

Two python libraries are required to work with AWS api i.e. boto3 and boto. We are going to create 3 instances (say nodes), 1 for Kube_Master and two for Kube_Slave so we have entered these instance these in loop variable inside vars folder main.yml file:

Done with the roles part now lets Finally Write our Setup.yml file that will automate all the tasks cumulatively by just calling those roles. We need to create the file inside our main workspace i.e kube_ansible:

We have given “localhost” as host of the playbook as whole task is running dynamically and with Automation over the AWS Cloud. Followed by the different roles that we have included would run step by step.

Step 6: To run the playbook using ansible-playbook command and giving vault password to authenticate and login into AWS Account.

Hence we have achieved our target of “Automating Kubernetes Cluster Over AWS Using Ansible ”.

Created by Akanksha

Following is the Github Repository for your reference just pull and check how this Automation is Done :)

I want to thank Vimal Sir and Raktim for guidance and full support in the task. Also if you wanna connect me on LinkedIN 👇🏻

Technology enhancement take a journey of learning and exploring!! On a way to achieve and Follow my own star!!