從現有資源建立應用程式

本快速入門導覽課程說明如何將現有 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. 建立網址對應,將連入的要求轉送至後端服務:

      gcloud compute url-maps create hello-url-map \
          --default-service=hello-backend-service \
          --project=${PROJECT_ID}
      
    5. 建立 HTTP Proxy,接收要求並使用網址對應轉送要求:

      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 位址和通訊埠,用於處理傳入的使用者要求,並將要求導向 Proxy。

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 的後端服務
    • 網址對應,可將連入要求轉送至後端服務。
    • HTTP Proxy,用於接收要求並透過網址對應轉送要求。
    • 全域轉送規則:提供公開 IP 位址和通訊埠,用於處理傳入的使用者要求,並將要求導向 Proxy。
  3. 初始化並套用 Terraform 設定:

    terraform init
    terraform apply
    

    Terraform 會將資源部署至專案。

在 App Hub 中定義應用程式

將資源部署為轉送規則和 Cloud Run 服務後,請按照下列步驟,將資源分組到 App Hub 中的應用程式:

控制台

  1. 從 App Hub 前往「應用程式」頁面:

    前往「Applications」(應用程式)

  2. 按一下「建立應用程式」

  3. 選取「Global」(全球) 做為應用程式位置。

  4. 在「應用程式名稱」欄位輸入 my-global-app,然後按一下「繼續」

  5. (選用) 新增顯示名稱、重要性、環境和擁有者。

  6. 點選「建立」

  7. 在「服務和工作負載」分頁中,按一下「註冊服務/工作負載」

  8. 選取轉送規則,將其命名為 frontend-service,然後按一下「Register」(註冊)

  9. 選取 Cloud Run 服務,將其命名為 backend-service,然後按一下「Register」(註冊)

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. 選用:如果您為本快速入門導覽課程建立了新專案,請刪除該專案

後續步驟