部署全球自行管理的憑證


本教學課程將說明如何使用憑證管理工具部署全球自管憑證。

下列負載平衡器支援全域自行管理憑證:

  • 全域外部應用程式負載平衡器
  • 傳統版應用程式負載平衡器
  • 全域外部 Proxy 網路負載平衡器
  • 傳統版 Proxy 網路負載平衡器

如要部署至區域內或跨區域負載平衡器,請參閱以下說明:

目標

本教學課程將說明如何完成下列工作:

  • 將自行管理的憑證上傳至憑證管理工具。
  • 使用目標 HTTPS Proxy,將憑證部署至支援的負載平衡器。

事前準備

  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. Enable the Compute Engine, Certificate Manager APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  10. Enable the Compute Engine, Certificate Manager APIs.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. 必要的角色

    請確認您具備下列角色,才能完成本教學課程中的任務:

    • Certificate Manager 擁有者 (roles/certificatemanager.owner)

      建立及管理 Certificate Manager 資源時必須使用。

    • Compute 負載平衡器管理員 (roles/compute.loadBalancerAdmin) 或 Compute 網路管理員 (roles/compute.networkAdmin)

      建立及管理 HTTPS 目標 Proxy 時必須使用。

    如要瞭解詳情,請參考下列資源:

    建立負載平衡器

    本教學課程假設您已建立並設定負載平衡器的後端、健康狀態檢查、後端服務和網址對應。如果您已建立外部應用程式負載平衡器,請記下網址對應項目的名稱,因為您稍後會在本教學課程中使用這項資訊。

    如果您尚未建立負載平衡器,請參閱下列頁面瞭解如何建立負載平衡器:

    建立私密金鑰和憑證

    如要建立私密金鑰和憑證,請執行下列步驟:

    1. 使用值得信賴的第三方憑證授權單位 (CA) 核發憑證,並附上相關聯的金鑰。

    2. 確認憑證已正確鏈結且受根信任。

    3. 準備下列 PEM 編碼檔案:

      • 憑證檔案 (CRT)
      • 對應的私密金鑰檔案 (KEY)

    如要瞭解如何要求及驗證憑證,請參閱「建立私密金鑰和憑證」。

將自行管理的憑證上傳至憑證管理工具

如要將憑證上傳至憑證管理工具,請按照下列步驟操作:

主控台

  1. 前往 Google Cloud 控制台的「Certificate Manager」頁面。

    前往 Certificate Manager

  2. 在「憑證」分頁中,按一下「新增憑證」

  3. 在「Certificate name」欄位中,輸入憑證的專屬名稱。

  4. 選用:在「說明」欄位中輸入證書的說明。您可以透過說明辨識憑證。

  5. 在「Location」(位置) 部分,選取「Global」

  6. 在「範圍」部分,選取「預設」

  7. 在「憑證類型」部分,選取「建立自行管理的憑證」

  8. 針對「Certificate」欄位,請執行下列任一操作:

    • 按一下「上傳」按鈕,然後選取 PEM 格式的憑證檔案。
    • 複製 PEM 格式憑證的內容,然後貼上。內容的開頭和結尾必須是 -----BEGIN CERTIFICATE----------END CERTIFICATE-----
  9. 針對「Private key certificate」欄位,請執行下列任一操作:

    • 按一下「上傳」按鈕,然後選取私密金鑰。您的私密金鑰必須採用 PEM 格式,且未受密碼字串保護。
    • 複製並貼上 PEM 格式私密金鑰的內容。私密金鑰開頭必須為 -----BEGIN PRIVATE KEY-----,結尾為 -----END PRIVATE KEY-----
  10. 在「Labels」欄位中,指定要與憑證建立關聯的標籤。如要新增標籤,請按一下 「Add label」,然後指定標籤的鍵和值。

  11. 按一下 [建立]。

    新的憑證會顯示在憑證清單中。

gcloud

如要建立全域自行管理憑證,請使用 certificate-manager certificates create 指令

gcloud certificate-manager certificates create CERTIFICATE_NAME \
    --certificate-file="CERTIFICATE_FILE" \
    --private-key-file="PRIVATE_KEY_FILE"

更改下列內容:

  • CERTIFICATE_NAME:憑證名稱。
  • CERTIFICATE_FILE:CRT 憑證檔案的路徑和檔案名稱。
  • PRIVATE_KEY_FILE:金鑰私密金鑰檔案的路徑和檔案名稱。

Terraform

如要上傳自行管理的憑證,您可以使用 self_managed 區塊搭配 google_certificate_manager_certificate 資源

API

certificates.create 方法發出 POST 要求,即可上傳憑證,如下所示:

POST /v1/projects/PROJECT_ID/locations/global/certificates?certificate_id=CERTIFICATE_NAME
{
  self_managed: {
    pem_certificate: "PEM_CERTIFICATE",
    pem_private_key: "PEM_KEY",
  }
}

更改下列內容:

  • PROJECT_ID: Google Cloud 專案的 ID。
  • CERTIFICATE_NAME:憑證名稱。
  • PEM_CERTIFICATE:憑證 PEM。
  • PEM_KEY:PEM 金鑰。

將自行管理的憑證部署至負載平衡器

如要部署全球自行管理憑證,請使用憑證對應項目。

建立憑證對應關係

建立憑證對應關係,參照與憑證相關聯的憑證對應項目:

gcloud

如要建立憑證對應,請使用 gcloud certificate-manager maps create 指令

gcloud certificate-manager maps create CERTIFICATE_MAP_NAME

CERTIFICATE_MAP_NAME 替換為目標憑證對應項目的名稱。

Terraform

如要建立憑證對應關係,您可以使用 google_certificate_manager_certificate_map 資源

resource "google_certificate_manager_certificate_map" "certificate_map" {
  name        = "${local.name}-certmap-${random_id.tf_prefix.hex}"
  description = "${local.domain} certificate map"
  labels = {
    "terraform" : true
  }
}

建立憑證對應關係項目

建立憑證對應項目,並將其與憑證和憑證對應關係建立關聯:

gcloud

如要建立憑證對應項目,請使用 gcloud certificate-manager maps entries create 指令

gcloud certificate-manager maps entries create CERTIFICATE_MAP_ENTRY_NAME \
    --map="CERTIFICATE_MAP_NAME" \
    --certificates="CERTIFICATE_NAME" \
    --hostname="HOSTNAME"

更改下列內容:

  • CERTIFICATE_MAP_ENTRY_NAME:憑證對應項目的名稱。
  • CERTIFICATE_MAP_NAME:憑證對應項目所屬的憑證對應關係名稱。
  • CERTIFICATE_NAME:您要與憑證對應項目建立關聯的憑證名稱。
  • HOSTNAME:您要與憑證對應項目建立關聯的主機名稱。

    如果您想建立涵蓋萬用字元網域和根網域的憑證,請使用根網域和萬用字元指定主機名稱,例如 example.com*.example.com。此外,您必須指定兩個憑證對應項目,一個用於 example.com,另一個用於 *.example.com

Terraform

如要建立含有根網域的憑證對應項目,請使用 google_certificate_manager_certificate_map_entry 資源

resource "google_certificate_manager_certificate_map_entry" "first_entry" {
  name        = "${local.name}-first-entry-${random_id.tf_prefix.hex}"
  description = "example certificate map entry"
  map         = google_certificate_manager_certificate_map.certificate_map.name
  labels = {
    "terraform" : true
  }
  certificates = [google_certificate_manager_certificate.root_cert.id]
  hostname     = local.domain
}

如要建立含萬用字元網域的憑證對應項目,請使用 google_certificate_manager_certificate_map_entry 資源

resource "google_certificate_manager_certificate_map_entry" "second_entry" {
  name        = "${local.name}-second-entity-${random_id.tf_prefix.hex}"
  description = "example certificate map entry"
  map         = google_certificate_manager_certificate_map.certificate_map.name
  labels = {
    "terraform" : true
  }
  certificates = [google_certificate_manager_certificate.root_cert.id]
  hostname     = "*.${local.domain}"
}

確認憑證對應項目是否處於啟用狀態

請先確認憑證對應項目是否處於啟用狀態,再將對應的憑證對應項目附加至目標 Proxy。

如要驗證憑證對應項目,請使用 gcloud certificate-manager maps entries describe 指令

gcloud certificate-manager maps entries describe CERTIFICATE_MAP_ENTRY_NAME \
    --map="CERTIFICATE_MAP_NAME"

更改下列內容:

  • CERTIFICATE_MAP_ENTRY_NAME:憑證對應項目的名稱。
  • CERTIFICATE_NAME:您要與憑證對應項目建立關聯的憑證名稱。

輸出結果會與下列內容相似:

certificates:
createTime: '2021-09-06T10:01:56.229472109Z'
hostname: example.com
name: projects/my-project/locations/global/certificateMaps/myCertMap/certificateMapEntries/myCertMapEntry
state: ACTIVE
updateTime: '2021-09-06T10:01:58.277031787Z'

將憑證對應附加至目標 Proxy

您可以將憑證對應表附加至新的目標 Proxy 或現有目標 Proxy。

gcloud

如要將憑證對應設定附加至新的目標 Proxy,請使用 gcloud compute target-https-proxies create 指令

gcloud compute target-https-proxies create PROXY_NAME \
    --certificate-map="CERTIFICATE_MAP_NAME" \
    --url-map="URL_MAP" \
    --global

更改下列內容:

  • PROXY_NAME:目標 Proxy 的名稱。
  • CERTIFICATE_MAP_NAME:參照憑證對應關係項目和相關憑證的憑證對應關係名稱。
  • URL_MAP:網址對應項目的名稱

如要將憑證對應表附加至現有的目標 HTTPS Proxy,請使用 gcloud compute target-https-proxies update 指令。如果不知道現有目標 Proxy 的名稱,請前往「目標 Proxy」頁面,並記下目標 Proxy 的名稱。

gcloud compute target-https-proxies update PROXY_NAME \
    --certificate-map="CERTIFICATE_MAP_NAME" \
    --global

建立或更新目標 Proxy 後,請執行下列指令來驗證:

gcloud compute target-https-proxies list

Terraform

如要將憑證對應項目附加至目標 Proxy,您可以使用 google_compute_target_https_proxy 資源

設定目標 Proxy 時,如果您直接附加 TLS (SSL) 憑證,並透過憑證對應附加,Proxy 會使用憑證對應參照的憑證,並忽略直接附加的 TLS (SSL) 憑證。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取您在本教學課程中使用資源的費用,請將這些資源刪除。

  1. 刪除負載平衡器及其資源。

    請參閱「清除負載平衡器設定」。

  2. 從 Proxy 中刪除或分離憑證對應關係。

    如要刪除憑證對應,請執行下列指令:

    gcloud compute target-https-proxies delete PROXY_NAME
    

    如果您想保留目標 HTTPS Proxy,請將憑證對應項目從 Proxy 中分離。卸除憑證對應前,請注意下列事項:

    • 如果有任何 TLS (SSL) 憑證直接附加至 Proxy,則解除憑證對應會導致 Proxy 恢復使用這些直接附加的 TLS (SSL) 憑證。
    • 如果沒有直接附加至 Proxy 的 TLS (SSL) 憑證,則無法將憑證對應項目從 Proxy 中分離。您必須先將至少一個 TLS (SSL) 憑證直接附加至 Proxy,才能解除憑證對應。

    如要卸除憑證對應,請執行下列指令:

    gcloud compute target-https-proxies update PROXY_NAME \
        --clear-certificate-map
    

    PROXY_NAME 替換為目標 Proxy 的名稱。

  3. 從憑證對應關係中刪除憑證對應項目:

    gcloud certificate-manager maps entries delete CERTIFICATE_MAP_ENTRY_NAME \
        --map="CERTIFICATE_MAP_NAME"
    

    更改下列內容:

    • CERTIFICATE_MAP_ENTRY_NAME:憑證對應項目的名稱。
    • CERTIFICATE_MAP_NAME:憑證對應關係的名稱。
  4. 刪除憑證對應關係:

    gcloud certificate-manager maps delete CERTIFICATE_MAP_NAME
    

    CERTIFICATE_MAP_NAME 替換為憑證對應項目的名稱。

  5. 刪除已上傳的憑證:

    gcloud certificate-manager certificates delete CERTIFICATE_NAME
    

    CERTIFICATE_NAME 改為憑證名稱。

後續步驟