更新部署

建立部署作業後,您可以隨著應用程式的發展更新部署作業。您可以使用 Deployment Manager,透過下列方式更新部署作業:

  • 新增或移除部署作業的資源
  • 更新部署作業中現有資源的部分屬性

Deployment Manager 會使用 Google Cloud 服務的基礎 API 來管理部署作業的資源。如果相應的 API 中有 updatepatch 方法,Deployment Manager 即可更新現有資源。

部署原始設定

在這個步驟中,部署您稍後將更新的設定。前往 GitHub 存放區中的資料夾,然後部署設定:

cd deploymentmanager-samples/examples/v2/step_by_step_guide/step8_metadata_and_startup_scripts/python

gcloud deployment-manager deployments create deployment-to-update --config config-with-many-templates.yaml

設定會部署兩個執行開機指令碼的虛擬機器 (VM)。VM 範本如下:

# 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.

"""Creates the virtual machine with environment variables and startup script."""


COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/'


def GenerateConfig(context):
  """Creates the virtual machine."""

  resources = [{
      'name': context.env['name'],
      'type': 'compute.v1.instance',
      'properties': {
          'zone': context.properties['zone'],
          'machineType': ''.join([COMPUTE_URL_BASE, 'projects/',
                                  context.env['project'], '/zones/',
                                  context.properties['zone'], '/machineTypes/',
                                  context.properties['machineType']]),
          'disks': [{
              'deviceName': 'boot',
              'type': 'PERSISTENT',
              'boot': True,
              'autoDelete': True,
              'initializeParams': {
                  'sourceImage': ''.join([COMPUTE_URL_BASE, 'projects/',
                                          'debian-cloud/global/',
                                          'images/family/debian-11'])
              }
          }],
          'networkInterfaces': [{
              'network': '$(ref.' + context.properties['network']
                         + '.selfLink)',
              'accessConfigs': [{
                  'name': 'External NAT',
                  'type': 'ONE_TO_ONE_NAT'
              }]
          }],
          'metadata': {
              'items': [{
                  'key': 'startup-script',
                  'value': ''.join(['#!/bin/bash\n',
                                    'python -m SimpleHTTPServer 80'])
              }]
          }
      }
  }]
  return {'resources': resources}

開啟更新的範本

現在請開啟更新的範本:

cd deploymentmanager-samples/examples/v2/step_by_step_guide/step9_update_a_deployment/python

nano vm-template.py

在更新範本的 metadata 區段中,VM 的開機指令碼已變更:

# 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.

"""Creates the virtual machine with environment variables and startup script."""


COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/'


def GenerateConfig(context):
  """Creates the virtual machine."""

  resources = [{
      'name': context.env['name'],
      'type': 'compute.v1.instance',
      'properties': {
          'zone': context.properties['zone'],
          'machineType': ''.join([COMPUTE_URL_BASE, 'projects/',
                                  context.env['project'], '/zones/',
                                  context.properties['zone'], '/machineTypes/',
                                  context.properties['machineType']]),
          'disks': [{
              'deviceName': 'boot',
              'type': 'PERSISTENT',
              'boot': True,
              'autoDelete': True,
              'initializeParams': {
                  'sourceImage': ''.join([COMPUTE_URL_BASE, 'projects/',
                                          'debian-cloud/global/',
                                          'images/family/debian-11'])
              }
          }],
          'networkInterfaces': [{
              'network': '$(ref.' + context.properties['network']
                         + '.selfLink)',
              'accessConfigs': [{
                  'name': 'External NAT',
                  'type': 'ONE_TO_ONE_NAT'
              }]
          }],
          'metadata': {
              'items': [{
                  'key': 'startup-script',
                  'value': ''.join(['#!/bin/bash\n',
                                    'INSTANCE=$(curl http://metadata.google.',
                                    'internal/computeMetadata/v1/instance/',
                                    'hostname -H "Metadata-Flavor: Google")\n',
                                    'echo "<html><header><title>Hello from ',
                                    'Deployment Manager!</title></header>',
                                    '<body><h2>Hello from $INSTANCE</h2><p>',
                                    'Deployment Manager bids you good day!</p>',
                                    '</body></html>" > index.html\n',
                                    'python -m SimpleHTTPServer 80\n'])
              }]
          }
      }
  }]
  return {'resources': resources}

您將使用這個更新範本來變更您建立的部署作業。

預覽更新

如要預覽更新的部署作業,請執行 update 指令並加上 --preview 旗標:

gcloud deployment-manager deployments update deployment-to-update --config config-with-many-templates.yaml --preview

提交更新

如要提交更新,請執行:

gcloud deployment-manager deployments update deployment-to-update

驗證更新

您必須先重新啟動執行個體來使用新的開機指令碼,才能確認更新是否有效。重新啟動 the-first-vm

gcloud compute instances reset the-first-vm

執行個體可能需要一些時間重新啟動,請稍等幾分鐘再確認變更。

確認新的開機指令碼

  1. 取得 the-first-vm 的外部 IP:

    gcloud compute instances describe the-first-vm | grep "natIP"
    
  2. 複製該值。

  3. 開啟瀏覽器,然後將 IP 位址貼入網址列來前往執行個體。

    這個頁面現在應該會顯示以下歡迎訊息:「Deployment Manager 祝您有美好的一天!」

您也可以使用 the-second-vm 重複這些步驟,畫面上會顯示稍微不同的訊息。

刪除部署作業

與先前的步驟一樣,建議您刪除部署作業以免產生費用。執行下列指令以刪除部署作業:

gcloud deployment-manager deployments delete deployment-to-update

後續步驟

在深入使用 Deployment Manager 的同時,以下是一些可探索的領域: