Automating Kubernetes Multi-Node Cluster on AWS using Ansible !

Sumayya Khatoon
7 min readJun 20, 2021

--

Hello Folks! In this blog post I will be explaining on how to create your own Kubernetes cluster using Ansible. The configurations and the required packages will be installed via Ansible. So, Guys let’s get going…

If you have ever set up a Kubernetes cluster on your own, then you definitely know how painful the task it is, it consumes a lot of time and also it has multiple stages for a perfect configuration of the same.

If you are new in Kubernetes and don’t know to how to setup the Kubernetes Multi-Node Cluster manually then check this

Let’s first have the overview of the Technologies used in this Blog Post.

What is Kubernetes?

Simply, Kubernetes is a Container Orchestration Tool. Kubernetes is an open-source platform use to manage the containers. It eliminates many of the manual processes involved in deploying and scaling containerized applications.

What is Ansible?

Ansible is a open source IT Configuration Management , Deployment , & Orchestration tool. This tool is very Simple to use yet powerful enough to automate complex multi-tier IT application environments. It includes its own declarative language to describe system configuration

Why Ansible?

In this Agile/Automation World, we have to use automation for the task, because it not only saves a lot of time, but it also reduces the chances of mistake/human error which can be occurred by humans. For Example, if there are 100 machines that are to be configured, then there is a very high chance for human error in the configuration process. Therefore, to eliminate the error, we have to adopt automation tools like Terraform, Ansible, etc., based on the requirement/use-case. Here in this case Ansible is used because it is a configuration management tool & we have the use-case associated with configuration management only.

Let’s implement the Practical →

Is there any Pre-requisites?

  • Umm🤔! Yes, Ansible to be Installed and Configured.
  • Some basics of Ansible is required.
  • Boto and Boto3 library to be installed.
  • Concept of Dynamic Inventory in Ansible is Required. You can Refer below given Blog
  • AWS account and IAM User to be created and store your access key and secret key.

The main purpose of this Blog is to Automate Kubernetes Cluster on AWS using Ansible Roles…

So, Let’s move on →

Here is the Config File for Ansible.

Step 1: Launching Amazon EC2 Instance using Ansible Playbook.

For achieving success in this Step you should have Boto and Boto3 Library. This is Python SDK which has the capability to contact to AWS Cloud.

  pip3 install boto boto3

Now, create an Ansible Vault to secure your AWS Credentials i.e Access Key and Secret Key.

 ansible-vault create <name.yml>

After this, write an Ansible Playbook for Launching EC2 Instance.

Here I’m going to launch 3 EC2 Instances. Out of 3 Instances , 1 will work as Kubernetes Master and the Other 2 will be our Slave/Worker Nodes.

So, here is the Playbook for same.

Play for Master
Play for Slaves

Now, It’s time to Launch our Playbook.

  ansible-playbook — ask-vault-pass <name.yml>
You’ll see this kind of Output!

Go the AWS Portal and have a look — 3 Instances has been launched and all is in the Running state.

Instances launched!

Step 2: Creating Ansible Roles for Master and Slave Nodes.

Role is a way to manage the Playbook in an Efficient Manner.

Following is the command to create Roles in Ansible:

 ansible-galaxy init <role_name>
Role for K8s Master Node
Role for K8s Slave Node
  tree -C
Hierarchy of k8s Master Role
Hierarchy of K8s Slave Role

Step 3: Configuring Master Node inside the k8s_master role.

To configure Master Node inside the role we’ve to go to our tasks set up inside the role /k8s_master/tasks/main.yml

  main.yml

In the main.yml file we’ve to write the steps to Configure our Master Node

So, here is the Playbook for Configuring Master Node.

I know I know the code is so so Giant 😅 As the Problem Statement looks so simple but actually it’s not. Lemme Explain the Code in a simple manner…

  • For installing K8s, we need Docker as a Engine. So first step is to install the docker using yum command.
  • Secondly, Start the services of Docker and make it permanently enable.
  • Creating a proper yum repo file so that we can use yum commands to install the components of Kubernetes( kubelet, kubectl, kubeadm).
  • Installing kubeadm, kubectl, and kubelet, iproute-tc Package using yum command.
  • Enabling the Kubernetes Services(kubelet).
  • Pulling the Config Images using kubeadm.
  • Configuring the driver of Docker from cgroupfs to systemd.
  • After changing the driver , restarting the Docker Service.
  • Change the iptables.
  • Initializing the Kubernetes Master. Here we’ve to set the CIDR and I have used “— ignore-preflight-errors=NumCPU” and “— ignore-preflight-errors=Mem” because K8s cluster require 2 CPU and at least 2GiB RAM. But in my case I’ve launched the Instance using t2.micro so to skip this kind of warnings I’ve included that args, you can change according to your requirements.
  • Creating Overlay Network using CNI Plugin called Flannel.
  • The next step is to save the token as while initializing master using kubeadm this generate a kubeadm join command with some tokens. For doing so, use add_host .

Step 4: Configuring Slave Nodes inside the k8s_slave role.

To configure slave node role, we’ve to go in the tasks setup of the k8s_slave role i.e. /k8s_slave/tasks/main.yml

 main.yml

In the main.yml file we’ve to write the steps to Configure our Slave Nodes

So, here is the Playbook for Configuring Slave Nodes.

Oops😅again the Code is so so Giant. Lemme Explain this too.

  • Almost 90% of the steps is similar to Master Node the only change is you need to use that token which is generated by the Master.

So simple, right?😅

Step 5: Summing up our Both the Roles in one Playbook.

Now, create a playbook in the root project directory as setup.yml

 vim setup.yml

In this Playbook, I’ve used host as tag_Name_Master and tag_Name_Slave because I’m using Dynamic Inventory.

Step 6: Launching the Playbook.

Now , It’s high time to launch our Playbook…

Let’s Execute the Playbook

  ansible-playbook  setup.yml

And there comes the Output of our Giant Playbook😎

Even though the Code was Big but it’s feel great great when you do this kind of automation in just one click.

And look we’ve our Master and Slave Node Up and Running…

Voila! We’ve reached our Milestone. Yeahh😎

Hope you find this Blog Easy and Interesting!!🤞

Do like Comment and give a clap!!👏

That’s all. Signing off!😇

Thank You !

Source code:

--

--

Sumayya Khatoon
Sumayya Khatoon

Written by Sumayya Khatoon

Machine Learning || Deep Learning || Kubernetes|| Docker || AWS || Jenkins || Ansible(RH294) || Python || Linux(RHEL8 )

No responses yet