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

本教程介绍如何使用 Certificate Manager 部署具有 负载均衡器授权的 Google 管理的全局证书。

借助负载均衡器授权,您可以在流量由负载均衡器提供服务时获取 Google 管理的网域证书。此方法不需要任何额外的 DNS 记录即可预配证书。

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

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

目标

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

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

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

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud的新用户, 请创建一个账号,以便在 真实场景中评估我们产品的性能。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine, Certificate Manager APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. 安装 Google Cloud CLI。

  6. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  7. 如需初始化 gcloud CLI,请运行以下命令:

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

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Compute Engine, Certificate Manager APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. 安装 Google Cloud CLI。

  12. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  13. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

所需角色

请确保您拥有以下角色,以便完成本教程中的任务:

  • 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:项目的 ID。 Google Cloud
  • 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 替换为目标证书的名称。

后续步骤