Skip to content

Commit 74ed5dd

Browse files
committed
update k8s version and implement tls
- use coreos-config yaml to configure instances - create aws auto-scaling group - tls for workers is generated by workes themselves using the ca and ca-key uploaded via cloud-config (should probably be made more secure)
1 parent 1bc0c94 commit 74ed5dd

24 files changed

+920
-473
lines changed

Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
plan: etcd_discovery_url.txt kube_token.txt
1+
plan: etcd_discovery_url.txt kube_token.txt tls-assets/ca-key.pem tls-assets/ca.pem
22
terraform plan
33

44
etcd_discovery_url.txt:
5-
curl -s https://discovery.etcd.io/new?size=3 > etcd_discovery_url.txt
5+
curl -s https://discovery.etcd.io/new?size=2 > etcd_discovery_url.txt
66

77
destroy:
88
terraform destroy
99
rm etcd_discovery_url.txt kube_token.txt
10+
rm -rf tls-assets
1011

11-
apply: etcd_discovery_url.txt kube_token.txt
12+
apply: etcd_discovery_url.txt kube_token.txt tls-assets/ca-key.pem tls-assets/ca.pem
1213
terraform apply
1314

1415
kube_token.txt:
1516
openssl rand -base64 8 |md5 |head -c8 > kube_token.txt
1617
echo >> kube_token.txt
18+
19+
tls-assets/ca-key.pem:
20+
mkdir -p tls-assets
21+
openssl genrsa -out tls-assets/ca-key.pem 2048
22+
23+
tls-assets/ca.pem:
24+
mkdir -p tls-assets
25+
openssl req -x509 -new -nodes -key tls-assets/ca-key.pem -days 10000 -out tls-assets/ca.pem -subj "/CN=kube-ca"

kubernetes.tf

Lines changed: 211 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ resource "aws_security_group_rule" "allow_ssh" {
6565

6666
resource "aws_security_group_rule" "allow_kube_api" {
6767
type = "ingress"
68-
from_port = 6443
69-
to_port = 6443
68+
from_port = "${var.api_secure_port}"
69+
to_port = "${var.api_secure_port}"
7070
protocol = "tcp"
7171
cidr_blocks = ["0.0.0.0/0"]
7272
security_group_id = "${aws_security_group.kubernetes.id}"
@@ -90,29 +90,9 @@ resource "aws_security_group_rule" "allow_all_egress" {
9090
security_group_id = "${aws_security_group.kubernetes.id}"
9191
}
9292

93-
resource "template_file" "kubernetes" {
94-
template = "templates/kubernetes.sh"
95-
96-
vars = {
97-
etcd_dicovery_url = "${replace(file("etcd_discovery_url.txt"), "/\n/", "")}"
98-
containers_cidr = "${var.containers_cidr}"
99-
kubernetes_version = "${var.kubernetes_version}"
100-
portal_net = "${var.portal_net}"
101-
etcd_version = "${var.etcd_version}"
102-
}
103-
}
104-
105-
resource "template_file" "tokens" {
106-
template = "templates/tokens.csv"
107-
108-
vars = {
109-
token = "${replace(file("kube_token.txt"), "/\n/", "")}"
110-
}
111-
}
112-
11393
resource "aws_instance" "etcd" {
11494
ami = "${var.ami}"
115-
instance_type = "t2.medium"
95+
instance_type = "${var.etcd_instance_type}"
11696
count = "${var.num_etcd}"
11797
security_groups = [ "${aws_security_group.kubernetes.id}" ]
11898
subnet_id = "${aws_subnet.kubernetes.id}"
@@ -125,32 +105,74 @@ resource "aws_instance" "etcd" {
125105
}
126106

127107
tags {
128-
Name = "kubernetes-${var.cluster_name}-etcd"
108+
Name = "${var.cluster_name}-etcd"
129109
Cluster = "${var.cluster_name}"
130110
Role = "etcd"
131111
}
132112

113+
user_data = "${template_file.etcd-user-data.rendered}"
114+
}
133115

134-
provisioner "file" {
135-
source = "scripts"
136-
destination = "/tmp/scripts"
137-
}
116+
resource "aws_iam_instance_profile" "master" {
117+
name = "k8s-master"
118+
roles = ["${aws_iam_role.master.name}"]
119+
}
138120

139-
provisioner "remote-exec" {
140-
inline = [
141-
"echo 'PRIVATE_IP=${self.private_ip}' > /tmp/network.env",
142-
"echo 'PUBLIC_IP=${self.public_ip}' >> /tmp/network.env",
143-
"sudo mv /tmp/network.env /etc/network.env",
144-
"cat <<'EOF' > /tmp/kubernetes.env\n${template_file.kubernetes.rendered}\nEOF",
145-
"sudo mv /tmp/kubernetes.env /etc/kubernetes.env",
146-
"sudo bash /tmp/scripts/${self.tags.Role}.sh"
147-
]
121+
resource "aws_iam_role_policy" "master" {
122+
name = "k8s-master"
123+
role = "${aws_iam_role.master.id}"
124+
policy = <<EOF
125+
{
126+
"Version": "2012-10-17",
127+
"Statement": [
128+
{
129+
"Effect": "Allow",
130+
"Action": [
131+
"ec2:*"
132+
],
133+
"Resource": [
134+
"*"
135+
]
136+
},
137+
{
138+
"Effect": "Allow",
139+
"Action": [
140+
"elasticloadbalancing:*"
141+
],
142+
"Resource": [
143+
"*"
144+
]
145+
}
146+
]
147+
}
148+
EOF
149+
}
150+
151+
resource "aws_iam_role" "master" {
152+
name = "k8s-master"
153+
path = "/"
154+
assume_role_policy = <<EOF
155+
{
156+
"Version": "2012-10-17",
157+
"Statement": [
158+
{
159+
"Action": "sts:AssumeRole",
160+
"Principal": {
161+
"Service": "ec2.amazonaws.com"
162+
},
163+
"Effect": "Allow",
164+
"Sid": ""
148165
}
166+
]
167+
}
168+
EOF
149169
}
150170

151171
resource "aws_instance" "master" {
152172
ami = "${var.ami}"
153-
instance_type = "t2.medium"
173+
instance_type = "${var.master_instance_type}"
174+
count = "${var.num_master}"
175+
iam_instance_profile = "${aws_iam_instance_profile.master.name}"
154176
security_groups = [ "${aws_security_group.kubernetes.id}" ]
155177
subnet_id = "${aws_subnet.kubernetes.id}"
156178
associate_public_ip_address = true
@@ -162,7 +184,7 @@ resource "aws_instance" "master" {
162184
}
163185

164186
tags {
165-
Name = "kubernetes-${var.cluster_name}-master"
187+
Name = "${var.cluster_name}-master"
166188
Cluster = "${var.cluster_name}"
167189
Role = "master"
168190
}
@@ -174,67 +196,178 @@ resource "aws_instance" "master" {
174196

175197
provisioner "remote-exec" {
176198
inline = [
177-
"cat <<'EOF' > /tmp/tokens.csv\n${template_file.tokens.rendered}\nEOF",
178-
"sudo mkdir -p mkdir /etc/kubernetes",
179-
"sudo mv /tmp/tokens.csv /etc/kubernetes/tokens.csv",
180-
"echo 'PRIVATE_IP=${self.private_ip}' > /tmp/network.env",
181-
"echo 'PUBLIC_IP=${self.public_ip}' >> /tmp/network.env",
182-
"sudo mv /tmp/network.env /etc/network.env",
183-
"cat <<'EOF' > /tmp/kubernetes.env\n${template_file.kubernetes.rendered}\nEOF",
199+
"cat <<'EOF' > /tmp/kubernetes.env\n${template_file.master-env.rendered}\nEOF",
200+
"echo 'PRIVATE_IP=${self.private_ip}' >> /tmp/kubernetes.env",
201+
"echo 'PUBLIC_IP=${self.public_ip}' >> /tmp/kubernetes.env",
184202
"sudo mv /tmp/kubernetes.env /etc/kubernetes.env",
185-
"sudo bash /tmp/scripts/${self.tags.Role}.sh"
203+
"sudo bash /tmp/scripts/master.sh",
204+
# "cat << 'EOF' > /tmp/kubernetes/tokens.csv\n${template_file.tokens.rendered}\nEOF",
205+
"sudo mkdir -p /etc/kubernetes/ssl"
206+
# "sudo mv /tmp/kubernetes/tokens.csv /etc/kubernetes/ssl/tokens.csv",
186207
]
187208
}
209+
210+
user_data = "${template_file.master-user-data.rendered}"
188211
}
189212

190-
resource "aws_instance" "worker" {
191-
ami = "${var.ami}"
192-
instance_type = "t2.medium"
193-
count = "${var.num_worker}"
213+
resource "null_resource" "master" {
214+
count = "${var.num_master}"
215+
triggers {
216+
cluster_instance_ids = "${join(",", aws_instance.master.*.id)}"
217+
}
218+
connection {
219+
host = "${element(aws_instance.master.*.public_ip, count.index)}"
220+
user = "core"
221+
agent = true
222+
}
223+
provisioner "remote-exec" {
224+
inline = [
225+
"sudo rm -rf /tmp/ssl",
226+
"sudo rm -rf /etc/kubernetes/ssl",
227+
"mkdir /tmp/ssl",
228+
"sudo mkdir -p /etc/kubernetes"
229+
]
230+
}
231+
provisioner "local-exec" {
232+
command = "cat << 'EOF' > tls-assets/openssl.cnf\n${template_file.openssl.rendered}\nEOF\n${template_file.create-master-tls.rendered} && scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q tls-assets/ca.pem tls-assets/apiserver.pem tls-assets/apiserver-key.pem core@${element(aws_instance.master.*.public_ip, count.index)}:/tmp/ssl/"
233+
}
234+
provisioner "remote-exec" {
235+
inline = [
236+
"sudo mv /tmp/ssl /etc/kubernetes/ssl",
237+
"sudo chmod 600 /etc/kubernetes/ssl/*-key.pem",
238+
"sudo chown root:root /etc/kubernetes/ssl/*-key.pem"
239+
]
240+
}
241+
}
242+
243+
resource "aws_iam_role_policy" "worker" {
244+
name = "k8s-worker"
245+
role = "${aws_iam_role.worker.id}"
246+
policy = <<EOF
247+
{
248+
"Version": "2012-10-17",
249+
"Statement": [
250+
{
251+
"Effect": "Allow",
252+
"Action": "ec2:Describe*",
253+
"Resource": "*"
254+
},
255+
{
256+
"Effect": "Allow",
257+
"Action": "ec2:AttachVolume",
258+
"Resource": "*"
259+
},
260+
{
261+
"Effect": "Allow",
262+
"Action": "ec2:DetachVolume",
263+
"Resource": "*"
264+
},
265+
{
266+
"Effect": "Allow",
267+
"Action": [
268+
"ecr:GetAuthorizationToken",
269+
"ecr:BatchCheckLayerAvailability",
270+
"ecr:GetDownloadUrlForLayer",
271+
"ecr:GetRepositoryPolicy",
272+
"ecr:DescribeRepositories",
273+
"ecr:ListImages",
274+
"ecr:BatchGetImage"
275+
],
276+
"Resource": "*"
277+
}
278+
]
279+
}
280+
EOF
281+
}
282+
283+
resource "aws_iam_instance_profile" "worker" {
284+
name = "k8s-worker"
285+
roles = ["${aws_iam_role.worker.name}"]
286+
}
287+
288+
resource "aws_iam_role" "worker" {
289+
name = "k8s-worker"
290+
path = "/"
291+
assume_role_policy = <<EOF
292+
{
293+
"Version": "2012-10-17",
294+
"Statement": [
295+
{
296+
"Action": "sts:AssumeRole",
297+
"Principal": {
298+
"Service": "ec2.amazonaws.com"
299+
},
300+
"Effect": "Allow",
301+
"Sid": ""
302+
}
303+
]
304+
}
305+
EOF
306+
}
307+
308+
resource "aws_launch_configuration" "worker" {
309+
image_id = "${var.ami}"
310+
instance_type = "${var.worker_instance_type}"
311+
iam_instance_profile = "${aws_iam_instance_profile.worker.name}"
194312
security_groups = [ "${aws_security_group.kubernetes.id}" ]
195-
subnet_id = "${aws_subnet.kubernetes.id}"
196313
associate_public_ip_address = true
197314
key_name = "${var.ssh_key_name}"
198315

199-
connection {
200-
user = "core"
201-
agent = true
316+
317+
user_data = "${template_file.worker-user-data.rendered}"
318+
}
319+
320+
resource "aws_autoscaling_group" "worker" {
321+
name = "${var.cluster_name}-k8s-worker"
322+
launch_configuration = "${aws_launch_configuration.worker.name}"
323+
max_size = "${var.num_worker}"
324+
min_size = "${var.num_worker}"
325+
desired_capacity = "${var.num_worker}"
326+
vpc_zone_identifier = [ "${aws_subnet.kubernetes.id}" ]
327+
328+
329+
tag {
330+
key = "Name"
331+
value = "${var.cluster_name}-worker"
332+
propagate_at_launch = true
202333
}
203334

204-
tags {
205-
Name = "kubernetes-${var.cluster_name}-worker"
206-
Cluster = "${var.cluster_name}"
207-
Role = "worker"
335+
tag {
336+
key = "Cluster"
337+
value = "${var.cluster_name}"
338+
propagate_at_launch = true
208339
}
209340

210-
provisioner "file" {
211-
source = "scripts"
212-
destination = "/tmp/scripts"
341+
tag {
342+
key = "Role"
343+
value = "worker"
344+
propagate_at_launch = true
213345
}
346+
}
214347

348+
resource "null_resource" "init-kubernetes" {
349+
triggers {
350+
id = "${element(aws_instance.master.*.id,0)}"
351+
}
352+
connection {
353+
host = "${element(aws_instance.master.*.public_ip, 0)}"
354+
user = "core"
355+
agent = true
356+
}
215357
provisioner "remote-exec" {
216358
inline = [
217-
"echo 'PRIVATE_IP=${self.private_ip}' > /tmp/network.env",
218-
"echo 'PUBLIC_IP=${self.public_ip}' >> /tmp/network.env",
219-
"sudo mv /tmp/network.env /etc/network.env",
220-
"cat <<'EOF' > /tmp/kubernetes.env\n${template_file.kubernetes.rendered}\nEOF",
221-
"echo 'KUBERNETES_MASTER=http://${aws_instance.master.private_ip}:8080' >> /tmp/kubernetes.env",
222-
"sudo mv /tmp/kubernetes.env /etc/kubernetes.env",
223-
"sudo bash /tmp/scripts/${self.tags.Role}.sh"
359+
"${template_file.init-kubernetes.rendered}"
224360
]
225361
}
226-
}
362+
provisioner "local-exec" {
363+
command = "${template_file.create-admin-tls.rendered}"
364+
}
227365

228-
resource "template_file" "kubectl-config" {
229-
template = "templates/kubectl-config.sh"
230-
vars = {
231-
cluster_name = "${var.cluster_name}"
232-
token = "${replace(file("kube_token.txt"), "/\n/", "")}"
233-
server = "${aws_instance.master.public_ip}"
366+
provisioner "local-exec" {
367+
command = "cat <<EOF > kubeconfig\n${template_file.kubectl-config-file.rendered}\nEOF"
234368
}
235369
}
236370

237371
output "kubernetes-api-server" {
238372
value = "${template_file.kubectl-config.rendered}"
239-
}
240-
373+
}

0 commit comments

Comments
 (0)