forked from weibeld/terraform-aws-kubeadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
234 lines (209 loc) · 7.56 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
terraform {
required_version = ">= 0.12"
}
#------------------------------------------------------------------------------#
# Common local values
#------------------------------------------------------------------------------#
locals {
tags = merge(var.tags, { "terraform-kubeadm:cluster" = var.cluster_name })
}
#------------------------------------------------------------------------------#
# Key pair
#------------------------------------------------------------------------------#
# Performs 'ImportKeyPair' API operation (not 'CreateKeyPair')
resource "aws_key_pair" "main" {
key_name_prefix = "${var.cluster_name}-"
public_key = file(var.public_key_file)
tags = local.tags
}
#------------------------------------------------------------------------------#
# Security groups
#------------------------------------------------------------------------------#
# The AWS provider removes the default "allow all "egress rule from all security
# groups, so it has to be defined explicitly.
resource "aws_security_group" "egress" {
name = "${var.cluster_name}-egress"
description = "Allow all outgoing traffic to everywhere"
vpc_id = var.vpc_id
tags = local.tags
egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "ingress_internal" {
name = "${var.cluster_name}-ingress-internal"
description = "Allow all incoming traffic from nodes and Pods in the cluster"
vpc_id = var.vpc_id
tags = local.tags
ingress {
protocol = -1
from_port = 0
to_port = 0
self = true
description = "Allow incoming traffic from cluster nodes"
}
ingress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = var.pod_network_cidr_block != null ? [var.pod_network_cidr_block] : null
description = "Allow incoming traffic from the Pods of the cluster"
}
}
resource "aws_security_group" "ingress_k8s" {
name = "${var.cluster_name}-ingress-k8s"
description = "Allow incoming Kubernetes API requests (TCP/6443) from outside the cluster"
vpc_id = var.vpc_id
tags = local.tags
ingress {
protocol = "tcp"
from_port = 6443
to_port = 6443
cidr_blocks = var.allowed_k8s_cidr_blocks
}
}
resource "aws_security_group" "ingress_ssh" {
name = "${var.cluster_name}-ingress-ssh"
description = "Allow incoming SSH traffic (TCP/22) from outside the cluster"
vpc_id = var.vpc_id
tags = local.tags
ingress {
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = var.allowed_ssh_cidr_blocks
}
}
#------------------------------------------------------------------------------#
# Elastic IP for master node
#------------------------------------------------------------------------------#
# EIP for master node because it must know its public IP during initialisation
resource "aws_eip" "master" {
vpc = true
tags = local.tags
}
resource "aws_eip_association" "master" {
allocation_id = aws_eip.master.id
instance_id = aws_instance.master.id
}
#------------------------------------------------------------------------------#
# Bootstrap token for kubeadm
#------------------------------------------------------------------------------#
# Generate bootstrap token
# See https://kubernetes.io/docs/reference/access-authn-authz/bootstrap-tokens/
resource "random_string" "token_id" {
length = 6
special = false
upper = false
}
resource "random_string" "token_secret" {
length = 16
special = false
upper = false
}
locals {
token = "${random_string.token_id.result}.${random_string.token_secret.result}"
}
#------------------------------------------------------------------------------#
# EC2 instances
#------------------------------------------------------------------------------#
data "aws_ami" "ubuntu" {
# AMI owner ID of Canonical
owners = ["099720109477"]
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
}
resource "aws_instance" "master" {
ami = data.aws_ami.ubuntu.image_id
instance_type = var.master_instance_type
subnet_id = var.subnet_id
key_name = aws_key_pair.main.key_name
vpc_security_group_ids = [
aws_security_group.egress.id,
aws_security_group.ingress_internal.id,
aws_security_group.ingress_k8s.id,
aws_security_group.ingress_ssh.id
]
tags = merge(local.tags, { "terraform-kubeadm:node" = "master" })
# Saved in: /var/lib/cloud/instances/<instance-id>/user-data.txt [1]
# Logs in: /var/log/cloud-init-output.log [2]
# [1] https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts
# [2] https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts
user_data = templatefile(
"${path.module}/user-data.tftpl",
{
node = "master",
token = local.token,
cidr = var.pod_network_cidr_block
master_public_ip = aws_eip.master.public_ip,
master_private_ip = null,
worker_index = null
}
)
}
resource "aws_instance" "workers" {
count = var.num_workers
ami = data.aws_ami.ubuntu.image_id
instance_type = var.worker_instance_type
subnet_id = var.subnet_id
associate_public_ip_address = true
key_name = aws_key_pair.main.key_name
vpc_security_group_ids = [
aws_security_group.egress.id,
aws_security_group.ingress_internal.id,
aws_security_group.ingress_ssh.id
]
tags = merge(local.tags, { "terraform-kubeadm:node" = "worker-${count.index}" })
user_data = templatefile(
"${path.module}/user-data.tftpl",
{
node = "worker",
token = local.token,
cidr = null,
master_public_ip = null,
master_private_ip = aws_instance.master.private_ip,
worker_index = count.index
}
)
}
#------------------------------------------------------------------------------#
# Wait for bootstrap to finish on all nodes
#------------------------------------------------------------------------------#
resource "null_resource" "wait_for_bootstrap_to_finish" {
provisioner "local-exec" {
command = <<-EOF
alias ssh='ssh -q -i ${var.private_key_file} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
while true; do
sleep 2
! ssh ubuntu@${aws_eip.master.public_ip} [[ -f /home/ubuntu/done ]] >/dev/null && continue
%{for worker_public_ip in aws_instance.workers[*].public_ip~}
! ssh ubuntu@${worker_public_ip} [[ -f /home/ubuntu/done ]] >/dev/null && continue
%{endfor~}
break
done
EOF
}
triggers = {
instance_ids = join(",", concat([aws_instance.master.id], aws_instance.workers[*].id))
}
}
#------------------------------------------------------------------------------#
# Download kubeconfig file from master node to local machine
#------------------------------------------------------------------------------#
resource "null_resource" "download_kubeconfig_file" {
provisioner "local-exec" {
command = <<-EOF
alias scp='scp -q -i ${var.private_key_file} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
scp ubuntu@${aws_eip.master.public_ip}:/home/ubuntu/admin.conf ${var.kubeconfig != null ? var.kubeconfig : "${var.cluster_name}.conf"} >/dev/null
EOF
}
triggers = {
wait_for_bootstrap_to_finish = null_resource.wait_for_bootstrap_to_finish.id
}
}