ניהול משאבי Google Cloud כפריסה

אתם יכולים להשתמש ב-Google Cloud Deployment Manager כדי ליצור קבוצה של משאבי Google Cloud ולנהל אותם כיחידה אחת שנקראת פריסה. לדוגמה, אם סביבת הפיתוח של הצוות שלכם צריכה שתי מכונות וירטואליות (VM) ומסד נתונים של BigQuery, אתם יכולים להגדיר את המשאבים האלה בקובץ תצורה ולהשתמש ב-Deployment Manager כדי ליצור, לשנות או למחוק את המשאבים האלה. אתם יכולים להפוך את קובץ התצורה לחלק ממאגר המקורות של הקוד של הצוות, כדי שכל אחד יוכל ליצור את אותה סביבה עם תוצאות עקביות.

במדריך הזה תשתמשו ב-Google Cloud Deployment Manager כדי ליצור מכונה וירטואלית (VM). תגדירו את המכונה הווירטואלית בקובץ תצורה בסיסי, ותשתמשו בקובץ התצורה הזה כדי ליצור פריסה.

כדי להשלים את המדריך הזה, אתם צריכים לדעת איך להריץ פקודות בטרמינל של Linux,‏ macOS או Windows.

כדי להשתמש בגרסה אינטראקטיבית של המדריך הזה, פתחו אותו ב-Cloud Shell. שם תוכלו לערוך את הגדרות הדוגמה ולפרוס את המשאבים בלי להתקין שום דבר בתחנת העבודה. אם אתם רוצים לעקוב אחרי המדריך במחשב שלכם, דלגו לקטע הבא.

מדריך למתחילים ב-Cloud Shell

לפני שמתחילים

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. מפעילים את Deployment Manager ואת Compute Engine API, אם הם עדיין לא מופעלים.

    התפקידים שנדרשים כדי להפעיל ממשקי API

    כדי להפעיל ממשקי API, צריך את ההרשאה serviceusage.services.enable. אם אתם יצרתם את הפרויקט, סביר להניח שכבר יש לכם את ההרשאה הזו דרך התפקיד "בעלים" (roles/owner). אחרת, אתם יכולים לקבל את ההרשאה הזו דרך התפקיד "אדמין בממשק 'שימוש בשירות'" (roles/serviceusage.serviceUsageAdmin). איך מקצים תפקידים

    הפעלת ממשקי ה-API

  4. מתקינים את Google Cloud CLI בתחנת העבודה.
  5. מגדירים את Google Cloud CLI כך שישתמש בפרויקט שלכם. בפקודה הבאה, מחליפים את [MY_PROJECT] במזהה הפרויקט:
    gcloud config set project [MY_PROJECT]

הגדרת המשאבים

אתם מתארים את המשאבים בקובץ תצורה שכתוב בתחביר YAML.

  1. מעתיקים את ההגדרות לדוגמה שבהמשך ומדביקים אותן בכלי לעריכת טקסט.

    # 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.
    
    
    # Put all your resources under `resources:`. For each resource, you need:
    # - The type of resource. In this example, the type is a Compute VM instance.
    # - An internal name for the resource.
    # - The properties for the resource. In this example, for VM instances, you add
    #   the machine type, a boot disk, network information, and so on.
    #
    # For a list of supported resources,
    # see https://cloud.google.com/deployment-manager/docs/configuration/supported-resource-types.
    resources:
    - type: compute.v1.instance
      name: quickstart-deployment-vm
      properties:
        # The properties of the resource depend on the type of resource. For a list
        # of properties, see the API reference for the resource.
        zone: us-central1-f
        # Replace [MY_PROJECT] with your project ID
        machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/f1-micro
        disks:
        - deviceName: boot
          type: PERSISTENT
          boot: true
          autoDelete: true
          initializeParams:
            # See a full list of image families at https://cloud.google.com/compute/docs/images#os-compute-support
            # The format of the sourceImage URL is: https://www.googleapis.com/compute/v1/projects/[IMAGE_PROJECT]/global/images/family/[FAMILY_NAME]
            sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-11
        # Replace [MY_PROJECT] with your project ID
        networkInterfaces:
        - network: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/global/networks/default
          # Access Config required to give the instance a public IP address
          accessConfigs:
          - name: External NAT
            type: ONE_TO_ONE_NAT
    

    קובץ התצורה הבסיסי הזה מתאר פריסה שמכילה מכונה וירטואלית אחת עם המאפיינים הבאים:

    • סוג המכונה: f1-micro
    • משפחת התמונות: debian-11
    • אזור: us-central1-f
    • דיסק אחסון מתמיד ברמה הבסיסית (root): boot
    • כתובת IP חיצונית שמוקצית באופן אקראי
  2. מחליפים את כל המופעים של [MY_PROJECT] במזהה הפרויקט.

  3. שומרים את הקובץ בשם vm.yaml.

פריסת המשאבים

כדי לפרוס את המשאבים, משתמשים ב-Google Cloud CLI כדי ליצור פריסה חדשה באמצעות קובץ התצורה:

gcloud deployment-manager deployments create quickstart-deployment --config vm.yaml

אם הפריסה בוצעה בהצלחה, תתקבל הודעה דומה לדוגמה הבאה:

Create operation operation-1432319707382-516afeb5d00f1-b864f0e7-b7103978 completed successfully.
NAME                    TYPE                STATE      ERRORS
quickstart-deployment   compute.v1.instance COMPLETED  -

הפריסה הראשונה שלכם מוכנה.

בדיקת הפריסה החדשה

כדי לבדוק את סטטוס הפריסה, מריצים את הפקודה הבאה:

gcloud deployment-manager deployments describe quickstart-deployment

יוצג תיאור של הפריסה, כולל שעת ההתחלה והסיום שלה, המשאבים שנוצרו ואזהרות או שגיאות:

fingerprint: xmVVeTtPq-5rr8F-vWFlrg==
id: '54660732508021769'
insertTime: '2016-03-09T04:45:26.032-08:00'
manifest: https://www.googleapis.com/deploymentmanager/v2/projects/myproject/global/deployments/my-first-deployment/manifests/manifest-1457527526037
name: quickstart-deployment
operation:
  endTime: '2016-03-09T04:46:19.480-08:00'
  id: '8993923014899639305'
  kind: deploymentmanager#operation
  name: operation-1457527525951-52d9d126f4618-f1ca6e72-3404bd3b
  operationType: insert
  progress: 100
  startTime: '2016-03-09T04:45:27.275-08:00'
  status: DONE
...
resources:
NAME                     TYPE                 STATE      ERRORS
quickstart-deployment-vm  compute.v1.instance  COMPLETED  -

בדיקת המשאבים

אחרי שיוצרים את הפריסה, אפשר לבדוק את המשאבים במסוףGoogle Cloud .

  1. כדי לראות את רשימת הפריסות, פותחים את הדף Deployment Manager.

    כניסה ל-Deployment Manager

  2. כדי לראות את המשאבים בפריסה, לוחצים על quickstart-deployment. תיפתח הסקירה הכללית של הפריסה, עם מידע על הפריסה ועל המשאבים שהם חלק מהפריסה.

  3. כדי לראות מידע על ה-VM, לוחצים על quickstart-deployment-vm.

הסרת המשאבים

כדי לא לצבור חיובים בחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, מבצעים את הפעולות הבאות.

gcloud deployment-manager deployments delete quickstart-deployment

מקלידים y בשורת הפקודה:

The following deployments will be deleted:
- quickstart-deployment

Do you want to continue (y/N)?

הפריסה והמשאבים שיצרתם נמחקים באופן סופי.

המאמרים הבאים