Infra
수정·

[AWS] Terraform-aws-modules를 통한 EKS 구축 후 ALB 오작동 문제

by Dohoon Kim · 23년 04월 13일 09:00:00

문제 상황

회사에서 서비스를 위해 API 서버를 만든 뒤, AWS를 이용하여 서비스를 하기로 하였다. 현재 회사의 AWS 리소스들은 관리가 전혀 되고 있지 않은 상태이다보니, 서비스 레벨의 인프라는 Terraform 을 이용해 관리할 필요성이 있어 이를 기반으로 EKS 클러스터를 구축하는 작업을 진행하였다.

terraform-aws-modules/eks 모듈을 이용하여 EKS 클러스터를 구축하고 난 뒤, eks_managed_node_groups 을 지정하여 노드 그룹을 배포한 뒤 Kubernetes Manifest 파일들을 작성하여 디플로이먼트, 서비스 연결을 확인 한 뒤 최종적으로 Ingress 를 생성하여 AWS Application Load balancer 를 통해 연결을 시도하였으나 계속해서 503 에러가 뜨는 것을 확인하였다.

에러 메시지

$ kubectl logs <ingress-controller-name> -n kube-system
{"level":"error","ts":1643107355.1327415,"logger":"controller-runtime.manager.controller.targetGroupBinding","msg":"Reconciler error","reconciler group":"elbv2.k8s.aws","reconciler kind":"TargetGroupBinding","name":"k8s-monitor-promethe-e1f79f04c5","namespace":"monitor","error":"expect exactly one securityGroup tagged with kubernetes.io/cluster/dev for eni eni-0b6ed10e3b4d8b5c1, got: [sg-0b21ca401c50b3bad sg-0d450bf11239af6a2]"}

정확히 내가 본 메시지를 가져온 것은 아니고, 해결 방법을 찾기 위해 검색을 하던 중 거의 똑같은 메시지를 가져왔다. 핵심은 "error":"expect exactly one securityGroup tagged with kubernetes.io/cluster/dev for eni eni-0b6ed10e3b4d8b5c1, got: [sg-0b21ca401c50b3bad sg-0d450bf11239af6a2]" 이 부분인데 terraform-aws-modules/eks 내부에서 무언가 잘못하여 Security Group 을 여러개 생성해버려서 Ingress Controller 가 라우팅을 정상적으로 하지 못하는 상황이라고 판단이 되었다.

일반적으로 AWS Console 에서 직접 EKS 를 구축할 경우 "kubernetes.io/cluster/${local.cluster_name}" = "shared" "kubernetes.io/role/elb" = "1" 이런 태그들을 subnet, vpc 등에 주게 되는데 security group 에도 마찬가지로 태그를 지정해주어야 한다.

다음은 AWS 공식 가이드 부분이다. imageURL

Terraform Code



module "eks" {
  source = "terraform-aws-modules/eks/aws"
  cluster_name = local.cluster_name
  cluster_version = local.cluster_version

  cluster_endpoint_public_access = true

  cluster_addons = {
    coredns = {
      most_recent = true
    }

    kube-proxy = {
      most_recent = true
    }

    vpc-cni = {
      most_recent = true
    }
  }

  manage_aws_auth_configmap = true

  vpc_id = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets

  eks_managed_node_groups = {
    nginx_node_group = {
      min_size = 1
      max_size = 4
      desired_size = 1
      instance_type = ["t3.medium"]
    },

    nginx_multi_node_group = {
      min_size = 2
      max_size = 4
      desired_size = 2
      instance_type = ["t3.medium"]
    },
  }
}

위 코드를 통해 생성한 EKS 클러스터의 경우 아래와 같이 불필요한 Security Group 이 생긴 것을 확인할 수 있었다. imageURL

imageURL

그림과 코드상의 리소스 네임이 정확하게 일치하지 않지만, 같은 현상을 목격할 수 있을것이다. eks-cluster-sg-test-~ 시큐리티 그룹의 경우 EKS 자체에서 생성하는 SG 이며, test-node 의 경우 eks_managed_node_group 을 추가하면서 자체적으로 생성하는 것 같은데, 둘다 같은 kubernetes.io/cluster/test 테그를 가지고 있음을 확인할 수 있다.

해결 방안

위 테라폼 코드에 아래 한줄을 추가한다.

module "eks"{
  ...
  node_security_group_tags = {
     "kubernetes.io/cluster/${local.name}" = null
  }
}

위 코드를 통해 불필요하게 노드 그룹 태그로 인해 ALB 가 혼란을 격는 상황을 방지할 수 있다.