forked from Ashfaque-9x/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AKS-cluster-using-Terraform.txt
277 lines (218 loc) · 7.16 KB
/
AKS-cluster-using-Terraform.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
Video -- https://youtu.be/UxOBAJlgzKM
===============================================================================================================================================================================================================================
A] Prerequisites
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
https://code.visualstudio.com/download
https://learn.microsoft.com/en-us/azure/developer/terraform/configure-vs-code-extension-for-terraform?tabs=azure-cli
https://developer.hashicorp.com/terraform/install
https://kubernetes.io/releases/download/
===============================================================================================================================================================================================================================
B] Implement the Terraform code
1) provider.tf
terraform {
required_version = ">=1.0"
required_providers {
azapi = {
source = "azure/azapi"
version = "~>1.5"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
time = {
source = "hashicorp/time"
version = "0.9.1"
}
}
}
provider "azurerm" {
features {}
}
2) ssh.tf
resource "random_pet" "ssh_key_name" {
prefix = "ssh"
separator = ""
}
resource "azapi_resource_action" "ssh_public_key_gen" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
resource_id = azapi_resource.ssh_public_key.id
action = "generateKeyPair"
method = "POST"
response_export_values = ["publicKey", "privateKey"]
}
resource "azapi_resource" "ssh_public_key" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
name = random_pet.ssh_key_name.id
location = azurerm_resource_group.rg.location
parent_id = azurerm_resource_group.rg.id
}
output "key_data" {
value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
3)main.tf
# Generate random resource group name
resource "random_pet" "rg_name" {
prefix = var.resource_group_name_prefix
}
resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = random_pet.rg_name.id
}
resource "random_pet" "azurerm_kubernetes_cluster_name" {
prefix = "cluster"
}
resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" {
prefix = "dns"
}
resource "azurerm_kubernetes_cluster" "k8s" {
location = azurerm_resource_group.rg.location
name = random_pet.azurerm_kubernetes_cluster_name.id
resource_group_name = azurerm_resource_group.rg.name
dns_prefix = random_pet.azurerm_kubernetes_cluster_dns_prefix.id
identity {
type = "SystemAssigned"
}
default_node_pool {
name = "agentpool"
vm_size = "Standard_D2_v2"
node_count = var.node_count
}
linux_profile {
admin_username = var.username
ssh_key {
key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
}
network_profile {
network_plugin = "kubenet"
load_balancer_sku = "standard"
}
}
4)variables.tf
variable "resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}
variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}
variable "node_count" {
type = number
description = "The initial quantity of nodes for the node pool."
default = 2
}
variable "msi_id" {
type = string
description = "The Managed Service Identity ID. Set this value if you're running this example using Managed Identity as the authentication method."
default = null
}
variable "username" {
type = string
description = "The admin username for the new cluster."
default = "azureadmin"
}
5)outputs.tf
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "kubernetes_cluster_name" {
value = azurerm_kubernetes_cluster.k8s.name
}
output "client_certificate" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_certificate
sensitive = true
}
output "client_key" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_key
sensitive = true
}
output "cluster_ca_certificate" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].cluster_ca_certificate
sensitive = true
}
output "cluster_password" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].password
sensitive = true
}
output "cluster_username" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].username
sensitive = true
}
output "host" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].host
sensitive = true
}
output "kube_config" {
value = azurerm_kubernetes_cluster.k8s.kube_config_raw
sensitive = true
}
===============================================================================================================================================================================================================================
C] Create Resources using Terraform
terraform init –upgrade
terraform plan -out main.tfplan
terraform apply main.tfplan
===============================================================================================================================================================================================================================
D] Verify the results
az login
az account set --subscription your-subscription-id
az aks get-credentials --resource-group rg-your-rg-name --name cluster-your-cluster-name
Note: Replace these commands with your details or copy these commands directly from portal.
kubectl get nodes
===============================================================================================================================================================================================================================
E] Deploy Application
1)deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: swiggy-app
labels:
app: swiggy-app
spec:
replicas: 2
selector:
matchLabels:
app: swiggy-app
template:
metadata:
labels:
app: swiggy-app
spec:
terminationGracePeriodSeconds: 30
containers:
- name: swiggy-app
image: ashfaque9x/swiggy-clone:latest
imagePullPolicy: "Always"
ports:
- containerPort: 3000
2)service.yml
apiVersion: v1
kind: Service
metadata:
name: swiggy-app
labels:
app: swiggy-app
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 3000
selector:
app: swiggy-app
3) Kubectl apply –f deployment.yml
4) Kubectl apply –f service.yml
5) Kubectl get svc
===============================================================================================================================================================================================================================
F] Cleanup
kubectl get all
kubectl delete service/swiggy-app
kubectl delete deployment.apps/swiggy-app
terraform plan -destroy -out main.destroy.tfplan
terraform apply "main.destroy.tfplan"