Configuring Kubernetes Multi-Node Cluster using Ansible over AWS
Tags: aws, ansible, kubernetes
Published: 2021-05-04 12:04:41

Hello everyone, 

In this blog, we will discuss how we can create Kubernetes multi-node cluster using Ansible role over Amazon Web Service (AWS).

So, before we start with the topic let's clear some terminologies that will help you in understanding the blog.

## Kubernetes 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p21qn04wkzuo9mxfd0xs.png)

In simple terms, we can define Kubernetes as it is an Open Source container orchestration tool.
It helps to automate application deployment, scaling, and management. [For Detail](https://kubernetes.io/)


- containers

Containers are a form of operating system virtualization. A single container might be used to run anything from a small microservice or software process to a larger application. Inside a container are all the necessary executables, binary code, libraries, and configuration files. [For Detail](https://www.netapp.com/devops-solutions/what-are-containers/#:~:text=Containers%20are%20a%20form%20of,%2C%20libraries%2C%20and%20configuration%20files.)

- Pods

A pod is a group of one or more containers, with shared storage and network resources. Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. [For Detail](https://kubernetes.io/docs/concepts/workloads/pods/)

- Kubernetes Cluster

A Kubernetes cluster is a set of nodes that run containerized applications. Containerizing applications package an app with its dependencies and some necessary services. They are more lightweight and flexible than virtual machines. In this way, Kubernetes clusters allow for applications to be more easily developed, moved, and managed. 

- Master Node: 
The master node controls the state of the cluster; for example, which applications are running and their corresponding container images. The master node is the origin for all task assignments. 

- Worker/Slave Node:
The worker nodes are the components that run these applications. Worker nodes perform tasks assigned by the master node. They can either be virtual machines or physical computers, all operating as part of one system. [For Detail](https://www.vmware.com/topics/glossary/content/kubernetes-cluster#:~:text=A%20Kubernetes%20cluster%20is%20a,dependences%20and%20some%20necessary%20services.&text=Kubernetes%20clusters%20allow%20containers%20to,based%2C%20and%20on%2Dpremises.)

## Ansible

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikznlwis3jhhckf6u8ik.png)

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.

- Playbook:

Ansible Playbooks offer a repeatable, reusable, simple configuration management and multi-machine deployment system, one that is well suited to deploying complex applications.[For Detail](https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html)

- Role:

An Ansible role is a set of tasks to configure a host to serve a certain purpose like configuring a service. Roles are defined using YAML files with a predefined directory structure. A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers.

**Now let's start with our main topic**

## prerequisites:

- Aws account
- Ansible installed in your system [ I am using Linux system ]
- boto3 library installed in your system
# Create Ansible role to create Ec2 instances in AWS

To create role use command `ansible-galaxy init <role-name>`

Before creating a role it is good practice to create a separate workspace or directory where you will store file and create ansible roles

Here I have created a role with the name **ec2-instances**

After creating the role you will see that subfolders are created and in that folders, there is a file named **main**, we have to add our code in that file.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwcujazugd2j7xpjkr9f.png)

**In ansible we create code in YAML format**

Now our **ec2-instances role is created** now we need to add our code.

- Go inside the Role that you have created and then go inside the directory named as **tasks**

Noy you will see the file named as **main.yml**, edit that file and write code that defines the following things:

-> about your aws region-name
-> your aws key name
-> Type of instance you want to launch
-> Os image name
-> about your aws security group and subnet id

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8z849advl4i0hzm7j15w.png)

That's how we write code in YAML format.

-  Now go to vars director and edit **main.yml** file and add variable values used in the task file

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvpelodbhse0ag6vemeq.png)

- Now download the inventory files from [here](https://github.com/Nitesh-thapliyal/Kubernetes_Multi_Node_Cluster/tree/master/mydb)

Edit both files and add your aws **access key and secret key**

- Now create an ansible configuration file **ansible.cfg**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xivz0dg3565vopocve46.png)

- Now create an ansible playbook to run the role 

Here I have created a file named **ec2.yml*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2oq97hyamim93xg93oab.png)

-Now run the playbook

Before we run the playbook let's check is there any instance running in our aws account

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nzoo1g2testdolhwgrqy.png)

Here we can see there is no instance running.

Now run the playbook, to run the playbook using command `ansible-playbook <playbook-name>` in my case **ansible-playbook ec2.yml**

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5tozrbn6qexlrcg8ov4.png)

Now let's check in aws, the instance is created or not

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hoxxl43lzjyha4sihl74.png)

Here we can see instances are created.

- Now try to ping to the instances to check the connectivity

To ping the instances using the command ` ansible all -m ping`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ld8pitnpzzrz3f72q4ho.png)

Here we can see we have connectivity with our instances.

We can also check detailed info about instances by running the **ec2.py** file 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cd765ghtmsy4oqb8rux.png)

Now we have our instances ready, now it is time to configure the Kubernetes cluster

## Kubernetes

- Create an Ansible role for Kubernetes master node

To create role use command `ansible-galaxy init <role-name>`
here I have given the role name **k8s-master**

- Go to the role created and open the directory named as **tasks** and edit **main.yml** file

This file includes:
-> configuration of packages required in Kubernetes cluster.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5v6wr1t3ebd4ju36wc1e.png
)

- Now open the var directory and edit the **main.yml file**

This file includes the name of the packages required in the configuration of the Kubernetes cluster

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh6tbz6eflme6587xjp9.png)

- Now open the directory named as **files**

create **daemon.json** and kubnenertes config file **k8s.cfg**

In the **daemon.json** file add the driver 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4uvun50c08hkuq62po6f.png)

and in **k8s.cfg** file add:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1f09ncgw59gx8klbmco.png)

In the same way, create the role for **K8s-slave** and perform the same things that we have done in **K8s-master**

- Now create an ansible-playbook and add both the roles in it.

Here I have created file name **Kubernetes.yml**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n28nmqmnxal1cg232gz2.png)

Now run the **Kubernertes.yml** file using the command `ansible-playbook Kubernetes.yml` 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jook3jdnc6nerr1b8of0.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1thlzvb7ed1bap6wr3k2.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/puk2w07bgsej7jcjiffr.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t2vr4cnp9cyw56kdp60.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgy3zdtr3ec7w88robf6.png)

- Now check Docker is installed or not

To check docker status use the command `systemctl status docker`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8n95tr1lk5y5nuvy0es.png)

- Now check Kubelet is running or not

To check kubelet[ it is Kubernetes agent ]  status use the command `systemctl status kubelet`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqhdxlwfmez1od6twrki.png)

- Now check if the cluster is ready or not

To check use the command `kubectl get pods -n kube-system`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7b2z9pp1l88559kvzbik.png)

- Now launch a pod and create a webpage inside it 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mowxgxq524mfu03ghqf.png)

So that's how we can create a Kubernetes multi-node cluster using Ansible over AWS.

Thank you!!