以部署的形式管理 Google Cloud 资源

您可以使用 Google Cloud Deployment Manager 创建一组 Google Cloud 资源,并将其作为称作部署的单元进行管理。例如,如果团队的开发环境需要两个虚拟机 (VM) 和一个 BigQuery 数据库,您可以在配置文件中定义这些资源,然后使用 Deployment Manager 来创建、更改或删除这些资源。您可以将配置文件存入团队代码库中,这样,任何人都可以创建具有一致结果的相同环境。

在本教程中,您将使用 Google Cloud Deployment Manager 来创建虚拟机 (VM) 实例,在基本配置文件中定义虚拟机,并使用该配置文件创建部署。

要完成本教程,您必须能够熟练地在 Linux、macOS 或 Windows 终端中运行命令。

您可以在 Cloud Shell 中打开本教程的交互式版本,其中,您可以修改示例配置并部署资源,而无需在自己的工作站上安装任何内容。如果您想在自己的计算机上运行本教程,请跳到下一部分。

Cloud Shell 中的快速入门

准备工作

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  6. Enable the Deployment Manager and Compute Engine APIs.

    Enable the APIs

  7. 在您的工作站上,安装 Google Cloud CLI
  8. 配置 Google Cloud CLI 以使用您的项目。在以下命令中,将 [MY_PROJECT] 替换为您的项目 ID:
    gcloud config set project [MY_PROJECT]

定义资源

您可以在配置文件中描述您的资源,该文件使用 YAML 语法编写。

  1. 复制下面的示例配置,并将其粘贴到文本编辑器中。

    # 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.
    
    
    # Put all your resources under `resources:`. For each resource, you need:
    # - The type of resource. In this example, the type is a Compute VM instance.
    # - An internal name for the resource.
    # - The properties for the resource. In this example, for VM instances, you add
    #   the machine type, a boot disk, network information, and so on.
    #
    # For a list of supported resources,
    # see https://cloud.google.com/deployment-manager/docs/configuration/supported-resource-types.
    resources:
    - type: compute.v1.instance
      name: quickstart-deployment-vm
      properties:
        # The properties of the resource depend on the type of resource. For a list
        # of properties, see the API reference for the resource.
        zone: us-central1-f
        # Replace [MY_PROJECT] with your project ID
        machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/f1-micro
        disks:
        - deviceName: boot
          type: PERSISTENT
          boot: true
          autoDelete: true
          initializeParams:
            # See a full list of image families at https://cloud.google.com/compute/docs/images#os-compute-support
            # The format of the sourceImage URL is: https://www.googleapis.com/compute/v1/projects/[IMAGE_PROJECT]/global/images/family/[FAMILY_NAME]
            sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-11
        # Replace [MY_PROJECT] with your project ID
        networkInterfaces:
        - network: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/global/networks/default
          # Access Config required to give the instance a public IP address
          accessConfigs:
          - name: External NAT
            type: ONE_TO_ONE_NAT
    

    此基本配置文件描述了一个包含具有以下属性的虚拟机实例的部署:

    • 机器类型:f1-micro
    • 映像系列:debian-9
    • 地区:us-central1-f
    • 根级永久性磁盘:boot
    • 一个随机分配的外部 IP 地址
  2. 将所有 [MY_PROJECT] 实例替换为您的项目 ID。

  3. 将该文件另存为 vm.yaml

部署资源

如需部署资源,请使用 Google Cloud CLI 使用您的配置文件创建新部署:

gcloud deployment-manager deployments create quickstart-deployment --config vm.yaml

如果部署成功,您将收到一条类似于以下示例的消息:

Create operation operation-1432319707382-516afeb5d00f1-b864f0e7-b7103978 completed successfully.
NAME                    TYPE                STATE      ERRORS
quickstart-deployment   compute.v1.instance COMPLETED  -

您现在有了第一个部署!

查看新部署

要查看部署的状态,请运行以下命令:

gcloud deployment-manager deployments describe quickstart-deployment

您会看到部署的描述,包括其开始和结束时间、创建的资源以及任何警告或错误:

fingerprint: xmVVeTtPq-5rr8F-vWFlrg==
id: '54660732508021769'
insertTime: '2016-03-09T04:45:26.032-08:00'
manifest: https://www.googleapis.com/deploymentmanager/v2/projects/myproject/global/deployments/my-first-deployment/manifests/manifest-1457527526037
name: quickstart-deployment
operation:
  endTime: '2016-03-09T04:46:19.480-08:00'
  id: '8993923014899639305'
  kind: deploymentmanager#operation
  name: operation-1457527525951-52d9d126f4618-f1ca6e72-3404bd3b
  operationType: insert
  progress: 100
  startTime: '2016-03-09T04:45:27.275-08:00'
  status: DONE
...
resources:
NAME                     TYPE                 STATE      ERRORS
quickstart-deployment-vm  compute.v1.instance  COMPLETED  -

查看资源

创建 Deployment 后,可以在以下位置查看您的资源: Google Cloud 控制台。

  1. 要查看部署列表,请打开 Deployment Manager 页面。

    转到 Deployment Manager

  2. 要查看部署中的资源,请点击 quickstart-deployment。此时将打开部署概览页面,其中包含部署信息以及部署中的资源。

  3. 要查看您的虚拟机信息,请点击 quickstart-deployment-vm

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

gcloud deployment-manager deployments delete quickstart-deployment

在提示符下输入 y

The following deployments will be deleted:
- quickstart-deployment

Do you want to continue (y/N)?

您创建的部署和资源将被永久删除。

后续步骤