-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
257 lines (222 loc) · 7.32 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
locals {
# e.g. east-11 is 11
az_num = reverse(split("-", var.availability_zone))[0]
# e.g. east-11 is e11
az_short_name = "${substr(reverse(split("-", var.availability_zone))[1], 0, 1)}${local.az_num}"
# Port used by the protocol
port_ssh = 22
port_squid = 3128
port_kubectl = 6443
port_kubelet = 10250
extra_userdata = templatefile("${path.module}/templates/extra_userdata.tftpl", {})
}
resource "nifcloud_private_lan" "this" {
private_lan_name = "${var.prefix}lan"
availability_zone = var.availability_zone
cidr_block = var.private_network_cidr
accounting_type = var.accounting_type
}
#####
# Security Group
#
resource "nifcloud_security_group" "bn" {
group_name = "${var.prefix}bn"
description = "${var.prefix} bastion"
availability_zone = var.availability_zone
}
resource "nifcloud_security_group" "px" {
group_name = "${var.prefix}px"
description = "${var.prefix} proxy"
availability_zone = var.availability_zone
}
resource "nifcloud_security_group" "cp" {
group_name = "${var.prefix}cp"
description = "${var.prefix} control plane"
availability_zone = var.availability_zone
}
resource "nifcloud_security_group" "wk" {
group_name = "${var.prefix}wk"
description = "${var.prefix} worker"
availability_zone = var.availability_zone
}
#####
# LB
#
resource "nifcloud_load_balancer" "this" {
load_balancer_name = "${local.az_short_name}${var.prefix}cp"
accounting_type = var.accounting_type
balancing_type = 1 // Round-Robin
load_balancer_port = 6443
instance_port = 6443
instances = [for v in module.cp : v.instance_id]
}
#####
# Module
#
module "px" {
source = "ystkfujii/instance/nifcloud"
version = "0.0.5"
availability_zone = var.availability_zone
instance_name = "${local.az_short_name}${var.prefix}px01"
security_group_name = nifcloud_security_group.px.group_name
key_name = var.instance_key_name
instance_type = var.instance_type_px
accounting_type = var.accounting_type
public_ip_address = var.elasticip_px
interface_private = {
ip_address = var.private_ip_px
network_id = nifcloud_private_lan.this.network_id
}
depends_on = [
nifcloud_private_lan.this,
nifcloud_security_group.px,
]
}
module "bn" {
source = "ystkfujii/instance/nifcloud"
version = "0.0.5"
availability_zone = var.availability_zone
instance_name = "${local.az_short_name}${var.prefix}bn01"
security_group_name = nifcloud_security_group.bn.group_name
key_name = var.instance_key_name
instance_type = var.instance_type_bn
accounting_type = var.accounting_type
public_ip_address = var.elasticip_bn
interface_private = {
ip_address = var.private_ip_bn
network_id = nifcloud_private_lan.this.network_id
}
depends_on = [
nifcloud_private_lan.this,
nifcloud_security_group.px,
]
}
module "cp" {
source = "ystkfujii/instance/nifcloud"
version = "0.0.5"
for_each = var.instances_cp
availability_zone = var.availability_zone
instance_name = "${local.az_short_name}${var.prefix}${each.key}"
security_group_name = nifcloud_security_group.cp.group_name
key_name = var.instance_key_name
instance_type = var.instance_type_cp
accounting_type = var.accounting_type
interface_private = {
ip_address = each.value.private_ip
network_id = nifcloud_private_lan.this.network_id
}
extra_userdata = local.extra_userdata
depends_on = [
nifcloud_private_lan.this,
nifcloud_security_group.cp,
]
}
module "wk" {
source = "ystkfujii/instance/nifcloud"
version = "0.0.5"
for_each = var.instances_wk
availability_zone = var.availability_zone
instance_name = "${local.az_short_name}${var.prefix}${each.key}"
security_group_name = nifcloud_security_group.wk.group_name
key_name = var.instance_key_name
instance_type = var.instance_type_wk
accounting_type = var.accounting_type
interface_private = {
ip_address = each.value.private_ip
network_id = nifcloud_private_lan.this.network_id
}
extra_userdata = local.extra_userdata
depends_on = [
nifcloud_private_lan.this,
nifcloud_security_group.wk,
]
}
#####
# Security Group Rule
#
# ssh
resource "nifcloud_security_group_rule" "ssh_from_bn" {
security_group_names = [
nifcloud_security_group.px.group_name,
nifcloud_security_group.wk.group_name,
nifcloud_security_group.cp.group_name,
]
type = "IN"
from_port = local.port_ssh
to_port = local.port_ssh
protocol = "TCP"
source_security_group_name = nifcloud_security_group.bn.group_name
}
# kubectl
resource "nifcloud_security_group_rule" "kubectl_from_worker" {
security_group_names = [
nifcloud_security_group.cp.group_name,
]
type = "IN"
from_port = local.port_kubectl
to_port = local.port_kubectl
protocol = "TCP"
source_security_group_name = nifcloud_security_group.wk.group_name
}
resource "nifcloud_security_group_rule" "kubectl_from_bn" {
security_group_names = [
nifcloud_security_group.cp.group_name,
]
type = "IN"
from_port = local.port_kubectl
to_port = local.port_kubectl
protocol = "TCP"
source_security_group_name = nifcloud_security_group.bn.group_name
}
# kubelet
resource "nifcloud_security_group_rule" "kubelet_from_worker" {
security_group_names = [
nifcloud_security_group.cp.group_name,
]
type = "IN"
from_port = local.port_kubelet
to_port = local.port_kubelet
protocol = "TCP"
source_security_group_name = nifcloud_security_group.wk.group_name
}
resource "nifcloud_security_group_rule" "kubelet_from_control_plane" {
security_group_names = [
nifcloud_security_group.wk.group_name,
]
type = "IN"
from_port = local.port_kubelet
to_port = local.port_kubelet
protocol = "TCP"
source_security_group_name = nifcloud_security_group.cp.group_name
}
# squid
resource "nifcloud_security_group_rule" "squid_from_bn" {
security_group_names = [
nifcloud_security_group.px.group_name,
]
type = "IN"
from_port = local.port_squid
to_port = local.port_squid
protocol = "TCP"
source_security_group_name = nifcloud_security_group.bn.group_name
}
resource "nifcloud_security_group_rule" "squid_from_worker" {
security_group_names = [
nifcloud_security_group.px.group_name,
]
type = "IN"
from_port = local.port_squid
to_port = local.port_squid
protocol = "TCP"
source_security_group_name = nifcloud_security_group.wk.group_name
}
resource "nifcloud_security_group_rule" "squid_from_control_plane" {
security_group_names = [
nifcloud_security_group.px.group_name,
]
type = "IN"
from_port = local.port_squid
to_port = local.port_squid
protocol = "TCP"
source_security_group_name = nifcloud_security_group.cp.group_name
}