Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement autoscaling in Kubernetes for Terraform EKS #238

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions deployment-size.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,44 @@
locals {
deployment_size = {
small = {
db = "db.r6g.large",
node_count = 2,
node_instance = "r6i.xlarge"
cache = "cache.m6g.large"
db = "db.r6g.large",
node_count = 2,
node_instance = "r6i.xlarge",
cache = "cache.m6g.large",
min_node_count = 1,
max_node_count = 3
},
medium = {
db = "db.r6g.xlarge",
node_count = 2,
node_instance = "r6i.xlarge"
cache = "cache.m6g.large"
db = "db.r6g.xlarge",
node_count = 2,
node_instance = "r6i.xlarge",
cache = "cache.m6g.large",
min_node_count = 2,
max_node_count = 5
},
large = {
db = "db.r6g.2xlarge",
node_count = 2,
node_instance = "r6i.2xlarge"
cache = "cache.m6g.xlarge"
db = "db.r6g.2xlarge",
node_count = 2,
node_instance = "r6i.2xlarge",
cache = "cache.m6g.xlarge",
min_node_count = 2,
max_node_count = 6
},
xlarge = {
db = "db.r6g.4xlarge",
node_count = 3,
node_instance = "r6i.2xlarge"
cache = "cache.m6g.xlarge"
db = "db.r6g.4xlarge",
node_count = 3,
node_instance = "r6i.2xlarge",
cache = "cache.m6g.xlarge",
min_node_count = 2,
max_node_count = 8
},
xxlarge = {
db = "db.r6g.8xlarge",
node_count = 3,
node_instance = "r6i.4xlarge"
cache = "cache.m6g.2xlarge"
db = "db.r6g.8xlarge",
node_count = 3,
node_instance = "r6i.4xlarge",
cache = "cache.m6g.2xlarge",
min_node_count = 3,
max_node_count = 10
}
}
}
3 changes: 2 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ module "app_eks" {

instance_types = try([local.deployment_size[var.size].node_instance], var.kubernetes_instance_types)
desired_capacity = try(local.deployment_size[var.size].node_count, var.kubernetes_node_count)
min_capacity = try(local.deployment_size[var.size].min_node_count, var.min_node_count)
max_capacity = try(local.deployment_size[var.size].max_node_count, var.max_node_count)
map_accounts = var.kubernetes_map_accounts
map_roles = var.kubernetes_map_roles
map_users = var.kubernetes_map_users
Expand Down Expand Up @@ -383,7 +385,6 @@ module "wandb" {
}
}
}

mysql = { install = false }
redis = { install = false }

Expand Down
38 changes: 19 additions & 19 deletions modules/app_eks/add-ons.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,43 @@ resource "aws_iam_role" "oidc" {
### add-ons for eks version 1.28

resource "aws_eks_addon" "aws_efs_csi_driver" {
depends_on = [
aws_eks_addon.vpc_cni
]
cluster_name = var.namespace
addon_name = "aws-efs-csi-driver"
addon_version = "v2.0.4-eksbuild.1"
resolve_conflicts = "OVERWRITE"
depends_on = [
aws_eks_addon.vpc_cni
]
cluster_name = var.namespace
addon_name = "aws-efs-csi-driver"
addon_version = "v2.0.4-eksbuild.1"
resolve_conflicts = "OVERWRITE"
}

resource "aws_eks_addon" "aws_ebs_csi_driver" {
depends_on = [
aws_eks_addon.vpc_cni
]
cluster_name = var.namespace
addon_name = "aws-ebs-csi-driver"
addon_version = "v1.31.0-eksbuild.1"
resolve_conflicts = "OVERWRITE"
cluster_name = var.namespace
addon_name = "aws-ebs-csi-driver"
addon_version = "v1.31.0-eksbuild.1"
resolve_conflicts = "OVERWRITE"
}

resource "aws_eks_addon" "coredns" {
depends_on = [
aws_eks_addon.vpc_cni
]
cluster_name = var.namespace
addon_name = "coredns"
addon_version = "v1.10.1-eksbuild.11"
resolve_conflicts = "OVERWRITE"
cluster_name = var.namespace
addon_name = "coredns"
addon_version = "v1.10.1-eksbuild.11"
resolve_conflicts = "OVERWRITE"
}

resource "aws_eks_addon" "kube_proxy" {
depends_on = [
aws_eks_addon.vpc_cni
]
cluster_name = var.namespace
addon_name = "kube-proxy"
addon_version = "v1.28.8-eksbuild.5"
resolve_conflicts = "OVERWRITE"
cluster_name = var.namespace
addon_name = "kube-proxy"
addon_version = "v1.28.8-eksbuild.5"
resolve_conflicts = "OVERWRITE"
}

resource "aws_eks_addon" "vpc_cni" {
Expand Down
4 changes: 2 additions & 2 deletions modules/app_eks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ module "eks" {
iam_role_arn = aws_iam_role.node.arn,
instance_types = var.instance_types,
kubelet_extra_args = local.system_reserved != "" ? "--system-reserved=${local.system_reserved}" : "",
max_capacity = 5,
max_capacity = var.max_capacity,
metadata_http_put_response_hop_limit = 2
metadata_http_tokens = "required",
min_capacity = var.desired_capacity,
min_capacity = var.min_capacity,
version = var.cluster_version,
}
}
Expand Down
12 changes: 12 additions & 0 deletions modules/app_eks/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ variable "desired_capacity" {
default = 2
}

variable "min_capacity" {
description = "Minimum number of worker nodes."
type = number
default = 1
}

variable "max_capacity" {
description = "Maximum number of worker nodes."
type = number
default = 6
}

variable "system_reserved_cpu_millicores" {
description = "(Optional) The amount of 'system-reserved' CPU millicores to pass to the kubelet. For example: 100. A value of -1 disables the flag."
type = number
Expand Down
4 changes: 2 additions & 2 deletions modules/app_lb/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ output "tg_app_arn" {
}

output "alb_name" {
value = aws_lb.alb.arn
value = aws_lb.alb.arn
}

output "nlb_security_group" {
value = var.enable_private_only_traffic? aws_security_group.inbound_private[0].id : null
value = var.enable_private_only_traffic ? aws_security_group.inbound_private[0].id : null
}
10 changes: 5 additions & 5 deletions modules/endpoint/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
resource "aws_vpc_endpoint" "default" {
vpc_id = var.network_id
service_name = var.service_name
vpc_endpoint_type = "Gateway"
auto_accept = true
route_table_ids = var.private_route_table_id
vpc_id = var.network_id
service_name = var.service_name
vpc_endpoint_type = "Gateway"
auto_accept = true
route_table_ids = var.private_route_table_id

policy = <<POLICY
{
Expand Down
10 changes: 5 additions & 5 deletions modules/endpoint/variables.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
variable "network_id" {
type = string
description = "ID of the network (VPC) where infrastructure resources will be deployed."
}
type = string
description = "ID of the network (VPC) where infrastructure resources will be deployed."
}

variable "private_route_table_id" {
type = list(string)
type = list(string)
description = "Private route table ID within the specified network (VPC) where resources will be deployed"
}

variable "service_name" {
type = string
type = string
description = "Name of the service or vpc endpoint"
}
2 changes: 1 addition & 1 deletion modules/iam_role/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ variable "namespace" {
}

variable "aws_iam_openid_connect_provider_url" {
type = string
type = string
}

variable "yace_sa_name" {
Expand Down
10 changes: 5 additions & 5 deletions modules/private_link/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
locals {
max_lb_name_length = 32 - length("-nlb")
lb_name_truncated = var.enable_private_only_traffic ? "${substr(var.namespace, 0, local.max_lb_name_length)}-private-link-nlb" : "${substr(var.namespace, 0, local.max_lb_name_length)}-nlb"
lb_name_truncated = var.enable_private_only_traffic ? "${substr(var.namespace, 0, local.max_lb_name_length)}-private-link-nlb" : "${substr(var.namespace, 0, local.max_lb_name_length)}-nlb"
}

resource "aws_lb" "nlb" {
Expand All @@ -9,10 +9,10 @@ resource "aws_lb" "nlb" {
load_balancer_type = "network"
subnets = var.network_private_subnets
enable_deletion_protection = var.deletion_protection
security_groups = var.enable_private_only_traffic ? [var.nlb_security_group] : []
lifecycle {
create_before_destroy = true
}
security_groups = var.enable_private_only_traffic ? [var.nlb_security_group] : []
lifecycle {
create_before_destroy = true
}
}

resource "aws_lb_target_group" "nlb" {
Expand Down
4 changes: 2 additions & 2 deletions modules/private_link/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ variable "vpc_id" {
}

variable "enable_private_only_traffic" {
type = bool
type = bool
}
variable "nlb_security_group" {
type = string
type = string
}
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,18 @@ variable "kubernetes_node_count" {
default = 2
}

variable "min_node_count" {
description = "Number of nodes"
type = number
default = 1
}

variable "max_node_count" {
description = "Number of nodes"
type = number
default = 6
}

variable "eks_policy_arns" {
type = list(string)
description = "Additional IAM policy to apply to the EKS cluster"
Expand Down
Loading