Configure K8S Multi Node Cluster over AWS Cloud using Ansible Role

Ayush Rawat
5 min readJan 12, 2022

What is Kubernetes ?

Kubernetes is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. Kubernetes makes it easy to deploy and operate applications in a microservice architecture.

What is Kubernetes Cluster?

Kubernetes cluster is the collection of all the connected nodes that are working together to provide the facility for Container orchestration. In this cluster, there are two types of nodes Master Node and Worker Node connected together in master-slave topology.

In Master Node there were multiple programs are running like API server, Kube Scheduler, etcd database, Controller Manager and many more that are required for the overall management of the cluster.

Worker Node is the one who takes instruction from the Master Node and launches and Launches a Pod or other resources on themselves. Worker nodes are connected to the Master node with an agent program called kubelet.

Why we need a Multi-Node Cluster Required?

We require a Multi-Node cluster because we have a big challenge of a single point of failure in the Single Node cluster. If we deploy an application on a single node and due to some issue like network failure or power failure our application will go down.

One of the most common ways of creating the Kubernetes Cluster is Kubeadm

But Configuring Kubernetes Cluster using the Kubeadm is a tedious task that’s why we can use Configuration Management Tools like Ansible.

What is Ansible?

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

Now, Let’s create the Kubernetes Multinode Cluster dynamically using Ansible for this we have to do some Configuration using steps mentioned below

I have created a pre-created Ansible Role i.e. Roles that let you automatically load related vars, files, tasks, handlers, and other Ansible artifacts based on known file structure. Which help to create a Kubernetes Cluster in one Single Click.

What will It Do?

  • Launch an infrastructure on AWS.
  • Configure a Master Node.
  • Configure a Worker Node.

Let’s look out how is it works,

First step is to install AWS CLI 2 and set default profile for the account you want to configure the cluster.

Create a workspace or directory with any name to where all the roles and files we have to put. Command for this is

mkdir k8s_ansible_automation

Then we have to create the ansible configuration file like this

ansible.cfg

here we have to put the path of the inventory file, the private key of the AWS we are using to attach to our Instances, roles path, here it is launching Amazon Linux 2 so the default user on this AMI is ec2-user and at last we have to put some Privilege Escalation.

Create the inventory folder and put the Dynamic inventory for ec2 instance like this

mkdir inventory
cd inventory
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.4/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.4/contrib/inventory/ec2.ini

Then come back to the root directory of the project and create the folder for ansible roles.

mkdir roles

Role for provisioning instances on AWS

First Role is for provisioning of the infrastructure on the AWS cloud that will launch an Amazon Linux 2 and Configure the Security Group as we required in the cluster.

For installing this role in this directory execute the following command

ansible-galaxy install ayushrawat_17.k8s_aws_ec2 -p ./roles 

By default it launches 2 worker nodes but we change it according to the requirement.

To change the number of worker nodes

  • Open this file
./roles/ayushrawat_17.k8s_aws_ec2/vars/main.yml
  • Then change the value of variable no_of_slave to the one less then number of slaves required.

Example-

If we have to launch 2 worker nodes we have to mention 1 in this variable

Role for Configuring Kubernetes Master

This role is configuring the master node it will configure the instance which is tagged as type:master launched by the above role it will dynamically get the IP using dynamic inventory of the ec2 instance

To install this role execute the following command from the root directory of the project

ansible-galaxy install ayushrawat_17.k8s_aws_master -p ./roles

Role for Configuring Kubernetes Slave

Now the last role is for configuring the Kubernetes Worker Nodes and join them to the master node created above. This will configure to the instance which are tagged with type: worker launched by the above role dynamically by getting an IP address using the dynamic inventory.

To install the role execute the following command

ansible-galaxy install ayushrawat_17.k8s_aws_slave -p ./role

Here we done with roles work.

Last step is to copy the AWS private key into the project root directory with any name (preferred awskey.pem ) if you are using a key name other than awskey.pem then you have to update the configuration file i.e ansible.cfg with the following variable

private_key_file = <keyname with its path>

and also update the one of the variable of the ayushrawat_17.k8s_aws_ec2 role that is

key: "<keyname>"

in the file ./roles/ayushrawat_17.k8s_aws_ec2/vars/main.yml from root directory of the project.

We are done with the setup . Now we are ready to launch the setup.

Completed setup Github Linkhttps://github.com/AyushRawat-17/Ansible_K8S_Configuration

To launch the setup we only need to execute the one command from project root directory

ansible-playbook -v role.yml

This will executes like the following

After the execution completed it will lauch the setup on the AWS cloud to check the working login to the kubernetes master instance switch user to root then execute the command

kubectl get nodes

It will returns the number of nodes connected if it is working fine.

Conclusion

Configuring the Kubernetes Multi node Cluster manually is a very tedious task due to which we can’t able to focus on the application. By using this Project we can do it dynamically on one click which will helps to save huge time.

Thanks for Reading 😊

--

--