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

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

이 빠른 시작은 기존 인프라를 사용하여 리소스를 Google Cloud 논리적 애플리케이션으로 그룹화하여 가시성과 운영 제어 기능을 얻으려는 사용자를 지원합니다.

시작하기 전에

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

  1. 애플리케이션 설정 모델을 선택합니다. 이 빠른 시작에서는 애플리케이션 관리 경계를 정의하고 애플리케이션 관리 경계 및 관리 프로젝트를 사용하여 관리 프로젝트애플리케이션을 만든다고 가정합니다. 이 리소스 구성에 대한 자세한 내용은 App Hub 리소스 모델을 참조하세요.

  2. 이 문서 전체에서 사용할 관리 프로젝트의 프로젝트 ID를 기록해 둡니다. 자세한 내용은 프로젝트 이름, 번호, ID 찾기를 참고하세요.

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

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

    API 사용 설정

필요한 역할

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

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

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

애플리케이션의 샘플 구성요소 배포

먼저 애플리케이션 구성요소로 등록하여 App Hub에서 전역 애플리케이션을 정의하는 데 사용할 샘플 Google Cloud 리소스를 배포해야 합니다.

다음 단계에 따라 이러한 리소스를 배포합니다.

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. HTTP 프록시를 만들어 요청을 수신하고 URL 맵을 사용하여 라우팅합니다.

      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"
    }
    

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

    • 부하 분산기의 백엔드 역할을 하며 Cloud Run 서비스를 가리키는 서버리스 네트워크 엔드포인트 그룹 (NEG)
    • 트래픽을 서버리스 NEG로 전달하는 백엔드 서비스
    • 수신되는 요청을 백엔드 서비스로 라우팅하는 URL 맵
    • HTTP 프록시를 만들어 요청을 수신하고 URL 맵을 사용하여 라우팅합니다.
    • 수신되는 사용자 요청을 처리하고 프록시로 전달하는 공개 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. 애플리케이션 리소스 탭에서 인프라 리소스를 찾아보고 전달 규칙 및 Cloud Run 서비스의 체크박스를 선택합니다.

  8. 계속 을 클릭합니다.

  9. 속성 및 소유자 탭에서 중요도, 환경, 애플리케이션 소유자와 같은 애플리케이션의 속성을 추가할 수 있습니다.

    애플리케이션에 등록하는 리소스에 동일한 속성과 소유자를 사용하거나 나중에 정의할 수 있습니다.

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

애플리케이션이 생성되면 애플리케이션 페이지에 서비스로 등록한 리소스를 비롯한 세부정보가 표시됩니다.

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. 선택사항: 이 빠른 시작을 위해 새 프로젝트를 만든 경우 프로젝트를 삭제합니다.

다음 단계