עדכון פריסה

אחרי הפריסה, אפשר לעדכן אותה כשהאפליקציה מתפתחת. אפשר להשתמש ב-Deployment Manager כדי לעדכן פריסת תוכנה על ידי:

  • הוספה או הסרה של משאבים לפריסה
  • עדכון חלק מהמאפיינים של משאבים קיימים בפריסה

‫Deployment Manager משתמש בממשקי ה-API הבסיסיים של Google Cloud שירותים כדי לנהל משאבים בפריסה. ‫Deployment Manager יכול לעדכן משאבים קיימים אם יש שיטה update או patch ב-API המתאים.

פריסת ההגדרות המקוריות

בשלב הזה, פורסים את ההגדרה שרוצים לעדכן בהמשך. עוברים לתיקייה במאגר 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, סקריפט לטעינה בזמן ההפעלה של המכונה הווירטואלית השתנה:

# 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. קבלת כתובת ה-IP החיצונית של the-first-vm:

    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, תוכלו לבדוק את התחומים הבאים: