|
| 1 | +#--------------------------------------------------------------- |
| 2 | +# EKS Cluster |
| 3 | +#--------------------------------------------------------------- |
| 4 | + |
| 5 | +module "eks" { |
| 6 | + source = "terraform-aws-modules/eks/aws" |
| 7 | + version = "~> 19.15" |
| 8 | + |
| 9 | + cluster_name = local.name |
| 10 | + cluster_version = var.eks_cluster_version |
| 11 | + |
| 12 | + cluster_endpoint_public_access = true # if true, Your cluster API server is accessible from the internet. You can, optionally, limit the CIDR blocks that can access the public endpoint. |
| 13 | + |
| 14 | + vpc_id = module.vpc.vpc_id |
| 15 | + # Filtering only Secondary CIDR private subnets starting with "100.". Subnet IDs where the EKS Control Plane ENIs will be created |
| 16 | + subnet_ids = compact([for subnet_id, cidr_block in zipmap(module.vpc.private_subnets, module.vpc.private_subnets_cidr_blocks) : substr(cidr_block, 0, 4) == "100." ? subnet_id : null]) |
| 17 | + |
| 18 | + manage_aws_auth_configmap = true |
| 19 | + aws_auth_roles = [ |
| 20 | + { |
| 21 | + rolearn = module.eks_blueprints_addons.karpenter.iam_role_arn |
| 22 | + username = "system:node:{{EC2PrivateDNSName}}" |
| 23 | + groups = [ |
| 24 | + "system:bootstrappers", |
| 25 | + "system:nodes", |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + # Required for EMR on EKS virtual cluster |
| 30 | + rolearn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/AWSServiceRoleForAmazonEMRContainers" |
| 31 | + username = "emr-containers" |
| 32 | + groups = [] |
| 33 | + }, |
| 34 | + ] |
| 35 | + |
| 36 | + #--------------------------------------- |
| 37 | + # Note: This can further restricted to specific required for each Add-on and your application |
| 38 | + #--------------------------------------- |
| 39 | + # Extend cluster security group rules |
| 40 | + cluster_security_group_additional_rules = { |
| 41 | + ingress_nodes_ephemeral_ports_tcp = { |
| 42 | + description = "Nodes on ephemeral ports" |
| 43 | + protocol = "tcp" |
| 44 | + from_port = 1025 |
| 45 | + to_port = 65535 |
| 46 | + type = "ingress" |
| 47 | + source_node_security_group = true |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + # Extend node-to-node security group rules |
| 52 | + node_security_group_additional_rules = { |
| 53 | + ingress_self_all = { |
| 54 | + description = "Node to node all ports/protocols" |
| 55 | + protocol = "-1" |
| 56 | + from_port = 0 |
| 57 | + to_port = 0 |
| 58 | + type = "ingress" |
| 59 | + self = true |
| 60 | + } |
| 61 | + # Allows Control Plane Nodes to talk to Worker nodes on all ports. Added this to simplify the example and further avoid issues with Add-ons communication with Control plane. |
| 62 | + # This can be restricted further to specific port based on the requirement for each Add-on e.g., metrics-server 4443, spark-operator 8080, karpenter 8443 etc. |
| 63 | + # Change this according to your security requirements if needed |
| 64 | + ingress_cluster_to_node_all_traffic = { |
| 65 | + description = "Cluster API to Nodegroup all traffic" |
| 66 | + protocol = "-1" |
| 67 | + from_port = 0 |
| 68 | + to_port = 0 |
| 69 | + type = "ingress" |
| 70 | + source_cluster_security_group = true |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + eks_managed_node_group_defaults = { |
| 75 | + iam_role_additional_policies = { |
| 76 | + # Not required, but used in the example to access the nodes to inspect mounted volumes |
| 77 | + AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + eks_managed_node_groups = { |
| 82 | + # We recommend to have a MNG to place your critical workloads and add-ons |
| 83 | + # Then rely on Karpenter to scale your workloads |
| 84 | + # You can also make uses on nodeSelector and Taints/tolerations to spread workloads on MNG or Karpenter provisioners |
| 85 | + core_node_group = { |
| 86 | + name = "core-node-group" |
| 87 | + description = "EKS managed node group example launch template" |
| 88 | + # Filtering only Secondary CIDR private subnets starting with "100.". Subnet IDs where the nodes/node groups will be provisioned |
| 89 | + subnet_ids = compact([for subnet_id, cidr_block in zipmap(module.vpc.private_subnets, module.vpc.private_subnets_cidr_blocks) : substr(cidr_block, 0, 4) == "100." ? subnet_id : null]) |
| 90 | + |
| 91 | + min_size = 3 |
| 92 | + max_size = 9 |
| 93 | + desired_size = 3 |
| 94 | + |
| 95 | + ami_type = "AL2_x86_64" |
| 96 | + instance_types = ["m5.xlarge"] |
| 97 | + |
| 98 | + ebs_optimized = true |
| 99 | + block_device_mappings = { |
| 100 | + xvda = { |
| 101 | + device_name = "/dev/xvda" |
| 102 | + ebs = { |
| 103 | + volume_size = 100 |
| 104 | + volume_type = "gp3" |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + labels = { |
| 110 | + WorkerType = "ON_DEMAND" |
| 111 | + NodeGroupType = "core" |
| 112 | + "nvidia.com/gpu.deploy.operands" = false |
| 113 | + } |
| 114 | + |
| 115 | + tags = { |
| 116 | + Name = "core-node-grp", |
| 117 | + "karpenter.sh/discovery" = local.name |
| 118 | + } |
| 119 | + } |
| 120 | + spark_driver_ng = { |
| 121 | + name = "spark-driver-ng" |
| 122 | + description = "Spark managed node group for Driver pods with cpu and Ubuntu AMI" |
| 123 | + # Filtering only Secondary CIDR private subnets starting with "100.". Subnet IDs where the nodes/node groups will be provisioned |
| 124 | + subnet_ids = [element(compact([for subnet_id, cidr_block in zipmap(module.vpc.private_subnets, module.vpc.private_subnets_cidr_blocks) : substr(cidr_block, 0, 4) == "100." ? subnet_id : null]), 0)] |
| 125 | + |
| 126 | + # Ubuntu image for EKs Cluster 1.26 https://cloud-images.ubuntu.com/aws-eks/ |
| 127 | + ami_id = data.aws_ami.ubuntu.image_id |
| 128 | + |
| 129 | + # This will ensure the bootstrap user data is used to join the node |
| 130 | + # By default, EKS managed node groups will not append bootstrap script; |
| 131 | + # this adds it back in using the default template provided by the module |
| 132 | + # Note: this assumes the AMI provided is an EKS optimized AMI derivative |
| 133 | + enable_bootstrap_user_data = true |
| 134 | + |
| 135 | + min_size = 1 |
| 136 | + max_size = 8 |
| 137 | + desired_size = 1 |
| 138 | + |
| 139 | + force_update_version = true |
| 140 | + instance_types = ["m5.xlarge"] # 4 vCPU and 16GB |
| 141 | + |
| 142 | + ebs_optimized = true |
| 143 | + # This bloc device is used only for root volume. Adjust volume according to your size. |
| 144 | + # NOTE: Dont use this volume for Spark workloads |
| 145 | + block_device_mappings = { |
| 146 | + xvda = { |
| 147 | + device_name = "/dev/sda1" |
| 148 | + ebs = { |
| 149 | + volume_size = 100 |
| 150 | + volume_type = "gp3" |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + labels = { |
| 156 | + WorkerType = "ON_DEMAND" |
| 157 | + NodeGroupType = "spark-driver-ca" |
| 158 | + "nvidia.com/gpu.deploy.operands" = false |
| 159 | + } |
| 160 | + |
| 161 | + taints = [{ |
| 162 | + key = "spark-driver-ca" |
| 163 | + value = true |
| 164 | + effect = "NO_SCHEDULE" |
| 165 | + }] |
| 166 | + |
| 167 | + tags = { |
| 168 | + Name = "spark-driver-ca" |
| 169 | + } |
| 170 | + } |
| 171 | + spark_gpu_ng = { |
| 172 | + name = "spark-gpu-ng" |
| 173 | + description = "Spark managed Ubuntu GPU node group for executor pods with launch template" |
| 174 | + # Filtering only Secondary CIDR private subnets starting with "100.". Subnet IDs where the nodes/node groups will be provisioned |
| 175 | + subnet_ids = [element(compact([for subnet_id, cidr_block in zipmap(module.vpc.private_subnets, module.vpc.private_subnets_cidr_blocks) : substr(cidr_block, 0, 4) == "100." ? subnet_id : null]), 0)] |
| 176 | + |
| 177 | + # Ubuntu image for EKS Cluster 1.26 https://cloud-images.ubuntu.com/aws-eks/ |
| 178 | + ami_id = data.aws_ami.ubuntu.image_id |
| 179 | + |
| 180 | + # This will ensure the bootstrap user data is used to join the node |
| 181 | + # By default, EKS managed node groups will not append bootstrap script; |
| 182 | + # this adds it back in using the default template provided by the module |
| 183 | + # Note: this assumes the AMI provided is an EKS optimized AMI derivative |
| 184 | + enable_bootstrap_user_data = true |
| 185 | + |
| 186 | + # NVMe instance store volumes are automatically enumerated and assigned a device |
| 187 | + pre_bootstrap_user_data = <<-EOT |
| 188 | + echo "Running a custom user data script" |
| 189 | + set -ex |
| 190 | + apt-get update |
| 191 | + apt-get install -y nvme-cli mdadm xfsprogs |
| 192 | +
|
| 193 | + # Fetch the list of NVMe devices |
| 194 | + DEVICES=$(lsblk -d -o NAME | grep nvme) |
| 195 | +
|
| 196 | + DISK_ARRAY=() |
| 197 | +
|
| 198 | + for DEV in $DEVICES |
| 199 | + do |
| 200 | + # Exclude the root disk, /dev/nvme0n1, from the list of devices |
| 201 | + if [[ $${DEV} != "nvme0n1" ]]; then |
| 202 | + NVME_INFO=$(nvme id-ctrl --raw-binary "/dev/$${DEV}" | cut -c3073-3104 | tr -s ' ' | sed 's/ $//g') |
| 203 | + # Check if the device is Amazon EC2 NVMe Instance Storage |
| 204 | + if [[ $${NVME_INFO} == *"ephemeral"* ]]; then |
| 205 | + DISK_ARRAY+=("/dev/$${DEV}") |
| 206 | + fi |
| 207 | + fi |
| 208 | + done |
| 209 | +
|
| 210 | + DISK_COUNT=$${#DISK_ARRAY[@]} |
| 211 | +
|
| 212 | + if [ $${DISK_COUNT} -eq 0 ]; then |
| 213 | + echo "No NVMe SSD disks available. No further action needed." |
| 214 | + else |
| 215 | + if [ $${DISK_COUNT} -eq 1 ]; then |
| 216 | + TARGET_DEV=$${DISK_ARRAY[0]} |
| 217 | + mkfs.xfs $${TARGET_DEV} |
| 218 | + else |
| 219 | + mdadm --create --verbose /dev/md0 --level=0 --raid-devices=$${DISK_COUNT} $${DISK_ARRAY[@]} |
| 220 | + mkfs.xfs /dev/md0 |
| 221 | + TARGET_DEV=/dev/md0 |
| 222 | + fi |
| 223 | +
|
| 224 | + mkdir -p /local1 |
| 225 | + echo $${TARGET_DEV} /local1 xfs defaults,noatime 1 2 >> /etc/fstab |
| 226 | + mount -a |
| 227 | + /usr/bin/chown -hR +999:+1000 /local1 |
| 228 | + fi |
| 229 | + EOT |
| 230 | + |
| 231 | + min_size = 8 |
| 232 | + max_size = 8 |
| 233 | + desired_size = 8 |
| 234 | + |
| 235 | + capacity_type = "SPOT" |
| 236 | + instance_types = ["g5.2xlarge"] |
| 237 | + |
| 238 | + ebs_optimized = true |
| 239 | + # This block device is used only for root volume. Adjust volume according to your size. |
| 240 | + # NOTE: Don't use this volume for Spark workloads |
| 241 | + # Ubuntu uses /dev/sda1 as root volume |
| 242 | + block_device_mappings = { |
| 243 | + xvda = { |
| 244 | + device_name = "/dev/sda1" |
| 245 | + ebs = { |
| 246 | + volume_size = 100 |
| 247 | + volume_type = "gp3" |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + labels = { |
| 253 | + WorkerType = "SPOT" |
| 254 | + NodeGroupType = "spark-ubuntu-gpu-ca" |
| 255 | + } |
| 256 | + |
| 257 | + taints = [{ key = "spark-ubuntu-gpu-ca", value = true, effect = "NO_SCHEDULE" }] |
| 258 | + |
| 259 | + tags = { |
| 260 | + Name = "spark-ubuntu-gpu", |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + tags = local.tags |
| 266 | +} |
0 commit comments