Skip to content

Commit 9f8cab1

Browse files
Merge pull request #93 from max-rocket-internet/autoscaling_policies
Adding autoscaling setting, policy and documentation
2 parents 2e2dd0e + c37dc6d commit 9f8cab1

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1212
- add spot_price option to aws_launch_configuration
1313
- add enable_monitoring option to aws_launch_configuration
1414
- add t3 instance class settings
15+
- Added autoscaling policies into module that are optionally attached when enabled for a worker group. (by @max-rocket-internet)
1516

1617
### Changed
1718

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ module "eks" {
3131
}
3232
```
3333

34+
## Other documentation
35+
36+
- [Autoscaling](docs/autoscaling.md): How to enabled worker node autoscaling.
37+
3438
## Release schedule
3539

3640
Generally the maintainers will try to release the module once every 2 weeks to

docs/autoscaling.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Autoscaling
2+
3+
Autoscaling of worker nodes can be easily enabled by setting the `autoscaling_enabled` variable to `true` for a worker group in the `worker_groups` map.
4+
This will add the required tags to the autoscaling group for the [cluster-autoscaler](https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler).
5+
6+
You will also need to install the cluster-autoscaler into your cluster. The easiest way to do this is with [helm](https://helm.sh/).
7+
8+
The [helm chart](https://github.com/helm/charts/tree/master/stable/cluster-autoscaler) for the cluster-autoscaler requires some specific settings to work in an EKS cluster. These settings are supplied via YAML values file when installing the helm chart. Here is an example values file:
9+
10+
```yaml
11+
rbac:
12+
create: true
13+
14+
sslCertPath: /etc/ssl/certs/ca-bundle.crt
15+
16+
autoDiscovery:
17+
clusterName: YOUR_CLUSTER_NAME
18+
enabled: true
19+
```
20+
21+
To install the chart, simply run helm with the `--values` option:
22+
23+
```
24+
helm install stable/cluster-autoscaler --values=path/to/your/values-file.yaml
25+
```

main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
* }
3333
* ```
3434
35+
* ## Other documentation
36+
*
37+
* - [Autoscaling](docs/autoscaling.md): How to enabled worker node autoscaling.
38+
3539
* ## Release schedule
3640
3741
* Generally the maintainers will try to release the module once every 2 weeks to

variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ variable "workers_group_defaults" {
9898
public_ip = false # Associate a public ip address with a worker
9999
kubelet_node_labels = "" # This string is passed directly to kubelet via --node-labels= if set. It should be comma delimited with no spaces. If left empty no --node-labels switch is added.
100100
subnets = "" # A comma delimited string of subnets to place the worker nodes in. i.e. subnet-123,subnet-456,subnet-789
101+
autoscaling_enabled = false # Sets whether policy and matching tags will be added to allow autoscaling.
101102
}
102103
}
103104

workers.tf

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ resource "aws_autoscaling_group" "workers" {
1111
list(
1212
map("key", "Name", "value", "${aws_eks_cluster.this.name}-${lookup(var.worker_groups[count.index], "name", count.index)}-eks_asg", "propagate_at_launch", true),
1313
map("key", "kubernetes.io/cluster/${aws_eks_cluster.this.name}", "value", "owned", "propagate_at_launch", true),
14+
map("key", "k8s.io/cluster-autoscaler/${lookup(var.worker_groups[count.index], "autoscaling_enabled", count.index) == 1 ? "enabled" : "disabled" }", "value", "true", "propagate_at_launch", false),
1415
),
1516
local.asg_tags)
1617
}"]
@@ -122,3 +123,56 @@ resource "null_resource" "tags_as_list_of_maps" {
122123
"propagate_at_launch", "true"
123124
)}"
124125
}
126+
127+
resource "aws_iam_role_policy_attachment" "workers_autoscaling" {
128+
policy_arn = "${aws_iam_policy.worker_autoscaling.arn}"
129+
role = "${aws_iam_role.workers.name}"
130+
}
131+
132+
resource "aws_iam_policy" "worker_autoscaling" {
133+
name_prefix = "eks-worker-autoscaling-${aws_eks_cluster.this.name}"
134+
description = "EKS worker node autoscaling policy for cluster ${aws_eks_cluster.this.name}"
135+
policy = "${data.aws_iam_policy_document.worker_autoscaling.json}"
136+
}
137+
138+
data "aws_iam_policy_document" "worker_autoscaling" {
139+
statement {
140+
sid = "eksWorkerAutoscalingAll"
141+
effect = "Allow"
142+
143+
actions = [
144+
"autoscaling:DescribeAutoScalingGroups",
145+
"autoscaling:DescribeAutoScalingInstances",
146+
"autoscaling:DescribeLaunchConfigurations",
147+
"autoscaling:DescribeTags",
148+
"autoscaling:GetAsgForInstance",
149+
]
150+
151+
resources = ["*"]
152+
}
153+
154+
statement {
155+
sid = "eksWorkerAutoscalingOwn"
156+
effect = "Allow"
157+
158+
actions = [
159+
"autoscaling:SetDesiredCapacity",
160+
"autoscaling:TerminateInstanceInAutoScalingGroup",
161+
"autoscaling:UpdateAutoScalingGroup",
162+
]
163+
164+
resources = ["*"]
165+
166+
condition {
167+
test = "StringEquals"
168+
variable = "autoscaling:ResourceTag/kubernetes.io/cluster/${aws_eks_cluster.this.name}"
169+
values = ["owned"]
170+
}
171+
172+
condition {
173+
test = "StringEquals"
174+
variable = "autoscaling:ResourceTag/k8s.io/cluster-autoscaler/enabled"
175+
values = ["true"]
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)