DEV Community

Cover image for Configuring Kubernetes Multi-Node Cluster using Ansible over AWS
Nitesh Thapliyal
Nitesh Thapliyal

Posted on

Configuring Kubernetes Multi-Node Cluster using Ansible over AWS

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.


Alt Text

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

  • 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

  • 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

  • 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


Alt Text

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

  • 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


  • 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

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

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

  • Now download the inventory files from here

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

  • Now create an ansible configuration file ansible.cfg

Alt Text

  • Now create an ansible playbook to run the role

Here I have created a file named *ec2.yml

Alt Text

-Now run the playbook

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

Alt Text

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

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

Alt Text

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

Here we can see we have connectivity with our instances.

We can also check detailed info about instances by running the file

Alt Text

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


  • 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

  • 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

  • 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

and in k8s.cfg file add:

Alt Text

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

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

Alt Text

Alt Text

Alt Text

Alt Text

Alt Text

  • Now check Docker is installed or not

To check docker status use the command systemctl status docker

Alt Text

  • Now check Kubelet is running or not

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

Alt Text

  • Now check if the cluster is ready or not

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

Alt Text

  • Now launch a pod and create a webpage inside it

Alt Text

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

Thank you!!

Discussion (1)

adityamangal1 profile image
Aditya Mangal