Skip to content

Commit 3ecb9e4

Browse files
committed
Merge branch 'use-asg'
2 parents 001da65 + 2de4903 commit 3ecb9e4

File tree

9 files changed

+152
-64
lines changed

9 files changed

+152
-64
lines changed

data.tf

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,3 @@ data "aws_subnet" "private" {
5454
id = each.key
5555
}
5656

57-
locals {
58-
cluster_id = var.cluster_id
59-
master_count = 1
60-
node_count = var.node_count
61-
master_ami = data.aws_ami.amz2-x86_64.id
62-
node_ami = var.node_instance_arch == "arm64" ? data.aws_ami.amz2-arm64.id : data.aws_ami.amz2-x86_64.id
63-
master_vol = 50
64-
node_vol = 50
65-
private_subnets = var.private_subnets
66-
}

examples/k3s-in-existing-vpc/main.tf

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
provider "aws" {
22
region = "ap-southeast-1" # change this
33
profile = "default" # can be changed to other profile
4+
5+
ignore_tags {
6+
# required to prevent tag from messing terraform state
7+
key_prefixes = ["kubernetes.io"]
8+
}
49
}
510

611
data "aws_region" "current" {}
@@ -30,26 +35,25 @@ module "subnets" {
3035
}
3136

3237
module "k3s-in-existing-vpc" {
33-
# source = "../.."
34-
source = "sagittaros/private-cloud/k3s"
38+
source = "../.."
39+
# source = "sagittaros/private-cloud/k3s"
3540

36-
# context
37-
name = "kay3s"
38-
stage = "staging"
41+
# main
42+
cluster_id = "k3s-in-existing-vpc"
3943

4044
# networking
41-
region = data.aws_region.current.name
42-
availability_zones = data.aws_availability_zones.all.names
43-
vpc_id = data.aws_vpc.this.id
44-
public_subnets = module.subnets.public_subnet_ids
45-
private_subnets = module.subnets.private_subnet_ids
46-
create_discovery_tags = true
45+
region = data.aws_region.current.name
46+
availability_zones = data.aws_availability_zones.all.names
47+
vpc_id = data.aws_vpc.this.id
48+
public_subnets = module.subnets.public_subnet_ids
49+
private_subnets = module.subnets.private_subnet_ids
4750

4851
# node instances
4952
master_instance_type = "t3a.small"
5053
node_count = 3
5154
node_instance_arch = "x86_64"
52-
node_instance_type = "t3a.small"
55+
node_instance_types = ["t3a.small", "t3.small"]
56+
on_demand_percentage = 0 # all spot instances
5357

5458
# # run on Arm architecture, where g == ARM-based graviton
5559
# node_instance_arch = "arm64"

examples/k3s-in-new-vpc/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ module "k3s-in-new-vpc" {
5858
master_instance_type = "t3a.small"
5959
node_count = 3
6060
node_instance_arch = "x86_64"
61-
node_instance_type = "t3a.small"
61+
node_instance_types = ["t3a.small", "t3.small"]
62+
on_demand_percentage = 0 # all spot instances
6263

6364
# # run on Arm architecture, where g == ARM-based graviton
6465
# node_instance_arch = "arm64"

extras/ssm_vpc_endpoints/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ variable "private_subnets" {
1919
type = list(any)
2020
description = "List of private subnet ids to use. If blank, infer from VPC"
2121
}
22+

k3s_master.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ resource "aws_instance" "k3s_master" {
7979
user_data = data.cloudinit_config.k3s_master.rendered
8080

8181
tags = {
82+
"Name" = "${local.cluster_id}-master",
8283
"KubernetesCluster" = local.cluster_id,
8384
"kubernetes.io/cluster/${local.cluster_id}" = "owned"
8485
"k3s-role" = "master"

k3s_node.tf

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,42 +42,3 @@ data "cloudinit_config" "k3s_node" {
4242
}
4343
}
4444

45-
46-
resource "aws_instance" "k3s_node" {
47-
count = local.node_count
48-
ami = local.node_ami
49-
instance_type = var.node_instance_type
50-
iam_instance_profile = aws_iam_instance_profile.k3s_node.name
51-
52-
# spread instances across subnets
53-
subnet_id = element(local.private_subnets, count.index)
54-
associate_public_ip_address = false
55-
56-
vpc_security_group_ids = concat([
57-
aws_security_group.self.id,
58-
aws_security_group.node_ports.id,
59-
aws_security_group.egress.id
60-
], var.extra_node_security_groups)
61-
62-
root_block_device {
63-
volume_size = local.node_vol
64-
encrypted = true
65-
}
66-
67-
user_data = data.cloudinit_config.k3s_node.rendered
68-
69-
tags = {
70-
"KubernetesCluster" = local.cluster_id
71-
"kubernetes.io/cluster/${local.cluster_id}" = "owned"
72-
"k3s-role" = "node"
73-
}
74-
75-
lifecycle {
76-
ignore_changes = [
77-
ami, # new ami changes by amazon should not affect change to this instance
78-
user_data, # https://github.com/hashicorp/terraform-provider-aws/issues/4954
79-
tags,
80-
volume_tags,
81-
]
82-
}
83-
}

k3s_node_pool.tf

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
resource "aws_autoscaling_group" "node_pool" {
2+
name_prefix = local.cluster_id
3+
4+
desired_capacity = local.node_count
5+
min_size = local.node_count
6+
max_size = local.node_count
7+
default_cooldown = local.asg_default_cooldown
8+
health_check_grace_period = local.asg_health_check_grace_period
9+
10+
# network
11+
vpc_zone_identifier = local.private_subnets
12+
13+
# template
14+
mixed_instances_policy {
15+
launch_template {
16+
launch_template_specification {
17+
launch_template_id = aws_launch_template.node_pool.id
18+
version = local.asg_launch_template_version
19+
}
20+
21+
dynamic "override" {
22+
for_each = local.asg_equiv_instance_types
23+
content {
24+
instance_type = override.value
25+
}
26+
}
27+
}
28+
29+
# Refer following doc for more parameters
30+
# https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_InstancesDistribution.html
31+
instances_distribution {
32+
on_demand_percentage_above_base_capacity = local.asg_on_demand_percentage
33+
}
34+
}
35+
36+
target_group_arns = local.asg_target_group_arns
37+
38+
lifecycle {
39+
create_before_destroy = true
40+
ignore_changes = [tag]
41+
}
42+
43+
dynamic "tag" {
44+
for_each = local.node_pool_tags
45+
46+
content {
47+
key = tag.key
48+
value = tag.value
49+
propagate_at_launch = true
50+
}
51+
}
52+
}
53+
54+
resource "aws_launch_template" "node_pool" {
55+
name_prefix = local.cluster_id
56+
image_id = local.node_ami
57+
user_data = data.cloudinit_config.k3s_node.rendered
58+
59+
iam_instance_profile {
60+
arn = aws_iam_instance_profile.k3s_node.arn
61+
}
62+
63+
instance_type = local.asg_base_instance_type
64+
65+
block_device_mappings {
66+
device_name = local.node_root_device_name
67+
ebs {
68+
volume_size = local.node_vol
69+
encrypted = true
70+
}
71+
}
72+
73+
network_interfaces {
74+
associate_public_ip_address = false
75+
security_groups = concat([
76+
aws_security_group.self.id,
77+
aws_security_group.node_ports.id,
78+
aws_security_group.egress.id
79+
], var.extra_node_security_groups)
80+
}
81+
82+
tags = {
83+
Cluster = local.cluster_id
84+
}
85+
86+
lifecycle {
87+
create_before_destroy = true
88+
}
89+
}

locals.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
locals {
2+
cluster_id = var.cluster_id
3+
master_count = 1
4+
node_count = var.node_count
5+
master_ami = data.aws_ami.amz2-x86_64.id
6+
node_ami = var.node_instance_arch == "arm64" ? data.aws_ami.amz2-arm64.id : data.aws_ami.amz2-x86_64.id
7+
node_root_device_name = var.node_instance_arch == "arm64" ? data.aws_ami.amz2-arm64.root_device_name : data.aws_ami.amz2-x86_64.root_device_name
8+
master_vol = 50
9+
node_vol = 50
10+
private_subnets = var.private_subnets
11+
12+
# ASG configuration
13+
asg_launch_template_version = "$Latest"
14+
asg_target_group_arns = var.target_group_arns
15+
asg_default_cooldown = 30
16+
asg_health_check_grace_period = 30
17+
asg_on_demand_percentage = var.on_demand_percentage
18+
asg_base_instance_type = element(var.node_instance_types, 0)
19+
asg_equiv_instance_types = slice(var.node_instance_types, 1, length(var.node_instance_types))
20+
node_pool_tags = {
21+
"Name" = "${var.cluster_id}-nodes"
22+
"KubernetesCluster" = var.cluster_id
23+
"kubernetes.io/cluster/${var.cluster_id}" = "owned"
24+
"k3s-role" = "node"
25+
}
26+
27+
}

variables.tf

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ variable "node_instance_arch" {
5656
default = "arm64"
5757
}
5858

59-
variable "node_instance_type" {
59+
variable "node_instance_types" {
60+
type = list(string)
6061
description = "Instance size for k3s instance, Must match architecture (codename a=arm, g=graviton)"
61-
default = "r6g.medium" # 1vcpu, 4GB memory
62+
default = [
63+
"r6g.medium", # 1vcpu, 4GB memory
64+
]
6265
}
6366

6467
variable "extra_master_security_groups" {
@@ -73,3 +76,14 @@ variable "extra_node_security_groups" {
7376
description = "Additional security groups to attach to k3s agent instances"
7477
}
7578

79+
variable "on_demand_percentage" {
80+
default = 100
81+
type = number
82+
description = "Percentage(ratio) of on-demand against spot instances (0-100)"
83+
}
84+
85+
variable "target_group_arns" {
86+
type = list(string)
87+
description = "Attach worker nodes to a list of target groups. (Needed for exposure)"
88+
default = []
89+
}

0 commit comments

Comments
 (0)