Skip to content

Commit b62dc32

Browse files
author
Anurag Guda
committed
kubernetes on cloud
1 parent 0427146 commit b62dc32

File tree

9 files changed

+254
-2
lines changed

9 files changed

+254
-2
lines changed

‎README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
[![HitCount](http://hits.dwyl.com/angudadevops/k8s_aws.svg)](http://hits.dwyl.com/angudadevops/k8s_aws)
1+
[![HitCount](http://hits.dwyl.com/angudadevops/k8s-on-cloud.svg)](http://hits.dwyl.com/angudadevops/k8s-on-cloud)
22

3-
<h1> Kubernetes on AWS with Terraform </h1>
3+
<h1> Kubernetes on cloud with Terraform </h1>
44

55
This repository helps to spin up AWS environment and create kubernetes cluster on top of that.
66

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎terraform/azure/Readme.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<h1> Terraform on AWS </h1>
2+
3+
For kubernetes multi node cluster we need to bring up multi nodes with help of terraform
4+
5+
- Prerequisites
6+
- aws_access_key
7+
- aws_secret_key
8+
- aws_keypair_name
9+
10+
Make sure to update these values on varaiable.tf to access your aws account
11+
12+
If you want to modify any details like use another aws AMI, use variable.tf file to refer that
13+
14+
### Usage
15+
16+
Make sure to inititate the terraform to load all plugins
17+
18+
```
19+
terraform init
20+
```
21+
22+
Now verify the terrafor plan with below command
23+
24+
```
25+
terraform plan
26+
```
27+
28+
Once verify the plan, now apply the terraform state to aws account
29+
30+
```
31+
terraform apply -auto-aprrove
32+
```
33+
34+
To create a ansible inventory, run the below command . if you want to change the format of inventory file modify outputs.tf file.
35+
36+
```
37+
terraform output inventory > ../ansible/inventory
38+
```
39+

‎terraform/azure/main.tf

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
provider "azurerm" {
2+
version = "2.0.0"
3+
features {}
4+
}
5+
6+
resource "azurerm_resource_group" "azure-terraform" {
7+
name = "acctestrg"
8+
location = "West US 2"
9+
}
10+
11+
resource "azurerm_virtual_network" "azure-terraform" {
12+
name = "acctvn"
13+
address_space = ["10.0.0.0/16"]
14+
location = azurerm_resource_group.azure-terraform.location
15+
resource_group_name = azurerm_resource_group.azure-terraform.name
16+
}
17+
18+
resource "azurerm_subnet" "azure-terraform" {
19+
name = "acctsub"
20+
resource_group_name = azurerm_resource_group.azure-terraform.name
21+
virtual_network_name = azurerm_virtual_network.azure-terraform.name
22+
address_prefix = "10.0.2.0/24"
23+
}
24+
25+
resource "azurerm_public_ip" "master" {
26+
count = var.master_count
27+
name = "accpublicIP-master${count.index}"
28+
location = azurerm_resource_group.azure-terraform.location
29+
resource_group_name = azurerm_resource_group.azure-terraform.name
30+
allocation_method = "Dynamic"
31+
}
32+
33+
34+
resource "azurerm_public_ip" "worker" {
35+
count = var.worker_count
36+
name = "accpublicIP-worker${count.index}"
37+
location = azurerm_resource_group.azure-terraform.location
38+
resource_group_name = azurerm_resource_group.azure-terraform.name
39+
allocation_method = "Dynamic"
40+
}
41+
42+
resource "azurerm_network_interface" "master" {
43+
count = var.master_count
44+
name = "acctni-master${count.index}"
45+
location = azurerm_resource_group.azure-terraform.location
46+
resource_group_name = azurerm_resource_group.azure-terraform.name
47+
48+
ip_configuration {
49+
name = "testConfiguration"
50+
subnet_id = azurerm_subnet.azure-terraform.id
51+
private_ip_address_allocation = "dynamic"
52+
public_ip_address_id = azurerm_public_ip.master[count.index].id
53+
}
54+
}
55+
56+
resource "azurerm_network_interface" "worker" {
57+
count = var.worker_count
58+
name = "acctni-worker${count.index}"
59+
location = azurerm_resource_group.azure-terraform.location
60+
resource_group_name = azurerm_resource_group.azure-terraform.name
61+
62+
ip_configuration {
63+
name = "testConfiguration"
64+
subnet_id = azurerm_subnet.azure-terraform.id
65+
private_ip_address_allocation = "dynamic"
66+
public_ip_address_id = azurerm_public_ip.worker[count.index].id
67+
}
68+
}
69+
70+
resource "azurerm_managed_disk" "worker" {
71+
count = var.worker_count
72+
name = "datadisk_existing-worker_${count.index}"
73+
location = azurerm_resource_group.azure-terraform.location
74+
resource_group_name = azurerm_resource_group.azure-terraform.name
75+
storage_account_type = "Standard_LRS"
76+
create_option = "Empty"
77+
disk_size_gb = "1023"
78+
}
79+
80+
resource "azurerm_managed_disk" "master" {
81+
count = var.master_count
82+
name = "datadisk_existing-master_${count.index}"
83+
location = azurerm_resource_group.azure-terraform.location
84+
resource_group_name = azurerm_resource_group.azure-terraform.name
85+
storage_account_type = "Standard_LRS"
86+
create_option = "Empty"
87+
disk_size_gb = "1023"
88+
}
89+
90+
# Create (and display) an SSH key
91+
resource "tls_private_key" "example_ssh" {
92+
algorithm = "RSA"
93+
rsa_bits = 4096
94+
}
95+
96+
resource "azurerm_linux_virtual_machine" "master" {
97+
count = var.master_count
98+
name = "acctvm-master${count.index}"
99+
location = azurerm_resource_group.azure-terraform.location
100+
resource_group_name = azurerm_resource_group.azure-terraform.name
101+
network_interface_ids = azurerm_network_interface.master.*.id
102+
size = "Standard_B2s"
103+
104+
# Uncomment this line to delete the OS disk automatically when deleting the VM
105+
# delete_os_disk_on_termination = true
106+
107+
# Uncomment this line to delete the data disks automatically when deleting the VM
108+
# delete_data_disks_on_termination = true
109+
110+
source_image_reference {
111+
publisher = "Canonical"
112+
offer = "UbuntuServer"
113+
sku = "18.04-LTS"
114+
version = "latest"
115+
}
116+
117+
os_disk {
118+
name = "accdisk-master-${count.index}"
119+
caching = "ReadWrite"
120+
storage_account_type = "Premium_LRS"
121+
}
122+
123+
disable_password_authentication = true
124+
admin_username = "azureuser"
125+
126+
admin_ssh_key {
127+
username = "azureuser"
128+
public_key = tls_private_key.example_ssh.public_key_openssh
129+
}
130+
131+
tags = {
132+
environment = "master"
133+
}
134+
}
135+
136+
resource "azurerm_linux_virtual_machine" "worker" {
137+
count = var.worker_count
138+
name = "acctvm-worker${count.index}"
139+
location = azurerm_resource_group.azure-terraform.location
140+
resource_group_name = azurerm_resource_group.azure-terraform.name
141+
network_interface_ids = [element(azurerm_network_interface.worker.*.id, count.index)]
142+
size = "Standard_DS1_v2"
143+
144+
# Uncomment this line to delete the OS disk automatically when deleting the VM
145+
# delete_os_disk_on_termination = true
146+
147+
# Uncomment this line to delete the data disks automatically when deleting the VM
148+
# delete_data_disks_on_termination = true
149+
150+
source_image_reference {
151+
publisher = "Canonical"
152+
offer = "UbuntuServer"
153+
sku = "18.04-LTS"
154+
version = "latest"
155+
}
156+
157+
os_disk {
158+
name = "accdisk-worker-${count.index}"
159+
caching = "ReadWrite"
160+
storage_account_type = "Premium_LRS"
161+
}
162+
163+
disable_password_authentication = true
164+
admin_username = "azureuser"
165+
166+
admin_ssh_key {
167+
username = "azureuser"
168+
public_key = tls_private_key.example_ssh.public_key_openssh
169+
}
170+
171+
tags = {
172+
environment = "worker"
173+
}
174+
}
175+

‎terraform/azure/output.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
output "tls_private_key" {
2+
value = tls_private_key.example_ssh.private_key_pem
3+
}
4+
5+
data "template_file" "masters_ansible" {
6+
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'"
7+
count = var.master_count
8+
vars = {
9+
host = "${azurerm_linux_virtual_machine.master[count.index].public_ip_address}"
10+
}
11+
}
12+
13+
data "template_file" "workers_ansible" {
14+
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'"
15+
count = var.worker_count
16+
vars = {
17+
host = "${azurerm_linux_virtual_machine.worker[count.index].public_ip_address}"
18+
}
19+
}
20+
21+
22+
data "template_file" "inventory" {
23+
template = "\n[k8s-masters]\n$${masters}\n\n[k8s-workers]\n$${workers}"
24+
vars = {
25+
masters = "${join("\n",data.template_file.masters_ansible.*.rendered)}"
26+
workers = "${join("\n",data.template_file.workers_ansible.*.rendered)}"
27+
}
28+
}
29+
30+
output "inventory" {
31+
value = "${data.template_file.inventory.rendered}"
32+
}

‎terraform/azure/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
variable "worker_count" {
2+
default = 2
3+
}
4+
variable "master_count" {
5+
default = 1
6+
}

0 commit comments

Comments
 (0)