-
Notifications
You must be signed in to change notification settings - Fork 4
/
masters.tf
265 lines (229 loc) · 7.26 KB
/
masters.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
resource "aws_s3_object" "master" {
bucket = aws_s3_bucket.userdata.id
key = "master-config.json"
content = var.master_user_data
}
resource "aws_iam_role" "master" {
name = "${local.iam_prefix}${var.cluster_name}-master"
path = var.iam_path
permissions_boundary = var.permissions_boundary
assume_role_policy = <<EOS
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}
EOS
}
resource "aws_iam_instance_profile" "master" {
name = "${local.iam_prefix}${var.cluster_name}-master"
role = aws_iam_role.master.name
path = var.iam_path
}
data "aws_iam_policy_document" "master" {
statement {
actions = [
"ec2:*"
]
resources = ["*"]
}
statement {
actions = [
"elasticloadbalancing:*",
]
resources = ["*"]
}
# https://github.com/kubernetes/kops/blob/master/pkg/model/iam/tests/iam_builder_master_strict.json#L158
statement {
actions = [
"kms:CreateGrant",
"kms:Decrypt",
"kms:DescribeKey",
"kms:Encrypt",
"kms:GenerateDataKey*",
"kms:ReEncrypt*"
]
resources = var.master_kms_ebs_key_arns
}
statement {
actions = [
"s3:GetObject"
]
resources = ["arn:aws:s3:::${aws_s3_bucket.userdata.id}/master-*"]
}
}
resource "aws_iam_role_policy" "master" {
name = "${local.iam_prefix}${var.cluster_name}-master"
role = aws_iam_role.master.id
policy = data.aws_iam_policy_document.master.json
}
resource "aws_launch_template" "master" {
name_prefix = "${var.cluster_name}-master-"
iam_instance_profile { name = aws_iam_instance_profile.master.name }
image_id = var.containerlinux_ami_parameter != "" ? var.containerlinux_ami_parameter : var.containerlinux_ami_id
instance_type = var.master_instance_type
key_name = var.key_name
vpc_security_group_ids = [aws_security_group.master.id]
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html#available-ec2-device-names
#
# Flatcar Linux mentions using "/dev/xvda" as root:
# https://flatcar-linux.org/docs/latest/reference/developer-guides/sdk-disk-partitions/#read-only-usr
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = 100
delete_on_termination = true
}
}
# Allow containers to talk to IMDSv2 endpoint if needed by allowing 2 hops
metadata_options {
http_tokens = "optional"
http_put_response_hop_limit = 2
}
user_data = base64encode(
templatefile("${path.module}/userdata.tftpl",
{
region = var.region,
source = "s3://${aws_s3_bucket.userdata.id}/master-config.json"
}
)
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "master" {
name = "master ${var.cluster_name}"
desired_capacity = var.master_instance_count
health_check_grace_period = 60
health_check_type = "EC2"
force_delete = true
max_size = var.master_instance_count
min_size = var.master_instance_count
vpc_zone_identifier = var.control_plane_private_subnet_ids
target_group_arns = [aws_lb_target_group.control_plane_443.arn]
default_cooldown = 60
launch_template {
id = aws_launch_template.master.id
version = aws_launch_template.master.latest_version
}
tag {
key = "Name"
value = "master ${var.cluster_name}"
propagate_at_launch = true
}
tag {
key = "terraform.io/component"
value = "${var.cluster_name}/master"
propagate_at_launch = true
}
tag {
// kube uses this tag to learn its cluster name and tag managed resources
key = "kubernetes.io/cluster/${var.cluster_name}"
value = "owned"
propagate_at_launch = true
}
tag {
key = "owner"
value = "system"
propagate_at_launch = true
}
}
resource "aws_lb" "control_plane" {
name = "${var.cluster_name}-control-plane-lb"
load_balancer_type = "network"
internal = true
subnets = var.control_plane_private_subnet_ids
idle_timeout = 3600
tags = {
"Name" = "${var.cluster_name}-control-plane-lb"
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
}
}
resource "aws_lb_listener" "control_plane_443" {
load_balancer_arn = aws_lb.control_plane.arn
port = "443"
protocol = "TCP"
default_action {
target_group_arn = aws_lb_target_group.control_plane_443.arn
type = "forward"
}
}
resource "aws_lb_target_group" "control_plane_443" {
name = "${var.cluster_name}-control-plane-443"
vpc_id = var.vpc_id
port = 443
protocol = "TCP"
health_check {
protocol = "TCP"
port = 443
}
tags = {
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
}
}
resource "aws_security_group" "master" {
name = "${var.cluster_name}-master"
description = "k8s master security group"
vpc_id = var.vpc_id
// kube uses the kubernetes.io tag to learn its cluster name and tag managed resources
tags = {
"Name" = "master ${var.cluster_name}"
"terraform.io/component" = "${var.cluster_name}/master"
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
"owner" = "system"
}
}
resource "aws_security_group_rule" "egress-from-master" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.master.id
}
resource "aws_security_group_rule" "ingress-master-to-self" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
security_group_id = aws_security_group.master.id
self = true
}
resource "aws_security_group_rule" "ingress-worker-to-master" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = aws_security_group.worker.id
security_group_id = aws_security_group.master.id
}
resource "aws_security_group_rule" "ingress-world-to-master" {
security_group_id = aws_security_group.master.id
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "master-ssh" {
count = length(var.ssh_security_group_ids)
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
source_security_group_id = element(var.ssh_security_group_ids, count.index)
security_group_id = aws_security_group.master.id
}
resource "aws_route53_record" "master-lb" {
zone_id = var.route53_zone_id
name = "elb.master.${var.cluster_subdomain}.${data.aws_route53_zone.main.name}"
type = "CNAME"
ttl = "30"
records = [aws_lb.control_plane.dns_name]
}