デプロイの更新

デプロイが完了したら、アプリの進化に合わせてデプロイを更新できます。Deployment Manager を使用すると、次の方法でデプロイを更新できます。

  • デプロイのリソースの追加と削除
  • デプロイ内にあるリソースのプロパティの更新

Deployment Manager は、Google Cloud サービスの基盤となる API を使用してデプロイ内のリソースを管理します。API に update または patch メソッドを存在する場合、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

この構成により、起動スクリプトを実行する 2 台の仮想マシン(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}

この更新されたテンプレートを使用して、作成したデプロイを変更します。

更新をプレビュー

更新されたデプロイをプレビューするには、--preview フラグを指定して update コマンドを実行します。

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

更新を commit する

更新を commit するには、次のコマンドを実行します。

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 を使用する際は、次の情報もご覧ください。