פריסת אפליקציה בקונטיינר ב-Cloud Run באמצעות Cloud Build
בדף הזה מוסבר איך להשתמש ב-Cloud Build כדי לפרוס אפליקציה בקונטיינר ב-Cloud Run.
בלחיצה על תראו לי איך תקבלו הסבר מפורט על המשימה ישירות ב-Cloud Shell Editor:
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init
מתן הרשאות
כדי לפרוס תמונה ל-Cloud Run, צריך להעניק ל-Cloud Build כמה הרשאות. כדי להעניק את ההרשאות האלה:
פותחים חלון טרמינל.
מגדירים משתני סביבה כדי לאחסן את מזהה הפרויקט ואת מספר הפרויקט.
PROJECT_ID=$(gcloud config list --format='value(core.project)') PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')מקצים את התפקיד אדמין של 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 \נותנים לחשבון השירות ב-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 \מקצים את התפקיד משתמש בחשבון שירות לחשבון השירות ב-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.
כדי לפרוס תמונה מוכנה מראש:
פותחים חלון טרמינל (אם עדיין לא פתוח).
יוצרים ספרייה חדשה בשם
helloworldועוברים אליה:mkdir helloworld cd helloworldיוצרים קובץ בשם
cloudbuild.yamlעם התוכן הבא. הקובץ הזה הוא קובץ התצורה של Cloud Build. הוא מכיל הוראות ל-Cloud Build לפריסת האימג' בשםus-docker.pkg.dev/cloudrun/container/helloבשירות Cloud Run בשםcloudrunservice.מפעילים את הפקודה הבאה כדי לפרוס את האימג':
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.
הפעלת התמונה שנפרסה
פותחים את הדף Cloud Run במסוף Google Cloud :
בוחרים את הפרויקט ולוחצים על פתיחה.
יוצג הדף Cloud Run Services.
בטבלה, מאתרים את השורה עם השם cloudrunservice ולוחצים על cloudrunservice.
מוצג הדף פרטי השירות של cloudrunservice.
כדי להפעיל את האימג' שפרסתם ב-cloudrunservice, לוחצים על כתובת ה-URL: