Just one click and launch your kubernetes cluster on AWS using ansible collection/roles

Deepanshu Yadav
7 min readMay 16, 2021

Prerequisites- Ansible installed and aws cli configured into your system

if ansible is not installed you can install it using the command pip3 install ansible. or you can also use yum, first configure the epel and then use yum install ansible(this one is better way).

To configure aws cli see the below story👇

(Note- configuring aws cli in windows or mac is same)

Now lets move towards making the ansible playbook.

For this, there is one more requirement i.e. you need to have dynamic inventory configured. To configure dynamic inventory first download the ec2.py and ec2.ini in a folder and give the path of this folder in ansible.cfg file as shown highlighted in the image.

you also have to give permission to the ec2.py command. use below command

chmod +x ec2.py

Also, open this file and change the path of the python as shown below

Now is the time to write the playbook

🔰we will make 3 roles, one to launch ec2 instances on aws, one for configuring one instance as master node of k8s cluster and the rest to configure as slave nodes. you should make a seprate folder and work there. you also need some files to download or create by yourself(you can download from my github, link present at the end). After making the roles, we can bind all in a collection🔰.

The name of the 3 roles are:-

  1. ec2
  2. k8s_master
  3. k8s_slave

Now lets see one by one each role👇

  1. ec2

In the main.yml file of tasks folder and vars folder, write as shown below👇

Here you can see that you have to give the path of your key. you need to give extra permission to your key. use bellow command for this👇

chmod 600 your_key_name.pem

Put your Key in the files folder as shown below👇

Here, i have just used the ec2 module to launch the ec2 instance. we used this module 2 times so as to give ours instances 2 different tags where k8s_master we will configure as master and k8s_slave as slave. you can give count as per how much slaves you want, i have used amazon ami2 with type t2.micro, you can change it as per your wish.

Now lets configure master

2. k8s_master

In the main.yml file tasks folder of k8s_master role, write as shown below👇

This is a long file😅. let me explain:-

  1. By using package and service module, we downloaded and started docker services.
  2. Then we are setting up yum repository for downloading k8s as you can see the next command module is downloading and installing it.
  3. the next service module is starting kubeadm services and the command module runs command to install the required docker images to setup master
  4. Now we have to change the default driver of docker for which the code is written in and after that we need to restart the docker services as you can see we did this by service module.
  5. Then we have installed some more packages like iproute-tc and did some changes in the networking by changing the bridge.conf file.
  6. Then we initialized the kubeadm by giving a particular cidr and ignoring some errors as these comes when we have ram and cpu les than 2 and this is actually as i used t2.micro. otherwise no need to write thhis option but its good to write so that our role dont fail.
  7. Then we just run some more commands required to setup master and finally copied the token which we will use later for slaves to join them with master.

Now lets make role for configuring slsave.

3. k8s_slave

In the main.yml file tasks folder of k8s_master role, write as shown below👇

Again a long file😅. let me explain:-

if you see properly, then most of the steps are same which are actually for configuring k8s. only there is one difference in this role in the last where we are giving the command to join master.

Now our roles are ready to work. you just need to create two more files where we would write playbook to run these roles. i have created this file with the name playbook_deploy_instances.yml and write as below👇

Now run it and it would configure ec2 instances for us.

Now create one more playbook with any name for running master and slave configuring roles. i created with name playbook_deploy_instances.yml.

Now you can make the collection also of these roles which is very simple to do.

Now is the time to run the files. First run the ec2 configuration file then cluster configuration file as shown below.

so as you can see 2 os started with this command. Now time to configure these by running the second playbook👇

You can confirm also by logging to master and use the below command to see that you have one master and 2 slaves.

kubectl get nodes

Now you can make collection of these roles also. First initialize a collection using the command below👇

ansible-galaxy collection init your_namespace_name.your_collection_name

now use ls command and see there must be a folder made with the same name as your namespace name

Now, in the roles folder, copy all the roles that we have made.

There are some more files present where you can put information about your collection like galaxy.yml, docs where you can put more info how collection will work, etc.

Now build the tar file so that we can upload it using the comman

ansible-galaxy collection build dipuyadav/k8s_cluster_on_aws

Now finally upload it using the command

ansible-galaxy collection publish YOUR_COLLECTION.tar.gz --api-key=YOUR_API_KEY

Read the below story to see in detail how to make ansible colletions👇

Git hub link for code👇

so, task done🔥

Message me anytime if you have any problem in using it. Thanks for your time.😊

--

--