EKS 의 Nodegroup 가용 영역 배치 문제

LEE EUI JOO·2023년 4월 20일
0

terraform

목록 보기
2/2

문제

  • Terraform aws provider로 eks 노드 그룹들을 provisioning 후, 모든 노드 그룹들이 단일 가용영역 ap-northeast-2a 혹은 ap-northeast-2c 로 몰리는 문제가 발생


Environment

  • Terraform Version
    • 1.4.0
  • aws CLI Version
    • aws-cli/2.11.2
    • Python/3.11.2
    • Linux/5.4.0-146-generic exe
    • /x86_64.ubuntu.20 prompt/off
  • aws provider
required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.47.0"
    }

문제 발생한 Terraform Code

provider "aws" {
  region = var.region
  shared_credentials_files = ["$HOME/.aws/credentials"]
  profile = "default"
}

# data "aws_availability_zones" "available" {}

locals {
  cluster_name = "joo-eks" //수정
}

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.19.0"

  name = "joo-vpc"

  cidr = "20.0.0.0/16"
  # azs  = slice(data.aws_availability_zones.available.names, 0, 3)
  azs = ["ap-northeast-2a", "ap-northeast-2c"]

  private_subnets = ["20.0.1.0/24", "20.0.2.0/24"]
  public_subnets  = ["20.0.4.0/24", "20.0.5.0/24"]

  enable_nat_gateway   = true
  single_nat_gateway   = true
  enable_dns_hostnames = true

  public_subnet_tags = {
    "kubernetes.io/cluster/${local.cluster_name}" = "shared"
    "kubernetes.io/role/elb"                      = 1
  }

  private_subnet_tags = {
    "kubernetes.io/cluster/${local.cluster_name}" = "shared"
    "kubernetes.io/role/internal-elb"             = 1
  }
}

module "s3_bucket" {
  source = "terraform-aws-modules/s3-bucket/aws"

  bucket = "test.s3.bucket.for.joo.team"
  acl    = "private"

  versioning = {
    enabled = true
  }

}

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "19.10.0"

  cluster_name    = local.cluster_name
  cluster_version = "1.24"


  vpc_id                         = module.vpc.vpc_id
  subnet_ids                     = module.vpc.private_subnets
  cluster_endpoint_public_access = true

  eks_managed_node_group_defaults = {
    ami_type = "AL2_x86_64"
  }

  eks_managed_node_groups = {
    one = {
      name = "joo-eks-node-group-1"

      instance_types = ["t2.medium"]

      min_size     = 1
      max_size     = 2
      desired_size = 1
      subnets = [module.vpc.private_subnets[0]]
    }

    two = {
      name = "joo-eks-node-group-2"

      instance_types = ["t2.medium"]

      min_size     = 1
      max_size     = 2
      desired_size = 1
      subnets = [module.vpc.private_subnets[1]]
    }
  }
}
  • 아래 코드 처럼 vpc 모듈의 private_subnet 을 참조하여 첫번째 배열이면 20.0.1.0/24, 두번째 배열이면 20.0.2.0/24 를 가져가게 설정해 놓았지만 막상 프로비저닝이 완료된 후에는 한쪽의 가용영역으로 몰리는 문제가 발생했다.
eks_managed_node_groups = {
    one = {
      name = "joo-eks-node-group-1"

      instance_types = ["t2.medium"]

      min_size     = 1
      max_size     = 2
      desired_size = 1
      subnets = [module.vpc.private_subnets[0]]
    }

    two = {
      name = "joo-eks-node-group-2"

      instance_types = ["t2.medium"]

      min_size     = 1
      max_size     = 2
      desired_size = 1
      subnets = [module.vpc.private_subnets[1]]
    }

해결 방법

  • azs 및 private_subnets 변수를 매핑하여 코드를 수정
  • locals 블록에 서브넷을 매핑
locals {
  cluster_name = "joo-eks" //수정
  az_subnet_mapping = {
    "ap-northeast-2a" = "20.0.1.0/24"
    "ap-northeast-2c" = "20.0.2.0/24"
  }
}
  • eks_managed_node_groups 코드 수정
eks_managed_node_groups = {
    one = {
      name = "joo-eks-node-group-1"

      instance_types = ["t2.medium"]

      min_size     = 1
      max_size     = 2
      desired_size = 1
      subnets = [local.az_subnet_mapping["ap-northeast-2a"]]
    }

    two = {
      name = "joo-eks-node-group-2"

      instance_types = ["t2.medium"]

      min_size     = 1
      max_size     = 2
      desired_size = 1
      subnets = [local.az_subnet_mapping["ap-northeast-2c"]]
    }
  }
  • 각 가용영역에 Provisioning 완료


locals

  • Terraform 스크립트에서 사용할 수 있는 로컬 변수를 정의하는 데 사용되는 블록이다.

  • 로컬 변수는 중복을 줄이고 코드를 재사용하기 쉽게 만드는 데 도움이 된다

  • locals 블록 안에서 정의된 변수는 local.변수명 형식으로 참조할 수 있다.


그러면, locals 의 변수와 variables.tf 파일에서 정의하는 변수 차이는 무엇일까?

  • variables.tf 파일과 locals 블록은 Terraform 스크립트에서 변수를 정의하고 관리하는 두 가지 다른 방법이다.

1. variables.tf 파일

  • variables.tf 파일은 입력 변수를 정의하는 데 사용되고, 입력 변수는 Terraform 구성 외부에서 전달되는 값이다.

  • 이를 사용하면 사용자나 다른 Terraform 모듈로부터 값을 받아와 동적으로 구성할 수 있다는 장점이 있다.

  • 입력 변수는 var.변수명 형식으로 참조하며 variables.tf 파일에 아래와 같이 변수를 정의할 수 있다.

variable "region" {
  description = "The AWS region to deploy the resources."
  default     = "us-west-2"
}

2. locals 블록

  • Terraform 구성 내부에서 사용되는 로컬 변수를 정의하는 데 사용된다

  • 로컬 변수는 중복을 줄이고 코드의 가독성과 재사용성을 높이는데 도움이 될 수 있으며, 로컬 변수는 local.변수명 형식으로 참조가능하다.

  • 예를 들어, locals 블록에 아래와 같이 변수를 정의할 수 있다.

locals {
  cluster_name = "joo-eks"
}
profile
무럭무럭 자라볼까

0개의 댓글