במדריך הזה נסביר איך לתזמן פונקציית Cloud Run מסוג HTTP על ידי טירגוט לנקודת קצה (endpoint) מסוג HTTP ב-Cloud Scheduler. משתמשים בפונקציית HTTP כשרוצים שלפונקציה תהיה נקודת קצה (endpoint) של כתובת URL והיא תגיב לבקשות HTTP, למשל ל-webhooks.
לחלופין, אם אתם יוצרים פונקציה מבוססת-אירועים שמבוססת על CloudEvents, אתם יכולים להשתמש בטריגרים של Eventarc כדי לתזמן את הפונקציה להגיב לאירועים בפרויקט. Eventarc יכול להפעיל הודעות בנושא Pub/Sub, שינויים בקטגוריה של Cloud Storage ועוד. מידע נוסף זמין במאמר כתיבה של פונקציות Cloud Run.
תזמון ההפעלה של פונקציית Cloud Run הוא תרחיש לדוגמה נפוץ לשימוש ב-Cloud Scheduler. במדריך הזה תלמדו:
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:
בעזרת מחשבון העלויות אתם יכולים להעריך את העלויות בהתאם לשימוש הצפוי.
לפני שמתחילים
- נכנסים לחשבון 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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build, Cloud Run Admin, Cloud Scheduler APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click Add another role and add each additional role.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build, Cloud Run Admin, Cloud Scheduler APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click Add another role and add each additional role.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
שימו לב שצריך להקצות את התפקיד "הפעלת Cloud Run", כי בפונקציות Cloud Run, הרשאות ההפעלה זמינות באמצעות ניהול שירות Cloud Run הבסיסי.
התפקידים הנדרשים
כדי לקבל את ההרשאות שדרושות להשלמת המדריך, בקשו מהאדמין לתת לכם את התפקידים הבאים ב-IAM ברמת הפרויקט:
- מפתח מקורות ב-Cloud Run (
roles/run.sourceDeveloper) - אדמין ב-Cloud Scheduler (
roles/cloudscheduler.admin) - גישה לתצוגת יומנים (
roles/logging.viewAccessor) - אדמין IAM בפרויקט (
roles/resourcemanager.projectIamAdmin) - אדמין בחשבון שירות (
roles/iam.serviceAccountAdmin) - משתמש בחשבון שירות (
roles/iam.serviceAccountUser)
להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.
יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.
מתן התפקידים
המסוף
-
נכנסים לדף IAM במסוף Google Cloud .
כניסה לדף IAM - בוחרים את הפרויקט.
- לוחצים על Grant access.
-
בשדה New principals, מזינים את מזהה המשתמש. בדרך כלל זו כתובת האימייל שמשמשת לפריסת השירות של Cloud Run.
- בוחרים תפקיד מהרשימה Select a role.
- כדי לתת עוד תפקידים, לוחצים על Add another role ומוסיפים אותם.
- לוחצים על Save.
gcloud
כדי להקצות לחשבון שלכם את תפקידי ה-IAM הנדרשים בפרויקט:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=PRINCIPAL \ --role=ROLE
מחליפים את:
- PROJECT_NUMBER במספר הפרויקט ב- Google Cloud .
- PROJECT_ID במזהה הפרויקט ב- Google Cloud .
- PRINCIPAL עם החשבון שאליו מוסיפים את הקישור. בדרך כלל זו כתובת האימייל שמשמשת לפריסת השירות של Cloud Run.
- ROLE בתפקיד שמוסיפים לחשבון הפריסה.
יצירת פונקציית Cloud Run מסוג HTTP
יוצרים פונקציית HTTP שמופעלת על ידי Cloud Scheduler.
במסוף Google Cloud , נכנסים לדף Cloud Run > Services.
לוחצים על כתיבת פונקציה.
בקטע Configure, מבצעים את הפעולות הבאות:
- בשדה Service name, נותנים שם לפונקציה.
- ברשימה Region, בוחרים אזור.
- אל תשנו את שפת ברירת המחדל של סביבת זמן הריצה.
לאימות, בוחרים באפשרות נדרש אימות.
אם בפרויקט שלכם חלה מדיניות ארגונית של הגבלה לפי דומיין, שמגבילה הפעלות לא מאומתות, תצטרכו לגשת לשירות שנפרס כמו שמתואר בקטע בדיקת שירותים פרטיים.
למידע נוסף, קראו את הסקירה הכללית על אימות.
מאשרים את שאר ברירות המחדל ולוחצים על Create.
מעתיקים את כתובת ה-URL מסוג HTTP של הפונקציה.
מידע נוסף, כולל איך לפרוס פונקציית Cloud Run באמצעות Terraform, זמין במאמר פריסת פונקציית Cloud Run.
יצירת משימה ב-Cloud Scheduler
יוצרים משימה ב-Cloud Scheduler שמפעילה פעולה באמצעות HTTP.
במסוף Google Cloud , נכנסים לדף Cloud Scheduler > Jobs.
לוחצים על Create job.
נותנים שם למשימה.
ברשימה Region, בוחרים אזור.
מציינים את התדירות של המשימה, בפורמט unix-cron:
30 16 * * 7מידע נוסף זמין במאמר פורמט ואזור זמן של משימת Cron.
ברשימה Timezone, בוחרים אזור זמן.
לוחצים על Continue.
ברשימה Target type, בוחרים באפשרות HTTP.
בשדה URL, מזינים את כתובת ה-URL מסוג HTTP של הפונקציה שהעתקתם קודם.
ברשימה Auth header, בוחרים באפשרות Add OIDC token.
ברשימה Service account, בוחרים את חשבון השירות שיצרתם קודם.
בשדה Audience, מזינים את כתובת ה-URL מסוג HTTP של הפונקציה שהעתקתם קודם.
לוחצים על יצירה.
יצרתם משימת cron שמריצה את פונקציית Cloud Run בימי ראשון בשעה 16:30. כדי להפוך את פונקציית ה-HTTP לאידמפוטנטית ולמנוע עיבוד כפול כש-Cloud Scheduler מנסה להפעיל מחדש משימה, קראו את המאמר ביטול כפילויות של בקשות למשימות.
הרצת המשימה ב-Cloud Scheduler
עכשיו אפשר להריץ את המשימה שיצרתם.
במסוף Google Cloud , נכנסים לדף Cloud Scheduler > Jobs.
מסמנים את התיבה של המשימה שיצרתם.
לוחצים על Force run.
בפעם הראשונה שמפעילים את המשימה הראשונה שנוצרה בפרויקט, יכול להיות שייקח כמה דקות להגדיר ולהריץ אותה.
אחרי שהמשימה תרוץ, הערך של Status of last execution אמור להיות
Success.
אימות התוצאות בפונקציות Cloud Run
אתם יכולים לוודא שפונקציית Cloud Run מופעלת ומבוצעת בהצלחה על ידי משימת ה-cron.
במסוף Google Cloud , נכנסים לדף Cloud Run > Services.
כדי לסנן את רשימת השירותים לפונקציות, בוחרים באפשרות filter_list Filter > Deployment type > Function.
לוחצים על שם הפונקציה.
הדף Service details נפתח ומוצגים בו מדדי השירות.
לוחצים על הכרטיסייה Logs.
אמורה להופיע רשומת יומן דומה לזו:
POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME
הסרת המשאבים
כדי להימנע מחיובים בחשבון Google Cloud בגלל השימוש במשאבים שנעשה במסגרת המדריך הזה, אפשר למחוק את הפרויקט שמכיל את המשאבים, או להשאיר את הפרויקט ולמחוק את המשאבים הנפרדים.
מחק את הפרויקט
- במסוף Google Cloud , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.
מחיקת המשאבים של המדריך
במסוף Google Cloud , נכנסים לדף Cloud Scheduler > Jobs.
מסמנים את התיבה לצד המשימה.
לוחצים על Delete ומאשרים את המחיקה.
במסוף Google Cloud , נכנסים לדף Cloud Run > Services.
מסמנים את התיבה לצד השירות.
לוחצים על Delete ומאשרים את המחיקה.
נכנסים לדף Service Accounts במסוף Google Cloud .
מסמנים את התיבה ליד חשבון השירות שיצרתם.
לוחצים על Delete ומאשרים את המחיקה.
המאמרים הבאים
- ביטול כפילויות של בקשות למשימות
- שימוש ב-Cloud Scheduler כדי להפעיל את Cloud Run לפי לוח זמנים
- כתיבת פונקציות של CloudEvents כדי לתזמן את הפונקציה להגיב לאירועים בפרויקט.