以部署形式管理 Google Cloud 資源

您可以使用 Google Cloud Deployment Manager 建立一組 Google Cloud 資源,並將其當做一個單元 (稱為部署作業) 進行管理。舉例來說,如果團隊的開發環境需要兩個虛擬機器 (VM) 和一個 BigQuery 資料庫,您可以在設定檔中定義這些資源,然後使用 Deployment Manager 建立、變更或刪除這些資源。您可以將設定檔加入團隊程式碼存放區中,這樣任何人都可以建立相同的環境並取得一致的結果。

在本教學課程中,您將使用 Google Cloud Deployment Manager 建立虛擬機器 (VM) 執行個體。您會在基本設定檔中定義 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-11
    • 區域: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  -

查看資源

建立部署作業後,您可以在Google Cloud console 中查看您的資源。

  1. 如要查看部署作業的清單,請開啟「Deployment Manager」頁面。

    前往「Deployment Manager」頁面

  2. 如要查看部署作業中的資源,請按一下 [quickstart-deployment]。部署作業總覽即會開啟,其中包含部署作業的相關資訊,以及部署作業中的資源。

  3. 如要查看 VM 的相關資訊,請按一下 [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)?

如此便會永久刪除您建立的部署作業和資源。

後續步驟