-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
589 lines (531 loc) · 20 KB
/
main.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
###########
# VPC part
###########
resource "opentelekomcloud_vpc_v1" "vpc" {
name = var.environment
cidr = var.vpc_cidr
shared = true
}
resource "opentelekomcloud_vpc_subnet_v1" "subnet" {
name = var.environment
vpc_id = opentelekomcloud_vpc_v1.vpc.id
cidr = var.subnet_cidr
gateway_ip = var.subnet_gateway_ip
primary_dns = var.subnet_primary_dns
secondary_dns = var.subnet_secondary_dns
}
###########
# ELB part
###########
resource "opentelekomcloud_networking_floatingip_v2" "eip" {
pool = "admin_external_net"
port_id = opentelekomcloud_lb_loadbalancer_v2.lb.vip_port_id
}
resource "opentelekomcloud_lb_loadbalancer_v2" "lb" {
name = "${var.environment}-lb"
vip_subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.subnet_id
}
resource "opentelekomcloud_lb_listener_v2" "listener_80" {
protocol = "TCP"
name = "${var.environment}-listener_80"
protocol_port = 80
loadbalancer_id = opentelekomcloud_lb_loadbalancer_v2.lb.id
depends_on = [opentelekomcloud_lb_listener_v2.listener_6443]
}
resource "opentelekomcloud_lb_listener_v2" "listener_443" {
protocol = "TCP"
name = "${var.environment}-listener_443"
protocol_port = 443
loadbalancer_id = opentelekomcloud_lb_loadbalancer_v2.lb.id
}
resource "opentelekomcloud_lb_listener_v2" "listener_6443" {
protocol = "TCP"
name = "${var.environment}-listener_6443"
protocol_port = 6443
loadbalancer_id = opentelekomcloud_lb_loadbalancer_v2.lb.id
depends_on = [opentelekomcloud_lb_listener_v2.listener_443]
}
resource "opentelekomcloud_lb_pool_v2" "pool_80" {
protocol = "TCP"
name = "${var.environment}-pool_80"
lb_method = "ROUND_ROBIN"
listener_id = opentelekomcloud_lb_listener_v2.listener_80.id
depends_on = [opentelekomcloud_lb_pool_v2.pool_6443]
}
resource "opentelekomcloud_lb_pool_v2" "pool_443" {
protocol = "TCP"
name = "${var.environment}-pool_443"
lb_method = "ROUND_ROBIN"
listener_id = opentelekomcloud_lb_listener_v2.listener_443.id
}
resource "opentelekomcloud_lb_pool_v2" "pool_6443" {
protocol = "TCP"
name = "${var.environment}-pool_6443"
lb_method = "ROUND_ROBIN"
listener_id = opentelekomcloud_lb_listener_v2.listener_6443.id
depends_on = [opentelekomcloud_lb_pool_v2.pool_443]
}
resource "opentelekomcloud_lb_monitor_v2" "monitor_80" {
pool_id = opentelekomcloud_lb_pool_v2.pool_80.id
type = "HTTP"
delay = 10
timeout = 5
max_retries = 10
domain_name = "${var.rancher_host}.${var.rancher_domain}"
url_path = "/healthz"
http_method = "GET"
expected_codes = "200"
monitor_port = 80
}
resource "opentelekomcloud_lb_monitor_v2" "monitor_443" {
pool_id = opentelekomcloud_lb_pool_v2.pool_443.id
type = "HTTP"
delay = 10
timeout = 5
max_retries = 10
domain_name = "${var.rancher_host}.${var.rancher_domain}"
url_path = "/healthz"
http_method = "GET"
expected_codes = "200"
monitor_port = 80
}
resource "opentelekomcloud_lb_monitor_v2" "monitor_6443" {
pool_id = opentelekomcloud_lb_pool_v2.pool_6443.id
type = "HTTP"
delay = 10
timeout = 5
max_retries = 10
domain_name = "${var.rancher_host}.${var.rancher_domain}"
url_path = "/healthz"
http_method = "GET"
expected_codes = "200"
monitor_port = 80
}
# server 1
resource "opentelekomcloud_lb_member_v2" "member_80_1" {
address = opentelekomcloud_compute_instance_v2.k3s-server-1.access_ip_v4
protocol_port = 80
pool_id = opentelekomcloud_lb_pool_v2.pool_80.id
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.subnet_id
}
resource "opentelekomcloud_lb_member_v2" "member_443_1" {
address = opentelekomcloud_compute_instance_v2.k3s-server-1.access_ip_v4
protocol_port = 443
pool_id = opentelekomcloud_lb_pool_v2.pool_443.id
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.subnet_id
}
resource "opentelekomcloud_lb_member_v2" "member_6443_1" {
address = opentelekomcloud_compute_instance_v2.k3s-server-1.access_ip_v4
protocol_port = 6443
pool_id = opentelekomcloud_lb_pool_v2.pool_6443.id
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.subnet_id
}
# server 2
resource "opentelekomcloud_lb_member_v2" "member_80_2" {
address = opentelekomcloud_compute_instance_v2.k3s-server-2.access_ip_v4
protocol_port = 80
pool_id = opentelekomcloud_lb_pool_v2.pool_80.id
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.subnet_id
}
resource "opentelekomcloud_lb_member_v2" "member_443_2" {
address = opentelekomcloud_compute_instance_v2.k3s-server-2.access_ip_v4
protocol_port = 443
pool_id = opentelekomcloud_lb_pool_v2.pool_443.id
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.subnet_id
}
resource "opentelekomcloud_lb_member_v2" "member_6443_2" {
address = opentelekomcloud_compute_instance_v2.k3s-server-2.access_ip_v4
protocol_port = 6443
pool_id = opentelekomcloud_lb_pool_v2.pool_6443.id
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.subnet_id
}
resource "opentelekomcloud_lb_whitelist_v2" "whitelist_80" {
count = var.elb_whitelistips != "" && var.elb_whitelist ? 1 : 0
enable_whitelist = true
whitelist = var.elb_whitelistips
listener_id = opentelekomcloud_lb_listener_v2.listener_80.id
}
resource "opentelekomcloud_lb_whitelist_v2" "whitelist_443" {
count = var.elb_whitelistips != "" && var.elb_whitelist ? 1 : 0
enable_whitelist = true
whitelist = var.elb_whitelistips
listener_id = opentelekomcloud_lb_listener_v2.listener_443.id
}
resource "opentelekomcloud_lb_whitelist_v2" "whitelist_6443" {
count = var.elb_whitelistips != "" && var.elb_whitelist ? 1 : 0
enable_whitelist = true
whitelist = var.elb_whitelistips
listener_id = opentelekomcloud_lb_listener_v2.listener_6443.id
}
###########
# RDS part
###########
resource "opentelekomcloud_networking_secgroup_v2" "secgroup" {
name = "${var.environment}-rds-secgroup"
description = "terraform security group rds"
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "secgroup_rule" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = var.rds_port
port_range_max = var.rds_port
remote_ip_prefix = var.subnet_cidr
security_group_id = opentelekomcloud_networking_secgroup_v2.secgroup.id
}
resource "opentelekomcloud_rds_parametergroup_v3" "pg" {
name = "${var.environment}-rds-pg"
description = "Parameter Group for ${var.environment}-rds RDS"
values = {
local_infile = "OFF"
max_user_connections = "1000"
validate_password_length = "10"
validate_password_number_count = "1"
validate_password_special_char_count = "1"
}
datastore {
type = "mysql"
version = var.rds_version
}
}
resource "opentelekomcloud_rds_instance_v3" "rds" {
availability_zone = var.rds_az
db {
password = var.rds_root_password
type = var.rds_type
version = var.rds_version
port = var.rds_port
}
name = "${var.environment}-rds"
security_group_id = opentelekomcloud_networking_secgroup_v2.secgroup.id
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.id
vpc_id = opentelekomcloud_vpc_v1.vpc.id
volume {
type = var.rds_volume_type
size = var.rds_volume_size
}
ha_replication_mode = var.rds_ha_mode
param_group_id = opentelekomcloud_rds_parametergroup_v3.pg.id
flavor = var.rds_flavor
backup_strategy {
start_time = "08:00-09:00"
keep_days = 30
}
# lifecycle {
# prevent_destroy = true
# }
}
###########
# DNS part
###########
resource "opentelekomcloud_dns_zone_v2" "dns" {
count = var.create_dns ? 1 : 0
name = "${var.rancher_domain}."
email = var.admin_email
description = "tf managed zone"
ttl = 300
type = "public"
}
resource "opentelekomcloud_dns_recordset_v2" "public_record" {
count = var.create_dns ? 1 : 0
zone_id = opentelekomcloud_dns_zone_v2.dns[0].id
name = "${var.rancher_host}.${var.rancher_domain}."
description = "tf managed zone"
type = "A"
ttl = 300
records = [ opentelekomcloud_networking_floatingip_v2.eip.address ]
}
###########
# ECS part
###########
locals {
k3s_server = templatefile("${path.module}/files/k3s_server",{
rds_root_password = var.rds_root_password
rds_db = var.rds_db
rds_port = var.rds_port
rds_host = opentelekomcloud_rds_instance_v3.rds.private_ips.0
admin_email = var.admin_email
admin_password = var.admin_password
rancher_host = var.rancher_host
rancher_domain = var.rancher_domain
rancher_version = var.rancher_version
rancher_tag = var.rancher_tag
registry = var.registry
system-default-registry = var.system-default-registry
image_traefik = var.image_traefik
repo_certmanager = var.repo_certmanager
k3s_version = var.k3s_version
k3s_registry = var.k3s_registry
k3s_addon = var.k3s_addon
token = var.token
cert-manager_version = trim(var.cert-manager_version,"v")
})
k3s_node = templatefile("${path.module}/files/k3s_node",{
rds_root_password = var.rds_root_password
rds_db = var.rds_db
rds_port = var.rds_port
rds_host = opentelekomcloud_rds_instance_v3.rds.private_ips.0
k3s_version = var.k3s_version
k3s_registry = var.k3s_registry
token = var.token
cert-manager_version = var.cert-manager_version
})
# k3s_worker = templatefile("${path.module}/files/k3s_worker",{
# rancher_host = var.rancher_host
# rancher_domain = var.rancher_domain
# k3s_version = var.k3s_version
# token = var.token
# })
wireguard = templatefile("${path.module}/files/wireguard",{
wg_server_address = var.wg_server_address
wg_server_port = var.wg_server_port
wg_server_private_key = var.wg_server_private_key
wg_server_public_key = var.wg_server_public_key
wg_peer_address = var.wg_peer_address
wg_peer_public_key = var.wg_peer_public_key
})
}
data "opentelekomcloud_images_image_v2" "image-1" {
name = var.image_name_server-1
most_recent = true
}
data "opentelekomcloud_images_image_v2" "image-2" {
name = var.image_name_server-2
most_recent = true
}
# Secgroup part (ECS)
resource "opentelekomcloud_networking_secgroup_v2" "k3s-server-secgroup" {
description = "K3S Server Group"
name = "${var.environment}-secgroup"
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_all_out" {
description = "Rancher/K3S accept all traffic"
direction = "egress"
ethertype = "IPv4"
remote_ip_prefix = "0.0.0.0/0"
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_22_in_self" {
count = var.deploy_wireguard ? 1 : 0
description = "K3S Server ssh internal"
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 22
port_range_max = 22
remote_group_id = opentelekomcloud_networking_secgroup_v2.wireguard-secgroup[0].id
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_80_in" {
description = "Rancher HTTP ELB network"
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 80
port_range_max = 80
remote_ip_prefix = "100.125.0.0/16"
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_443_in" {
description = "Rancher HTTPS ELB network"
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 443
port_range_max = 443
remote_ip_prefix = "100.125.0.0/16"
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_443_in_self" {
description = "Rancher HTTPS internal"
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 443
port_range_max = 443
remote_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_6443_in" {
description = "Kube API ELB network"
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 6443
port_range_max = 6443
remote_ip_prefix = "100.125.0.0/16"
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_6443_in_self" {
description = "Kube API internal"
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 6443
port_range_max = 6443
remote_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_8472_in" {
description = "Flannel VXLAN"
direction = "ingress"
ethertype = "IPv4"
protocol = "udp"
port_range_min = 8472
port_range_max = 8472
remote_ip_prefix = "0.0.0.0/0"
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_9796_in" {
description = "Prometheus Node Exporter"
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 9796
port_range_max = 9796
remote_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_k3s_10250_in" {
description = "Kubelet"
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 10250
port_range_max = 10250
remote_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
security_group_id = opentelekomcloud_networking_secgroup_v2.k3s-server-secgroup.id
}
resource "opentelekomcloud_networking_secgroup_v2" "wireguard-secgroup" {
count = var.deploy_wireguard ? 1 : 0
description = "K3S Wireguard Server"
name = "${var.environment}-secgroup-wg"
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_wg_all_out" {
count = var.deploy_wireguard ? 1 : 0
description = "K3S Wireguard accept all traffic"
direction = "egress"
ethertype = "IPv4"
remote_ip_prefix = "0.0.0.0/0"
security_group_id = opentelekomcloud_networking_secgroup_v2.wireguard-secgroup[0].id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_wg_in" {
count = var.deploy_wireguard ? 1 : 0
description = "K3S Wireguard"
direction = "ingress"
ethertype = "IPv4"
protocol = "udp"
port_range_min = var.wg_server_port
port_range_max = var.wg_server_port
remote_ip_prefix = "0.0.0.0/0"
security_group_id = opentelekomcloud_networking_secgroup_v2.wireguard-secgroup[0].id
}
resource "opentelekomcloud_networking_secgroup_rule_v2" "sg_icmp_in" {
count = var.deploy_wireguard ? 1 : 0
description = "K3S Wireguard"
direction = "ingress"
ethertype = "IPv4"
protocol = "icmp"
remote_ip_prefix = "0.0.0.0/0"
security_group_id = opentelekomcloud_networking_secgroup_v2.wireguard-secgroup[0].id
}
# ssh key part
resource "opentelekomcloud_compute_keypair_v2" "k3s-server-key" {
name = "${var.environment}-key"
public_key = var.public_key
}
# ECS part (instances)
resource "opentelekomcloud_compute_instance_v2" "k3s-server-1" {
name = "${var.environment}-server-1"
availability_zone = var.availability_zone1
flavor_id = var.flavor_id
key_pair = opentelekomcloud_compute_keypair_v2.k3s-server-key.name
security_groups = ["${var.environment}-secgroup"]
user_data = local.k3s_server
power_state = var.power_state
network {
uuid = opentelekomcloud_vpc_subnet_v1.subnet.id
}
block_device {
boot_index = 0
source_type = "image"
destination_type = "volume"
uuid = data.opentelekomcloud_images_image_v2.image-1.id
delete_on_termination = true
volume_size = 30
}
}
resource "opentelekomcloud_compute_instance_v2" "k3s-server-2" {
name = "${var.environment}-server-2"
availability_zone = var.availability_zone2
flavor_id = var.flavor_id
key_pair = opentelekomcloud_compute_keypair_v2.k3s-server-key.name
security_groups = ["${var.environment}-secgroup"]
user_data = local.k3s_node
power_state = var.power_state
network {
uuid = opentelekomcloud_vpc_subnet_v1.subnet.id
}
block_device {
boot_index = 0
source_type = "image"
destination_type = "volume"
uuid = data.opentelekomcloud_images_image_v2.image-2.id
delete_on_termination = true
volume_size = 30
}
depends_on = [
opentelekomcloud_compute_instance_v2.k3s-server-1
]
}
#resource "opentelekomcloud_compute_instance_v2" "k3s-worker-1" {
# depends_on = [opentelekomcloud_compute_instance_v2.k3s-server-1]
# name = "${var.environment}-worker-1"
# availability_zone = var.availability_zone1
# flavor_id = var.flavor_id
# key_pair = opentelekomcloud_compute_keypair_v2.k3s-server-key.name
# security_groups = ["${var.environment}-secgroup"]
# user_data = local.k3s_worker
# power_state = var.power_state
# network {
# uuid = opentelekomcloud_vpc_subnet_v1.subnet.id
# }
# block_device {
# boot_index = 0
# source_type = "image"
# destination_type = "volume"
# uuid = data.opentelekomcloud_images_image_v2.image-1.id
# delete_on_termination = true
# volume_size = 100
# }
#}
resource "opentelekomcloud_compute_instance_v2" "wireguard" {
count = var.deploy_wireguard ? 1 : 0
name = "${var.environment}-wireguard"
availability_zone = var.availability_zone1
flavor_id = var.wg_flavor_id
key_pair = opentelekomcloud_compute_keypair_v2.k3s-server-key.name
security_groups = ["${var.environment}-secgroup-wg"]
user_data = local.wireguard
power_state = var.power_state
network {
uuid = opentelekomcloud_vpc_subnet_v1.subnet.id
}
block_device {
boot_index = 0
source_type = "image"
destination_type = "volume"
uuid = data.opentelekomcloud_images_image_v2.image-1.id
delete_on_termination = true
volume_size = 30
}
}
resource "opentelekomcloud_networking_floatingip_v2" "wireguard" {
count = var.deploy_wireguard ? 1 : 0
pool = "admin_external_net"
}
resource "opentelekomcloud_networking_floatingip_associate_v2" "wireguard" {
count = var.deploy_wireguard ? 1 : 0
floating_ip = opentelekomcloud_networking_floatingip_v2.wireguard[0].address
port_id = opentelekomcloud_compute_instance_v2.wireguard[0].network.0.port
}