기존 리소스에서 애플리케이션 만들기

이 빠른 시작에서는 기존 Google Cloud 서비스를 App Hub 애플리케이션으로 구성하는 방법을 보여줍니다. 먼저 샘플 구성을 배포한 다음 이를 사용하여 App Hub에 구성요소를 애플리케이션으로 등록하여 전역 웹 애플리케이션을 정의합니다.

이 접근 방식은 기존 인프라를 사용하여 리소스를 논리적 애플리케이션으로 그룹화하여 가시성과 운영 제어 기능을 확보하려는 사용자를 위한 것입니다.

시작하기 전에

이 빠른 시작을 시작하기 전에 다음을 수행하세요.

  1. 앱 지원 폴더로 App Hub 설정

  2. 이 문서 전체에서 사용할 관리 프로젝트의 프로젝트 ID를 기록해 둡니다.

  3. 관리 프로젝트에서 다음 API가 사용 설정되어 있는지 확인합니다. 앱 지원 폴더를 구성하면 이 빠른 시작에 필요한 대부분의 API가 자동으로 사용 설정됩니다.

    • Compute Engine API(compute.googleapis.com)
    • Infrastructure Manager API (config.googleapis.com)

    API 사용 설정

필요한 역할

필수 API를 사용 설정하고 기존 리소스에서 샘플 애플리케이션을 만드는 데 필요한 권한을 얻으려면 관리자에게 관리 프로젝트에 대한 다음 IAM 역할을 부여해 달라고 요청하세요.

역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.

커스텀 역할이나 다른 사전 정의된 역할을 통해 필요한 권한을 얻을 수도 있습니다.

애플리케이션 리소스 배포

먼저 나중에 App Hub에서 전역 애플리케이션을 정의하는 데 사용할 샘플 리소스 집합을 배포해야 합니다.

  • 애플리케이션의 백엔드 역할을 하는 Cloud Run 서비스
  • Cloud Run 서비스로 트래픽을 전달 규칙으로 안내하는 전역 외부 애플리케이션 부하 분산기

다음 단계에 따라 이러한 리소스를 배포하세요.

gcloud

  1. 필수 환경 변수를 설정합니다.

    export PROJECT_ID="PROJECT_ID"
    export REGION="REGION"
    

    다음을 바꿉니다.

    • PROJECT_ID: 관리 프로젝트의 ID입니다.
    • REGION: 리소스에 대해 선택한 리전(예: us-central1)
  2. hello-run라는 샘플 Cloud Run 서비스를 배포합니다.

    gcloud run deploy hello-run \
        --image=us-docker.pkg.dev/cloudrun/container/hello \
        --allow-unauthenticated \
        --region=${REGION} \
        --project=${PROJECT_ID}
    
  3. 전역 외부 애플리케이션 부하 분산기를 만듭니다. 이 프로세스는 다음과 같은 단계로 진행됩니다.

    1. hello-run-neg라는 서버리스 네트워크 엔드포인트 그룹 (NEG)을 만듭니다.

      gcloud compute network-endpoint-groups create hello-run-neg \
          --region=${REGION} \
          --network-endpoint-type=serverless \
          --cloud-run-service=hello-run \
          --project=${PROJECT_ID}
      

      NEG는 부하 분산기의 백엔드 역할을 하며 hello-run 서비스를 가리킵니다.

    2. 트래픽이 NEG에 분산되는 방식을 관리하는 백엔드 서비스를 만듭니다.

      gcloud compute backend-services create hello-backend-service \
          --global \
          --load-balancing-scheme=EXTERNAL_MANAGED \
          --project=${PROJECT_ID}
      
    3. 서버리스 NEG를 백엔드 서비스에 백엔드로 추가합니다.

      gcloud compute backend-services add-backend hello-backend-service \
          --global \
          --network-endpoint-group=hello-run-neg \
          --network-endpoint-group-region=${REGION} \
          --project=${PROJECT_ID}
      
    4. 수신되는 요청을 백엔드 서비스로 라우팅하는 URL 맵을 만듭니다.

      gcloud compute url-maps create hello-url-map \
          --default-service=hello-backend-service \
          --project=${PROJECT_ID}
      
    5. 요청을 수신하고 URL 맵을 사용하여 라우팅할 HTTP 프록시를 만듭니다.

      gcloud compute target-http-proxies create hello-http-proxy \
          --url-map=hello-url-map \
          --project=${PROJECT_ID}
      
    6. 전역 전달 규칙을 만듭니다.

      gcloud compute forwarding-rules create hello-forwarding-rule \
          --global \
          --load-balancing-scheme=EXTERNAL_MANAGED \
          --target-http-proxy=hello-http-proxy \
          --ports=80 \
          --project=${PROJECT_ID}
      

      이 전달 규칙은 수신 사용자 요청을 처리할 공개 IP 주소와 포트를 제공하고 이를 프록시로 전달합니다.

Terraform

  1. main.tf 파일을 만들고 다음 코드를 추가합니다.

    # Provider configuration
    provider "google" {
      project = "PROJECT_ID"
    }
    
    # Cloud Run service
    resource "google_cloud_run_v2_service" "default" {
      name     = "hello-run"
      location = "REGION"
      template {
        containers {
          image = "us-docker.pkg.dev/cloudrun/container/hello"
        }
      }
    }
    
    # Allow unauthenticated access to the Cloud Run service
    resource "google_cloud_run_v2_service_iam_member" "noauth" {
      project  = google_cloud_run_v2_service.default.project
      location = google_cloud_run_v2_service.default.location
      name     = google_cloud_run_v2_service.default.name
      role     = "roles/run.invoker"
      member   = "allUsers"
    }
    

    다음을 바꿉니다.

    • PROJECT_ID: 관리 프로젝트의 ID입니다.
    • REGION: 리소스에 대해 선택한 리전(예: us-central1)

    이 블록은 Google Cloud 제공자를 정의하고 샘플 hello-world 컨테이너 이미지를 사용하여 공개 Cloud Run 서비스를 구성합니다. 또한 인증되지 않은 호출을 허용하는 IAM 정책 바인딩이 포함되어 서비스에 공개적으로 액세스할 수 있습니다.

  2. 다음 코드를 main.tf 파일에 추가하여 전역 외부 애플리케이션 부하 분산기를 만듭니다.

    # Serverless NEG for the Cloud Run service
    resource "google_compute_region_network_endpoint_group" "serverless_neg" {
      name                  = "hello-run-neg"
      network_endpoint_type = "SERVERLESS"
      region                = "REGION"
      cloud_run {
        service = google_cloud_run_v2_service.default.name
      }
    }
    
    # Global external backend service
    resource "google_compute_backend_service" "default" {
      name                            = "hello-backend-service"
      protocol                        = "HTTP"
      load_balancing_scheme           = "EXTERNAL_MANAGED"
      backend {
        group = google_compute_region_network_endpoint_group.serverless_neg.id
      }
    }
    
    # URL map to route requests to the backend service
    resource "google_compute_url_map" "default" {
      name            = "hello-url-map"
      default_service = google_compute_backend_service.default.id
    }
    
    # HTTP proxy to route requests to the URL map
    resource "google_compute_target_http_proxy" "default" {
      name    = "hello-http-proxy"
      url_map = google_compute_url_map.default.id
    }
    
    # Global forwarding rule to handle incoming requests
    resource "google_compute_global_forwarding_rule" "default" {
      name       = "hello-forwarding-rule"
      target     = google_compute_target_http_proxy.default.id
      port_range = "80"
    }
    

    이 블록은 다음 구성요소를 정의합니다.

    • 서버리스 네트워크 엔드포인트 그룹 (NEG): 부하 분산기의 백엔드 역할을 하며 Cloud Run 서비스를 가리킵니다.
    • 서버리스 NEG로 트래픽을 전달하는 백엔드 서비스
    • 수신되는 요청을 백엔드 서비스로 라우팅하는 URL 맵
    • 요청을 수신하고 URL 맵을 사용하여 요청을 라우팅하는 HTTP 프록시
    • 수신되는 사용자 요청을 처리하고 프록시로 전달하는 공개 IP 주소와 포트를 제공하는 전역 전달 규칙
  3. Terraform 구성을 초기화하고 적용합니다.

    terraform init
    terraform apply
    

    Terraform이 리소스를 프로젝트에 배포합니다.

App Hub에서 애플리케이션 정의

리소스를 전달 규칙 및 Cloud Run 서비스로 배포한 후 다음 단계에 따라 App Hub에서 애플리케이션으로 그룹화합니다.

콘솔

  1. App Hub에서 애플리케이션 페이지로 이동합니다.

    애플리케이션으로 이동

  2. 애플리케이션 만들기를 클릭합니다.

  3. 애플리케이션 위치로 전역을 선택합니다.

  4. 애플리케이션 이름my-global-app을 입력하고 계속을 클릭합니다.

  5. 선택적으로 표시 이름, 심각도, 환경, 소유자를 추가합니다.

  6. 만들기를 클릭합니다.

  7. 서비스 및 워크로드 탭에서 서비스/워크로드 등록을 클릭합니다.

  8. 전달 규칙을 선택하고 이름을 frontend-service로 지정한 후 등록을 클릭합니다.

  9. Cloud Run 서비스를 선택하고 이름을 backend-service로 지정한 후 등록을 클릭합니다.

gcloud

  1. 애플리케이션을 만듭니다.

    gcloud apphub applications create my-global-app \
        --location=global \
        --display-name="My Global Application" \
        --project=${PROJECT_ID}
    
  2. 해당 리전에서 전달 규칙과 Cloud Run 서비스의 ID를 확인합니다.

    gcloud apphub discovered-services list \
        --location=global \
        --project=${PROJECT_ID}
    
    gcloud apphub discovered-services list \
        --location=${REGION} \
        --project=${PROJECT_ID}
    

    전달 규칙과 Cloud Run 서비스의 ID를 기록해 둡니다.

  3. 전역 애플리케이션에 전달 규칙을 등록합니다.

    gcloud apphub applications services create frontend-service \
        --application=my-global-app \
        --discovered-service=projects/${PROJECT_ID}/locations/global/discoveredServices/FRONTEND_ID \
        --display-name="Frontend Service" \
        --location=global \
        --project=${PROJECT_ID}
    

    FRONTEND_ID를 전달 규칙의 ID로 바꿉니다.

  4. Cloud Run 서비스를 전역 애플리케이션에 등록합니다.

    gcloud apphub applications services create backend-service \
        --application=my-global-app \
        --discovered-service=projects/${PROJECT_ID}/locations/${REGION}/discoveredServices/BACKEND_ID \
        --display-name="Backend Service" \
        --location=global \
        --project=${PROJECT_ID}
    

    BACKEND_ID을 Cloud Run 서비스 ID로 바꿉니다.

Terraform

  1. application.tf 파일을 만들고 다음 코드를 추가합니다.

    # Application
    resource "google_apphub_application" "my_global_app" {
      project        = "PROJECT_ID"
      location       = "global"
      application_id = "my-global-app"
      display_name   = "My Global Web App"
      description    = "A sample global web application."
      scope {
        type = "GLOBAL"
      }
      attributes {
        criticality {
          type = "MEDIUM"
        }
        environment {
          type = "DEVELOPMENT"
        }
        business_owners {
          display_name = "Example Business Owner"
          email        = "business-owner@example.com"
        }
        developer_owners {
          display_name = "Example Developer"
          email        = "dev-owner@example.com"
        }
        operator_owners {
          display_name = "Example Operator"
          email        = "operator-owner@example.com"
        }
      }
    }
    

    이 블록은 google_apphub_application 리소스를 사용하여 서비스와 워크로드의 논리적 그룹을 만듭니다.

    이 예시에서는 전역 애플리케이션을 만들고 중요도, 환경, 소유자와 같은 거버넌스 및 검색 가능성 속성을 정의합니다. 샘플 구성의 값을 수정할 수 있습니다.

  2. 배포된 리소스를 검색하려면 application.tf에 다음 코드를 추가하세요.

    # Discover the forwarding rule
    data "google_apphub_discovered_service" "frontend_service" {
      location = "global"
      service_uri = "//compute.googleapis.com/${google_compute_global_forwarding_rule.default.id}"
    }
    
    # Discover the Cloud Run service
    data "google_apphub_discovered_service" "backend_service" {
      location    = "REGION"
      service_uri = "//run.googleapis.com/${google_cloud_run_v2_service.default.id}"
    }
    

    google_apphub_discovered_service 데이터 소스는 URI를 기반으로 기존 인프라의 리소스 이름을 찾습니다. 이 단계를 통해 App Hub에서 등록할 특정 리소스를 식별할 수 있습니다.

  3. application.tf에 다음 코드를 추가하여 검색된 리소스를 등록합니다.

    # Register the forwarding rule as a service in the application
    resource "google_apphub_service" "frontend" {
      project            = "PROJECT_ID"
      location           = "global"
      application_id     = google_apphub_application.my_global_app.application_id
      service_id         = "frontend-service"
      display_name       = "Frontend Service (LB)"
      discovered_service = data.google_apphub_discovered_service.frontend_service.name
    }
    
    # Register the Cloud Run service as a service in the application
    resource "google_apphub_service" "backend" {
      project            = "PROJECT_ID"
      location           = "global"
      application_id     = google_apphub_application.my_global_app.application_id
      service_id         = "backend-service"
      display_name       = "Backend Service (Cloud Run)"
      discovered_service = data.google_apphub_discovered_service.backend_service.name
    }
    

    google_apphub_service 리소스는 검색된 리소스를 애플리케이션에 공식적으로 등록합니다. 이 단계에서는 인프라와 App Hub에서 정의한 애플리케이션 간의 링크를 만듭니다.

  4. Terraform 구성을 초기화하고 적용합니다.

    terraform init
    terraform apply
    

    Terraform은 App Hub에서 my-global-app 애플리케이션에 리소스를 등록합니다.

선택사항: 새 애플리케이션 모니터링

App Hub에서 애플리케이션을 정의한 후 통합된Google Cloud 서비스를 사용하여 상태와 성능을 모니터링할 수 있습니다.

삭제

이 페이지에서 사용한 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 다음 단계를 수행합니다.

  1. 서비스 및 워크로드 등록 취소
  2. 전역 애플리케이션을 삭제합니다.
  3. Terraform을 사용하여 애플리케이션을 배포한 경우 Terraform 파일이 포함된 디렉터리에서 terraform destroy를 실행하여 생성한 모든 리소스를 프로비저닝 해제합니다.
  4. 선택사항: 이 빠른 시작을 위해 새 프로젝트를 만든 경우 프로젝트를 삭제합니다.

다음 단계