從現有資源建立應用程式
本快速入門導覽課程說明如何將現有 Google Cloud 資源註冊為服務,在 App Hub 應用程式中加以整理。首先,您要部署範例元件,然後在 App Hub 中定義全域網路應用程式。
本快速入門指南適用於想使用現有基礎架構的使用者,他們可將 Google Cloud 資源分組到邏輯應用程式中,藉此掌握資源的瀏覽權限和作業控制權。
事前準備
開始本快速入門導覽課程前,請先完成下列事項:
選擇應用程式設定模式。本快速入門指南假設您已定義資料夾層級的應用程式管理邊界,並使用資料夾的管理專案建立應用程式。如要進一步瞭解這項資源設定,請參閱 App Hub 資源模型。
請記下管理專案的專案 ID,以便在本文件中使用。詳情請參閱「尋找專案名稱、編號和 ID」。
確認管理專案已啟用下列 API。設定管理專案時,系統會自動啟用本快速入門指南所需的大部分 API。
- Compute Engine API (
compute.googleapis.com) - Infrastructure Manager API (
config.googleapis.com)
- Compute Engine API (
必要的角色
如要取得啟用必要 API 及從現有資源建立範例應用程式所需的權限,請要求管理員在管理專案中授予您下列 IAM 角色:
-
如要啟用必要 API:
服務使用情形管理員 (
roles/serviceusage.serviceUsageAdmin) -
如要完整存取必要服務,請按照下列步驟操作:
-
App Hub 管理員 (
roles/apphub.admin) - Cloud Run 管理員 (
roles/run.admin) -
Compute 管理員 (
roles/compute.admin) -
Cloud Infrastructure Manager 管理員 (
roles/config.admin)
-
App Hub 管理員 (
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
部署應用程式的範例元件
您必須先部署一組範例 Google Cloud 資源,稍後將這些資源註冊為應用程式元件,在 App Hub 中定義全域應用程式:
- 做為應用程式後端服務的 Cloud Run 服務。
- 全域外部應用程式負載平衡器:做為應用程式的前端服務,將流量導向 Cloud Run 服務 (做為轉送規則)。
如要部署這些資源,請按照下列步驟操作:
gcloud
設定必要環境變數:
export PROJECT_ID="PROJECT_ID" export REGION="REGION"更改下列內容:
PROJECT_ID:管理專案的 ID。REGION:您為資源選擇的地區,例如us-central1。
部署名為
hello-run的範例 Cloud Run 服務:gcloud run deploy hello-run \ --image=us-docker.pkg.dev/cloudrun/container/hello \ --allow-unauthenticated \ --region=${REGION} \ --project=${PROJECT_ID}建立全域外部應用程式負載平衡器。這個程序包含下列步驟:
建立名為「
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服務。建立後端服務,管理流量分配至 NEG 的方式:
gcloud compute backend-services create hello-backend-service \ --global \ --load-balancing-scheme=EXTERNAL_MANAGED \ --project=${PROJECT_ID}將無伺服器 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}建立網址對應,將連入要求轉送至後端服務:
gcloud compute url-maps create hello-url-map \ --default-service=hello-backend-service \ --project=${PROJECT_ID}建立 HTTP Proxy,接收要求並使用網址對應轉送要求:
gcloud compute target-http-proxies create hello-http-proxy \ --url-map=hello-url-map \ --project=${PROJECT_ID}建立通用轉送規則:
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
建立
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 政策繫結,允許未經驗證的叫用,讓服務可公開存取。在
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。
初始化並套用 Terraform 設定:
terraform init terraform applyTerraform 會將資源部署至專案。
在 App Hub 中定義應用程式
將資源部署為轉送規則和 Cloud Run 服務後,請按照下列步驟將資源註冊為應用程式的服務,在 App Hub 應用程式中將資源分組:
控制台
前往 App Hub 的「應用程式」頁面:
按一下「建立應用程式」。
選取「Global」(全球) 做為應用程式位置。
在「應用程式名稱」欄位輸入
my-global-app,然後按一下「繼續」。(選用) 新增顯示名稱、重要性、環境和擁有者。
點選「建立」。
在「服務和工作負載」分頁中,按一下「註冊服務/工作負載」。
選取轉送規則,將其命名為
frontend-service,然後按一下「註冊」。選取 Cloud Run 服務,將其命名為
backend-service,然後按一下「註冊」。
gcloud
建立應用程式:
gcloud apphub applications create my-global-app \ --location=global \ --display-name="My Global Application" \ --project=${PROJECT_ID}在適當的區域中找出轉送規則和 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。
將轉送規則註冊至全域應用程式:
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。將 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
建立
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資源,建立應用程式元件的邏輯分組。本範例會建立全域應用程式,並定義屬性以利管理及探索,例如重要性、環境和擁有者。您可以修改範例設定中的這些值。
在
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 會透過這個步驟,找出您要註冊為服務的特定資源。在
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 中定義的應用程式。初始化並套用 Terraform 設定:
terraform init terraform applyTerraform 會在 App Hub 中向您的
my-global-app應用程式註冊資源。
選用:監控新應用程式
在 App Hub 中定義應用程式後,即可使用整合式Google Cloud 產品監控應用程式的健康狀態和效能:
在 Cloud Hub 中查看作業資料:
前往 Google Cloud 控制台的 Cloud Hub「首頁」頁面。
從應用程式選取器中選擇
my-global-app應用程式。 這個頁面會顯示應用程式健康狀態的摘要。詳情請參閱「Cloud Hub 總覽」。
在應用程式監控中查看詳細資訊主頁:
-
前往 Google Cloud 控制台的「Application monitoring」(應用程式監控) 頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。
在 Google Cloud 控制台的專案選擇工具中,選取管理專案。
「應用程式監控」頁面會顯示應用程式的預先定義資訊主頁。詳情請參閱「監控應用程式健康狀態和效能」。
如需有關如何使用預先定義的資訊主頁,以及如何探索遙測資料的詳細操作說明,請參閱「查看應用程式遙測資料」。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。