-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
285 lines (241 loc) · 6.92 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
# Terraform AWS Provider - https://registry.terraform.io/providers/hashicorp/aws/latest/docs
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
# Configure options
provider "aws" {
region = "us-east-1"
}
# Defile any local vars
locals {
pem_file = "~/.ssh/cp_k8s_kp.pem"
key_name = "cp_k8s_kp"
}
# https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key
resource "tls_private_key" "rsa_key" {
algorithm = "RSA"
rsa_bits = 4096
}
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/key_pair
resource "aws_key_pair" "cp_k8s_ec2_key_pair" {
key_name = local.key_name
public_key = tls_private_key.rsa_key.public_key_openssh
provisioner "local-exec" {
command = <<-EOT
rm -rf ${local.pem_file}
echo '${tls_private_key.rsa_key.private_key_pem}' > ${local.pem_file}
chmod 400 ${local.pem_file}
ls -l ${local.pem_file} > /tmp/out
EOT
}
}
resource "aws_vpc" "cp_k8s_vpc" {
cidr_block = "11.0.0.0/22"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "cp_k8s_VPC"
}
}
resource "aws_subnet" "cp_k8s_subnet" {
vpc_id = aws_vpc.cp_k8s_vpc.id
cidr_block = "11.0.0.0/24"
availability_zone = var.availability_zone
tags = {
Name = "cp_k8s_subnet"
}
}
resource "aws_internet_gateway" "cp_k8s_igw" {
vpc_id = aws_vpc.cp_k8s_vpc.id
tags = {
Name = "cp_k8s_igw"
}
}
/* Routing table for public subnet */
resource "aws_route_table" "cp_k8s_public_route_table" {
depends_on = [
aws_subnet.cp_k8s_subnet
]
vpc_id = aws_vpc.cp_k8s_vpc.id
tags = {
Name = "cp_k8s_public_route_table"
}
}
resource "aws_route" "cp_k8s_public_route" {
depends_on = [
aws_route_table.cp_k8s_public_route_table
]
route_table_id = aws_route_table.cp_k8s_public_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.cp_k8s_igw.id
}
resource "aws_route_table_association" "cp_k8s_public_subnet_association" {
depends_on = [
aws_route.cp_k8s_public_route
]
subnet_id = aws_subnet.cp_k8s_subnet.id
route_table_id = aws_route_table.cp_k8s_public_route_table.id
}
resource "aws_security_group" "cp_k8s_ec2_sg_control_plane" {
name = "cp-sg-kube-control"
vpc_id = aws_vpc.cp_k8s_vpc.id
description = "Allow SSH inbound traffic"
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Kubernetes API"
from_port = 6443
to_port = 6443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = -1
self = true
}
egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.sg_control_plane_tags
}
resource "aws_security_group" "cp_k8s_ec2_sg_worker_nodes" {
name = "cp-sg-kube-worker"
vpc_id = aws_vpc.cp_k8s_vpc.id
description = "Allow SSH inbound traffic"
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "TCP"
from_port = 30000
to_port = 32767
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = -1
self = true
}
egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.sg_worker_node_tags
}
resource "aws_security_group_rule" "cp_to_worker_nodes" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
security_group_id = aws_security_group.cp_k8s_ec2_sg_worker_nodes.id
source_security_group_id = aws_security_group.cp_k8s_ec2_sg_control_plane.id
}
resource "aws_security_group_rule" "worker_nodes_to_cp" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
security_group_id = aws_security_group.cp_k8s_ec2_sg_control_plane.id
source_security_group_id = aws_security_group.cp_k8s_ec2_sg_worker_nodes.id
}
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
resource "aws_instance" "cp_k8s_ec2_instance_control_plane_node" {
depends_on = [
aws_subnet.cp_k8s_subnet
]
ami = var.ami_id
subnet_id = aws_subnet.cp_k8s_subnet.id
instance_type = var.cp_instance_type
key_name = aws_key_pair.cp_k8s_ec2_key_pair.key_name
private_ip = "11.0.0.10"
vpc_security_group_ids = [aws_security_group.cp_k8s_ec2_sg_control_plane.id]
associate_public_ip_address = true
root_block_device {
volume_size = 20
}
tags = {
Name = "k8s-control-plane-node"
# Schedule = "stop_when_I_sleep"
}
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.rsa_key.private_key_openssh
host = self.public_ip
}
provisioner "remote-exec" {
scripts = [
"./kubeadm-scripts/step-01-k8s-packages.sh",
# "./kubeadm-scripts/step-02-k8s-cp-init.sh",
]
}
# provisioner "local-exec" {
# command = <<-EOT
# join_cmd=$(ssh ec2-user@${self.public_ip} -o StrictHostKeyChecking=no -i ${local.pem_file} "kubeadm token create --print-join-command")
# rm -rf ./kubeadm-scripts/step-03-k8s-join.sh
# echo "sudo $join_cmd" > ./kubeadm-scripts/step-03-k8s-join.sh
# EOT
# }
}
# resource "aws_network_interface" "eni" {
# subnet_id = aws_subnet.my_subnet.id
# private_ips = ["172.16.10.100"]
# tags = {
# Name = "primary_network_interface"
# }
# }
resource "aws_instance" "cp_k8s_ec2_instance_worker_node" {
depends_on = [
aws_instance.cp_k8s_ec2_instance_control_plane_node
]
for_each = { for idx, worker_node in var.worker_nodes : idx => worker_node }
ami = var.ami_id
subnet_id = aws_subnet.cp_k8s_subnet.id
instance_type = var.worker_node_instance_type
key_name = aws_key_pair.cp_k8s_ec2_key_pair.key_name
private_ip = each.value.private_ip
vpc_security_group_ids = [aws_security_group.cp_k8s_ec2_sg_worker_nodes.id]
associate_public_ip_address = true
root_block_device {
volume_size = 20
}
tags = merge(
{ Name = each.value.Name },
var.worker_node_tags
)
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.rsa_key.private_key_openssh
host = self.public_ip
}
provisioner "remote-exec" {
scripts = [
"./kubeadm-scripts/step-01-k8s-packages.sh",
# "./kubeadm-scripts/step-03-k8s-join.sh",
]
}
}