From 44e7613cb7442e1d8bf6364261639d61b89744ee Mon Sep 17 00:00:00 2001 From: Hexeong <123macanic@naver.com> Date: Wed, 18 Nov 2026 23:29:37 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20stage=20API=20EC2=EC=97=90=20Node?= =?UTF-8?q?=20Exporter=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- environment/stage/main.tf | 1 + modules/app_stack/ec2.tf | 1 + modules/app_stack/modules/app/main.tf | 4 ++++ modules/app_stack/modules/app/variables.tf | 4 ++++ .../scripts/side_infra_setup.sh.tftpl | 21 ++++++++++++++++++- modules/app_stack/variables.tf | 6 ++++++ scripts/verify/render/templates.tf | 2 ++ 7 files changed, 38 insertions(+), 1 deletion(-) diff --git a/environment/stage/main.tf b/environment/stage/main.tf index 551131a..39e5ca4 100644 --- a/environment/stage/main.tf +++ b/environment/stage/main.tf @@ -40,4 +40,5 @@ module "stage_stack" { redis_version = var.redis_version redis_exporter_version = var.redis_exporter_version alloy_version = var.alloy_version + node_exporter_enabled = true } diff --git a/modules/app_stack/ec2.tf b/modules/app_stack/ec2.tf index 34e784c..665b9c9 100644 --- a/modules/app_stack/ec2.tf +++ b/modules/app_stack/ec2.tf @@ -17,4 +17,5 @@ module "app" { redis_version = var.redis_version redis_exporter_version = var.redis_exporter_version alloy_version = var.alloy_version + node_exporter_enabled = var.node_exporter_enabled } diff --git a/modules/app_stack/modules/app/main.tf b/modules/app_stack/modules/app/main.tf index b010fb2..4245852 100644 --- a/modules/app_stack/modules/app/main.tf +++ b/modules/app_stack/modules/app/main.tf @@ -72,12 +72,14 @@ locals { }) side_infra_script_b64 = base64encode(templatefile("${path.module}/../../scripts/side_infra_setup.sh.tftpl", { + api_private_ip = aws_instance.api_server.private_ip work_dir = var.work_dir alloy_env_name = var.alloy_env_name alloy_config_content = local.alloy_config redis_version = var.redis_version redis_exporter_version = var.redis_exporter_version alloy_version = var.alloy_version + node_exporter_enabled = var.node_exporter_enabled })) nginx_install_ssm_params = jsonencode({ @@ -241,6 +243,7 @@ resource "null_resource" "update_side_infra" { triggers = { script_hash = sha256(templatefile("${path.module}/../../scripts/side_infra_setup.sh.tftpl", { + api_private_ip = aws_instance.api_server.private_ip work_dir = var.work_dir alloy_env_name = var.alloy_env_name alloy_config_content = templatefile("${path.module}/../../../../config/side-infra/config.alloy.tftpl", { @@ -249,6 +252,7 @@ resource "null_resource" "update_side_infra" { redis_version = var.redis_version redis_exporter_version = var.redis_exporter_version alloy_version = var.alloy_version + node_exporter_enabled = var.node_exporter_enabled })) } diff --git a/modules/app_stack/modules/app/variables.tf b/modules/app_stack/modules/app/variables.tf index adc740f..64c6794 100644 --- a/modules/app_stack/modules/app/variables.tf +++ b/modules/app_stack/modules/app/variables.tf @@ -54,3 +54,7 @@ variable "redis_exporter_version" { variable "alloy_version" { type = string } + +variable "node_exporter_enabled" { + type = bool +} diff --git a/modules/app_stack/scripts/side_infra_setup.sh.tftpl b/modules/app_stack/scripts/side_infra_setup.sh.tftpl index 6e87f45..26369f4 100644 --- a/modules/app_stack/scripts/side_infra_setup.sh.tftpl +++ b/modules/app_stack/scripts/side_infra_setup.sh.tftpl @@ -58,6 +58,25 @@ services: network_mode: "host" restart: always +%{ if node_exporter_enabled ~} + node-exporter: + image: prom/node-exporter:v1.9.1 + container_name: node-exporter + network_mode: "host" + pid: "host" + restart: always + read_only: true + volumes: + - /proc:/host/proc:ro + - /sys:/host/sys:ro + - /:/host/root:ro,rslave + command: + - --path.procfs=/host/proc + - --path.sysfs=/host/sys + - --path.rootfs=/host/root + - --web.listen-address=${api_private_ip}:9100 + +%{ endif ~} EOF # 4. Start containers @@ -66,7 +85,7 @@ cd "$WORK_DIR" # Cleanup potential conflicts where ports might be locked by other Docker Compose instances. # 'rm -f' is executed first to ensure cleanup of any lingering network resources attached to existing containers. echo "Cleanup: Force removing leftover containers..." -docker rm -f redis redis-exporter alloy || true +docker rm -f redis redis-exporter alloy%{ if node_exporter_enabled } node-exporter%{ endif } || true echo "Stopping previous containers if running..." docker compose -f docker-compose.side-infra.yml down || true diff --git a/modules/app_stack/variables.tf b/modules/app_stack/variables.tf index 647d3e7..95ce0da 100644 --- a/modules/app_stack/variables.tf +++ b/modules/app_stack/variables.tf @@ -208,6 +208,12 @@ variable "alloy_version" { type = string } +variable "node_exporter_enabled" { + description = "Enable host metrics on the API EC2 instance" + type = bool + default = false +} + variable "internal_alarm_api_ports" { description = "DB EC2가 백업 실패 알림을 보내는 API 서버의 Blue/Green app 포트" type = list(number) diff --git a/scripts/verify/render/templates.tf b/scripts/verify/render/templates.tf index 2943465..2d46dc0 100644 --- a/scripts/verify/render/templates.tf +++ b/scripts/verify/render/templates.tf @@ -35,12 +35,14 @@ locals { conf_file_name = "api.conf" }) "app_stack-side_infra_setup.sh" = templatefile("${local.repo}/modules/app_stack/scripts/side_infra_setup.sh.tftpl", { + api_private_ip = "10.0.0.20" work_dir = "/home/ubuntu/side-infra" alloy_env_name = "verify" alloy_config_content = local.alloy_config redis_version = "7.2" redis_exporter_version = "v1.62.0" alloy_version = "v1.4.0" + node_exporter_enabled = true }) # modules/monitoring_stack/ec2.tf From aa345861459016165f76f5a38bf398162a2d00c3 Mon Sep 17 00:00:00 2001 From: Hexeong <123macanic@naver.com> Date: Wed, 18 Nov 2026 23:37:46 +0900 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=EB=B9=84=ED=99=9C=EC=84=B1=ED=99=94?= =?UTF-8?q?=20=EC=8B=9C=20Node=20Exporter=20=EC=BB=A8=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EB=84=88=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- environment/stage/main.tf | 1 + modules/app_stack/ec2.tf | 1 + modules/app_stack/modules/app/main.tf | 2 ++ modules/app_stack/modules/app/variables.tf | 4 ++++ modules/app_stack/scripts/side_infra_setup.sh.tftpl | 2 +- modules/app_stack/variables.tf | 6 ++++++ scripts/verify/render/templates.tf | 1 + 7 files changed, 16 insertions(+), 1 deletion(-) diff --git a/environment/stage/main.tf b/environment/stage/main.tf index 39e5ca4..23ffebb 100644 --- a/environment/stage/main.tf +++ b/environment/stage/main.tf @@ -41,4 +41,5 @@ module "stage_stack" { redis_exporter_version = var.redis_exporter_version alloy_version = var.alloy_version node_exporter_enabled = true + node_exporter_managed = true } diff --git a/modules/app_stack/ec2.tf b/modules/app_stack/ec2.tf index 665b9c9..40e873f 100644 --- a/modules/app_stack/ec2.tf +++ b/modules/app_stack/ec2.tf @@ -18,4 +18,5 @@ module "app" { redis_exporter_version = var.redis_exporter_version alloy_version = var.alloy_version node_exporter_enabled = var.node_exporter_enabled + node_exporter_managed = var.node_exporter_managed } diff --git a/modules/app_stack/modules/app/main.tf b/modules/app_stack/modules/app/main.tf index 4245852..5dd707f 100644 --- a/modules/app_stack/modules/app/main.tf +++ b/modules/app_stack/modules/app/main.tf @@ -80,6 +80,7 @@ locals { redis_exporter_version = var.redis_exporter_version alloy_version = var.alloy_version node_exporter_enabled = var.node_exporter_enabled + node_exporter_managed = var.node_exporter_managed })) nginx_install_ssm_params = jsonencode({ @@ -253,6 +254,7 @@ resource "null_resource" "update_side_infra" { redis_exporter_version = var.redis_exporter_version alloy_version = var.alloy_version node_exporter_enabled = var.node_exporter_enabled + node_exporter_managed = var.node_exporter_managed })) } diff --git a/modules/app_stack/modules/app/variables.tf b/modules/app_stack/modules/app/variables.tf index 64c6794..4a43ea4 100644 --- a/modules/app_stack/modules/app/variables.tf +++ b/modules/app_stack/modules/app/variables.tf @@ -58,3 +58,7 @@ variable "alloy_version" { variable "node_exporter_enabled" { type = bool } + +variable "node_exporter_managed" { + type = bool +} diff --git a/modules/app_stack/scripts/side_infra_setup.sh.tftpl b/modules/app_stack/scripts/side_infra_setup.sh.tftpl index 26369f4..b591523 100644 --- a/modules/app_stack/scripts/side_infra_setup.sh.tftpl +++ b/modules/app_stack/scripts/side_infra_setup.sh.tftpl @@ -85,7 +85,7 @@ cd "$WORK_DIR" # Cleanup potential conflicts where ports might be locked by other Docker Compose instances. # 'rm -f' is executed first to ensure cleanup of any lingering network resources attached to existing containers. echo "Cleanup: Force removing leftover containers..." -docker rm -f redis redis-exporter alloy%{ if node_exporter_enabled } node-exporter%{ endif } || true +docker rm -f redis redis-exporter alloy%{ if node_exporter_managed } node-exporter%{ endif } || true echo "Stopping previous containers if running..." docker compose -f docker-compose.side-infra.yml down || true diff --git a/modules/app_stack/variables.tf b/modules/app_stack/variables.tf index 95ce0da..122ed18 100644 --- a/modules/app_stack/variables.tf +++ b/modules/app_stack/variables.tf @@ -214,6 +214,12 @@ variable "node_exporter_enabled" { default = false } +variable "node_exporter_managed" { + description = "Remove an existing Node Exporter container when this environment disables it" + type = bool + default = false +} + variable "internal_alarm_api_ports" { description = "DB EC2가 백업 실패 알림을 보내는 API 서버의 Blue/Green app 포트" type = list(number) diff --git a/scripts/verify/render/templates.tf b/scripts/verify/render/templates.tf index 2d46dc0..0869582 100644 --- a/scripts/verify/render/templates.tf +++ b/scripts/verify/render/templates.tf @@ -43,6 +43,7 @@ locals { redis_exporter_version = "v1.62.0" alloy_version = "v1.4.0" node_exporter_enabled = true + node_exporter_managed = true }) # modules/monitoring_stack/ec2.tf From 944fb2ad3e09db31ac4dd8de2460db9da4e22789 Mon Sep 17 00:00:00 2001 From: Hexeong <123macanic@naver.com> Date: Thu, 19 Nov 2026 14:23:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20Node=20Exporter=20=EB=B0=B0=ED=8F=AC?= =?UTF-8?q?=20=EC=A4=91=20Redis=20=EC=BB=A8=ED=85=8C=EC=9D=B4=EB=84=88=20?= =?UTF-8?q?=EC=9C=A0=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app_stack/scripts/side_infra_setup.sh.tftpl | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/modules/app_stack/scripts/side_infra_setup.sh.tftpl b/modules/app_stack/scripts/side_infra_setup.sh.tftpl index b591523..58507f7 100644 --- a/modules/app_stack/scripts/side_infra_setup.sh.tftpl +++ b/modules/app_stack/scripts/side_infra_setup.sh.tftpl @@ -82,15 +82,29 @@ EOF # 4. Start containers cd "$WORK_DIR" +%{ if node_exporter_managed ~} +# Validate and download the exporter before changing any running service. +docker compose -f docker-compose.side-infra.yml config --quiet +%{ if node_exporter_enabled ~} +docker compose -f docker-compose.side-infra.yml pull node-exporter +%{ else ~} +docker rm -f node-exporter || true +%{ endif ~} + +# Compose recreates services only when their configuration changed. Adding or +# removing Node Exporter leaves the existing Redis container and its data intact. +docker compose -f docker-compose.side-infra.yml up -d +%{ else ~} # Cleanup potential conflicts where ports might be locked by other Docker Compose instances. # 'rm -f' is executed first to ensure cleanup of any lingering network resources attached to existing containers. echo "Cleanup: Force removing leftover containers..." -docker rm -f redis redis-exporter alloy%{ if node_exporter_managed } node-exporter%{ endif } || true +docker rm -f redis redis-exporter alloy || true echo "Stopping previous containers if running..." docker compose -f docker-compose.side-infra.yml down || true echo "Starting containers..." docker compose -f docker-compose.side-infra.yml up -d +%{ endif ~} echo "Side Infrastructure Setup Complete!" \ No newline at end of file From ed8b7b1ae8cfa049603d73b2fa771f0c2ff890bf Mon Sep 17 00:00:00 2001 From: Hexeong <123macanic@naver.com> Date: Thu, 19 Nov 2026 14:30:02 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20Node=20Exporter=20=EB=B0=B0=ED=8F=AC?= =?UTF-8?q?=20=EC=8B=9C=20Alloy=20=EC=84=A4=EC=A0=95=20=EC=9E=AC=EB=A1=9C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/app_stack/scripts/side_infra_setup.sh.tftpl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/app_stack/scripts/side_infra_setup.sh.tftpl b/modules/app_stack/scripts/side_infra_setup.sh.tftpl index 58507f7..12c8bcf 100644 --- a/modules/app_stack/scripts/side_infra_setup.sh.tftpl +++ b/modules/app_stack/scripts/side_infra_setup.sh.tftpl @@ -94,6 +94,8 @@ docker rm -f node-exporter || true # Compose recreates services only when their configuration changed. Adding or # removing Node Exporter leaves the existing Redis container and its data intact. docker compose -f docker-compose.side-infra.yml up -d +# Alloy's bind-mounted config can change without changing its Compose service. +docker compose -f docker-compose.side-infra.yml restart alloy %{ else ~} # Cleanup potential conflicts where ports might be locked by other Docker Compose instances. # 'rm -f' is executed first to ensure cleanup of any lingering network resources attached to existing containers.