-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
333 lines (284 loc) · 9.87 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
provider "openstack" {
}
################################################################################
# network, flavor
# resource name: tf_k8s, tf_k8s_flavor
################################################################################
resource "openstack_compute_keypair_v2" "tf_k8s" {
name = var.name
public_key = file("${var.ssh_key_file}.pub")
}
resource "openstack_networking_network_v2" "tf_k8s" {
name = var.name
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "tf_k8s" {
name = var.name
network_id = openstack_networking_network_v2.tf_k8s.id
cidr = "10.0.0.0/24"
ip_version = 4
dns_nameservers = ["8.8.8.8", "8.8.4.4"]
}
resource "openstack_networking_router_v2" "tf_k8s" {
name = var.name
admin_state_up = "true"
external_network_id = data.openstack_networking_network_v2.tf_k8s.id
}
resource "openstack_networking_router_interface_v2" "tf_k8s" {
router_id = openstack_networking_router_v2.tf_k8s.id
subnet_id = openstack_networking_subnet_v2.tf_k8s.id
}
resource "openstack_compute_flavor_v2" "tf_k8s_flavor_control_plane" {
name = "k8s-type-control-plane"
ram = "8192"
vcpus = "8"
disk = "20"
is_public = true
}
resource "openstack_compute_flavor_v2" "tf_k8s_flavor_worker_node" {
name = "k8s-type-worker-node"
ram = "4096"
vcpus = "4"
disk = "20"
is_public = true
}
################################################################################
# ssecurity group
# resource name: tf_k8s, tf_k8s_portnum, tf_k8s_portname
################################################################################
resource "openstack_networking_secgroup_v2" "tf_k8s" {
name = var.name
description = "Security group for the Terraform K8S Cluster"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_22" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 22
port_range_max = 22
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "ssh"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_80" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 80
port_range_max = 80
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "http"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_443" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 443
port_range_max = 443
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "https"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_8080" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 8080
port_range_max = 8080
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "http2"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_3000" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 3000
port_range_max = 3000
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "grafana"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_9090" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 9090
port_range_max = 9090
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "prometheus"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_icmp" {
direction = "ingress"
ethertype = "IPv4"
protocol = "icmp"
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "ping"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_nodeport_svc" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 30000
port_range_max = 32767
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "nodeport"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_internal" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
remote_ip_prefix = "10.0.0.0/24"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "openstack private subnet"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_protocol4_public" {
direction = "ingress"
ethertype = "IPv4"
protocol = "4"
remote_ip_prefix = "172.24.4.0/24"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "openstack public network"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_protocol4_private" {
direction = "ingress"
ethertype = "IPv4"
protocol = "4"
remote_ip_prefix = "10.0.0.0/22"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "openstack private network"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_cert_manager_10250" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 10250
port_range_max = 10250
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "cert manager in k8s"
}
resource "openstack_networking_secgroup_rule_v2" "tf_k8s_cert_manager_6080" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 6080
port_range_max = 6080
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.tf_k8s.id
description = "cert manager in k8s"
}
################################################################################
# control-plane
# resource name: tf_k8s
################################################################################
resource "openstack_networking_floatingip_v2" "tf_k8s" {
pool = var.pool
}
resource "openstack_compute_instance_v2" "tf_k8s" {
name = "${var.name}-control-plane"
image_name = var.image
flavor_name = var.flavor_control_plane
key_pair = openstack_compute_keypair_v2.tf_k8s.name
security_groups = ["${openstack_networking_secgroup_v2.tf_k8s.name}"]
network {
uuid = openstack_networking_network_v2.tf_k8s.id
fixed_ip_v4 = var.control_plane_private_ip
}
}
resource "openstack_compute_floatingip_associate_v2" "tf_k8s" {
floating_ip = openstack_networking_floatingip_v2.tf_k8s.address
instance_id = openstack_compute_instance_v2.tf_k8s.id
connection {
host = openstack_networking_floatingip_v2.tf_k8s.address
user = var.ssh_user_name
private_key = file(var.ssh_key_file)
}
provisioner "file" {
source = "kubeadm.sh"
destination = "/tmp/kubeadm.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/kubeadm.sh",
"/tmp/kubeadm.sh",
]
}
depends_on = [
openstack_networking_floatingip_v2.tf_k8s,
openstack_compute_instance_v2.tf_k8s
]
}
################################################################################
# worker node
# resource name: worker_node
################################################################################
resource "openstack_compute_instance_v2" "worker_node" {
count = var.worker_node_count
name = format("${var.name}-${var.worker_node_prefix}-%02d", count.index + 1)
image_name = var.image
flavor_name = var.flavor_worker_node
key_pair = openstack_compute_keypair_v2.tf_k8s.name
security_groups = ["${openstack_networking_secgroup_v2.tf_k8s.name}"]
network {
uuid = openstack_networking_network_v2.tf_k8s.id
}
depends_on = [openstack_compute_floatingip_associate_v2.tf_k8s]
}
resource "openstack_networking_floatingip_v2" "worker_node" {
count = var.worker_node_count
pool = var.pool
}
resource "openstack_compute_floatingip_associate_v2" "worker_node" {
count = var.worker_node_count
floating_ip = openstack_networking_floatingip_v2.worker_node.*.address[count.index]
instance_id = openstack_compute_instance_v2.worker_node.*.id[count.index]
connection {
host = openstack_networking_floatingip_v2.worker_node.*.address[count.index]
user = var.ssh_user_name
private_key = file(var.ssh_key_file)
}
provisioner "file" {
source = "worker_node.sh"
destination = "/tmp/worker_node.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/worker_node.sh",
"/tmp/worker_node.sh ${data.openstack_compute_instance_v2.tf_k8s.access_ip_v4}",
]
}
depends_on = [
openstack_networking_floatingip_v2.worker_node,
openstack_compute_instance_v2.worker_node
]
}
################################################################################
# kubernetes resources
# resource name: tf_k8s_resource
################################################################################
resource "null_resource" "tf_k8s_resource" {
connection {
host = openstack_networking_floatingip_v2.tf_k8s.address
user = var.ssh_user_name
private_key = file(var.ssh_key_file)
}
provisioner "file" {
source = "monitoring.sh"
destination = "/tmp/monitoring.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/monitoring.sh",
"/tmp/monitoring.sh",
]
}
depends_on = [
openstack_compute_floatingip_associate_v2.tf_k8s,
openstack_compute_floatingip_associate_v2.worker_node
]
}