FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Follow publication

Kubernetes Cluster Automation

Kubernetes has one of the most complex clusters and the setup of this cluster takes a very long time and a huge effort, so I have an idea on how to solve this.

Let’s see today's agenda

📌 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 that role on your Ansible Galaxy.
🔅 Also, Upload all the YAML code over your GitHub Repository.

Let’s start the practical directly

first, go to your AWS and create an IAM user and get the aws access key and secret key copy both the key in paste in a file called

vim ~/.boto

Now We have an access key and a private key to launch the aws instance so let's see how we can launch the aws instance. As we know we are launching a multi-node cluster so we need multiple ec2 instances so rather than writing a playbook for three instances I have created a role for you. The role is like a package or library in programming using this we can create our instance easily and it helps in code reusability and code maintainer

so how to create a Role, To do so we have a command called

ansible-galaxy init rolename

The above command will create a role for you. I have used the command “ansible-galaxy init ec2prove” to create a role for launching the ec2 instances

The above image will show you the folder inside your role. Here the tasks are very important — we are writing our main playbook and “vars” is the folder where we are giving the variables for the task so let's see how we can do this.

This is the main file in the tasks folder
This is vars file for the ec2 role

Note: each time you use this role you have to change the variables accordingly

Next Step:

As you all know k8s cluster has a master(control plane) so to configure this we are going to create an ansible role and Here is the main part of automation that comes into the picture

As I have taught you, create a role in that way

Now to configure the master you should have basic knowledge about the creation of the k8 cluster. but here you just have to copy my code which will help to create a cluster without any prerequisite.

Now our master node role has been configured successfully

Next step:

Now we are going to configure the worker node on the top of aws. so go and create an ansible role and upload the below code in the tasks file.

Now you have configured both master and slave roles and to set up this on aws cloud we have created another playbook that will be using these roles to create clusters. And here comes the final step of the cluster, let's go and break it.

Now here we are going to use dynamic inventory in ansible to retrieve the IP address of our nodes so that we can do further configuration on it.

you have to install two files to work with dynamic inventory

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

after installing both files we have to change the permissions to

chmod 755 ec2.py
chmod 755 ec2.ini

also in the ec2.ini file you have to access the key and secret key so that we can use it

Here you have to give access key and secret key and remove # from the line

Now go to the inventory file and put a command

ansible -i ec2.py region-name --list-hosts

Now we are going to write a setup file that will help you to complete the cluster

This is the setup file for the cluster

Note: in the hosts, you have to change the name of the node to “tag_Name_’yournodename’ ” in master and slave. and also in the role, you have to give the path of your role

Command to run ansible-playbook

ansible-playbook setup.yml

our first role has launched two instances for slave and one for master

now copy the token and paste it into the below prompt all some further tasks will run automatically

now you have to go to your master login as root and run the command

kubectl get nodes

Here comes the end for today see you in another article goodbye

you can connect me on LinkedIn

you will find complete code on GitHub go through that

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Written by Abhishek Prasad Kesare

Data science, , cloud computing, Artificial Intelligence, Cybersecurity,tech-blogger

No responses yet

Write a response