创建基本配置

本页面介绍了如何创建可用于创建部署的配置。如需详细了解部署,请阅读创建部署

配置文件定义构成部署的所有 Google Cloud 资源。您必须有配置文件才能创建部署。配置文件必须用 YAML 语法编写。

准备工作

配置文件结构

配置文件用 YAML 格式编写且采用以下结构:

#  imported templates, if applicable
imports:
  #  path relative to the configuration file
- path: path/to/template.jinja
  name: my-template
- path: path/to/another/template.py
  name: another-template

resources:
  - name: NAME_OF_RESOURCE
    type: TYPE_OF_RESOURCE
    properties:
      property-a: value
      property-b: value
      ...
      property-z: value
  - name: NAME_OF_RESOURCE
    type: TYPE_OF_RESOURCE
    properties:
      property-a: value
      property-b: value
      ...
      property-z: value

每个部分定义部署的不同部分:

  • imports 部分列出了配置将使用的模板文件。Deployment Manager 递归扩展任何导入的模板以形成最终配置。

  • resources 部分列出了构成此部署的资源。资源可以是:

您还可以添加其他可选部分,例如 outputs 部分和 metadata 部分。outputs 部分允许您将模板和配置中的数据作为输出公开,以供同一部署中的其他模板或最终用户使用,而 metadata 部分允许您使用其他功能,例如在资源之间设置显式依赖项

通常,配置至少必须声明 resources 部分,后跟资源列表。其他部分可选。

声明资源类型

您配置中的每个资源必须指定为类型。类型可以是 Google 托管的基本类型、复合类型、类型提供程序或导入的模板。

Google 托管的基本类型是解析为 Google Cloud 资源的类型。 例如,Cloud SQL 实例或 Cloud Storage 存储桶是 Google 托管的基本类型。您可以使用以下语法声明这些类型:

type: <api>.<api-version>.<resource-type>

例如,Compute Engine 实例将具有以下类型:

type: compute.v1.instance

对于 BigQuery 数据库,类型可能是:

type: bigquery.v2.dataset

如需所有支持类型的列表,请使用以下命令:

gcloud deployment-manager types list

如需支持的 Google 托管的基本类型的完整列表,请参阅支持的资源类型

如果您使用的是复合类型类型提供程序,请按如下方式声明类型:

# Composite type

resources:
- name: my-composite-type
  type: [PROJECT]/composite:[TYPE_NAME]

对于类型提供程序:

# Base type

resources:
- name: my-base-type
  type: [PROJECT_ID]/[TYPE_PROVIDER_NAME]:[TYPE_NAME]

您还可以使用 Google托管类型提供商(测试版)创建资源。如需查看 Google Cloud 类型提供程序的列表,请参阅支持的类型提供程序

如果要使用模板,请将模板声明为类型,并将模板名称或路径作为 type 的值。例如,以下配置会导入名为 my_vm_template.jinja 的模板,并将其作为类型提供:

imports:
- path: path/to/template/my_vm_template.jinja
  name: my-template.jinja

resources:
- name: my-first-virtual-machine
  type: my-template.jinja

如需详细了解模板,请阅读创建基本模板

声明资源属性

声明资源类型后,您还必须将资源命名为 name 并指定资源所需的属性。例如,以下配置文件定义了名为 vm-created-by-deployment-manager 的虚拟机实例及其所需的属性。Deployment Manager 使用此信息创建具有这些属性的虚拟机实例。

# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

resources:
- name: vm-created-by-deployment-manager
  type: compute.v1.instance
  properties:
    zone: us-central1-a
    machineType: zones/us-central1-a/machineTypes/n1-standard-1
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-11
    networkInterfaces:
    - network: global/networks/default

要确定资源的 properties,请使用资源的 API 文档:

  • 请参阅资源的 insertcreate 方法中的请求格式。
  • 如果请求 URI 包含地区,请将地区添加到属性。
  • 对于数组,使用 YAML 列表语法列出数组的元素。例如,如果您用 API 创建 Compute Engine 实例,则必须提供磁盘的数组以挂接到实例上,所用格式如下:

    "disks": [
      {
        "type": "PERSISTENT",
        "deviceName": "disk1",
        ...
      },
      {
        "type": "PERSISTENT",
        "deviceName": "disk2",
        ...
      }
    ]
    

    在 Deployment Manager 配置中,您可以通过以下语法添加这些磁盘:

    disks:
    - deviceName: disk1
      type: PERSISTENT
      ...
    - deviceName: disk2
      type: PERSISTENT
    

您还可以提供该资源的任何可写属性。要确定属性是否可写,请使用 API 引用文档作为资源类型。例如,Compute Engine 引用标记了仅输出的某些属性,因此您无法在配置中定义这些属性,因为这些属性不可变。

一些 API 需要最少的属性集来创建资源。例如,创建新磁盘时,Compute Engine 永久性磁盘需要磁盘名称、映像来源、磁盘大小等。如需了解特定资源,请查看该资源的 API 引用。

定义模板属性

如果导入要在配置中使用的模板,可以使用 properties 部分定义模板属性(而不是资源属性)的值。或者,如果模板没有模板属性,您则可以完全省略 properties 部分。

从不同的 Google Cloud 服务创建资源

最后,配置文件可以从不同的 Google Cloud 服务创建资源。例如,以下配置文件会从 Compute Engine 和 BigQuery 创建资源:

# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

resources:
- name: vm-created-by-deployment-manager
  type: compute.v1.instance
  properties:
    zone: us-central1-a
    machineType: zones/us-central1-a/machineTypes/n1-standard-1
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-11
    networkInterfaces:
    - network: global/networks/default

- name: big-query-dataset
  type: bigquery.v2.dataset
  properties:
    datasetReference:
      datasetId: example_id

支持的资源类型和属性

请参阅支持的资源类型文档中支持的 Google 托管资源的完整列表。

后续步骤

  • 在提交部署配置前预览配置
  • 满意配置后,使用它来创建部署
  • 最后,您应该考虑重新处理配置文件以使用模板