-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f71876d
Showing
13 changed files
with
896 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/bin/python | ||
# Shebang Line Should Point Python Installation Path | ||
import pprint | ||
import boto3 | ||
import json | ||
|
||
|
||
def getgroupofhosts(ec2): | ||
allgroups = {} | ||
|
||
for each_in in ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]): | ||
for tag in each_in.tags: | ||
if tag["Key"] in allgroups: | ||
hosts = allgroups.get(tag["Key"]) | ||
hosts.append(each_in.public_ip_address) | ||
allgroups[tag["Key"]] = hosts | ||
else: | ||
hosts = [each_in.public_ip_address] | ||
allgroups[tag["Key"]] = hosts | ||
|
||
if tag["Value"] in allgroups: | ||
hosts = allgroups.get(tag["Value"]) | ||
hosts.append(each_in.public_ip_address) | ||
allgroups[tag["Value"]] = hosts | ||
else: | ||
hosts = [each_in.public_ip_address] | ||
allgroups[tag["Value"]] = hosts | ||
|
||
return allgroups | ||
|
||
|
||
def main(): | ||
ec2 = boto3.resource("ec2") | ||
all_groups = getgroupofhosts(ec2) | ||
inventory = {} | ||
for key, value in all_groups.items(): | ||
hostsobj = {'hosts': value} | ||
inventory[key] = hostsobj | ||
print(json.dumps(inventory)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Ansible Installation | ||
``` sh | ||
$ sudo yum install python3 -y | ||
$ sudo alternatives --set python /usr/bin/python3 | ||
$ sudo yum -y install python3-pip -y | ||
|
||
$ sudo useradd ansible | ||
$ echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible | ||
$ sudo su ansible | ||
$ pip3 install ansible --user | ||
$ pip3 install boto3 --user | ||
``` | ||
## Infrastructure As A Code | ||
### Install Terraform | ||
|
||
``` sh | ||
$ 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 | ||
``` | ||
#### Clone terraform and ansible scripts | ||
``` sh | ||
$ git clone https://github.com/MithunTechnologiesDevOps/Kuberentes_Cluster_Terraform_Ansible.git | ||
$ cd Kuberentes_Cluster_Terraform_Ansible | ||
``` | ||
###### <span style="color:orange"> Update Your Key Name in variables.tf file before executing terraform script </span> | ||
|
||
##### Create Infrastructure As A Code Using Terraform Scripts | ||
``` sh | ||
# 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/ | ||
``` | ||
# Ansible command to setup k8s cluste using DynamicInventory. | ||
|
||
###### <span style="color:red">Replace \<Pemfile> with your pemfile path in server </span> | ||
```sh | ||
$ ansible-playbook -i DynamicInventory.py site.yml -u ubuntu --private-key=<PemFilePath> --ssh-common-args "-o StrictHostKeyChecking=no" | ||
``` | ||
## Destroy Infrastructure | ||
```sh | ||
$ terraform destroy --auto-approve terafrom_scripts/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
- hosts: Kubernetes_Servers | ||
become: yes | ||
tasks: | ||
- name: Docker Installation | ||
apt: | ||
name: docker.io | ||
state: present | ||
update_cache: true | ||
|
||
- name: install APT Transport HTTPS | ||
apt: | ||
name: apt-transport-https | ||
state: present | ||
|
||
- name: add Kubernetes apt-key for APT repository | ||
apt_key: | ||
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg | ||
state: present | ||
|
||
- name: add Kubernetes APT repository | ||
apt_repository: | ||
repo: deb http:https://apt.kubernetes.io/ kubernetes-xenial main | ||
state: present | ||
filename: 'kubernetes' | ||
|
||
- name: install kubelet | ||
apt: | ||
name: kubelet=1.18.3-00 | ||
state: present | ||
update_cache: true | ||
|
||
- name: install kubeadm | ||
apt: | ||
name: kubeadm=1.18.3-00 | ||
state: present | ||
|
||
- hosts: Kubernetes_Master | ||
become: yes | ||
tasks: | ||
- name: install kubectl | ||
apt: | ||
name: kubectl=1.18.3-00 | ||
state: present | ||
force: yes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
- hosts: Kubernetes_Master | ||
become: yes | ||
tasks: | ||
- name: Start the cluster | ||
shell: kubeadm init --pod-network-cidr=10.244.0.0/16 >> cluster.txt | ||
args: | ||
chdir: $HOME | ||
creates: cluster.txt | ||
|
||
- name: create .kube directory | ||
become: yes | ||
become_user: ubuntu | ||
file: | ||
path: $HOME/.kube | ||
state: directory | ||
mode: 0755 | ||
|
||
- name: copy admin.conf to user's kube config | ||
copy: | ||
src: /etc/kubernetes/admin.conf | ||
dest: /home/ubuntu/.kube/config | ||
remote_src: yes | ||
owner: ubuntu | ||
|
||
- name: install Pod network | ||
become: yes | ||
become_user: ubuntu | ||
shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml >> pod_setup.txt | ||
args: | ||
chdir: $HOME | ||
creates: pod_setup.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
- name: Install Kubernetes Dependencies | ||
import_playbook: kube-dependencies.yml | ||
- name: Initialaize Kubernetes Master | ||
import_playbook: master-cluster.yml | ||
- name: Get token from master and join workers | ||
import_playbook: workers-cluster.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
sudo useradd ansible | ||
echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible | ||
sudo sed -re 's/^(PasswordAuthentication)([[:space:]]+)yes/\1\2no/' -i.`date -I` /etc/ssh/sshd_config | ||
sudo service sshd restart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Generate ssh_key | ||
resource "tls_private_key" "ansible" { | ||
algorithm = "RSA" | ||
rsa_bits = 4096 | ||
} | ||
# Get ssh_key | ||
resource "aws_key_pair" "ansiblesshkey" { | ||
public_key = tls_private_key.ansible.public_key_openssh | ||
} | ||
|
||
resource "aws_instance" "kubernetes_Servers" { | ||
count = 1 | ||
ami = var.kubernetes_ami | ||
instance_type = var.master_instance_type | ||
vpc_security_group_ids = [aws_security_group.kubernetes_sg.id] | ||
subnet_id = element(aws_subnet.kubernetes_subnets.*.id, count.index) | ||
key_name = var.key_name | ||
user_data = "${file("terafrom_scripts/create_ansible_user.sh")}" | ||
|
||
tags = { | ||
Name = "Kubernetes_Servers" | ||
Type = "Kubernetes_Master" | ||
} | ||
|
||
# Copies the ssh_key file to new ec2. | ||
provisioner "file" { | ||
# Read ssh_key and copy to ansible | ||
content = aws_key_pair.ansiblesshkey.public_key | ||
destination = "/home/ansible/.ssh/authorized_keys" | ||
} | ||
|
||
} | ||
|
||
resource "aws_instance" "kubernetes_Workers" { | ||
count = 2 | ||
ami = var.kubernetes_ami | ||
instance_type = var.worker_instance_type | ||
vpc_security_group_ids = [aws_security_group.kubernetes_sg.id] | ||
subnet_id = element(aws_subnet.kubernetes_subnets.*.id, count.index) | ||
key_name = var.key_name | ||
user_data = "${file("terafrom_scripts/create_ansible_user.sh")}" | ||
|
||
tags = { | ||
Name = "Kubernetes_Servers" | ||
Type = "Kubernetes_Worker" | ||
} | ||
|
||
# Copies the ssh_key file to new ec2. | ||
provisioner "file" { | ||
# Read ssh_key and copy to ansible | ||
content = aws_key_pair.ansiblesshkey.public_key | ||
destination = "/home/ansible/.ssh/authorized_keys" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
resource "aws_security_group" "kubernetes_sg" { | ||
name = "Allow_All_Ports" | ||
description = "Allow All Ports All Protocals" | ||
vpc_id = aws_vpc.kubernetes.id | ||
ingress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
variable "aws_region" { | ||
default = "ap-south-1" | ||
} | ||
|
||
variable "key_name" { | ||
default = "devops" | ||
} | ||
variable "vpc_cidr" { | ||
default = "172.0.0.0/24" | ||
} | ||
variable "subnets_cidr" { | ||
type = list(string) | ||
default = ["172.0.0.0/25", "172.0.0.128/25"] | ||
} | ||
variable "availability_zones" { | ||
type = list(string) | ||
default = ["ap-south-1a", "ap-south-1b"] | ||
} | ||
variable "kubernetes_ami" { | ||
default = "ami-0b44050b2d893d5f7" | ||
} | ||
variable "master_instance_type" { | ||
default = "t2.medium" | ||
} | ||
variable "worker_instance_type" { | ||
default = "t2.micro" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
provider "aws" { | ||
region = "ap-south-1" | ||
} | ||
resource "aws_vpc" "kubernetes" { | ||
cidr_block = var.vpc_cidr | ||
tags = { | ||
Name = "kubernetes" | ||
} | ||
} | ||
resource "aws_internet_gateway" "kubernetes_vpc_igw" { | ||
vpc_id = aws_vpc.kubernetes.id | ||
tags = { | ||
Name = "kubernetes_vpc_igw" | ||
} | ||
} | ||
|
||
resource "aws_subnet" "kubernetes_subnets" { | ||
count = length(var.subnets_cidr) | ||
vpc_id = aws_vpc.kubernetes.id | ||
cidr_block = element(var.subnets_cidr, count.index) | ||
availability_zone = element(var.availability_zones, count.index) | ||
map_public_ip_on_launch = true | ||
tags = { | ||
Name = "kubernetes_subnets_${count.index + 1}" | ||
} | ||
} | ||
|
||
resource "aws_route_table" "kubernetes_public_rt" { | ||
vpc_id = aws_vpc.kubernetes.id | ||
route { | ||
cidr_block = "0.0.0.0/0" | ||
gateway_id = aws_internet_gateway.kubernetes_vpc_igw.id | ||
} | ||
tags = { | ||
Name = "kubernetes_vpc_public_rt" | ||
} | ||
} | ||
|
||
|
||
resource "aws_route_table_association" "rt_sub_association" { | ||
count = length(var.subnets_cidr) | ||
subnet_id = element(aws_subnet.kubernetes_subnets.*.id, count.index) | ||
route_table_id = aws_route_table.kubernetes_public_rt.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"version": 4, | ||
"terraform_version": "0.12.24", | ||
"serial": 24, | ||
"lineage": "9f314b5d-482b-61c8-ec96-96208356c297", | ||
"outputs": {}, | ||
"resources": [] | ||
} |
Oops, something went wrong.