使用 Cloud Storage 值區設定傳統版應用程式負載平衡器

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

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


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


使用 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 值區和負載平衡器。因此您必須是專案擁有者或編輯者,或是具有下列 Compute Engine 身分與存取權管理角色

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

詳情請參閱下列指南:

設定 SSL 憑證資源

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

建議使用 Google 代管的憑證。

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

準備 Cloud Storage 值區和內容

準備 Cloud Storage 值區的程序如下:

  • 建立值區。

  • 將內容複製到值區。

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

建立 Cloud Storage 值區

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

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

主控台

  1. 在 Google Cloud 控制台,前往「Cloud Storage 值區」頁面。

    前往 Cloud Storage 儲存桶

  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 值區頁面。按照這些操作說明建立第二個值區,不過請將「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

如要建立值區,請使用 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 值區複製到自己的 Cloud Storage 值區。

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 控制台中,按一下各個值區詳細資料頁面中的「Refresh」,藉此確認檔案已複製成功。

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

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

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

主控台

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

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

    前往 Cloud Storage 儲存桶

  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 位址) 頁面。

    前往「外部 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"
}

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

本說明涵蓋如何建立 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. 在「全域或單一區域部署」部分,選取「最適合全域工作負載」,然後點選「Next」
  6. 在「Load balancer generation」(負載平衡器代別) 部分,選取「Classic 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. 按一下「Host and path rules」(主機與路徑規則)
  2. 針對 dogs,請在「Hosts」欄位中輸入 *,並在「Paths」欄位中輸入 /love-to-fetch/*

設定前端

  1. 按一下「前端設定」

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

    屬性 值 (輸入值或選取指定的選項)
    通訊協定 HTTP
    網路服務級別 進階
    IP 版本 IPv4
    IP 位址 example-ip
    通訊埠 80

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

    屬性 值 (輸入值或選取指定的選項)
    通訊協定 HTTP(S)
    網路服務級別 Premium
    IP 版本 IPv4
    IP 位址 example-ip
    通訊埠 443
    憑證 選取您在「設定 SSL 憑證資源」一節中建立的 www-ssl-cert 憑證,或建立新的憑證。
    選用:啟用從 HTTP 重新導向至 HTTPS 的功能 使用這個核取方塊啟用重新導向。

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

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

  3. 按一下 [完成]

檢閱設定

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

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

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

  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 \
  --url-map=http-lb

設定轉送規則

gcloud compute forwarding-rules create http-lb-forwarding-rule \
  --load-balancing-scheme=EXTERNAL \
  --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 位址。

主控台

透過網路瀏覽器前往下列位址來測試負載平衡器,並將 IP_ADDRESS 替換為負載平衡器的 IP 位址

  • http://IP_ADDRESS/love-to-fetch/two-dogs.jpg

  • http://IP_ADDRESS/never-fetch/three-cats.jpg

如果您已設定 HTTP 負載平衡器,請確認瀏覽器不會自動重新導向至 HTTPS。

gcloud

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

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

Cloud Storage XML API 的查詢字串參數

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

下表概略說明當要求透過應用程式負載平衡器轉送時,Cloud Storage XML API 如何回應各種查詢參數。參數會依據觀察到的行為進行分組,協助您瞭解哪些參數在這個情境中受到支援、忽略或拒絕。

參數類型 參數 觀察到的行為
支援的參數 generationprefixmarkermax-keys 新增這些參數 (並提供適當值) 的運作方式,如 Cloud Storage XML API 說明文件所述。API 會傳回標準 HTTP 回應
已略過的參數 aclbillingcomposedelimiterencryptionencryptionConfigresponse-content-dispositionresponse-content-typetaggingversionswebsiteConfig 新增這些參數不會產生任何影響。

如果負載平衡器將這些參數傳遞至 Cloud Storage,Cloud Storage XML API 會忽略這些參數,並回應為參數不存在。
遭拒的參數 corslifecyclelocationloggingstorageClassversioning Cloud Storage XML API 會傳回「Unsupported query parameter」錯誤。

限制