Configuration of K8s Multinode Cluster over AWS by integrating ansible and terraform with dynamic inventory.
Integration of terraform, ansible, AWS and k8s. lets understand what is terraform, ansible, AWS and k8s.
Terraform: Terraform is an open-source infrastructure as code software tool. It is developed in Go programming language. The terraform is used to set up infrastructure on Cloud like AWS, google ,azure etc. It supports to multiple cloud providers. It is used for building, changing, and versioning infrastructure safely and efficiently. It allows users to manage cloud services through a language named HashiCorp Configuration Language (HCL).Terraform quickly deploys Infrastructure as Code, due to which environment installation and development is faster with Terraform.
Ansible: Ansible is open-source configuration management tool. ansible is agentless tool. Ansible is built on the top of python language. The node which actually run playbook is called control node and the target node means the node on which playbook will apply is called managed node.
AWS: Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 200 fully featured services from data centers globally. Millions of customers—including the fastest-growing startups, largest enterprises, and leading government agencies—are using AWS to lower costs, become more agile, and innovate faster.
Kubernetes: Kubernetes is an open-source container-orchestration system for automating computer application deployment, scaling, and management. Actually k8s manage the container engines like Docker, CRIO, Podman etc. k8s is developed in Go programming language. Kubernetes is a portable, extensible, platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem.
First lets provision AWS ec2 instances using terraform. create folder ec2_provision. then inside that folder create file ec2.tf then write code to launch EC2 instance. but here AWS AWS credentials required. The Terraform by default taking Credentials from home directory of user (/home/.aws/credentials). I have written code to launch three instances i.e. one for master node, and two for slave nodes in default VPC and using Amazon2 AMI.
after completing above code we have initialize current directory using terraform init then we have to use terraform apply -auto-approve. then you will see result
We done with infrastructure part. Now lets move towards configuration of K8s master and K8s slave. First we have to write ansible configuration file and dynamic inventory file using python like
after above setup of configuration file and dynamic host file we can check connectivity with target node
now its time to create role for master and slave configurations. first create role for k8s master using ansible-galaxy init master. there are many way to install k8s but we are using kubeadm. Kubeadm repo doesnot present bydefault so we have to configure repo for it. after that we have to install kubeadm, docker, iproute-tc. Here kubeadm install with kubelet etc. iproute-tc we have to install for networking. The k8s doesnot support to cgroup driver so we have to change docker conf. file like below or k8s support systemd driver so we have to add systemcd driver in docker conf. file.
after starting docker service we have to change Iptable file and also pull the docker images because all programs of master node run in container like kube-proxy etc. and after that we have to start and enable kubelet program because it is actually agent program that helps master node to connect docker engine.
Then we have to run kubeadm init command and it gives the hardware error then we have to use ignore-preflight option. Now our k8s master node setup is done. but if slave node want to connect master node then slave node should know token hence to generate token we need to execute kubeadm token create --print-join-command. then this command will give tokens. This token we are storing in one file like below
The template file for tokens
lets move towards to slave node configuration. we have to create role for slave using ansible-galaxy-init slave . and we need conatiner engine, kubelet program. also we need to do change in docker file for systemd driver.then we have to start docker and kubelet service then we have to execute token file on slave node.
Upto this we have setup infra-structure on AWS using and created roles for configuration of master and slave nodes. we have to run execute that two roles
# ansible-playbook k8s.yml
Output:
Then we have to set funnel CNI network after that lets launch multitier application means wordpress and mysql. first we have create ansible role then we need to put yaml files of frondend and backend in files folder then we have write task.
finally after completion of playbook you will get but here we need to add service port and public IP of instance.
Thank you !!