배포 업데이트

배포가 완료된 후에는 앱이 발전함에 따라 업데이트할 수 있습니다. 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

이 구성은 시작 스크립트를 실행하는 가상 머신(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

업데이트 커밋

업데이트를 커밋하려면 다음을 실행합니다.

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!(Deployment Manager에서 환영합니다)'라는 시작 메시지가 표시됩니다.

또한 the-second-vm에 이 단계를 반복하면 약간 다른 메시지가 표시될 수 있습니다.

배포 삭제

이전 단계와 마찬가지로 요금이 청구되지 않도록 배포를 삭제하는 것이 좋습니다. 다음 명령어를 실행하여 배포를 삭제합니다.

gcloud deployment-manager deployments delete deployment-to-update

다음 단계

다음은 Deployment Manager를 더 많이 사용하면서 살펴볼 수 있는 몇 가지 부분입니다.