Skip to content

Commit 6072fd9

Browse files
authored
enhancement: Added support for EKS prefix delegation, allowing nodes to have many more IPs (17 -> 110), also change how the eks module accepts node group parameters. (#59)
* enhancement: Added support for EKS prefix delegation, allowing nodes to have many more IPs (17 -> 110), also change how the eks module accepts node group parameters. breaking change: Switched back to using the worker security group instead of the "cluster primary" security group - we were only using it because we weren't creating a custom launch template and with this change we will be. If you are trying to upgrade, this map require tmeporarily adding a security group rule to allow your old node groups to access the db, bringing up new node groups with the new configuration, then removing your old node groups. * docs: Clarified node group config
1 parent a36cc77 commit 6072fd9

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

modules/eks/README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ See the necessary versions for each EKS version here:
1414

1515
[https://docs.aws.amazon.com/eks/latest/userguide/managing-kube-proxy.html](https://docs.aws.amazon.com/eks/latest/userguide/managing-kube-proxy.html)
1616

17+
*Node group configuration schema:*
18+
```
19+
{
20+
<group name>: {
21+
instance_types = list(string) - List of instance types to use for nodes in the node group. In order of preference. Instance types in a group should be similar in resources.
22+
asg_min_size = string (default: "1") - Smallest size of this node group in instances.
23+
asg_max_size = string (default: "3") - Largest size of this node group in instances.
24+
use_spot_instances = bool (default: false) - If true, use spot instances to save cost.
25+
ami_type = string (default: "AL2_x86_64") - The type of AMI to use. Other possibilities are AL2_x86_64_GPU for gpu instances or AL2_ARM_64 for ARM instances
26+
use_large_ip_range = bool (default: true) - If true, enable the "prefix delegation" feature of EKS. This will create a custom launch template for each node group.
27+
node_ip_limit = int (default: 110) - If using prefix delegation, the max that can be used per node. 110 is the limit for all but the largest instance types.
28+
},
29+
...
30+
}
31+
```
32+
1733
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
1834
## Requirements
1935

@@ -27,6 +43,7 @@ See the necessary versions for each EKS version here:
2743
| Name | Version |
2844
|------|---------|
2945
| aws | >= 3.37.0 |
46+
| null | n/a |
3047

3148
## Inputs
3249

@@ -37,7 +54,7 @@ See the necessary versions for each EKS version here:
3754
| addon\_vpc\_cni\_version | Version of the VPC CNI to install. If empty you will need to upgrade the CNI yourself during a cluster version upgrade | `string` | `""` | no |
3855
| cluster\_name | Name to be given to the EKS cluster | `any` | n/a | yes |
3956
| cluster\_version | EKS cluster version number to use. Incrementing this will start a cluster upgrade | `any` | n/a | yes |
40-
| eks\_node\_groups | Map of maps of EKS node group config where keys are node group names | <pre>map(object({<br> instance_types = list(string)<br> asg_min_size = string<br> asg_max_size = string<br> use_spot_instances = bool<br> ami_type = string<br> }))</pre> | n/a | yes |
57+
| eks\_node\_groups | Map of maps of EKS node group config where keys are node group names. See the readme for details. | `any` | n/a | yes |
4158
| environment | The environment (stage/prod) | `any` | n/a | yes |
4259
| iam\_account\_id | Account ID of the current IAM user | `any` | n/a | yes |
4360
| iam\_role\_mapping | List of mappings of AWS Roles to Kubernetes Groups | <pre>list(object({<br> iam_role_arn = string<br> k8s_role_name = string<br> k8s_groups = list(string)<br> }))</pre> | n/a | yes |
@@ -52,6 +69,6 @@ See the necessary versions for each EKS version here:
5269
| cluster\_id | Identifier of the EKS cluster |
5370
| worker\_iam\_role\_arn | The ARN of the EKS worker IAM role |
5471
| worker\_iam\_role\_name | The name of the EKS worker IAM role |
55-
| worker\_security\_group\_id | The security group of the EKS cluster |
72+
| worker\_security\_group\_id | The security group of the EKS workers |
5673

5774
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/eks/main.tf

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@ data "aws_eks_cluster_auth" "cluster" {
88
}
99

1010
locals {
11+
k8s_exec_context = "--context ${data.aws_eks_cluster.cluster.name} --server ${data.aws_eks_cluster.cluster.endpoint}"
12+
1113
# Map this module config to the upstream module config
1214
eks_node_group_config = { for n, config in var.eks_node_groups :
1315
n => {
1416
name = "${var.cluster_name}-${n}"
1517

16-
desired_capacity = config.asg_min_size
17-
max_capacity = config.asg_max_size
18-
min_capacity = config.asg_min_size
18+
desired_capacity = lookup(config, "asg_min_size", 1)
19+
max_capacity = lookup(config, "asg_max_size", 3)
20+
min_capacity = lookup(config, "asg_min_size", 1)
21+
22+
create_launch_template = lookup(config, "use_large_ip_range", true)
23+
launch_template_version = "1"
24+
# Hopefully temporary, as there is an issue with the upstream module that leads to this value being non-deterministic with the default of "$Latest"
25+
# See https://github.com/terraform-aws-modules/terraform-aws-eks/pull/1447
1926

20-
ami_type = config.ami_type
21-
instance_types = config.instance_types
22-
capacity_type = config.use_spot_instances ? "SPOT" : "ON_DEMAND"
23-
disk_size = 100
27+
ami_type = lookup(config, "ami_type", "AL2_x86_64")
28+
instance_types = lookup(config, "instance_types", [])
29+
capacity_type = lookup(config, "use_spot_instances", false) ? "SPOT" : "ON_DEMAND"
30+
disk_size = 100
31+
kubelet_extra_args = lookup(config, "use_large_ip_range", true) ? "--max-pods=${lookup(config, "node_ip_limit", 110)}" : ""
2432

2533
k8s_labels = {
2634
Environment = var.environment
@@ -101,3 +109,19 @@ resource "aws_eks_addon" "coredns" {
101109
resolve_conflicts = "OVERWRITE"
102110
addon_version = var.addon_coredns_version
103111
}
112+
113+
# Enable prefix delegation - this will enable many more IPs to be allocated per-node.
114+
# See https://docs.aws.amazon.com/eks/latest/userguide/cni-increase-ip-addresses.html
115+
resource "null_resource" "enable_prefix_delegation" {
116+
count = var.addon_vpc_cni_version == "" ? 0 : 1
117+
118+
triggers = {
119+
manifest_sha1 = sha1(var.addon_vpc_cni_version)
120+
}
121+
122+
provisioner "local-exec" {
123+
command = "kubectl set env daemonset aws-node ${local.k8s_exec_context} -n kube-system ENABLE_PREFIX_DELEGATION=true WARM_PREFIX_TARGET=1"
124+
}
125+
126+
depends_on = [aws_eks_addon.vpc_cni]
127+
}

modules/eks/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ output "worker_iam_role_name" {
1414
}
1515

1616
output "worker_security_group_id" {
17-
description = "The security group of the EKS cluster"
18-
value = module.eks.cluster_primary_security_group_id
17+
description = "The security group of the EKS workers"
18+
value = module.eks.worker_security_group_id
1919
}

modules/eks/variables.tf

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ variable "vpc_id" {
2424
}
2525

2626
variable "eks_node_groups" {
27-
type = map(object({
28-
instance_types = list(string)
29-
asg_min_size = string
30-
asg_max_size = string
31-
use_spot_instances = bool
32-
ami_type = string
33-
}))
34-
description = "Map of maps of EKS node group config where keys are node group names"
27+
type = any
28+
description = "Map of maps of EKS node group config where keys are node group names. See the readme for details."
3529
}
3630

3731
variable "iam_account_id" {

0 commit comments

Comments
 (0)