-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvm-autoscale.tf
executable file
·183 lines (152 loc) · 4.41 KB
/
vm-autoscale.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
# Объявление переменных для конфиденциальных параметров
variable "folder_id" {
type = string
}
variable "vm_user" {
type = string
}
variable "ssh_key" {
type = string
sensitive = true
}
# Настройка провайдера
terraform {
required_providers {
yandex = {
source = "yandex-cloud/yandex"
version = ">= 0.47.0"
}
}
}
provider "yandex" {
zone = "ru-central1-a"
}
# Создание сервисного аккаунта и назначение ему ролей
resource "yandex_iam_service_account" "for-autoscale" {
name = "for-autoscale"
}
resource "yandex_resourcemanager_folder_iam_member" "vm-autoscale-sa-role-compute" {
folder_id = var.folder_id
role = "editor"
member = "serviceAccount:${yandex_iam_service_account.for-autoscale.id}"
}
# Создание облачной сети и подсетей
resource "yandex_vpc_network" "vm-autoscale-network" {
name = "vm-autoscale-network"
}
resource "yandex_vpc_subnet" "vm-autoscale-subnet-a" {
name = "vm-autoscale-subnet-a"
zone = "ru-central1-a"
v4_cidr_blocks = ["192.168.1.0/24"]
network_id = yandex_vpc_network.vm-autoscale-network.id
}
resource "yandex_vpc_subnet" "vm-autoscale-subnet-b" {
name = "vm-autoscale-subnet-b"
zone = "ru-central1-b"
v4_cidr_blocks = ["192.168.2.0/24"]
network_id = yandex_vpc_network.vm-autoscale-network.id
}
# Создание группы безопасности
resource "yandex_vpc_security_group" "sg-1" {
name = "sg-autoscale"
network_id = yandex_vpc_network.vm-autoscale-network.id
egress {
protocol = "ANY"
description = "any"
v4_cidr_blocks = ["0.0.0.0/0"]
}
ingress {
protocol = "TCP"
description = "ext-http"
v4_cidr_blocks = ["0.0.0.0/0"]
port = 80
}
ingress {
protocol = "TCP"
description = "healthchecks"
predefined_target = "loadbalancer_healthchecks"
port = 80
}
}
# Указание готового образа ВМ
data "yandex_compute_image" "autoscale-image" {
family = "container-optimized-image"
}
# Создание группы ВМ
resource "yandex_compute_instance_group" "autoscale-group" {
name = "autoscale-vm-ig"
folder_id = var.folder_id
service_account_id = yandex_iam_service_account.for-autoscale.id
instance_template {
platform_id = "standard-v3"
resources {
memory = 2
cores = 2
}
boot_disk {
mode = "READ_WRITE"
initialize_params {
image_id = data.yandex_compute_image.autoscale-image.id
size = 30
}
}
network_interface {
network_id = yandex_vpc_network.vm-autoscale-network.id
subnet_ids = [
yandex_vpc_subnet.vm-autoscale-subnet-a.id,
yandex_vpc_subnet.vm-autoscale-subnet-b.id
]
security_group_ids = [ yandex_vpc_security_group.sg-1.id ]
nat = true
}
metadata = {
user-data = templatefile("config.tpl", {
VM_USER = var.vm_user
SSH_KEY = var.ssh_key
})
docker-container-declaration = file("declaration.yaml")
}
}
scale_policy {
auto_scale {
initial_size = 2
measurement_duration = 60
cpu_utilization_target = 40
min_zone_size = 1
max_size = 6
warmup_duration = 120
}
}
allocation_policy {
zones = [
"ru-central1-a",
"ru-central1-b"
]
}
deploy_policy {
max_unavailable = 1
max_expansion = 0
}
load_balancer {
target_group_name = "auto-group-tg"
target_group_description = "load balancer target group"
}
}
# Создание сетевого балансировщика
resource "yandex_lb_network_load_balancer" "balancer" {
name = "group-balancer"
listener {
name = "http"
port = 80
target_port = 80
}
attached_target_group {
target_group_id = yandex_compute_instance_group.autoscale-group.load_balancer[0].target_group_id
healthcheck {
name = "tcp"
tcp_options {
port = 80
}
}
}
}