Ansible Role to Configure K8S Multi Node Cluster over AWS Cloud.

Anil Yadav
5 min readApr 18, 2021

What is Kubernetes?

Kubernetes (also known as k8s or “Kube”) is an open-source container orchestration platform that automates many of the manual processes involved in deploying, managing, and scaling containerized applications.

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.

What is a Kubernetes cluster?

A Kubernetes cluster is a set of nodes that run containerized applications. It allows containers to run across multiple machines and environments: virtual, physical, cloud-based, and on-premises. Kubernetes containers are not restricted to a specific operating system, unlike virtual machines. Instead, they can share operating systems and run anywhere.

There are two kinds of Nodes:

· Master Node: Hosts the “Control Plane” i.e. it’s the control center that manages the deployed resources. Some of its components are kube-apiserver, kube-scheduler, kube-controller-manager, kubelet.

· Worker Nodes: Machines where the actual Containers are running on. Some of the active processes are kubelet service, container runtime (like Docker), kube-proxy service.

What is Ansible?

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy

ARTH — Task 19 👨🏻‍💻

Task Description📄

📌 Ansible Role to Configure K8S Multi Node Cluster over AWS Cloud.

🔅 Create Ansible Playbook to launch 3 AWS EC2 Instance

🔅 Create Ansible Playbook to configure Docker over those instances.

🔅 Create Playbook to configure K8S Master, K8S Worker Nodes on the above created EC2 Instances using kubeadm.

🔅 Convert Playbook into roles and Upload those role on your Ansible Galaxy.

In this Task, We have a Controller node and three Target Node for configuring. One Target node as a k8s_master and two target node as a k8s_node1 . In the Controller Node use “pip3 install ansible “ command to install Ansible.

Lets start………

Pre-Requisites:

  • Create IAM USER With ec2 power.
  • install boto library of python : |pip3 install boto|pip3 install boto3

Step1: update ansible.cfg file : “vim /etc/ansible/ansible.cfg”

Step2: Creating Role to launch ec2 instance in AWS :

ansible-galaxy init ec2_provisioning” ,

ansible-galaxy init kubernetes_master_node” ,

ansible-galaxy init kubernetes_worker_node

Step 3: create tasks_file : “vim ec2_provisioning/tasks/main.yml”

Step 4: vars_files : “vim ec2_provisioning/vars/main.yml”

Step 5 : Create setup.yml ansible-playbook : “vim setup.yml”

Step 6 : Run Ansible-playbook : “ansible-playbook setup.yml”

Now we have to find the ip of this instance by using python dynamic inventory code. So we download this code from github.

Write given command inside your inventory folder

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.pywget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

update the ec2.py file “vim ec2.py” | python to python3

Now, AWS_REGION, AWS_ACCESS_KEY_ID AND AWS_SECRET_ACCESS_KEY inside the ec2.ini file.

In the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY you have to write your aws access key and secret key which you got at the time of user creation in aws. “vim ec2.ini”

After doing this you have to export these things.

export AWS_REGION='ap-south-1'export AWS_ACCESS_KEY_ID='XXXXXXX'export AWS_SECRET_ACCESS_KEY='XXXXXXXXX'

Now , make these files executable. use following command :

chmod +x ec2.pychmod +x ec2.ini

Check ansible hosts list : “ansible all — list-hosts”

Step 7 : Create task for k8s_master :

“vim kubernetes_master_node/tasks/main.yml”

Step 8 : Create vars file :

“ vim kubernetes_master_node/vars/main.yml”

Step 9 : Create task for Kubernetes_worker_node :

“vim kubernetes_worker_node/tasks/main.yml”

Step 10 : Create vars files for k8s_worker_node :

“ vim kubernetes_worker_node/vars/main.yml”

Step 11 : Create setup.yml file for merging role : “vim setup.yml”

Step : 12 Run ansible playbook :

“ansible-playbook setup.yml”

OUTPUT

Now only thing to check is our k8s multi-node cluster is setup or not ?

we can check it by login into the master node and ask them How many nodes they have connected with command

“kubectl get nodes”

Finally our k8s multi-node cluster is ready 😃🙌

Thanks ❗❗

🔰Keep Learning❗❗ 🔰Keep Sharing❗❗

--

--