In this blog, we will discuss how we can create Kubernetes multi-node cluster using Ansible role over Amazon Web Service (AWS).
So, before we start with the topic let's clear some terminologies that will help you in understanding the blog.
In simple terms, we can define Kubernetes as it is an Open Source container orchestration tool.
It helps to automate application deployment, scaling, and management. For Detail
Containers are a form of operating system virtualization. A single container might be used to run anything from a small microservice or software process to a larger application. Inside a container are all the necessary executables, binary code, libraries, and configuration files. For Detail
A pod is a group of one or more containers, with shared storage and network resources. Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. For Detail
- Kubernetes Cluster
A Kubernetes cluster is a set of nodes that run containerized applications. Containerizing applications package an app with its dependencies and some necessary services. They are more lightweight and flexible than virtual machines. In this way, Kubernetes clusters allow for applications to be more easily developed, moved, and managed.
The master node controls the state of the cluster; for example, which applications are running and their corresponding container images. The master node is the origin for all task assignments.
The worker nodes are the components that run these applications. Worker nodes perform tasks assigned by the master node. They can either be virtual machines or physical computers, all operating as part of one system. For Detail
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.
Ansible Playbooks offer a repeatable, reusable, simple configuration management and multi-machine deployment system, one that is well suited to deploying complex applications.For Detail
An Ansible role is a set of tasks to configure a host to serve a certain purpose like configuring a service. Roles are defined using YAML files with a predefined directory structure. A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers.
Now let's start with our main topic
- Aws account
- Ansible installed in your system [ I am using Linux system ]
- boto3 library installed in your system # Create Ansible role to create Ec2 instances in AWS
To create role use command
ansible-galaxy init <role-name>
Before creating a role it is good practice to create a separate workspace or directory where you will store file and create ansible roles
Here I have created a role with the name ec2-instances
After creating the role you will see that subfolders are created and in that folders, there is a file named main, we have to add our code in that file.
In ansible we create code in YAML format
Now our ec2-instances role is created now we need to add our code.
- Go inside the Role that you have created and then go inside the directory named as tasks
Noy you will see the file named as main.yml, edit that file and write code that defines the following things:
-> about your aws region-name
-> your aws key name
-> Type of instance you want to launch
-> Os image name
-> about your aws security group and subnet id
That's how we write code in YAML format.
- Now go to vars director and edit main.yml file and add variable values used in the task file
- Now download the inventory files from here
Edit both files and add your aws access key and secret key
- Now create an ansible configuration file ansible.cfg
- Now create an ansible playbook to run the role
Here I have created a file named *ec2.yml
-Now run the playbook
Before we run the playbook let's check is there any instance running in our aws account
Here we can see there is no instance running.
Now run the playbook, to run the playbook using command
ansible-playbook <playbook-name> in my case ansible-playbook ec2.yml
Now let's check in aws, the instance is created or not
Here we can see instances are created.
- Now try to ping to the instances to check the connectivity
To ping the instances using the command
ansible all -m ping
Here we can see we have connectivity with our instances.
We can also check detailed info about instances by running the ec2.py file
Now we have our instances ready, now it is time to configure the Kubernetes cluster
- Create an Ansible role for Kubernetes master node
To create role use command
ansible-galaxy init <role-name>
here I have given the role name k8s-master
- Go to the role created and open the directory named as tasks and edit main.yml file
This file includes:
-> configuration of packages required in Kubernetes cluster.
- Now open the var directory and edit the main.yml file
This file includes the name of the packages required in the configuration of the Kubernetes cluster
- Now open the directory named as files
create daemon.json and kubnenertes config file k8s.cfg
In the daemon.json file add the driver
and in k8s.cfg file add:
In the same way, create the role for K8s-slave and perform the same things that we have done in K8s-master
- Now create an ansible-playbook and add both the roles in it.
Here I have created file name Kubernetes.yml
Now run the Kubernertes.yml file using the command
- Now check Docker is installed or not
To check docker status use the command
systemctl status docker
- Now check Kubelet is running or not
To check kubelet[ it is Kubernetes agent ] status use the command
systemctl status kubelet
- Now check if the cluster is ready or not
To check use the command
kubectl get pods -n kube-system
- Now launch a pod and create a webpage inside it
So that's how we can create a Kubernetes multi-node cluster using Ansible over AWS.