📚

GKE Backend best practice

2025/02/25に公開

GKE 클러스터를 Cloud Armor, Identity-Aware Proxy(IAP), Cloud Load Balancer와 함께 구성하는 베스트 프랙티스를 설명해드리겠습니다.

load-balancer.tf
# 백엔드 서비스 설정
resource "google_compute_backend_service" "gke_backend" {
  name                  = "gke-backend-service"
  protocol              = "HTTPS"
  port_name            = "http"
  load_balancing_scheme = "EXTERNAL"
  timeout_sec          = 30
  enable_cdn           = false
  
  # 헬스 체크 설정
  health_checks = [google_compute_health_check.default.id]

  # GKE NEG와 연결
  backend {
    group = google_compute_network_endpoint_group.neg.id
    balancing_mode = "RATE"
    max_rate_per_endpoint = 100
  }

  # Cloud Armor 정책 연결
  security_policy = google_compute_security_policy.policy.name

  # IAP 설정
  iap {
    oauth2_client_id     = google_iap_client.default.client_id
    oauth2_client_secret = google_iap_client.default.secret
  }

  log_config {
    enable      = true
    sample_rate = 1.0
  }
}

# Cloud Armor 정책
resource "google_compute_security_policy" "policy" {
  name = "security-policy"

  # 허용할 IP 범위 설정
  rule {
    action   = "allow"
    priority = "1000"
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        src_ip_ranges = ["10.0.0.0/8", "172.16.0.0/12"]  # 내부 네트워크
      }
    }
    description = "Allow internal traffic"
  }

  # 특정 국가 차단
  rule {
    action   = "deny(403)"
    priority = "2000"
    match {
      expr {
        expression = "origin.region_code == 'KP'"
      }
    }
    description = "Block specific countries"
  }

  # OWASP Top 10 보호
  rule {
    action   = "deny(403)"
    priority = "3000"
    match {
      expr {
        expression = "evaluatePreconfiguredExpr('xss-stable')"
      }
    }
    description = "Block XSS attacks"
  }

  # 기본 규칙
  rule {
    action   = "deny(403)"
    priority = "2147483647"
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        src_ip_ranges = ["*"]
      }
    }
    description = "Default deny"
  }
}

# SSL 인증서
resource "google_compute_managed_ssl_certificate" "default" {
  name = "lb-cert"
  managed {
    domains = ["dev.xxxx.com"]
  }
}

# 프론트엔드 설정
resource "google_compute_global_forwarding_rule" "default" {
  name                  = "lb-forwarding-rule"
  target                = google_compute_target_https_proxy.default.id
  port_range           = "443"
  load_balancing_scheme = "EXTERNAL"
}

resource "google_compute_target_https_proxy" "default" {
  name             = "lb-https-proxy"
  url_map          = google_compute_url_map.default.id
  ssl_certificates = [google_compute_managed_ssl_certificate.default.id]
}

# URL 맵
resource "google_compute_url_map" "default" {
  name            = "lb-url-map"
  default_service = google_compute_backend_service.gke_backend.id

  # 경로 기반 라우팅
  host_rule {
    hosts        = ["dev.xxxx.com"]
    path_matcher = "allpaths"
  }

  path_matcher {
    name            = "allpaths"
    default_service = google_compute_backend_service.gke_backend.id
    
    path_rule {
      paths   = ["/app1/*"]
      service = google_compute_backend_service.gke_backend.id
    }
    # 다른 경로 규칙들...
  }
}

# 헬스 체크
resource "google_compute_health_check" "default" {
  name               = "gke-health-check"
  timeout_sec        = 5
  check_interval_sec = 10

  http_health_check {
    port         = 80
    request_path = "/healthz"
  }
}
iap.tf
# IAP OAuth 설정
resource "google_iap_brand" "default" {
  support_email     = "support@your-domain.com"
  application_title = "Cloud IAP protected Application"
}

resource "google_iap_client" "default" {
  display_name = "IAP Client"
  brand        = google_iap_brand.default.name
}

# IAP 웹 설정
resource "google_iap_web" "default" {
  name = "iap-web"
}

# IAP 접근 정책
resource "google_iap_web_iam_binding" "binding" {
  project = var.project_id
  role    = "roles/iap.httpsResourceAccessor"
  members = [
    "group:devops@your-domain.com",
    "user:admin@your-domain.com",
  ]
}

베스트 프랙티스 포인트:

  1. 보안 계층화:

    • Cloud Armor로 외부 위협 방어
    • IAP로 사용자 인증
    • GKE 내부 RBAC로 세분화된 권한 관리
  2. 성능 최적화:

    • 적절한 헬스 체크 간격 설정
    • NEG 사용으로 효율적인 로드 밸런싱
    • 백엔드 서비스의 타임아웃 설정
  3. 모니터링과 로깅:

    • 로드 밸런서 로깅 활성화
    • 상세한 헬스 체크 구성
    • Cloud Armor 로그 수집
  4. 가용성 확보:

    • 글로벌 로드 밸런서 사용
    • 여러 리전에 백엔드 배포 가능
    • 자동 SSL 인증서 관리
  5. 유지보수 용이성:

    • Terraform으로 인프라 코드화
    • 모듈화된 구성
    • 명확한 리소스 명명 규칙

주의사항:

  1. IAP 설정 시 OAuth 동의 화면 구성 필요
  2. Cloud Armor 규칙의 우선순위 관리
  3. 적절한 모니터링 메트릭 설정
  4. 비용 최적화 고려

Discussion