Create Multi Node Kubernetes Cluster using Ansible (Automation) Roles inside EC2 Instance Of AWS.

Pritee Dharme .
9 min readFeb 19, 2021

Kubernetes Cluster(master-slave) On AWS ec2 instance using ansible.

Hello All..!!

We all know in today’s generation all are automated and managed so for that we are using lot’s of tool so here for automation we use Ansible and for manage the pods cluster we use a Kubernetes . So for that here in this article we are going to see how to create an multi-node cluster of kubernetes inside ec2 instance of aws cloud.

But before that we have to see some basic and important information regarding this setup .So let’s start…

Amazon Web Service (AWS) :

Amazon Web Services (AWS) is a secure cloud services platform, offering compute power, database storage, content delivery and other functionality to help businesses scale and grow. In simple words AWS allows you to do the following things- Running web and application servers in the cloud to host dynamic websites.Amazon Web Services offers a broad set of global cloud-based products including compute, storage, databases, analytics, networking, mobile, developer tools, management tools, IoT, security and enterprise applications. These services help organizations move faster, lower IT costs, and scale.

Kubernetes :

kubernetes

Kubernetes is an open-source container orchestration platform that enables the operation of an elastic web server framework for cloud applications. Kubernetes can support data center outsourcing to public cloud service providers or can be used for web hosting at scale. Kubernetes is itself an application (or set of applications), and these applications have to run somewhere. Despite what you may have heard, Kubernetes is not an operating system, but still depends on Linux (or Windows) to be installed on the nodes.

Ansible :

Ansible

Ansible is a system of configuration management written in Python programming language which uses a declarative markup language to describe configurations. It’s used for automation of configuration and OS setup. Ansible is often used to manage Linux-nodes, but Windows is also supported. Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

This is an basic concept regarding our setup. Now there is some pre-requisite for this..

Pre-requisite :

  • Ansible configured on your Base OS.
  • A new user having Administrator Access. Note: Ansible requires administrator access user access key and secret key so that ansible can used for configuration.
  • Private key in .pem extension.

Now for setup ansible in base OS refer below article..

To configure IAM (Administrator Access) user refer below article in which we can found basic details about how to create a IAM user. But for that you should have account on AWS cloud.

And for private key copy your key in .pem format in the base OS and give path of it in configuration file of ansible that is ansible.cfg .

Now our all pre-requisites are fulfill . So let’s move toward next part and this is an main part of our task.

We’ll see how to set up a Kubernetes cluster with 2 Worker Nodes and 1 Master Node on Amazon Linux 2 Server’s. We will do this configuration using the Ansible roles where the “kubeadm” tool is used to set up the cluster. Kubeadm is a tool built to provide “kubeadm init” and “kubeadm join” for creating Kubernetes clusters.

So let’s start doing step by step….

Provisioning ec2 Instances :

First we have to launch a ec2 instances on a AWS for that we are going to create a role. For this we have to give all the configuration values such as private key, remote-user, roles path, inventory path, etc in the ansible.cfg file in /etc/ansible/ location.

And pre-requisites for doing something on aws we have to install package that is boto3 or boto. Install one of them which support your OS. for install use command..

pip3 install boto3

pip3 install boto

Now we create a role name as “kube_nodes” for launching nodes in aws.

ansible-galaxy init kube_nodes

Now in the main.yml file of tasks folder we write a role that is give below..

In this role we writing two tasks one for launching a master node and one for launching worker nodes . In my case I am launching 2 worker nodes. In your case you can add or reduce the worker nodes.

Refer the code by visiting below link of github..

Now create a playbook for running this role. I am create a kube_nodes.yml file for writing playbook.

Now let’s run the playbook .

Here you can see that our playbook run successfully.Now check the node is launched or not.

Here you can see that our nodes are launched successfully.

Now put the IP of that nodes in a inventory by doing dynamic inventory file. For that we have to put two files in a base OS and that is ec2.py and ec2.ini file. And play ec2.py file and put our access and secret key in the ec2.ini file.

Now Let’s move towards our important setup of this task. And that is configure master node and worker nodes.

Configure Master Node :

First we are configure a master node. For that we are going to create a role and in that we are assign task which is given below..

In my case I am creating “k8s_master” as a role for configure the master node in aws. For creating a role use command..

“ansible-galaxy init (role name you want)”

Now in the main.yml file of tasks folder we are going to write our playbook. So for setup the master node do below things..

1.Installing docker and iproute-tc

2.Configuring the Yum repo for kubernetes

3.Installing kubeadm,kubelet kubectl program

4.Enabling the docker and Kubernetes

5.Pulling the config images

6.Confuring the docker daemon.json file

7.Restarting the docker service

8.Configuring the Ip tables and refreshing sysctl

9.Starting kubeadm service

10.Creating .kube Directory

11.Copying file config file

12.Installing Addons e.g flannel

13.Creating the token

14. Store output of token in a file.

Now let’s write the playbook..

Refer the below github link for code..

Now, we have to write a playbook for run the role for master setup.In my case I am write k8s_master.yml playbook. You can write by your own choice.

Now, it’s time to run the playbook for configure the master node of kubernetes cluster.So let’s run the playbook..

Here you can see our playbook run successfully. Now let’s move towards configuring slave nodes..

Configure Slave Node :

So now configuring slave node we take 2 instances . And here also we create an role for setup of slave node. In my case I am create a k8s_slave as a role.in your case you change the name.

Now in the main.yml file of tasks folder we write code for setup slave nodes. Below is an points we are going to setup in a slave node..

1.Installing docker and iproute-tc

2.Configuring the Yum repo for kubernetes

3.Installing kubeadm,kubelet kubectl program

4.Enabling the docker and Kubernetes

5.Pulling the config images

6.Confuring the docker daemon.json file

7.Restarting the docker service

8.Configuring the Ip tables and refreshing sysctl

9.Copy the join command which we store while configure master.

10.Run join command.

Refer the below github repo for code to setup slave node..

Now, we have to write a playbook for run the role for slave setup.In my case I am write k8s_slave.yml playbook. You can write by your own choice.

Now we are going to run the playbook for setup the slave node.

You can see our playbook for the slave setup run successfully.

Now for checking our multi-node cluster is created or not run the below command on master node.

kubectl get nodes

Here you can see our cluster is created successfully. Let’s check the kube-system pods running behind the seen.

Here you can see our all pods are running great and all are in ready state.

Now let’s launch one pod in a master node .And command for it is.

kubectl create deployment done --image=vimal13/apache-webserver-php

Here our pod is launched.Now let’s expose it using command..

kubectl expose deployments done --type=NodePort --port=80

Now check the pod in wide using command..

kubectl get pods -o wide

Now here our pod is created and expose also in a first slave. Here I am launching one more pod having name happy by using my own created image.

Here is an image for ssh service. You can found image in below link.

Now We expose it.

So here I am saying our setup of multi-node cluster is created successfully. I hope this article help you to create an setup of multi-node cluster of kubernetes on AWS using Ansible.

Below is an github repo link for entire setup..

So thank you so much for visiting and reading my article . I hope this help you.. !!

And if you have any issue about this setup or article then feel free to ask me on my LinkedIn profile which is given below..

Thank you so much..!!

Keep Learning..!! Have a Great Day ..!!

--

--