Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 55 additions & 25 deletions terraform/environments/prod/compute.tf
Original file line number Diff line number Diff line change
@@ -1,49 +1,79 @@
# ========================================
# 웹 서버 컴퓨트 모듈
# Kubernetes 마스터 노드 컴퓨트 모듈
# ========================================
module "web_servers" {
module "k8s_master_nodes" {
source = "../../modules/compute"

name_prefix = "${var.project}-${var.environment}-web"
name_prefix = "${var.project}-${var.environment}-k8s-master"
network = module.vpc.vpc_self_link
subnetwork = module.vpc.subnets["app"].self_link

# 관리형 인스턴스 그룹 설정
create_instance_template = true
create_instance_group = true

instance_group_zone = "${var.region}-a"
instance_group_target_size = var.k8s_master_instance_group_size

# 마스터 노드는 단일 인스턴스로 고정 운영합니다.
enable_autoscaling = false

# 공통 인스턴스 설정
machine_type = var.k8s_master_machine_type
source_image = var.k8s_node_source_image
boot_disk_size_gb = var.k8s_node_boot_disk_size_gb
boot_disk_type = "pd-balanced"
enable_external_ip = false
tags = ["k8s-master", var.environment]

# 태그
common_tags = merge(var.common_tags, {
Service = "Kubernetes"
Role = "Master"
})

# 서비스 계정 설정
service_account_email = var.service_account_email
service_account_scopes = [
"https://www.googleapis.com/auth/cloud-platform"
]
}

# ========================================
# Kubernetes 워커 노드 컴퓨트 모듈
# ========================================
module "k8s_worker_nodes" {
source = "../../modules/compute"

name_prefix = "${var.project}-${var.environment}-k8s-worker"
network = module.vpc.vpc_self_link
subnetwork = module.vpc.subnets["web"].self_link

# 관리형 인스턴스 그룹 설정
create_instance_template = var.use_instance_group
create_instance_group = var.use_instance_group
create_instance_template = true
create_instance_group = true

instance_group_zone = "${var.region}-a"
instance_group_target_size = var.instance_group_size
instance_group_target_size = var.k8s_worker_instance_group_size

# 오토스케일링 설정
# 워커 노드는 비용 절감을 우선하되 필요 시에만 오토스케일링합니다.
enable_autoscaling = var.enable_autoscaling
autoscaling_min_replicas = var.autoscaling_min_replicas
autoscaling_max_replicas = var.autoscaling_max_replicas
autoscaling_cpu_target = 0.7

# 관리형 인스턴스 그룹을 사용하지 않을 때만 개별 인스턴스를 정의합니다.
instances = !var.use_instance_group && var.create_web_instances ? tomap({
web1 = {
name = "${var.environment}-web-01"
zone = "${var.region}-a"
machine_type = var.web_machine_type
enable_external_ip = false # 프로덕션 환경은 로드 밸런서를 통한 접근만 허용합니다.
tags = ["web-server", var.environment]
deletion_protection = true # 실수로 삭제되지 않도록 보호합니다.
}
}) : tomap({})

# 공통 인스턴스 설정
machine_type = var.web_machine_type
source_image = var.web_source_image
boot_disk_size_gb = var.web_machine_ssd
boot_disk_type = "pd-ssd"
machine_type = var.k8s_worker_machine_type
source_image = var.k8s_node_source_image
boot_disk_size_gb = var.k8s_node_boot_disk_size_gb
boot_disk_type = "pd-balanced"
enable_external_ip = false
tags = ["web-server", var.environment]
tags = ["k8s-worker", var.environment]

# 태그
common_tags = merge(var.common_tags, {
Service = "Backend"
Service = "Kubernetes"
Role = "Worker"
})

# 서비스 계정 설정
Expand Down
28 changes: 9 additions & 19 deletions terraform/environments/prod/load-balancer.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ module "load_balancer" {

name_prefix = "${var.project}-${var.environment}-nlb"
region = var.region
lb_type = var.lb_type

# 헬스 체크 설정
create_health_check = true
health_check_protocol = var.lb_type == "NETWORK" ? "TCP" : (var.lb_type == "HTTPS" ? "HTTPS" : "HTTP")
health_check_port = var.lb_type == "HTTPS" ? 443 : 80
health_check_protocol = "TCP"
health_check_port = 80
health_check_request_path = "/health"
health_check_interval = 5
health_check_timeout = 5
Expand All @@ -24,24 +23,15 @@ module "load_balancer" {
backend_protocol = "TCP"
backend_timeout_sec = 30
session_affinity = "CLIENT_IP"
backend_groups = var.use_instance_group ? [
merge(
{
group = module.web_servers.instance_group_instance_group
balancing_mode = var.lb_type == "NETWORK" ? "CONNECTION" : "UTILIZATION"
},
var.lb_type == "NETWORK" ? {} : {
max_utilization = 0.8
}
)
] : []
ssl_certificates = var.ssl_certificates
backend_groups = [
{
group = module.k8s_worker_nodes.instance_group_instance_group
balancing_mode = "CONNECTION"
}
]

# 포워딩 규칙 설정
forwarding_rule_ip_protocol = "TCP"
forwarding_rule_port_range = var.lb_type == "HTTPS" ? "443" : "80"
forwarding_rule_port_range = "80"
network_tier = "PREMIUM"

# HTTP(S) 로드 밸런서일 때만 CDN을 활성화합니다.
enable_cdn = var.lb_type == "HTTP" || var.lb_type == "HTTPS" ? true : false
}
27 changes: 21 additions & 6 deletions terraform/environments/prod/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,31 @@ output "subnets" {
}

# ========================================
# 웹 서버 출력값
# Kubernetes 출력값
# ========================================
output "web_instances" {
description = "생성된 웹 인스턴스 정보입니다."
value = module.web_servers.instances
output "k8s_master_instances" {
description = "생성된 Kubernetes 마스터 인스턴스 정보입니다."
value = module.k8s_master_nodes.instances
}

output "k8s_worker_instances" {
description = "생성된 Kubernetes 워커 인스턴스 정보입니다."
value = module.k8s_worker_nodes.instances
}

output "k8s_master_instance_group_id" {
description = "생성된 Kubernetes 마스터 인스턴스 그룹 ID입니다."
value = module.k8s_master_nodes.instance_group_id
}

output "k8s_worker_instance_group_id" {
description = "생성된 Kubernetes 워커 인스턴스 그룹 ID입니다."
value = module.k8s_worker_nodes.instance_group_id
}

output "instance_group_id" {
description = "생성된 인스턴스 그룹 ID입니다."
value = module.web_servers.instance_group_id
description = "생성된 Kubernetes 워커 인스턴스 그룹 ID입니다."
value = module.k8s_worker_nodes.instance_group_id
}

# ========================================
Expand Down
2 changes: 1 addition & 1 deletion terraform/environments/prod/storage.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module "storage" {
buckets = merge(
var.create_storage_buckets ? tomap({
static_assets = {
name = "${var.project_id}-${var.environment}"
name = "${var.project}-${var.environment}"
storage_class = "STANDARD"
uniform_bucket_level_access = true
versioning_enabled = true
Expand Down
60 changes: 24 additions & 36 deletions terraform/environments/prod/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ variable "vpc_name" {
variable "ssh_source_ranges" {
description = "SSH 접근을 허용할 CIDR 목록입니다."
type = list(string)
default = [] # 운영 환경에서는 반드시 허용 대역을 명시해야 합니다.
default = []
}

variable "enable_nat" {
Expand All @@ -49,22 +49,16 @@ variable "enable_nat" {
# ========================================
# 컴퓨트 관련 변수
# ========================================
variable "use_instance_group" {
description = "관리형 인스턴스 그룹 사용 여부입니다."
type = bool
default = true
}

variable "create_web_instances" {
description = "개별 웹 인스턴스 생성 여부입니다."
type = bool
default = true
variable "k8s_master_instance_group_size" {
description = "Kubernetes 마스터 관리형 인스턴스 그룹의 목표 인스턴스 수입니다."
type = number
default = 1
}

variable "instance_group_size" {
description = "관리형 인스턴스 그룹의 목표 인스턴스 수입니다."
variable "k8s_worker_instance_group_size" {
description = "Kubernetes 워커 관리형 인스턴스 그룹의 목표 인스턴스 수입니다."
type = number
default = 1
default = 2
}

variable "enable_autoscaling" {
Expand All @@ -85,22 +79,28 @@ variable "autoscaling_max_replicas" {
default = 5
}

variable "web_machine_type" {
description = "웹 서버에 사용할 머신 타입입니다."
variable "k8s_master_machine_type" {
description = "Kubernetes 마스터 노드에 사용할 머신 타입입니다."
type = string
default = "e2-standard-2" # 운영 환경에 맞춘 고성능 인스턴스입니다.
default = "e2-standard-2"
}

variable "web_machine_ssd" {
description = "웹 서버 부팅 디스크 크기(GB)입니다."
variable "k8s_worker_machine_type" {
description = "Kubernetes 워커 노드에 사용할 머신 타입입니다."
type = string
default = "e2-standard-2"
}

variable "k8s_node_boot_disk_size_gb" {
description = "Kubernetes 노드 부팅 디스크 크기(GB)입니다."
type = number
default = 50
}

variable "web_source_image" {
description = "웹 서버 부팅 디스크에 사용할 이미지입니다."
variable "k8s_node_source_image" {
description = "Kubernetes 노드 부팅 디스크에 사용할 이미지입니다."
type = string
default = "debian-cloud/debian-11"
default = "ubuntu-os-cloud/ubuntu-2204-lts"
}

variable "service_account_email" {
Expand All @@ -121,13 +121,13 @@ variable "create_storage_buckets" {
variable "storage_location" {
description = "스토리지 버킷을 생성할 위치입니다."
type = string
default = "ASIA-NORTHEAST3" # 비용 절감을 위해 단일 리전을 기본값으로 사용합니다.
default = "ASIA-NORTHEAST3"
}

variable "allowed_cors_origins" {
description = "정적 자산 버킷에서 허용할 CORS Origin 목록입니다."
type = list(string)
default = [] # 실제 서비스 도메인을 명시해야 합니다.
default = []
}

# ========================================
Expand All @@ -139,18 +139,6 @@ variable "create_load_balancer" {
default = true
}

variable "lb_type" {
description = "생성할 로드 밸런서 타입입니다. NETWORK, HTTP, HTTPS 중 하나를 사용합니다."
type = string
default = "NETWORK"
}

variable "ssl_certificates" {
description = "HTTPS 로드 밸런서에 연결할 SSL 인증서 self_link 목록입니다."
type = list(string)
default = []
}

# ========================================
# 공통 태그 변수
# ========================================
Expand Down
5 changes: 3 additions & 2 deletions terraform/environments/prod/vpc.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module "vpc" {
}
]
source_ranges = ["0.0.0.0/0"]
target_tags = ["web-server"]
target_tags = ["k8s-worker"]
priority = 1000
}
allow_https = {
Expand All @@ -56,7 +56,7 @@ module "vpc" {
}
]
source_ranges = ["0.0.0.0/0"]
target_tags = ["web-server"]
target_tags = ["k8s-worker"]
priority = 1000
}
allow_ssh = {
Expand All @@ -68,6 +68,7 @@ module "vpc" {
}
]
source_ranges = var.ssh_source_ranges # 프로덕션에서는 반드시 관리된 IP 대역만 허용해야 합니다.
target_tags = ["k8s-master", "k8s-worker"]
priority = 1000
}
allow_internal = {
Expand Down
Loading
Loading