部署自行管理的全球证书

本教程介绍了如何使用 Certificate Manager 部署自行管理的全球证书。

以下负载平衡器支持自行管理的全球证书:

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

如果您想部署到区域级或跨区域负载平衡器,请参阅以下内容:

目标

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

  • 将自行管理的证书上传到 Certificate Manager。
  • 使用目标 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 目标代理时需要此角色。

详情请参阅以下内容:

创建负载均衡器

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

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

创建私钥和证书

如需创建私钥和证书,请执行以下操作:

  1. 使用可信的第三方证书授权机构 (CA) 颁发证书及其关联的密钥。

  2. 验证证书是否已正确链接且根可信。

  3. 准备以下 PEM 编码的文件:

    • 证书文件 (CRT)
    • 对应的私钥文件 (KEY)

如需了解如何请求和验证证书,请参阅创建 私钥和 证书

将自行管理的证书上传到 Certificate Manager

如需将证书上传到 Certificate Manager,请执行以下操作:

控制台

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

    前往 Certificate Manager

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

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

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

  5. 对于位置 ,选择全球

  6. 对于范围,选择Default

  7. 对于证书类型 ,选择创建自行管理的证书

  8. 对于证书 字段,请执行以下操作之一:

    • 点击上传 按钮,然后选择您的 PEM 格式的证书文件。
    • 复制并粘贴 PEM 格式证书的内容。内容必须以 -----BEGIN CERTIFICATE----- 开头,并以 -----END CERTIFICATE----- 结尾。
  9. 对于私钥证书 字段,请执行以下操作之一:

    • 点击上传 按钮,然后选择您的私钥。您的私钥必须采用 PEM 格式,且不受口令保护。
    • 复制并粘贴 PEM 格式的私钥内容。私钥必须以 -----BEGIN PRIVATE KEY----- 开头,并以 -----END PRIVATE KEY----- 结尾。
  10. 标签 字段中,指定要与证书关联的标签。如需添加标签,请点击 添加标签,并为标签指定 键和值。

  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:KEY 私钥文件的路径和文件名。

Terraform

如需上传自行管理的证书,您可以使用 google_certificate_manager_certificate 资源,其中包含 self_managed 代码块。

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

验证证书映射条目是否处于活跃状态

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

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

清理

为避免因本教程中使用的资源 而向您的 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. 删除已上传的证书:

    gcloud certificate-manager certificates delete CERTIFICATE_NAME
    

    CERTIFICATE_NAME 替换为证书的名称。

后续步骤