Configuring k8s Multi-Node cluster using Ansible

Ritesh Singh
5 min readMar 5, 2021

Problem Statement

Ansible Roles to Configure K8S Multi-Node Cluster over AWS Cloud.

Solution

For this problem statement, I am using the Ansible collection, in this ansible collection I am creating 3 roles, one for ec2, the second for Kubernetes master, third for Kubernetes slave 🙂.

Prerequisites

  • Ansible installed in OS.
  • Dynamic Inventory configured.
  • AWS CLI configured.
ansible conf file
  • For the dynamic inventory, download ec2.py and ec2.ini from this given URL, and paste into/mydb inventory folder:
https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory
  • change your python path /usr/bin/python3.
  • Save it.
  • After this make this file executable by the following command:
chmod +x ec2.py
  • Permission for key
chmod 600 Key_Name.pem

Step-1)Creating ec2 role

  • Now go to cd /roles folder
  • Create a role.
ansible-galaxy init ec2
  • Now open tasks/main.yml file
tasks/main.yml
  • Make sure that aws CLI configured.
  • I am using the ec2 module for the launching instance.
  • 1 master and 3 slaves, you can change the slave number as per your need.
  • Save it.
  • Now open vars/main.yml for variables.
vars/main.yml

Change variables as per your need:

  • key: instance key Name
  • aws_instance_type: family type
  • count_master: no. of master(1)
  • count_slave: no. of slave(3), you can change.
  • ami_id: AMI_ID
  • subnet_id: Subnet ID
  • aws_region: Region
  • security_group_id: SG’s

Note: You can also use Terraform(Iac) for provisioning, it’s the better choice instead of using Ansible for provisioning. I have used Ansible because my problem statement is different 😄.

Step-2)Creating Kubernetes master role

  • Now go cd /roles folder.
  • Create a role.
ansible-galaxy init k8s_master
  • Now open tasks/main.yml file
tasks/main.yml
  • Oops too long 😅. Let me explain this, for installing k8s, we need docker. So first we installed docker by using the yum module. we also need kubelet,kubectl,kubeadm,iproute-tc . for these, I used loop pkgs variables. before this, we have to configure the repo. After these whole installing. We need to change the docker driver cgroup to systemd
  • After this, we need to setup the CIDR for k8s i have use — ignore-preflight-errors=NumCPU — ignore-preflight-errors=Mem. Because k8s require 2 CPU and at least 2GB RAM, But we have launched t2.micro(1vCPU,1GB RAM). To skip this warning I have to use this, but here one challenge you won’t able to launch lots of pods 🤐, but if your requirement is different use other instance types 🙃. After the complete configuring, we need to configure the flannel.
  • Now the final thing we need to save the token use add_host with token_k8s, this is just a variable this token we’ll use for joining slave with master
vars/main.yml
  • Add these variables in vars/main.yml

Step-3)Creating Kubernetes Slave Role.

  • Now go cd /roles folder.
  • Create a role.
ansible-galaxy init k8s_master
  • Now open tasks/main.yml file
tasks/main.yml

Oops this also long 😅. Let me again explain this. The only change in this role all the packages will be the same. Only need to use that token generated by the master in the slave for calling this token I have used hostvars.

That’s it 😃.

Use This Collection

  • Now first we need to call the ec2 role for launching instances.
  • Create one Playbook.
  • Now run this Playbook
ansible-playbook PLAYBOOK_NAME.yml
  • After this create one more playbook for the k8s cluster.
  • In this playbook, I have used tag_Name_…… because I am using dynamic inventory.
  • Run this playbook
ansible-playbook PLAYBOOK_NAME.yml

--

--