更新部署

部署完成后,您可以随着应用的发展更新部署。您可以使用 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)。虚拟机模板如下:

# 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-9'])
              }
          }],
          '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 部分,虚拟机的启动脚本已更改:

# 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-9'])
              }
          }],
          '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}

您将使用这个更新后的模板更改您创建的部署。

预览更新

要预览更新后的部署,请运行带有 --preview 标志的 update 命令:

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 bids you good day!”

您还可以对 the-second-vm 重复执行这些步骤,并看到稍有不同的消息。

删除部署

与前面的步骤一样,我们建议您删除部署,以避免产生费用。运行以下命令可删除部署:

gcloud deployment-manager deployments delete deployment-to-update

后续步骤

下面列出了您在进一步使用 Deployment Manager 时可以加以探索的方面: