Skip to content

GECUS/k8s-ansible

 
 

Repository files navigation

Landmark Technology.

Contacts: +1437215 24 83 +1 437 215 2483
WebSite : http://mylandmarktech.com/

Agenda: Infrastructure Automation Using Terraform and Ansible With Dynamic Inventory to configure a Kubernetes Cluster

Terraform, Ansible Installation And Setup In AWS EC2 Redhat Instnace.

Prerequisite
  • AWS Acccount.
  • Create Redhat EC2 Instnace.
  • Create IAM Role With Required Polocies.
    • VPCFullAccess
    • EC2FullAcces
    • S3FullAccess ..etc
  • Attach IAM Role to EC2 Instance.

Create User To Install Ansible & Terraform

 sudo useradd ansible
 echo "ansible  ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible
 sudo su ansible

Enable password authentication and assign password to ansible user

sudo sed -i "/^[^#]*PasswordAuthentication[[:space:]]no/c\PasswordAuthentication yes" /etc/ssh/sshd_config
sudo service sshd restart
sudo passwd ansible
### Install Terraform
Install terraform
 sudo su - ansible
 sudo yum install wget unzip -y
 wget https://releases.hashicorp.com/terraform/0.12.26/terraform_0.12.26_linux_amd64.zip
 sudo unzip terraform_0.12.26_linux_amd64.zip -d /usr/local/bin/
# Export terraform binary path temporally
 export PATH=$PATH:/usr/local/bin
# Add path permanently for current user.By Exporting path in .bashrc file at end of file.
$ vi .bashrc
   export PATH="$PATH:/usr/local/bin"
# Source .bashrc to reflect for current session
 source ~/.bashrc   

# terraform installation in ubuntu 
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

 sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

 sudo apt-get update && sudo apt-get install terraform

Ansible Installation

 sudo su ansible
 sudo yum install python3 -y
 sudo alternatives --set python /usr/bin/python3
 sudo yum -y install python3-pip -y
 pip3 install ansible --user
 pip3 install boto3 --user

Clone terraform and ansible scripts

 git clone https://github.com/LandmakTechnology/k8s-ansible.git
 cd Kuberentes_Cluster_Terraform_Ansible
Update Your Key Name in variables.tf file before executing terraform script

Infrastructure As A Code

Create Infrastructure(VPC,Subnets,Route Tables,EC2 Instnaces ..etc) As A Code Using Terraform Scripts

# Initialise to install plugins
 terraform init terafrom_scripts/
# Validate teffaform scripts$ terraform validate terafrom_scripts/
# Plan terraform scripts which will list resouce which will be created
 terraform plan terafrom_scripts/
# Apply to create resources
 terraform apply --auto-approve terafrom_scripts/

Configuration Management

Check If DynamicInventory Script works.

 chmod +x DynamicInventory.py
#Add default Region so that DynamicInventory will fetch host details from that Region.
# temporally
 export AWS_DEFAULT_REGION=us-west-2
# Or Permanently
#Create config file in current user .aws folder And below content.
 mkdir ~/.aws
 vi ~/.aws/config
[default]
region=us-west-2

$ ./DynamicInventory.py --list

Kubernetes Cluster Setup Using Ansible (Configuration Management) With DynamicInventory.

  • Create a pem file and copy aws private key(pem) file content which you used in terraform.
 vi key.pem
 chmod 400 key.pem
  • Replace <Pemfile> with your pemfile path in server
 ansible-playbook -i DynamicInventory.py site.yml -u ubuntu --private-key=<PemFilePath>  --ssh-common-args='-o StrictHostKeyChecking=no'

Destroy Infrastructure

 terraform destroy --auto-approve terafrom_scripts/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HCL 68.8%
  • Python 31.2%