部署具有负载平衡器授权的全球 Google 代管证书


本教程介绍了如何使用证书管理服务部署具有负载均衡器授权的全球 Google 代管证书。

负载平衡器授权是获取 Google 代管证书的最有效方法。它可保持 DNS 配置的简洁性,并在您完成配置后预配 TLS 证书。

以下负载平衡器支持具有负载平衡器授权的 Google 管理的证书:

  • 全球外部应用负载均衡器
  • 传统应用负载均衡器
  • 全局外部代理网络负载均衡器

目标

本教程介绍如何完成以下任务:

  • 使用 Certificate Manager 创建由公开受信任的证书授权机构 (CA) 颁发的 Google 管理的证书,并具有负载均衡器授权。
  • 使用目标 HTTPS 代理将证书部署到受支持的负载均衡器。

如果您要将证书部署到生产网域,那么在负载均衡器上设置并激活证书期间,流量会短暂中断。

准备工作

  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 Owner (roles/certificatemanager.owner)

      创建和管理 Certificate Manager 资源时需要此权限。

    • Compute Load Balancer Admin (roles/compute.loadBalancerAdmin) 或 Compute Network Admin (roles/compute.networkAdmin)

      创建和管理 HTTPS 目标代理时需要此权限。

    • DNS Administrator (roles/dns.admin)

      如果您想使用 Cloud DNS 作为 DNS 解决方案,则必须添加此角色。

    详情请参阅以下内容:

    域名

    如需创建证书,请获取您拥有的网域的完全限定域名 (FQDN)。如果您没有网域,可以使用 Cloud Domains 注册网域

    创建负载均衡器

    本教程假定您已创建并配置负载平衡器的后端、健康检查、后端服务和网址映射。如果您已创建外部应用负载平衡器,请记下网址映射的名称,因为您需要在本教程的后续步骤中使用该名称。

    如果您尚未创建负载均衡器,请参阅以下页面来创建负载均衡器:

创建具有负载均衡器授权的 Google 管理的证书

如需创建具有负载均衡器授权的 Google 管理的证书,请执行以下操作:

控制台

  1. 在 Google Cloud 控制台中,前往 Certificate Manager 页面。

    前往 Certificate Manager

  2. 证书标签页上,点击添加证书

  3. 证书名称字段中,输入证书的唯一名称。

  4. 可选:在说明字段中,输入证书的说明。通过说明,您可以识别证书。

  5. 位置部分,选择全球

  6. 对于范围,选择默认

  7. 证书类型部分,选择创建 Google 管理的证书

  8. 对于证书授权机构类型,请选择公开

  9. 域名字段中,指定以英文逗号分隔的证书域名列表。每个域名都必须是完全限定域名,例如 myorg.example.com

  10. 对于授权类型,选择负载平衡器授权

  11. 标签字段中,指定要与证书关联的标签。如需添加标签,请点击 添加标签,并为标签指定键和值。

  12. 点击创建

    新证书会显示在证书列表中。

gcloud

如需创建具有负载均衡器授权的由 Google 管理的全局证书,请使用 certificate-manager certificates create 命令

gcloud certificate-manager certificates create CERTIFICATE_NAME \
    --domains="DOMAIN_NAMES"

替换以下内容:

  • CERTIFICATE_NAME:证书的名称。
  • DOMAIN_NAMES:以英文逗号分隔的目标网域列表。每个域名都必须是完全限定域名,例如 myorg.example.com

Terraform

使用 google_certificate_manager_certificate 资源

resource "google_certificate_manager_certificate" "default" {
  name        = "${local.name}-rootcert-${random_id.tf_prefix.hex}"
  description = "Cert with LB authorization"
  managed {
    domains = [local.domain]
  }
  labels = {
    "terraform" : true
  }
}

如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令

API

通过向 certificates.create 方法发出 POST 请求来创建证书,如下所示:

POST /v1/projects/PROJECT_ID/locations/global/certificates?certificate_id=CERTIFICATE_NAME"
{
 "managed": {
  "domains": ["DOMAIN_NAME"],
 }
}

替换以下内容:

  • PROJECT_ID: Google Cloud 项目的 ID。
  • CERTIFICATE_NAME:证书的名称。
  • DOMAIN_NAMES:以英文逗号分隔的目标网域列表。每个域名都必须是完全限定域名,例如 myorg.example.com

将证书部署到负载均衡器

如需部署全球 Google 管理的证书,请使用证书映射。

创建证书映射

创建引用与您的证书相关联的证书映射条目的证书映射:

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}"
}

验证证书映射条目是否处于有效状态

在将相应的证书映射关联到目标代理之前,请验证证书映射条目是否处于活跃状态。

如需验证证书映射条目,请使用 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'

将证书映射附加到目标代理

您可以将证书映射附加到新的目标代理或现有目标代理。

gcloud

如需将证书映射附加到新的目标代理,请使用 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:目标代理的名称。
  • CERTIFICATE_MAP_NAME:引用证书映射条目和关联证书的证书映射的名称。
  • URL_MAP:网址映射的名称

如需将证书映射附加到现有目标 HTTPS 代理,请使用 gcloud compute target-https-proxies update 命令。如果您不知道现有目标代理的名称,请前往目标代理页面,并记下目标代理的名称。

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

创建或更新目标代理后,运行以下命令进行验证:

gcloud compute target-https-proxies list

Terraform

如需将证书映射附加到目标代理,您可以使用 google_compute_target_https_proxy 资源

配置目标代理时,如果您直接附加 TLS (SSL) 证书,同时也通过证书映射附加 TLS (SSL) 证书,则代理会使用证书映射引用的证书,并忽略直接附加的 TLS (SSL) 证书。

验证证书的状态

在将证书部署到负载均衡器之前,请验证证书是否处于有效状态。证书状态可能需要几分钟才能更改为 ACTIVE

控制台

  1. 在 Google Cloud 控制台中,前往 Certificate Manager 页面。

    前往 Certificate Manager

  2. 证书标签页上,查看证书的状态列。

gcloud

如需验证证书的状态,请运行以下命令:

gcloud certificate-manager certificates describe CERTIFICATE_NAME

CERTIFICATE_NAME 替换为目标 Google 管理的证书的名称。

输出类似于以下内容:

createTime: '2021-10-20T12:19:53.370778666Z'
expireTime: '2022-05-07T05:03:49Z'
managed:
  authorizationAttemptInfo:
  - domain: myorg.example.com
    state: AUTHORIZED
  domains:
  - myorg.example.com
  state: ACTIVE
name: projects/myProject/locations/global/certificates/myCert
pemCertificate: |
  -----BEGIN CERTIFICATE-----
  [...]
  -----END CERTIFICATE-----
sanDnsnames:
  - myorg.example.com
updateTime: '2021-10-20T12:19:55.083385630Z'

如果几个小时后证书状态仍不是 ACTIVE,请检查以下事项:

如需了解更多问题排查步骤,请参阅 Certificate Manager 问题排查

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除这些资源。

  1. 删除负载均衡器及其资源。

    如需了解详情,请参阅清理负载均衡设置

  2. 从代理中删除或分离证书映射。

    如需删除证书映射,请运行以下命令:

    gcloud compute target-https-proxies delete PROXY_NAME
    

    如果您想保留目标 HTTPS 代理,请从该代理分离证书映射。在分离证书映射之前,请注意以下事项:

    • 如果任何 TLS (SSL) 证书直接附加到代理,分离证书映射会导致代理恢复使用这些直接附加的 TLS (SSL) 证书。
    • 如果没有直接附加到代理的 TLS (SSL) 证书,则无法从代理分离证书映射。您必须先将至少一个 TLS (SSL) 证书直接附加到代理,然后才能分离证书映射。

    如需分离证书映射,请运行以下命令:

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

    PROXY_NAME 替换为目标代理的名称。

  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. 删除 Google 托管的证书:

    控制台

    1. 在 Google Cloud 控制台中,前往 Certificate Manager 页面。

      前往 Certificate Manager

    2. 证书标签页上,选中相应证书的复选框。

    3. 点击删除

    4. 在显示的对话框中,点击删除进行确认。

    gcloud

    gcloud certificate-manager certificates delete CERTIFICATE_NAME
    

    CERTIFICATE_NAME 替换为目标证书的名称。

后续步骤