פריסת אפליקציית Python Bookshelf באמצעות Cloud Deployment Manager

במדריך הזה נסביר איך לפרוס את האפליקציה לדוגמה Bookshelf ב-Python באמצעות Cloud Deployment Manager.

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

מטרות

  • משכפלים ומגדירים את האפליקציה לדוגמה Bookshelf.
  • ליצור הגדרות ותבניות של Deployment Manager.
  • ליצור פריסות של Deployment Manager.
  • פורסים את האפליקציה לדוגמה Bookshelf באמצעות Deployment Manager.

עלויות

במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:

בעזרת מחשבון העלויות אתם יכולים להעריך את העלויות בהתאם לשימוש הצפוי.

משתמשים חדשים של Google Cloud ? יכול להיות שאתם זכאים לתקופת ניסיון בחינם.

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

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

  1. נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloudליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. 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

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

  4. Enable the Datastore API, Cloud Storage API, Pub/Sub API APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. 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

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

  10. Enable the Datastore API, Cloud Storage API, Pub/Sub API APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. מתקינים במערכת את Python, את pip ואת virtualenv. הוראות מופיעות במאמר הגדרת סביבת פיתוח ב-Python ל- Google Cloud.

יצירת קטגוריה של Cloud Storage

כאן מוסבר איך ליצור קטגוריה של Cloud Storage. קטגוריות הן הקונטיינרים הבסיסיים ששומרים את הנתונים שלכם ב-Cloud Storage.

  1. בחלון הטרמינל, יוצרים קטגוריה של Cloud Storage, כאשר YOUR_BUCKET_NAME מייצג את שם הקטגוריה:

    gcloud storage buckets create gs://YOUR_BUCKET_NAME
  2. כדי להציג תמונות שהועלו באפליקציית Bookshelf, צריך להגדיר את מדיניות ניהול הזהויות והרשאות הגישה (IAM) של הקטגוריה כך שהתפקיד legacyObjectReader יוקצה ל-allUsers:

    gcloud storage buckets add-iam-policy-binding gs://YOUR_BUCKET_NAME
    --member=allUsers --role=roles/storage.legacyObjectReader

שכפול האפליקציה לדוגמה

האפליקציה לדוגמה זמינה ב-GitHub בכתובת GoogleCloudPlatform/getting-started-python.

  1. משכפלים את המאגר:

    git clone https://github.com/GoogleCloudPlatform/getting-started-python.git -b steps
    
  2. עוברים לספריית הדוגמה:

    cd getting-started-python/7-gce
    

הגדרת האפליקציה

  1. פותחים את config.py לעריכה.

    • מגדירים את הערך של PROJECT_ID למזהה הפרויקט.

    • מגדירים את הערך CLOUD_STORAGE_BUCKET לשם של הקטגוריה ב-Cloud Storage.

  2. שומרים וסוגרים את config.py.

הפעלת האפליקציה במחשב המקומי

  1. יוצרים סביבת Python מבודדת ומתקינים יחסי תלות:

    ‫Linux/macOS

    virtualenv -p python3 env
    source env/bin/activate
    pip install -r requirements.txt
    

    Windows

    virtualenv -p python3 env
    env\scripts\activate
    pip install -r requirements.txt
    

  2. מריצים את האפליקציה:

    python main.py
    
  3. מזינים את הכתובת הבאה בדפדפן:

    http://localhost:8080

כדי לעצור את שרת האינטרנט המקומי, מקישים על Control+C. כדי לצאת מהסביבה הווירטואלית, מזינים את הפקודה deactivate.

פריסת האפליקציה לדוגמה

דחיפת הקוד למאגר

יש כמה דרכים להעביר את הקוד למכונה פעילה של Compute Engine. אחת מהדרכים היא להשתמש ב-Cloud Source Repositories. כל פרויקט כולל מאגר Git שזמין למכונות של Compute Engine. בזמן ההפעלה, המכונות שלכם ימשכו את הגרסה העדכנית של קוד האפליקציה. השימוש במאגר Git נוח כי עדכון האפליקציה לא דורש הגדרה של אימג'ים או מכונות חדשים, אלא רק הפעלה מחדש של מכונה קיימת או יצירה של מכונה חדשה.

  1. בפעם הראשונה שמשתמשים ב-Git, משתמשים ב-git config --global כדי להגדיר את הזהות.

  2. יוצרים מאגר במסוף Google Cloud :

    יצירת מאגר

  3. אחר כך מעבירים את קוד האפליקציה למאגר של הפרויקט, כאשר [YOUR_PROJECT_ID] הוא מזהה הפרויקט ו-[YOUR_REPO] הוא שם המאגר:

    git commit -am "Updating configuration"
    git config credential.helper gcloud.sh
    git remote add cloud https://source.developers.google.com/p/[YOUR_PROJECT_ID]/r/[YOUR_REPO]
    git push cloud master
    

יצירת הפריסה

אחרי ששומרים את ההגדרות ומעלים את הקוד ל-Cloud Source Repositories, אפשר להשתמש ב-Deployment Manager כדי ליצור את הפריסה:

  1. עוברים לספרייה deployment_manager:

    cd getting-started-python/7-gce/gce/deployment_manager
    
  2. יוצרים את הפריסה:

    gcloud deployment-manager deployments create my-deployment --config config.yaml
    
  3. כדי לראות רשימה של כל הפריסות:

    gcloud deployment-manager deployments list
    
  4. כדי לקבל תיאור של הפריסה והמשאבים שהיא יצרה:

    gcloud deployment-manager deployments describe my-deployment
    

הצגת האפליקציה

אחרי שיוצרים את כלל ההעברה, יכול להיות שיעברו כמה דקות עד שההגדרות יופצו והתנועה תנותב למכונות.

  1. כדי לבדוק את ההתקדמות:

    gcloud compute backend-services get-health bookshelf-my-deployment-frontend --global
    
  2. כשאחד מהמכונות לפחות מדווח על HEALTHY, מקבלים את כתובת ה-IP להעברה של מאזן העומסים:

    gcloud compute forwarding-rules list --global
    

    כתובת ה-IP של כללי ההעברה מופיעה בעמודה IP_ADDRESS.

  3. בדפדפן, מזינים את כתובת ה-IP מהרשימה.

    האפליקציה פועלת עכשיו ב- Google Cloud.

הסבר על הקוד

הגדרת הפריסה

אפשר לראות איך הפריסה מוגדרת ב-config.yaml:

imports:
- name: bookshelf.jinja
  path: ./bookshelf.jinja

resources:
- name: bookshelf
  type: bookshelf.jinja
  properties:
    zone: us-central1-f
    machine-type: n1-standard-1
    machine-image: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9
    min-instances: 1
    max-instances: 10
    target-utilization: 0.6
    scopes:
    - https://www.googleapis.com/auth/cloud-platform

קובץ התצורה מייבא את קובץ התבנית כמשאב ומשתמש בו כדי להגדיר משאב בשם bookshelf. התבנית מקבלת כמה מאפיינים כפרמטרים. למידע נוסף על יצירת קובצי תצורה של Deployment Manager, קראו את המאמר יצירת תצורה.

הגדרת הסכימה של תבנית הפריסה

מעיינים בקובץ הסכימה bookshelf.jinja.schema, שבו מוגדרים ומתועדים הפרמטרים שהפריסה אמורה לספק בקובץ התצורה שלה. מידע נוסף על סכימות של הגדרות זמין במאמר שימוש בסכימות.


info:
  title: Bookshelf GCE Deploy
  author: Google Inc.
  description: Creates a GCE Deployment

imports:
- name: startup-script
  path: ../startup-script.sh

required:
- zone
- machine-type
- min-instances
- max-instances
- scopes

properties:
  zone:
    description: Zone to create the resources in.
    type: string
  machine-type:
    description: Type of machine to use
    type: string
  machine-image:
    description: The OS image to use on the machines
    type: string
  min-instances:
    description: The minimum number of VMs the autoscaler will create
    type: integer
  max-instances:
    description: The maximum number of VMs the autoscaler will create
    type: integer
  target-utilization:
    description: The target CPU usage for the autoscaler to base its scaling on
    type: number
  scopes:
    description: A list of scopes to create the VM with
    type: array
    minItems: 1
    items:
      type: string

הגדרת תבנית הפריסה

התבנית מגדירה כמה משאבים שנדרשים כדי ליצור קבוצת מופעי מכונה מנוהלים עם התאמה לעומס ואיזון עומסים. לתיאור מלא של המשאבים שנוצרו, קראו את ההערות בתבנית ועיינו במדריך Bookshelf ב-Compute Engine.

חשוב לזכור ש-Deployment Manager מספק דרך להגדיר משאבים באופן הצהרתי, אבל רוב ההגדרות של משאב מסוים נקבעות על ידי ה-API שלו. לדוגמה, רוב אפשרויות ההגדרה של משאב תבנית של הגדרות מכונה נמצאות בהגדרה של משאב תבנית של הגדרות מכונה, במאמרי העזרה של Compute Engine API.

התבנית נכתבת באמצעות שפת התבניות Jinja. כשכותבים תבניות של Deployment Manager, אפשר להשתמש ב-Jinja או ב-Python. היתרון של Jinja הוא שהיא יותר הצהרתית, ולכן היא קריאה וקלה יותר להבנה מאשר Python. בפריסות מורכבות מסוימות, יכול להיות שיהיה קל יותר להשתמש ביכולות הביטוי המלאות של Python. במקרה כזה, Jinja מספיקה כדי ליצור את המשאבים הנדרשים.

בתבנית, משתני סביבה מסוימים מוגדרים באופן אוטומטי, ואפשר לגשת למשתנים האלה באמצעות המילון env. במקרה הזה, שם הפריסה משמש כשם שאפשר לעשות בו שימוש חוזר כשמקצים שמות למשאבים שנוצרים. למידע נוסף על משתני סביבה זמינים, ראו שימוש במשתני סביבה במסמכי התיעוד של Deployment Manager:

{% set NAME = "bookshelf-" + env["deployment"] %}
{% set SERVICE = "bookshelf-" + env["deployment"] + "-frontend" %}

התבנית משתמשת באותו סקריפט לטעינה בזמן ההפעלה שמשמש במדריך של Compute Engine. תוכן הסקריפט מוכנס לתבנית, והוראת הכניסה של Jinja יוצרת כניסה נכונה בתוכן:

            value: |
{{imports['startup-script']|indent(14, true)}}

בתבנית Jinja של Deployment Manager, אפשר גם להפנות למשאבים שנוצרו במקום אחר בתבנית. בדוגמה הבאה, שירות הקצה העורפי משתמש בהפניה של קבוצת המכונות המנוהלות כדי לקבל את קבוצת המכונות שאליה הוא צריך להפנות:

- group: $(ref.{{ NAME }}-frontend-group.instanceGroup)
  zone: {{ properties['zone'] }}

אפשר להשתמש בתבנית במאפיינים שצוינו ב-config.yaml:

minNumReplicas: {{ properties['min-instances'] }}
maxNumReplicas: {{ properties['max-instances'] }}
loadBalancingUtilization:
  utilizationTarget: {{ properties['target-utilization'] }}

הסרת המשאבים

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

מחיקת הפרויקט

הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך הזה.

כדי למחוק את הפרויקט:

  1. במסוף Google Cloud , נכנסים לדף Manage resources.

    כניסה לדף Manage resources

  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

מחיקת הפריסה

כדי למחוק את הפריסה, מזינים את הפקודה הבאה. הפקודה הזו מוחקת את מאזן העומסים ואת כל המכונות של Compute Engine שמשויכות לפריסה:

gcloud deployment-manager deployments delete my-deployment

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