פריסת אפליקציה בקונטיינר ב-Cloud Run באמצעות Cloud Build

בדף הזה מוסבר איך להשתמש ב-Cloud Build כדי לפרוס אפליקציה בקונטיינר ב-Cloud Run.


בלחיצה על תראו לי איך תקבלו הסבר מפורט על המשימה ישירות ב-Cloud Shell Editor:

תראו לי איך


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

  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 Cloud Build, Cloud Run, Artifact Registry, and Compute Engine APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. התקינו את ה-CLI של Google Cloud.

  6. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  7. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    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 Cloud Build, Cloud Run, Artifact Registry, and Compute Engine APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. התקינו את ה-CLI של Google Cloud.

  12. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  13. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init

מתן הרשאות

כדי לפרוס תמונה ל-Cloud Run, צריך להעניק ל-Cloud Build כמה הרשאות. כדי להעניק את ההרשאות האלה:

  1. פותחים חלון טרמינל.

  2. מגדירים משתני סביבה כדי לאחסן את מזהה הפרויקט ואת מספר הפרויקט.

    PROJECT_ID=$(gcloud config list --format='value(core.project)')
    PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
    
  3. מקצים את התפקיד אדמין של Cloud Run לחשבון השירות ב-Cloud Build.

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:$(gcloud projects describe $PROJECT_ID \
        --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
        --role=roles/run.admin \
    
  4. נותנים לחשבון השירות ב-Cloud Build את התפקיד משתמש באובייקט אחסון.

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:$(gcloud projects describe $PROJECT_ID \
        --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
        --role=roles/storage.objectUser \
    
  5. מקצים את התפקיד משתמש בחשבון שירות לחשבון השירות ב-Cloud Build.

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:$(gcloud projects describe $PROJECT_ID \
        --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
        --role=roles/iam.serviceAccountUser
    

פריסת תמונה מוכנה מראש

אתם יכולים להגדיר את Cloud Build כך שיפרוס אימג' שנוצר מראש ומאוחסן ב-Artifact Registry ב-Cloud Run.

כדי לפרוס תמונה מוכנה מראש:

  1. פותחים חלון טרמינל (אם עדיין לא פתוח).

  2. יוצרים ספרייה חדשה בשם helloworld ועוברים אליה:

    mkdir helloworld
    cd helloworld
    
  3. יוצרים קובץ בשם cloudbuild.yaml עם התוכן הבא. הקובץ הזה הוא קובץ התצורה של Cloud Build. הוא מכיל הוראות ל-Cloud Build לפריסת האימג' בשם us-docker.pkg.dev/cloudrun/container/hello בשירות Cloud Run בשם cloudrunservice.

    steps:
    - name: 'gcr.io/cloud-builders/gcloud'
      script: |
        gcloud run deploy cloudrunservice --image us-docker.pkg.dev/cloudrun/container/hello --region us-central1 --platform managed --allow-unauthenticated
  4. מפעילים את הפקודה הבאה כדי לפרוס את האימג':

    gcloud builds submit --region=us-west2 --config cloudbuild.yaml
    

בסיום ה-build יופיע פלט שדומה לזה:

DONE
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ID                                    CREATE_TIME                DURATION  SOURCE                                                                                            IMAGES  STATUS
784653b2-f00e-4c4b-9f5f-96a5f115bef4  2020-01-23T14:53:13+00:00  23S       gs://cloudrunqs-project_cloudbuild/source/1579791193.217726-ea20e1c787fb4784b19fb1273d032df2.tgz  -       SUCCESS

הרגע פרסתם את האימג' hello ב-Cloud Run.

הפעלת התמונה שנפרסה

  1. פותחים את הדף Cloud Run במסוף Google Cloud :

    פתיחת הדף Cloud Run

  2. בוחרים את הפרויקט ולוחצים על פתיחה.

    יוצג הדף Cloud Run Services.

  3. בטבלה, מאתרים את השורה עם השם cloudrunservice ולוחצים על cloudrunservice.

    מוצג הדף פרטי השירות של cloudrunservice.

  4. כדי להפעיל את האימג' שפרסתם ב-cloudrunservice, לוחצים על כתובת ה-URL:

    צילום מסך של דף הפרטים של שירות Cloud Run

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