forked from rancher/quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfra.tf
336 lines (283 loc) · 10.8 KB
/
infra.tf
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Azure Infrastructure Resources
resource "tls_private_key" "global_key" {
algorithm = "RSA"
rsa_bits = 2048
}
resource "local_file" "ssh_private_key_pem" {
filename = "${path.module}/id_rsa"
sensitive_content = tls_private_key.global_key.private_key_pem
file_permission = "0600"
}
resource "local_file" "ssh_public_key_openssh" {
filename = "${path.module}/id_rsa.pub"
content = tls_private_key.global_key.public_key_openssh
}
# Resource group containing all resources
resource "azurerm_resource_group" "rancher-quickstart" {
name = "${var.prefix}-rancher-win-quickstart"
location = var.azure_location
tags = {
Creator = "rancher-quickstart"
}
}
# Public IP of Rancher server
resource "azurerm_public_ip" "rancher-server-pip" {
name = "rancher-win-server-pip"
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
allocation_method = "Dynamic"
tags = {
Creator = "rancher-quickstart"
}
}
# Azure virtual network space for quickstart resources
resource "azurerm_virtual_network" "rancher-quickstart" {
name = "${var.prefix}-win-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
tags = {
Creator = "rancher-quickstart"
}
}
# Azure internal subnet for quickstart resources
resource "azurerm_subnet" "rancher-quickstart-internal" {
name = "rancher-win-quickstart-internal"
resource_group_name = azurerm_resource_group.rancher-quickstart.name
virtual_network_name = azurerm_virtual_network.rancher-quickstart.name
address_prefixes = ["10.0.0.0/16"]
}
# Azure network interface for quickstart resources
resource "azurerm_network_interface" "rancher-server-interface" {
name = "rancher-win-quickstart-interface"
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
ip_configuration {
name = "rancher_server_ip_config"
subnet_id = azurerm_subnet.rancher-quickstart-internal.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.rancher-server-pip.id
}
tags = {
Creator = "rancher-quickstart"
}
}
// ensure computer_name meets 15 character limit
// uses assumption that resources only use 4 characters for a suffix
locals {
computer_name_prefix = substr(var.prefix, 0, 11)
}
# Azure linux virtual machine for creating a single node RKE cluster and installing the Rancher Server
resource "azurerm_linux_virtual_machine" "rancher_server" {
name = "${var.prefix}-rancher-win-server"
computer_name = "${local.computer_name_prefix}-rws" // ensure computer_name meets 15 character limit
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
network_interface_ids = [azurerm_network_interface.rancher-server-interface.id]
size = var.instance_type
admin_username = local.node_username
custom_data = base64encode(
templatefile(
join("/", [path.module, "../cloud-common/files/userdata_rancher_server.template"]),
{
docker_version = var.docker_version
username = local.node_username
}
)
)
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
admin_ssh_key {
username = local.node_username
public_key = tls_private_key.global_key.public_key_openssh
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
tags = {
Creator = "rancher-quickstart"
}
provisioner "remote-exec" {
inline = [
"echo 'Waiting for cloud-init to complete...'",
"cloud-init status --wait > /dev/null",
"echo 'Completed cloud-init!'",
]
connection {
type = "ssh"
host = self.public_ip_address
user = local.node_username
private_key = tls_private_key.global_key.private_key_pem
}
}
}
# Rancher resources
module "rancher_common" {
source = "../rancher-common"
node_public_ip = azurerm_linux_virtual_machine.rancher_server.public_ip_address
node_internal_ip = azurerm_linux_virtual_machine.rancher_server.private_ip_address
node_username = local.node_username
ssh_private_key_pem = tls_private_key.global_key.private_key_pem
rke_kubernetes_version = var.rke_kubernetes_version
cert_manager_version = var.cert_manager_version
rancher_version = var.rancher_version
rancher_server_dns = join(".", ["rancher", azurerm_linux_virtual_machine.rancher_server.public_ip_address, "nip.io"])
admin_password = var.rancher_server_admin_password
workload_kubernetes_version = var.workload_kubernetes_version
workload_cluster_name = "quickstart-azure-custom"
rke_network_plugin = "flannel"
rke_network_options = {
flannel_backend_port = "4789"
flannel_backend_type = "vxlan"
flannel_backend_vni = "4096"
}
windows_prefered_cluster = true
}
# Public IP of quickstart node
resource "azurerm_public_ip" "quickstart-node-pip" {
name = "quickstart-win-node-pip"
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
allocation_method = "Dynamic"
tags = {
Creator = "rancher-quickstart"
}
}
# Azure network interface for quickstart resources
resource "azurerm_network_interface" "quickstart-node-interface" {
name = "quickstart-win-node-interface"
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
ip_configuration {
name = "rancher_server_ip_config"
subnet_id = azurerm_subnet.rancher-quickstart-internal.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.quickstart-node-pip.id
}
tags = {
Creator = "rancher-quickstart"
}
}
# Azure linux virtual machine for creating for the workload cluster
resource "azurerm_linux_virtual_machine" "quickstart-node" {
name = "${var.prefix}-quickstart-win-node-master"
computer_name = "${local.computer_name_prefix}-qm" // ensure computer_name meets 15 character limit
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
network_interface_ids = [azurerm_network_interface.quickstart-node-interface.id]
size = var.instance_type
admin_username = local.node_username
custom_data = base64encode(
templatefile(
join("/", [path.module, "files/userdata_quickstart_node.template"]),
{
docker_version = var.docker_version
username = local.node_username
register_command = module.rancher_common.custom_cluster_command
}
)
)
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
admin_ssh_key {
username = local.node_username
public_key = tls_private_key.global_key.public_key_openssh
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
tags = {
Creator = "rancher-quickstart"
}
provisioner "remote-exec" {
inline = [
"echo 'Waiting for cloud-init to complete...'",
"cloud-init status --wait > /dev/null",
"echo 'Completed cloud-init!'",
]
connection {
type = "ssh"
host = self.public_ip_address
user = local.node_username
private_key = tls_private_key.global_key.private_key_pem
}
}
}
# Public IP of quickstart node
resource "azurerm_public_ip" "quickstart-windows-node-pip" {
name = "quickstart-windows-node-pip"
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
allocation_method = "Dynamic"
tags = {
Creator = "rancher-quickstart"
}
}
# Azure network interface for quickstart resources
resource "azurerm_network_interface" "quickstart-windows-node-interface" {
name = "quickstart-windows-node-interface"
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
ip_configuration {
name = "rancher_server_ip_config"
subnet_id = azurerm_subnet.rancher-quickstart-internal.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.quickstart-windows-node-pip.id
}
tags = {
Creator = "rancher-quickstart"
}
}
# Azure windows virtual machine for joining the workload cluster
resource "azurerm_windows_virtual_machine" "quickstart-windows-node" {
name = "${var.prefix}-quickstart-win-node-worker"
computer_name = "${local.computer_name_prefix}-qw" // ensure computer_name meets 15 character limit
location = azurerm_resource_group.rancher-quickstart.location
resource_group_name = azurerm_resource_group.rancher-quickstart.name
network_interface_ids = [azurerm_network_interface.quickstart-windows-node-interface.id]
size = var.instance_type
admin_username = "adminuser"
admin_password = var.windows_admin_password
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter-with-Containers"
version = "latest"
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
tags = {
Creator = "rancher-quickstart"
}
}
# Join windows not to the cluster
resource "azurerm_virtual_machine_extension" "join-rancher" {
name = "${var.prefix}-quickstart-windows-node-join-rancher"
virtual_machine_id = azurerm_windows_virtual_machine.quickstart-windows-node.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
settings = <<SETTINGS
{
"commandToExecute": ${jsonencode(
replace(
module.rancher_common.custom_cluster_windows_command,
"| iex}",
"--address ${azurerm_windows_virtual_machine.quickstart-windows-node.public_ip_address} --internal-address ${azurerm_windows_virtual_machine.quickstart-windows-node.private_ip_address} --worker | iex}",
)
)}
}
SETTINGS
}