使用 Cloud Storage 儲存桶設定全域外部應用程式負載平衡器

本文說明如何建立外部應用程式負載平衡器,將靜態內容的要求轉送至 Cloud Storage 值區。透過後端值區設定負載平衡器後,系統會將網址路徑開頭為 /love-to-fetch 的要求傳送至 us-east1 Cloud Storage 值區,而所有其他要求則會傳送至 europe-north1 Cloud Storage 值區,不論使用者位於哪個區域。

如果後端透過 HTTP(S) 提供動態內容,建議使用後端服務,而非後端值區。

如果您是傳統版應用程式負載平衡器的現有使用者,請務必在規劃使用全域外部應用程式負載平衡器的新部署作業時,查看遷移作業總覽

Cloud Storage 值區做為負載平衡器後端

外部應用程式負載平衡器會利用網址對應,將來自指定網址路徑的流量導向後端。

在下圖中,負載平衡器將路徑為 /love-to-fetch/ 的流量傳送至 us-east1 區域的 Cloud Storage 值區,並將所有其他要求傳送至 europe-north1 區域的 Cloud Storage 值區。

負載平衡器會將流量傳送至 Cloud Storage 後端。
將流量分配至 Cloud Storage

根據預設,Cloud Storage 會使用與 Cloud CDN 相同的快取。在後端值區啟用 Cloud CDN 後,您就能使用 Cloud CDN 控制項管理內容。Cloud CDN 控制項包括快取模式、已簽署網址和撤銷等。Cloud CDN 也可快取大型內容 (超過 10 MB)。如果未在後端值區啟用 Cloud CDN,您只能使用來源 Cache-Control 標頭,控管 Cloud Storage 中繼資料設定的較小內容快取。

事前準備

請確認設定符合下列先決條件。如果您使用 gcloud storage 公用程式,可以按照「使用 gcloud 工具探索物件儲存空間」一文中的操作說明安裝。

設定預設專案

控制台

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

gcloud

gcloud config set project PROJECT_ID

PROJECT_ID 替換為您在本指南中使用的專案。

Terraform

export GOOGLE_CLOUD_PROJECT=PROJECT_ID

權限

如要按照本指南操作,您必須在專案中建立 Cloud Storage bucket 和負載平衡器,因此您必須是專案擁有者或編輯者,或是需要下列 Compute Engine 身分與存取權管理角色

工作 必要角色
建立負載平衡器元件 網路管理員
建立 Cloud Storage 值區 Storage 物件管理員

詳情請參閱下列指南:

設定 SSL 憑證資源

如為 HTTPS 負載平衡器,請按照下列文件說明建立 SSL 憑證資源:

建議使用 Google 代管的憑證。

這個範例假設您已有名為 www-ssl-cert 的 SSL 憑證資源。

準備 Cloud Storage 值區和內容

準備 Cloud Storage bucket 的程序如下:

  • 建立 bucket。

  • 將內容複製到 bucket。

  • 提供值區的公開存取權。

建立 Cloud Storage 值區

在本範例中,您會建立兩個 Cloud Storage bucket,供負載平衡器存取。如為實際工作環境的部署作業,建議您選擇多區域 bucket,系統會自動在多個 Google Cloud 區域複製物件。這有助於提高內容的可用性,並提升應用程式的容錯能力。

請記下您建立的 Cloud Storage bucket 名稱,以便稍後使用。在本指南中,我們將這兩個值分別稱為 BUCKET_1_NAMEBUCKET_2_NAME

主控台

  1. 前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。

    前往 Cloud Storage bucket

  2. 點選「建立值區」

  3. 在「Name your bucket」(為值區命名) 方塊中,輸入符合命名規範的全域不重複名稱。

  4. 按一下「Choose where to store your data」(選取資料的儲存位置)

  5. 將「Location type」(位置類型) 設為「Region」(區域)

  6. 將「Location」(位置) 設為「europe-north1」。本指南中的 BUCKET_1_NAME

  7. 點選「建立」

  8. 按一下「Buckets」(值區),返回 Cloud Storage Buckets 頁面。 按照這些操作說明建立第二個 bucket,不過請將「Location」(位置)設為「us-east1」。本指南將說明如何BUCKET_2_NAME

gcloud

gcloud storage buckets create gs://BUCKET_1_NAME --project=PROJECT_ID --default-storage-class=standard --location=europe-north1 --uniform-bucket-level-access
gcloud storage buckets create gs://BUCKET_2_NAME --project=PROJECT_ID --default-storage-class=standard --location=us-east1 --uniform-bucket-level-access

BUCKET_1_NAMEBUCKET_2_NAME 替換為要建立的值區名稱。

Terraform

如要建立 bucket,請使用 google_storage_bucket 資源

# Create Cloud Storage buckets
resource "random_id" "bucket_prefix" {
  byte_length = 8
}

resource "google_storage_bucket" "bucket_1" {
  name                        = "${random_id.bucket_prefix.hex}-bucket-1"
  location                    = "us-east1"
  uniform_bucket_level_access = true
  storage_class               = "STANDARD"
  // delete bucket and contents on destroy.
  force_destroy = true
}

resource "google_storage_bucket" "bucket_2" {
  name                        = "${random_id.bucket_prefix.hex}-bucket-2"
  location                    = "us-east1"
  uniform_bucket_level_access = true
  storage_class               = "STANDARD"
  // delete bucket and contents on destroy.
  force_destroy = true
}

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。

將內容轉移至 Cloud Storage 值區

這樣一來,您就能測試設定,並將下列映像檔從公開 Cloud Storage bucket 複製到自己的 Cloud Storage bucket。

gcloud

  1. 按一下「Activate Cloud Shell」(啟用 Cloud Shell)

  2. 在 Cloud Shell 中執行下列指令,並將值區名稱變數改為您的 Cloud Storage 值區名稱:

gcloud storage cp gs://gcp-external-http-lb-with-bucket/three-cats.jpg gs://BUCKET_1_NAME/never-fetch/
gcloud storage cp gs://gcp-external-http-lb-with-bucket/two-dogs.jpg gs://BUCKET_2_NAME/love-to-fetch/

Terraform

如要將項目複製到值區,可以使用 google_storage_bucket_object 資源

resource "google_storage_bucket_object" "cat_image" {
  name         = "never-fetch/three-cats.jpg"
  source       = "images/three-cats.jpg"
  content_type = "image/jpeg"

  bucket = google_storage_bucket.bucket_1.name
}

resource "google_storage_bucket_object" "dog_image" {
  name         = "love-to-fetch/two-dogs.jpg"
  source       = "images/two-dogs.jpg"
  content_type = "image/jpeg"

  bucket = google_storage_bucket.bucket_2.name
}

或者,您也可以使用 null_resource 資源

resource "null_resource" "upload_cat_image" {
provisioner "local-exec" {
  command = "gcloud storage cp gs://gcp-external-http-lb-with-bucket/three-cats.jpg gs://${google_storage_bucket.bucket_1.name}/never-fetch/"
}
}

resource "null_resource" "upload_dog_image" {
provisioner "local-exec" {
  command = "gcloud storage cp gs://gcp-external-http-lb-with-bucket/two-dogs.jpg gs://${google_storage_bucket.bucket_2.name}/love-to-fetch/"
}
}

在 Google Cloud 控制台按一下各個 bucket 詳細資料頁面中的「Refresh」(重新整理),藉此確認檔案已複製成功。

將 Cloud Storage 值區設為可公開讀取

將 Cloud Storage bucket 設為可公開讀取之後,網際網路中的所有使用者都能列出及查看當中的物件,並檢視物件的中繼資料 (ACL 除外)。請勿在公開 bucket 中存放機密資訊。

為降低機密資訊意外曝光的可能性,請勿在同一個值區中儲存公開物件和機密資料。

主控台

如要向所有使用者授予 bucket 中物件的檢視權限,請為每個 bucket 重複執行下列程序:

  1. 前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。

    前往 Cloud Storage bucket

  2. 依序點選值區名稱和「Permissions」(權限) 分頁標籤。

  3. 按一下「新增」

  4. 在「New principals」(新增主體) 方塊中輸入 allUsers

  5. 在「Select a role」(請選擇角色) 方塊中,依序選取「Cloud Storage」>「Storage Object Viewer」(Storage 物件檢視者)

  6. 按一下「Save」(儲存)

  7. 按一下「Allow public access」(允許公開存取)

gcloud

如要向所有使用者授予值區中物件的檢視權限,請執行下列指令:

gcloud storage buckets add-iam-policy-binding gs://BUCKET_1_NAME --member=allUsers --role=roles/storage.objectViewer
gcloud storage buckets add-iam-policy-binding gs://BUCKET_2_NAME --member=allUsers --role=roles/storage.objectViewer

Terraform

如要向所有使用者授予值區中物件的檢視權限,請使用 google_storage_bucket_iam_member 資源並指定 allUsers 成員。

# Make buckets public
resource "google_storage_bucket_iam_member" "bucket_1" {
  bucket = google_storage_bucket.bucket_1.name
  role   = "roles/storage.objectViewer"
  member = "allUsers"
}

resource "google_storage_bucket_iam_member" "bucket_2" {
  bucket = google_storage_bucket.bucket_2.name
  role   = "roles/storage.objectViewer"
  member = "allUsers"
}

保留外部 IP 位址

設定 Cloud Storage 值區後,您可以保留全域靜態外部 IP 位址,供觀眾連上負載平衡器。

這個步驟為選用,但建議您執行,因為靜態外部 IP 位址會提供單一地址,讓網域指向該地址。

主控台

  1. 前往 Google Cloud 控制台的「External IP addresses」(外部 IP 位址) 頁面。

    前往「External IP addresses」(外部 IP 位址)

  2. 按一下 [Reserve static address] (保留靜態位址)。

  3. 在「Name」(名稱) 方塊中輸入 example-ip

  4. 將「Network Service Tier」(網路服務級別) 設為「Premium」(進階級)

  5. 將「IP version」(IP 版本) 設為「IPv4」

  6. 將「Type」(類型) 設為「Global」(通用)

  7. 按一下「保留」

gcloud

gcloud compute addresses create example-ip \
    --network-tier=PREMIUM \
    --ip-version=IPV4 \
    --global

請注意預留的 IPv4 位址:

gcloud compute addresses describe example-ip \
    --format="get(address)" \
    --global

Terraform

如要保留外部 IP 位址,請使用 google_compute_global_address 資源

# Reserve IP address
resource "google_compute_global_address" "default" {
  name = "example-ip"
}

建立具備後端 bucket 的外部應用程式負載平衡器

這些操作說明涵蓋建立 HTTP 或 HTTPS 負載平衡器。如要建立 HTTPS 負載平衡器,請在負載平衡器的前端新增 SSL 憑證資源。詳情請參閱「安全資料傳輸層 (SSL) 憑證總覽」。

主控台

開始設定

  1. 前往 Google Cloud 控制台的「Load balancing」(負載平衡)頁面。

    前往「Load balancing」(負載平衡) 頁面

  2. 點選「建立負載平衡器」
  3. 在「Type of load balancer」(負載平衡器類型) 部分,選取「Application Load Balancer (HTTP/HTTPS)」(應用程式負載平衡器 (HTTP/HTTPS)),然後點選「Next」(下一步)
  4. 在「公開或內部」部分,選取「公開 (外部)」,然後點選「下一步」
  5. 在「Global or single region deployment」(全域或單一區域部署) 部分,選取「Best for global workloads」(最適合全域工作負載),然後點選「Next」(下一步)
  6. 在「Load balancer generation」(負載平衡器代別) 部分,選取「Global external Application Load Balancer」(全域外部應用程式負載平衡器),然後點選「Next」(下一步)
  7. 按一下 [設定]

基本設定

  1. 在「Name」(名稱) 方塊中輸入 http-lb

設定後端

  1. 按一下「後端設定」

  2. 按一下「Backend services and backend buckets」(後端服務和後端 bucket) 方塊,然後點選「Create a backend bucket」(建立後端 bucket)

  3. 在「Backend bucket name」(後端 bucket 名稱) 方塊中輸入 cats

  4. 按一下「Cloud Storage bucket」(Cloud Storage 值區) 方塊中的「Browse」(瀏覽)

  5. 選取「BUCKET_1_NAME」,然後按一下「Select」(選取)。如果先建立 cats 後端 bucket,系統就會將其設為預設 bucket,並將所有不相符的流量要求導向該bucket。您無法在負載平衡器中變更預設後端 bucket 的重新導向規則。

  6. 點選「建立」

  7. 按照相同的程序建立名稱為 dogs 的後端值區,然後選取「BUCKET_2_NAME」。

  8. 按一下 [確定]

設定轉送規則

轉送規則會決定流量的導向方式,如要設定轉送,請設定主機規則和路徑比對器,這些是外部應用程式負載平衡器網址對應的設定元件。如要設定這個範例的規則,請按照下列指示操作:

  1. 按一下「轉送規則」。
  2. dogs 的「Hosts」(主機) 欄位中輸入 *,並在「Paths」(路徑) 欄位中輸入 /love-to-fetch/*

設定前端

  1. 按一下「前端設定」

  2. 確認下列選項已設為指定的值:

    屬性 值 (按照指示輸入值或選取選項)
    通訊協定 HTTP
    網路服務級別 進階
    IP 版本 IPv4
    IP 位址 example-ip
    通訊埠 80
    選用:HTTP 保持運作逾時 請輸入介於 5 到 1200 秒之間的逾時值。預設值為 610 秒。

    如要建立 HTTPS 負載平衡器,而非 HTTP 負載平衡器,您必須具備 SSL 憑證 (gcloud compute ssl-certificates list),並填寫下列欄位:

    屬性 值 (按照指示輸入值或選取選項)
    通訊協定 HTTP(S)
    網路服務級別 進階版
    IP 版本 IPv4
    IP 位址 example-ip
    通訊埠 443
    選用:HTTP 保持運作逾時 請輸入介於 5 到 1200 秒之間的逾時值。預設值為 610 秒。
    憑證 選取您在「設定 SSL 憑證資源」一節中建立的 www-ssl-cert 憑證,或建立新憑證。
    選用:啟用從 HTTP 重新導向至 HTTPS 的功能 勾選這個核取方塊即可啟用重新導向。

    勾選這個核取方塊後,系統會建立額外的部分 HTTP 負載平衡器,該平衡器會使用與 HTTPS 負載平衡器相同的 IP 位址,並將 HTTP 要求重新導向至負載平衡器的 HTTPS 前端。

    只有在選取 HTTPS 通訊協定並使用預留 IP 位址時,才能勾選這個核取方塊。

  3. 按一下 [完成]

檢閱設定

  1. 按一下「檢查並完成」

  2. 檢查「Frontend」(前端)、「Host and path rules」(主機與路徑規則) 和「Backend buckets」(後端值區)。

  3. 按一下「建立」,然後等待負載平衡器建立完成。

  4. 按一下負載平衡器的名稱 (http-lb)。

  5. 記下負載平衡器的 IP 位址,以便在下一項工作中使用。在本指南中,這項配額的顯示名稱為 IP_ADDRESS

gcloud

設定後端

gcloud compute backend-buckets create cats \
  --gcs-bucket-name=BUCKET_1_NAME
gcloud compute backend-buckets create dogs \
  --gcs-bucket-name=BUCKET_2_NAME

設定網址對應

gcloud compute url-maps create http-lb \
  --default-backend-bucket=cats
gcloud compute url-maps add-path-matcher http-lb \
  --path-matcher-name=path-matcher-2 \
  --new-hosts=* \
  --backend-bucket-path-rules="/love-to-fetch/*=dogs" \
  --default-backend-bucket=cats

設定目標 Proxy

gcloud compute target-http-proxies create http-lb-proxy \
  --http-keep-alive-timeout-sec=HTTP_KEEP_ALIVE_TIMEOUT_SEC \
  --url-map=http-lb

HTTP_KEEP_ALIVE_TIMEOUT_SEC 替換為 client HTTP keepalive timeout 值,範圍為 5 到 1200 秒。預設值為 610 秒。這是選填欄位。

設定轉送規則

gcloud compute forwarding-rules create http-lb-forwarding-rule \
  --load-balancing-scheme=EXTERNAL_MANAGED \
  --network-tier=PREMIUM \
  --address=example-ip \
  --global \
  --target-http-proxy=http-lb-proxy \
  --ports=80

Terraform

如要建立負載平衡器,請使用下列 Terraform 資源。

設定後端

如要建立後端,請使用 google_compute_backend_bucket 資源

# Create LB backend buckets
resource "google_compute_backend_bucket" "bucket_1" {
  name        = "cats"
  description = "Contains cat image"
  bucket_name = google_storage_bucket.bucket_1.name
}

resource "google_compute_backend_bucket" "bucket_2" {
  name        = "dogs"
  description = "Contains dog image"
  bucket_name = google_storage_bucket.bucket_2.name
}

設定網址對應

如要建立網址對應,請使用 google_compute_url_map 資源

# Create url map
resource "google_compute_url_map" "default" {
  name = "http-lb"

  default_service = google_compute_backend_bucket.bucket_1.id

  host_rule {
    hosts        = ["*"]
    path_matcher = "path-matcher-2"
  }
  path_matcher {
    name            = "path-matcher-2"
    default_service = google_compute_backend_bucket.bucket_1.id

    path_rule {
      paths   = ["/love-to-fetch/*"]
      service = google_compute_backend_bucket.bucket_2.id
    }
  }
}

設定目標 Proxy

如要建立目標 HTTP Proxy,請使用 google_compute_target_http_proxy 資源

# Create HTTP target proxy
resource "google_compute_target_http_proxy" "default" {
  name    = "http-lb-proxy"
  url_map = google_compute_url_map.default.id
}

設定轉送規則

如要建立轉送規則,請使用 google_compute_global_forwarding_rule 資源

# Create forwarding rule
resource "google_compute_global_forwarding_rule" "default" {
  name                  = "http-lb-forwarding-rule"
  ip_protocol           = "TCP"
  load_balancing_scheme = "EXTERNAL_MANAGED"
  port_range            = "80"
  target                = google_compute_target_http_proxy.default.id
  ip_address            = google_compute_global_address.default.id
}

注意:如要將模式變更為傳統版應用程式負載平衡器,請將 load_balancing_scheme 屬性設為 "EXTERNAL",而非 "EXTERNAL_MANAGED"

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。

將流量傳送至負載平衡器

負載平衡器設定完畢的幾分鐘後,您就可以開始將流量傳送至負載平衡器的 IP 位址。

主控台

系統不支援 Google Cloud 控制台。

gcloud

使用 curl 指令測試下列網址的回覆。將 IP_ADDRESS 替換為負載平衡器的 IPv4 位址

curl http://IP_ADDRESS/love-to-fetch/two-dogs.jpg
curl http://IP_ADDRESS/never-fetch/three-cats.jpg

額外設定

本節會延伸說明設定範例,並提供替代和其他設定選項。所有工作都是選擇性的。您可以按任何順序執行這些工作。

更新用戶端 HTTP 保持運作逾時

先前步驟中建立的負載平衡器已設定 用戶端 HTTP 保持連線逾時的預設值。

如要更新用戶端 HTTP 保持連線逾時,請按照下列操作說明操作。

控制台

  1. 前往 Google Cloud 控制台的「Load balancing」(負載平衡)頁面。

    前往「Load balancing」(負載平衡)

  2. 按一下要修改的負載平衡器名稱。
  3. 按一下「編輯」
  4. 按一下「前端設定」
  5. 展開「進階功能」。在「HTTP 保持運作逾時」部分, 輸入逾時值。
  6. 按一下「更新」
  7. 如要檢查變更,請依序按一下「檢查並完成」和「更新」

gcloud

如果是 HTTP 負載平衡器,請使用 gcloud compute target-http-proxies update 指令更新目標 HTTP Proxy:

    gcloud compute target-http-proxies update TARGET_HTTP_PROXY_NAME \
        --http-keep-alive-timeout-sec=HTTP_KEEP_ALIVE_TIMEOUT_SEC \
        --global
    

如果是 HTTPS 負載平衡器,請使用 gcloud compute target-https-proxies update 指令更新目標 HTTPS Proxy:

    gcloud compute target-https-proxies update TARGET_HTTPS_PROXY_NAME \
        --http-keep-alive-timeout-sec=HTTP_KEEP_ALIVE_TIMEOUT_SEC \
        --global
    

更改下列內容:

  • TARGET_HTTP_PROXY_NAME:目標 HTTP Proxy 的名稱。
  • TARGET_HTTPS_PROXY_NAME:目標 HTTPS Proxy 的名稱。
  • HTTP_KEEP_ALIVE_TIMEOUT_SEC:HTTP 保持運作逾時值,範圍為 5 到 600 秒。

Cloud Storage XML API 的查詢字串參數

如果透過應用程式負載平衡器傳送至後端 bucket 的要求包含特定查詢字串參數,用戶端會收到 HTTP 404 回應,並顯示「Unsupported query parameter」(不支援的查詢參數) 錯誤。這是因為要求來自應用程式負載平衡器時,Cloud Storage XML API 不支援這些參數。

下表摘要說明透過 Application Load Balancer 路由傳送要求時,Cloud Storage XML API 如何回應各種查詢參數。參數會依觀察到的行為分組,方便您識別在此情境中支援、忽略或拒絕的參數。

參數類型 參數 觀察到的行為
支援的參數 generation, prefix, marker, max-keys 如要新增這些參數 (並提供適當的值),請參閱 Cloud Storage XML API 說明文件中的說明。API 會傳回標準 HTTP 回應
已忽略的參數 aclbillingcomposedelimiterencryptionencryptionConfigresponse-content-dispositionresponse-content-typetaggingversionswebsiteConfig 新增這些參數不會有任何效果。

如果負載平衡器將這些參數傳遞至 Cloud Storage,Cloud Storage XML API 會忽略這些參數,並回應這些參數不存在。
遭拒的參數 cors, lifecycle, location, logging, storageClass, versioning Cloud Storage XML API 會傳回「Unsupported query parameter」(不支援的查詢參數) 錯誤。

限制

  • 後端儲存空間僅適用於全域外部應用程式負載平衡器和傳統版應用程式負載平衡器。區域外部應用程式負載平衡器或任何其他負載平衡器類型都不支援這些功能。
  • Identity-Aware Proxy 不支援後端 bucket。
  • 全域外部應用程式負載平衡器不支援上傳至 Cloud Storage 值區。
  • 除非啟用 Cloud CDN,否則全域外部應用程式負載平衡器不支援使用已簽署的網址。

後續步驟