Configure kubernetes cluster using ansible role

Dhiraj Kumar
3 min readMay 7, 2021

Ansible is an opensource software, which is used for configuration management . Ansible is written in python language, it supports YAML language for configuration management.

Ansible is idempotance in nature , it supports declarative language .

In ansible, playbook is created for configuration automaticly . In playbook all the steps are mentioned and if it is run then all tasks are done and completed the configuration of any software. Ansible playbook helps to manage the configuration without facing any error . once playbook is created then many times it can be used, every time the configuration set will be same.

Ansible role is the concept in ansible by which tasks of playbook is divided into multiple files and becomes easy to manage it . in ansible role

  • tasks/main.yml is the main file of tasks that the role executes.
  • handlers/main.yml is the file which may be used within or outside the role.
  • defaults/main.yml is the file for default variables for the role
  • vars/main.yml is the file for storing extra variable for the role
  • files/main.yml is the file that role deploys by default
  • templates/main.yml is template that role deploys
  • meta/main.yml is the file for metadata of role

In this task role is created for configuration of kubernetes cluster.

Kubernetes is an opensource software which is used as container management tool . kubernetes cluster works on master-slave concept , Master node manage all the slave node , slave node provides the resources for launch container.

I have created a role for setup the kubernetes cluster and uploaded on ansible galaxy . This is the link for role on ansible galaxy.

Link for github repo of ansible role

Playbook for launching ec2 instance on aws and configure master node and slave node

- hosts: localhost
tasks:
- ec2:
image: ami-048f6ed62451373d9
region: us-east-1
instance_type: t2.micro
vpc_subnet_id: subnet-b0037d91
group_id: sg-08cb0a77090774f8f
key_name: k8s_master_node
instance_tags:
Name: "{{ item }}"
count_tag:
Name: "{{ item }}"
exact_count: 1
aws_access_key: 'xyzzzzzzzzzzzz'
aws_secret_key: 'abccccccccc'
register: ec2
loop:
- "MasterNode"
- "SlaveNode"
- hosts: tag_Name_MasterNode
tasks:
- include_role:
name: k8s-cluster
vars:
kubernetes_node: "MasterNode"
- hosts: tag_Name_SlaveNode
tasks:
- include_role:
name: k8s-cluster
vars:
kubernetes_node: "SlaveNode"

--

--

Dhiraj Kumar

Expertise on Cloud Computing who has helped many startups to reduce cloud cost upto 40% based on business need. Focused to optimize development process.