Automate K8s Multi Node Cluster Over AWS using Ansible

Automate K8s Multi Node Cluster Over AWS using Ansible


Kubernetes Cluster -

A Kubernetes cluster is a set of node machines for running containerized applications. If you’re running Kubernetes, you’re running a cluster. The cluster is the heart of Kubernetes’ key advantage: the ability to schedule and run containers across a group of machines, be they physical or virtual, on premises or in the cloud. Kubernetes containers aren’t tied to individual machines. Rather, they’re abstracted across the cluster.

  • How do you work with a K8s Cluster -

A Kubernetes cluster has a desired state, which defines which applications or other workloads should be running, along with which images they use, which resources should be made available for them, and other such configuration details. A desired state is defined by configuration files made up of manifests, which are JSON or YAML files that declare the type of application to run and how many replicas are required to run a healthy system.

  • The configuration tool Ansible -

Ansible is a configuration management platform that automates storage, servers, and networking. When you use Ansible to configure these components, difficult manual tasks become repeatable and less vulnerable to error.

  • The cloud service provider AWS -

Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

  • This project contains the following resources -
  1. Ansible role to launch 3 instances- 1 MN, 2 SN.
  2. Ansible role for setting up the MN and the SN-including installing, starting, enabling the services required like docker , kubeadm and iproute-tc.
  3. Finally, joining the slave nodes to the master and launch the cluster.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Step 1 : Create the Ansible Config file

Ansible being an Agentless Automation Tool needs the inventory file at the Controller node which I have mentioned to be our local System. This file can either be created globally or in the workspace in which we are working currently.

No alt text provided for this image
Step 2 : Create the roles

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.

  • Role for launching the AWS instance-

Before creating this role , we need to create a credential.yml vault file containing the access and secret key. The vault file is used because it encrypts the content using AES256 Encryption Algorithm.

ansible-galaxy init aws-ec2


No alt text provided for this image

We also need a key-pair .pem file , changing the key in read-only mode using :

chmod 400 key.pem
No alt text provided for this image
No alt text provided for this image

Two python libraries are required to work with AWS API i.e. boto3 and boto and as we are going to create 3 instances , one for master and two for slave so we have entered these instances in loop variable inside vars folder main.yml file:

No alt text provided for this image
  • Role for setting up the k8s-master-
ansible-galaxy init kube-master

The code will be written in the roles/kube_master/tasks/main.yml

No alt text provided for this image
No alt text provided for this image
cd roles/kube-master/vars/main.yml
No alt text provided for this image
  • Role for setting up the k8s-slave -
ansible-galaxy init kube-slave

cd roles/kube-slave/tasks/main.yml
No alt text provided for this image
No alt text provided for this image
cd kube-slave/vars/main.yml
No alt text provided for this image

Same as k8s-master, slave pre-requisites are the three software i.e. Docker, Kubeadm and ip-tables. We have to update the IP table for which we have used /etc/sysctl.d/k8s.conf file in slave. Registering / Joining of Slave node to master node could only be done via the key that is provided by the mater after the whole setup and initialization. We need to copy the key in slave nodes. For this purpose we have used tokens here.

Step 3 : After the completion of all the roles now we will finally create our final.yml file that will automate all the tasks collectively by just calling the roles. We will create our "final.yml" file inside our workspace "k8s_ansible".
No alt text provided for this image

I have provided the host:localhost as the whole thing is running automatically from our local system over AWS followed by all the roles.

Step 4 : Run the playbook , along with providing the vault password.
ansible-playbook final.yml --ask-vault-pass
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
Finally succeeded with "Automating K8s Multi Node Cluster over AWS using Ansible.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

要查看或添加评论,请登录

Sujagi Verma的更多文章

社区洞察

其他会员也浏览了