Mengupdate deployment

Setelah memiliki deployment, Anda dapat mengupdatenya seiring perkembangan aplikasi. Anda dapat menggunakan Deployment Manager untuk mengupdate deployment dengan:

  • Menambahkan atau menghapus resource ke deployment
  • Mengupdate beberapa properti resource yang ada dalam deployment Anda

Deployment Manager menggunakan API Google Cloud layanan yang mendasarinya untuk mengelola resource dalam deployment Anda. Deployment Manager dapat mengupdate resource yang ada jika ada metode update atau patch di API yang sesuai.

Men-deploy konfigurasi asli

Pada langkah ini, deploy konfigurasi yang akan Anda update nanti. Buka folder di repositori GitHub, lalu deploy konfigurasi:

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

Konfigurasi ini men-deploy dua virtual machine (VM) yang menjalankan skrip startup. Template VM-nya adalah:

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

Membuka template yang diupdate

Sekarang, buka template yang diupdate:

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

nano vm-template.py

Di template yang diupdate, di bagian metadata, skrip startup VM telah diubah:

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

Anda akan menggunakan template yang diupdate ini untuk mengubah deployment yang Anda buat.

Mempratinjau update

Untuk mempratinjau deployment yang diupdate, jalankan perintah update dengan tanda --preview:

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

Melakukan commit update

Untuk melakukan commit update, jalankan:

gcloud deployment-manager deployments update deployment-to-update

Memverifikasi update

Untuk memeriksa apakah update berhasil, Anda harus memulai ulang instance terlebih dahulu untuk menggunakan skrip startup baru. Mulai ulang the-first-vm:

gcloud compute instances reset the-first-vm

Instance mungkin memerlukan waktu beberapa saat untuk memulai ulang. Tunggu beberapa menit sebelum mengonfirmasi perubahan.

Mengonfirmasi skrip startup baru

  1. Dapatkan IP eksternal the-first-vm:

    gcloud compute instances describe the-first-vm | grep "natIP"
    
  2. Salin nilainya.

  3. Buka browser dan tempelkan alamat IP ke kolom URL untuk mengunjungi instance Anda.

    Halaman tersebut kini akan menampilkan pesan sambutan yang bertuliskan "Deployment Manager bids you good day!"

Anda juga dapat mengulangi langkah-langkah ini dengan the-second-vm dan melihat pesan yang sedikit berbeda.

Menghapus deployment

Seperti langkah-langkah sebelumnya, sebaiknya hapus deployment untuk menghindari biaya. Jalankan perintah berikut untuk menghapus deployment:

gcloud deployment-manager deployments delete deployment-to-update

Langkah berikutnya

Berikut beberapa area yang dapat Anda pelajari saat menggunakan Deployment Manager lebih lanjut: