Skip to content

Commit

Permalink
kubernetes on cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
angudadevops committed Oct 22, 2020
1 parent b62dc32 commit 8e430d0
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 40 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
[![HitCount](http://hits.dwyl.com/angudadevops/k8s-on-cloud.svg)](http://hits.dwyl.com/angudadevops/k8s-on-cloud)

<h1> Kubernetes on cloud with Terraform </h1>
<h1> Kubernetes on Cloud with Terraform </h1>

This repository helps to spin up AWS environment and create kubernetes cluster on top of that.
This repository helps to spin up cloud environment and create kubernetes cluster on top of that using kubeadm

- Prerequisites
- AWS account details
- Ansible on your local machine
- Terraform on your local machine
- EC2 instance Key Pair
- Linux machine or Mac Os Machine

### Usage

Update the aws account details in terrform varaiable file, then run the below command to install kubernetes cluster on AWS
#### AWS
Update the aws account details in terrform varaiable file, then run the below command to install kubernetes cluster on

```
bash k8scluster.sh
bash k8scluster.sh aws
```

To clean up the AWS environment with kubernetes, run the below command

```
cd terrform
cd terrform/aws
terraform destroy -auto-approve
```

#### Azure
Make sure you have azure account setup, as below command will open to login to your azure account. Once succesful login it will provision k8s cluster on Azure Virtual Machines

```
bash k8scluster.sh azure
```

To clean up the AWS environment with kubernetes, run the below command

```
cd terrform/azure
terraform destroy -auto-approve
```

Expand Down
7 changes: 0 additions & 7 deletions ansible/inventory
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@

[k8s-masters]
ec2-52-52-180-22.us-west-1.compute.amazonaws.com ansible_ssh_host=52.52.180.22 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/aguda/Downloads/AWS/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'

[k8s-workers]
ec2-13-57-111-53.us-west-1.compute.amazonaws.com ansible_ssh_host=13.57.111.53 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/aguda/Downloads/AWS/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
ec2-13-57-45-138.us-west-1.compute.amazonaws.com ansible_ssh_host=13.57.45.138 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/aguda/Downloads/AWS/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
118 changes: 107 additions & 11 deletions k8scluster.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,111 @@

cd terraform
terraform init
terraform plan
terraform apply -auto-approve
terraform output inventory > ../ansible/inventory
azurecli() {
systype=$(uname -s)

echo "Please wait for a while to bring aws instances up"
if [[ $systype == "Linux" ]]; then
echo "Installing Azure CLI for Azure Authentication"
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
elif [[ $systype == "Darwin" ]]; then
echo "Installing Azure CLI for Azure Authentication"
brew update && brew install azure-cli
else
echo "System should be either Mac or Linux"
exit 0
fi
}

sleep 60
cd ../ansible
ansible -m ping -i inventory all
ansible-playbook -i inventory prerequisites.yaml
ansible-playbook -i inventory k8s.yaml
terraforminstall() {
systype=$(uname -s)

if [[ $systype == "Linux" ]]; then
echo "Installing Terraform"
curl -sL https://raw.github.com/robertpeteuil/terraform-installer/master/terraform-install.sh > terraform-install.sh
chmod +x terraform-install.sh
./terraform-install.sh
rm -rf terraform-install.sh
elif [[ $systype == "Darwin" ]]; then
echo "Installing Terraform"
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
else
echo "System should be either Mac or Linux"
exit 0
fi
}

installansible() {
systype=$(uname -s)
if [[ $systype == "Linux" ]]; then
echo "Installing Ansible"
os=$(cat /etc/os-release | grep -iw ID | awk -F'=' '{print $2}')
version=$(cat /etc/os-release | grep -i VERSION_CODENAME | awk -F'=' '{print $2}')
if [[ $os == "ubuntu" && $version != "focal" ]]; then
echo "Installing Ansible"
sudo apt-add-repository ppa:ansible/ansible -y
sudo apt update
sudo apt install ansible -y
elif [[ $os == "ubuntu" && $version == "focal" ]]; then
echo "Installing Ansible"
sudo apt update
sudo apt install ansible -y
elif [ $os == "rhel*" ]; then
version=$(cat /etc/os-release | grep VERSION_ID | awk -F'=' '{print $2}')
if [ $version == "*7.*" ]; then
sudo subscription-manager repos --enable rhel-7-server-ansible-2.9-rpms
sudo yum install ansible -y
elif [ $version == "*8.*" ]; then
sudo subscription-manager repos --enable ansible-2.9-for-rhel-8-x86_64-rpms
sudo yum install ansible -y
fi
fi
elif [[ $systype == "Darwin" ]]; then
echo "Installing Ansible"
brew install ansible
else
echo "System should be either Mac or Linux"
exit 0
fi
}

if [[ $1 == "azure" ]]; then
installansible
azurecli
echo "Login to Azure Account using your browser"
az login
echo "Login Successfull"
terraforminstall
cd terraform/azure
terraform init
terraform plan
terraform apply -auto-approve
terraform output inventory > ../../ansible/inventory
rm -rf azure.pem
terraform ouput tls_private_key > azure.pem
chmod 400 azure.pem

echo "Please wait for a while to bring azure vm's are up"

sleep 60
cd ../../ansible
ansible -m ping -i inventory all
ansible-playbook -i inventory prerequisites.yaml
ansible-playbook -i inventory k8s.yaml
elif [[ $1 == "aws" ]]; then
installansible
terraforminstall
cd terraform/aws
terraform init
terraform plan
terraform apply -auto-approve
terraform output inventory > ../../ansible/inventory

echo "Please wait for a while to bring aws instances up"

sleep 60
cd ../../ansible
ansible -m ping -i inventory all
ansible-playbook -i inventory prerequisites.yaml
ansible-playbook -i inventory k8s.yaml
else
echo -e "Usage\n\nAvailable Options:\n\n aws: To Provision Kubernetes Cluster on AWS\n azure: To Provision Kubernetes Cluster on Azure\n"
fi
4 changes: 2 additions & 2 deletions terraform/aws/output.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
data "template_file" "masters_ansible" {
template = "$${host} ansible_ssh_host=$${ip} ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/aguda/Downloads/AWS/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'"
template = "$${host} ansible_ssh_host=$${ip} ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=../terraform/aws/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'"
count = var.master_count
vars = {
host = "${aws_eip.eip-master.*.public_dns[count.index]}"
Expand All @@ -8,7 +8,7 @@ data "template_file" "masters_ansible" {
}

data "template_file" "workers_ansible" {
template = "$${host} ansible_ssh_host=$${ip} ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/aguda/Downloads/AWS/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'"
template = "$${host} ansible_ssh_host=$${ip} ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=../terraform/aws/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'"
count = var.worker_count
vars = {
host = "${aws_eip.eip-worker.*.public_dns[count.index]}"
Expand Down
13 changes: 6 additions & 7 deletions terraform/azure/Readme.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<h1> Terraform on AWS </h1>
<h1> Terraform on Azure </h1>

For kubernetes multi node cluster we need to bring up multi nodes with help of terraform
For kubernetes with kubeadm multi node cluster we need to bring up multi nodes with help of terraform

- Prerequisites
- aws_access_key
- aws_secret_key
- aws_keypair_name
- azure account

Make sure to update these values on varaiable.tf to access your aws account
Make sure you have an access to your azure account

If you want to modify any details like use another aws AMI, use variable.tf file to refer that
If you want to modify any details like number of worker node, use variable.tf file to refer that

### Usage

Expand All @@ -35,5 +33,6 @@ To create a ansible inventory, run the below command . if you want to change the

```
terraform output inventory > ../ansible/inventory
terraform output tls_private_key > azure.pem
```

8 changes: 4 additions & 4 deletions terraform/azure/output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ output "tls_private_key" {
}

data "template_file" "masters_ansible" {
template = "$${host} ansible_ssh_host=$${host} ansible_ssh_port=22 ansible_ssh_user=azureuser ansible_ssh_private_key_file=/Users/aguda/Downloads/azure/azure.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'"
count = var.master_count
template = "$${host} ansible_ssh_host=$${host} ansible_ssh_port=22 ansible_ssh_user=azureuser ansible_ssh_private_key_file=../terraform/azure/azure.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'"
count = var.master_count
vars = {
host = "${azurerm_linux_virtual_machine.master[count.index].public_ip_address}"
}
}

data "template_file" "workers_ansible" {
template = "$${host} ansible_ssh_host=$${host} ansible_ssh_port=22 ansible_ssh_user=azureuser ansible_ssh_private_key_file=/Users/aguda/Downloads/azure/azure.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'"
template = "$${host} ansible_ssh_host=$${host} ansible_ssh_port=22 ansible_ssh_user=azureuser ansible_ssh_private_key_file=../terraform/azure/azure.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'"
count = var.worker_count
vars = {
host = "${azurerm_linux_virtual_machine.worker[count.index].public_ip_address}"
Expand All @@ -29,4 +29,4 @@ data "template_file" "inventory" {

output "inventory" {
value = "${data.template_file.inventory.rendered}"
}
}

0 comments on commit 8e430d0

Please sign in to comment.