-
Notifications
You must be signed in to change notification settings - Fork 3
/
_instance-worker.tf
104 lines (85 loc) · 3.05 KB
/
_instance-worker.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
data "template_file" "k8s-worker" {
template = "${file("assets/cloud-config/worker/cloud-config.yml")}"
vars {
ETCD_ENDPOINTS = "${join(",", formatlist("http://%s:%s", aws_instance.k8s-etcd.*.private_ip, "2379") ) }"
discovery_url = "${file("assets/discovery/etcd_discovery_url.txt")}"
tls-root-ca = "${file("assets/tls/ca.pem")}"
tls-root-ca-key = "${file("assets/tls/ca-key.pem")}"
tls-client-conf = "${file("assets/tls/api-client.cnf")}"
MASTER_HOST = "${ aws_instance.k8s-master.private_ip }"
node_label = "worker"
}
}
resource "aws_launch_configuration" "worker" {
image_id = "${lookup(var.amis, var.region)}"
instance_type = "t2.medium"
key_name = "${var.aws_key_name}"
iam_instance_profile = "${aws_iam_instance_profile.worker_instance_profile.id}"
/*TODO*/
associate_public_ip_address = true
root_block_device {
volume_size = 16
}
security_groups = [
"${aws_security_group.k8s-worker.id}",
]
user_data = "${ data.template_file.k8s-worker.rendered }"
}
resource "aws_autoscaling_group" "worker" {
name = "k8s-aws_autoscaling_group"
desired_capacity = "1"
health_check_grace_period = 60
health_check_type = "EC2"
force_delete = true
launch_configuration = "${ aws_launch_configuration.worker.name }"
max_size = "8"
min_size = "1"
vpc_zone_identifier = ["${aws_subnet.k8s-public.id}"]
tag {
key = "Name"
value = "k8s-worker"
propagate_at_launch = true
}
}
resource "aws_autoscaling_policy" "scale_up" {
name = "scale_up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.worker.name}"
}
resource "aws_autoscaling_policy" "scale_down" {
name = "scale_down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.worker.name}"
}
resource "aws_cloudwatch_metric_alarm" "high_cpu" {
alarm_name = "high_cpu"
comparison_operator = "GreaterThanOrEqualToThreshold"
threshold = "50"
statistic = "Average"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
evaluation_periods = "2"
period = "60"
alarm_actions = ["${aws_autoscaling_policy.scale_up.arn }"]
dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.worker.name}"
}
}
resource "aws_cloudwatch_metric_alarm" "low_cpu" {
alarm_name = "low_cpu"
comparison_operator = "LessThanOrEqualToThreshold"
threshold = "20"
statistic = "Average"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
evaluation_periods = "2"
period = "60"
alarm_actions = ["${aws_autoscaling_policy.scale_down.arn }"]
dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.worker.name}"
}
}