Configuration of K8s_Cluster over AWS by using Ansible

Anushka0104
8 min readApr 17, 2021

👨‍💻 Hola Amigos 👨‍💻

Today I am back with one more interesting use case of automation . In this article I have Configure the K8S Multi-Node Cluster over AWS Using Ansible Automation Script☁️.

For the configuration I have use the ANSIBLE ROLE.

What is Ansible?

  1. Ansible is simple open-source IT engine which automates application deployment, intra service orchestration, cloud provisioning and many other IT tools.
  2. Ansible uses playbook to describe automation jobs, and playbook uses quite simple language i.e., YAML (It’s a human-readable data serialization language & is commonly used for configuration files but could be used in many applications where data is being stored) which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed (YAML — It is in human readable form).
  3. Ansible is designed for multi-tier deployment. Ansible does not manage one system at time, it models IT infrastructure by describing all your systems are interrelated. Ansible is completely agentless which means Ansible works by connecting your nodes through SSH (by default). But if you want other method for connection like Kerberos, Ansible gives that option to you.

How Ansible works ?

In Ansible, there are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.

Ansible works by connecting to nodes (clients, servers, or whatever you’re configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes. SSH keys are the most common way to provide access, but other forms of authentication are also supported.

What is Kubernetes?

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.

⭐Steps required for the configuration of K8S Multi-Node Cluster.

1 . We need to launch three ec2-instances over AWS cloud one as Master Node and two are Slave Nodes.

2. Configure the K8S Master

3. Configure the K8S Slaves

🔶 Steps to launch the ec2-instances

First check the Ansible version and then edit the Ansible Configuration file by using the command

“ vim /etc/ansible/ansible.cfg ”

Create the ansible role to launch the ec2_instanaces by using the command

ansible-galaxy init < role_name >

After creating ansible roles we need to write the yaml code inside the respective files. we have vars folder to keep variables and tasks folder to write tasks.

Task file to launch the ec2_instance

var file to launch the ec2_instances

Create the main playbook which run the role to launch the ec2_instances

Now run the playbook to launch the instances

Command: ansible-playbook <file_name>.yml

Now check the ec2_dashboard to confirm the instances are created or not

Here you can see the instances are launched successfully

Now configure the inventory file by adding the IP’s into respective group

Check the connectivity between controller node and managed node by using ping module

After launching the ec2-instances successfully Now configure Kubernetes Multi Node Cluster on these instances.

🔶 Steps for configuration of Master node

Create the ansible role for the configuration of master node

command : ansible-galaxy init < role_name >

After creating the role write the required tasks in the main.yml file of tasks folder.

Install the docker , start and enable the docker service. (As I am using Amazon Linux 2 image so I don’t need to configure repo for docker but if you are using another image then you need to configure the docker repo first)

To know how to configure the docker visit the following link

Configure the yum repo for Kubernetes

Install the kubeadm, kubectl, kubelet software's . ( In this task I have use the variable which are add into the vars folder of roles )

Variable file of software’s

Pull the images of docker which are required to start the service of kubeadm

Kubeadm works on the systemd driver of docker but by default docker doesn’t work on systemd driver so change the driver of docker . ( we have made the changes into the configuration file of docker that’s way we have to restart the docker )

Install the iproute-tc software which required to change the settings of bridge which helps to create the tunnel for connection

Setting the bridge-nf-call-iptables to 1 to create the tunnel

Now start the master node

Create the .kube directory. Then copy the kubernetes config file into the .kube directory which we have created recently and then change the permission

Configure the flannel plugin and Generate the token and register this token to one variable so that we can give this token to the slave node so that slave can able to connect with master

Here I had register the token to the k8s_token variable and print it on the screen by using the debug module so that I can copy this token and give to the slave which running the playbook of slave node

🔶 Steps for configuration of Slave node

Create the ansible role for the configuration of slave node

command : ansible-galaxy init < role_name >

After creating the role write the required tasks in the main.yml file of tasks folder.

Install the docker , start and enable the docker service. (As I am using Amazon Linux 2 image so I don’t need to configure repo for docker but if you are using another image then you need to configure the docker repo first)

Configure the yum repo for Kubernetes

Install the kubeadm, kubectl, kubelet software’s . ( In this task I have use the variable which are add into the vars folder of roles )

Variable file of software’s

Pull the images of docker which are required to start the service of kubeadm

As you can see in master node kubernetes work on systemd driver so here also we have to change the driver restart the docker

Install the iproute-tc software and set the bridge-nf-call-iptables to 1

Here all the setting required for slave to connect with master is set so now we ready connect to the with master by using the following task

The tasks required to set both master node and slave node are all set now we have to create the playbook which help to run both the roles to configure the k8s_multinode cluster. ( Here I have use prompt module to give the token to slave node which is generated by the master for connection )

Now run the playbook to launch the instances

Command: ansible-playbook <file_name>.yml

Now visit the Master Node to see the cluster is successfully configured or not!

From the above output you can see that we have successfully automated the kuberenetes_multi_node_cluster by using Ansible on AWS Cloud☁

YES !!! We have successfully solved the challenge!!

THANKS FOR READING TILL THE END🤩

--

--