ECS multiple target groups ์„ค์ •์œผ๋กœ ALB response time ์ธก์ • ๊ฐœ์„ 

geunsuryu-wonderwall
  • #ECS
  • #multiple target groups
  • #ALB
  • #response time

๋“ค์–ด๊ฐ€๋ฉฐ

์•ˆ๋…•ํ•˜์„ธ์š”. ๋…ธ๋จธ์Šค์—์„œ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ๋Š” ์œ ๊ทผ์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

fromm ๋ฉ”์‹œ์ง€ ์•ฑ์€ ์•„ํ‹ฐ์ŠคํŠธ์™€ ํ”„๋ผ์ด๋น— ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.
์„œ๋น„์Šค์˜ ํŠน์„ฑ์ƒ ํ‰์†Œ์—๋Š” ์ž ์ž ํ•˜๋‹ค๊ฐ€๋„ ์•„ํ‹ฐ์ŠคํŠธ๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋ฉด ์ˆœ์‹๊ฐ„์— ํŠธ๋ž˜ํ”ฝ์ด ๋ชฐ๋ ค๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์„œ๋น„์Šค๊ฐ€ ์ ์  ๋” ์ปค์ง€๊ณ , ํŠธ๋ž˜ํ”ฝ์ด ๋Š˜์–ด๋‚˜๋ฉด์„œ ECS/ALB ๊ด€๋ จ ์ง€ํ‘œ ํ™•์ธ์ด ์ ์  ๋” ์ค‘์š”ํ•ด์ง€๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ชฐ๋ ค์˜ค๋Š” ํŠธ๋ž˜ํ”ฝ

fromm ์„œ๋น„์Šค ์ดˆ์ฐฝ๊ธฐ์—๋Š” AWS console์„ ํ†ตํ•ด ECS/ALB๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๊ณ , ์ฃผ์š” ์ง€ํ‘œ๋Š” CloudWatch ๋Œ€์‹œ๋ณด๋“œ์— ECS CPU/Memory ์‚ฌ์šฉ๋ฅ , ALB request count / response time ๋“ฑ์„ ์„ค์ •ํ•ด์„œ ๋ชจ๋‹ˆํ„ฐ๋ง์— ํ™œ์šฉํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

ํŠธ๋ž˜ํ”ฝ์ด ์ ์ฐจ ๋Š˜์–ด๋‚˜๊ณ , ๋Œ€ํ˜• ์•„ํ‹ฐ์ŠคํŠธ์˜ ์ž…์ฃผ๋กœ ์ธํ•ด ์ŠคํŒŒ์ดํฌ ์„ฑ์œผ๋กœ ๋ชฐ๋ ค์˜ฌ ํŠธ๋ž˜ํ”ฝ์— ๋Œ€ํ•œ ๋Œ€์‘์ด ์ข€ ๋” ์ค‘์š”ํ•ด์กŒ์Šต๋‹ˆ๋‹ค.
๊ด€๋ จํ•ด์„œ ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ , ECS/RDS ๋“ฑ์˜ ์ŠคํŽ™์„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
(๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•œ ๊ณผ์ •์€ ์ €ํฌ ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ์˜ ์ด์ „ ๊ธ€์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

์–ด๋–ค ์ง€ํ‘œ๋ฅผ ๋ด์•ผ ํ• ๊นŒ์š”?

ํŠธ๋ž˜ํ”ฝ์— ๋Œ€๋น„ํ•ด์„œ ECS ์„œ๋ฒ„๋ฅผ ๋Š˜๋ ค๋†“๊ธด ํ–ˆ์ง€๋งŒ, ์‹ค์ œ๋กœ ์‚ฌ์šฉ์ž๋“ค์˜ ์‚ฌ์šฉ์„ฑ์— ๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€, ์„œ๋ฒ„์˜ ๊ทœ๋ชจ๊ฐ€ ์ ์ •ํ•œ ์ˆ˜์ค€์ธ์ง€ ํŒ๋ณ„ํ•  ์ง€ํ‘œ๊ฐ€ ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค.
ECS CPU/Memory ์‚ฌ์šฉ๋ฅ ์„ 1์ฐจ๋กœ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ํ‘œ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค๋งŒ,
์‹ค์ œ ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ์›ํ™œํ•˜๊ฒŒ ์„œ๋น„์Šค๋˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ๋ณด๋ ค๋ฉด ALB์˜ response time์„ ์ถ”์ ํ•  ํ•„์š”๊ฐ€ ๋ณด์˜€์Šต๋‹ˆ๋‹ค.

ALB์˜ response time์€ ๊ทธ๋™์•ˆ ํฌ๊ฒŒ ์ฐธ๊ณ ํ•˜๊ณ  ์žˆ์ง€ ๋ชปํ–ˆ์—ˆ๋Š”๋ฐ,
๊ทธ ์ด์œ ๋Š” ์ด๋ฏธ์ง€๋‚˜ ๋™์˜์ƒ ๋“ฑ ์—…๋กœ๋“œ api๋‚˜ ๋ฒˆ์—ญ ํ˜น์€ App ๋งˆ์ผ“ ๋“ฑ ์™ธ๋ถ€ ์„œ๋น„์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋Š” api ๋“ค์ด response time ์ง€ํ‘œ์— ์„ž์—ฌ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

์‹ค์ œ ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์ด ๋Š๋ฆฌ๊ฒŒ ์‘๋‹ต์ด ๊ฐ„ ๊ฒƒ์ธ์ง€, ์—…๋กœ๋“œ api ๋“ฑ์œผ๋กœ ์ธํ•ด response time์ด ๋†’๊ฒŒ ๋‚˜์˜จ ๊ฒƒ์ธ์ง€๊ฐ€ ๊ตฌ๋ถ„์ด ์•ˆ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, endpoint ๋ณ„๋กœ response time์„ ๋ถ„๋ฆฌํ•ด์„œ ๋ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด์ „์— ์„ž์—ฌ์„œ ๋ณ„๋กœ ๋„์›€์ด ์•ˆ๋˜๋˜ response time.png

ECS์˜ multiple target groups ์„ค์ •

CloudWatch ๋Œ€์‹œ๋ณด๋“œ์˜ ALB response time์„ endpoint ๋ณ„๋กœ ๊ฐ๊ฐ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š”, ALB์˜ target group์„ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ์ œ์ผ ์ ํ•ฉํ•ด ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ALB์˜ target group์„ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•ด์„œ๋Š” ECS์— multiple target groups ์„ค์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์•ž์—์„œ ์ดˆ๋ฐ˜์— ECS๋ฅผ console๋กœ ์ง์ ‘ ์ƒ์„ฑํ–ˆ์—ˆ๋‹ค๊ณ  ๋ง์”€์„ ๋“œ๋ ธ๋Š”๋ฐ์š”. console ์ƒ์—์„œ๋Š” multiple target groups ์„ ์„ค์ •ํ•  ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค.
์ดˆ๋ฐ˜์—๋Š” console๋กœ ๊ด€๋ฆฌ๋ฅผ ํ•˜๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ์–ผ๋งˆ ์ „๋ถ€ํ„ฐ๋Š” terraform์„ ๋„์ž…ํ•ด์„œ ECS๋ฅผ ๊ด€๋ฆฌํ•ด ์˜ค๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

terraform์—์„œ๋Š” ์‰ฝ๊ฒŒ multiple target groups ์„ค์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

# target group ์„ค์ •
resource "aws_lb_target_group" "tg1" {
  name        = "tg1"
  port        = 80
  protocol    = "HTTP"
  vpc_id      = data.aws_vpc.env.id
  target_type = "ip"
}

resource "aws_lb_target_group" "tg2" {
  name        = "tg2"
  port        = 80
  protocol    = "HTTP"
  vpc_id      = data.aws_vpc.env.id
  target_type = "ip"
}

# listener ์„ค์ •
resource "aws_lb_listener" "lb_listener" {
  ...

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.tg1.arn
  }
}

resource "aws_lb_listener_rule" "listener_rule" {
  listener_arn = aws_lb_listener.lb_listener.arn
  priority     = 100

  action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.tg2.arn
  }

  condition {
    path_pattern {
      values = ["/upload", "/translation", "/market"]
    }
  }
}

# ecs service ์„ค์ •
resource "aws_ecs_service" "ecs_service" {
  ...

  load_balancer {
    target_group_arn = aws_lb_target_group.tg1.arn
    container_name   = "fromm_container"
    container_port   = 80
  }

  load_balancer {
    target_group_arn = aws_lb_target_group.tg2.arn
    container_name   = "fromm_container"
    container_port   = 80
  }
}

์—…๋กœ๋“œ api / ์™ธ๋ถ€ ์š”์ฒญ api ๋“ฑ์˜ endpoint๋ฅผ ๋ณ„๋„์˜ target group์œผ๋กœ ์„ค์ •ํ•˜๊ณ , ๋‚˜๋จธ์ง€ ์„œ๋น„์Šค์˜ ์ฃผ์š” api endpoint ๋“ค์€ ๋˜ ๋‹ค๋ฅธ target group์œผ๋กœ ๋ฌถ์–ด์„œ, target group๋ณ„ response time์„ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์„ค์ • ์ดํ›„ response time.png

์ด์ œ๋Š” ALB์˜ request count ์ถ”์ด๋ฅผ ๋ณด๋ฉด์„œ, ํŠธ๋ž˜ํ”ฝ์ด ๋ชฐ๋ ธ์„ ๋•Œ ECS CPU ์‚ฌ์šฉ๋ฅ  / ALB response time ๋“ฑ์„ ํ™•์ธํ•˜๋ฉด์„œ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ์กฐ๊ธˆ ๊ณผํ•˜๊ฒŒ ์ ์šฉํ–ˆ๋˜ ์„œ๋ฒ„ ๊ทœ๋ชจ๋„ ์ ์ • ์ˆ˜์ค€์œผ๋กœ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๊ณ , ๊ทธ์— ๋”ฐ๋ฅธ ๋น„์šฉ ์ ˆ๊ฐ์—๋„ ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋งˆ๋ฌด๋ฆฌ

๋ชจ๋‹ˆํ„ฐ๋ง์€ ์•„์ง ๊ฑธ์Œ๋งˆ ์ˆ˜์ค€์ด์ง€๋งŒ, ์ ์  ๋” ์ข‹์€ ๋ฐฉํ–ฅ์„ ์ฐพ์•„๊ฐ€๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์šด์šฉํ•˜๋ฉด์„œ ๋งŽ์€ ๋„์›€์„ ์ฃผ๊ณ  ๊ณ„์‹  ๊ฐœ๋ฐœํŒ€ ๋™๋ฃŒ๋“ค์—๊ฒŒ ์ด ๊ธ€์„ ๋นŒ์–ด ๊ฐ์‚ฌ์˜ ๋งˆ์Œ์„ ์ „ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
๋๊นŒ์ง€ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~ ๐Ÿ˜†

โ† ๋ชฉ๋ก์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ

Art Changes Life

๋…ธ๋จธ์Šค์™€ ํ•จ๊ป˜ ์—”ํ„ฐํ…Œํฌ ์‚ฐ์—…์„ ํ˜์‹ ํ•ด๋‚˜๊ฐˆ ๋ฉค๋ฒ„๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

์ฑ„์šฉ ์ค‘์ธ ๊ณต๊ณ  ๋ณด๊ธฐ