במדריך הזה מוסבר איך לתזמן פונקציית HTTP Cloud Run על ידי טירגוט של נקודת קצה (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.
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.-
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.
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.-
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 Invoker כי בפונקציות Cloud Run, הרשאות ההפעלה זמינות באמצעות ניהול שירות Cloud Run הבסיסי.
התפקידים הנדרשים
כדי לקבל את ההרשאות שדרושות להשלמת המדריך, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים בפרויקט:
- Cloud Run Source Developer (
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, בוחרים אזור.
- אל תשנו את שפת זמן הריצה שמוגדרת כברירת המחדל.
כדי להגדיר אימות, בוחרים באפשרות דרישת אימות.
אם אתם כפופים למדיניות ארגונית להגבלת דומיין שמגבילה קריאות לא מאומתות בפרויקט שלכם, תצטרכו לגשת לשירות הפרוס שלכם כמו שמתואר בקטע בדיקת שירותים פרטיים.
מידע נוסף זמין במאמר סקירה כללית על אימות.
מאשרים את שאר הגדרות ברירת המחדל ולוחצים על יצירה.
מעתיקים את כתובת ה-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 ואזור זמן.
ברשימה אזור זמן, בוחרים אזור זמן.
לוחצים על Continue.
ברשימה סוג היעד, בוחרים באפשרות HTTP.
בשדה כתובת URL, מזינים את כתובת ה-URL של הפונקציה מסוג HTTP שהעתקתם קודם.
ברשימה Auth header, בוחרים באפשרות Add OIDC token.
ברשימה Service account, בוחרים את חשבון השירות שיצרתם קודם.
בשדה קהל, מזינים את כתובת ה-URL של הפונקציה מסוג HTTP שהעתקתם קודם.
לוחצים על יצירה.
יצרתם משימת cron שמריצה את פונקציית Cloud Run בשעה 16:30 בימי ראשון.
הרצת המשימה ב-Cloud Scheduler
עכשיו אפשר להריץ את העבודה שיצרתם.
במסוף Google Cloud , נכנסים לדף Cloud Scheduler > Jobs.
מסמנים את התיבה שלצד המשרה שיצרתם.
לוחצים על הפעלת הרצה בכוח.
כשמפעילים אותו בפעם הראשונה, יכולות לחלוף כמה דקות עד שהעבודה הראשונה שנוצרה בפרויקט מוגדרת ומופעלת.
אחרי שהעבודה תפעל, בסטטוס של ההרצה האחרונה יופיע
Success.
אימות התוצאות בפונקציות Cloud Run
אפשר לוודא שהפונקציה ב-Cloud Run מופעלת ומוצאת לפועל בהצלחה על ידי עבודת ה-cron.
נכנסים לדף Cloud Run > Services במסוף Google Cloud .
כדי לסנן את רשימת השירותים לפי פונקציות, בוחרים באפשרות filter_list סינון > סוג הפריסה > פונקציה.
לוחצים על שם הפונקציה.
ייפתח הדף Service details עם מדדי השירות.
לוחצים על הכרטיסייה יומנים.
אמורה להופיע רשומה ביומן שדומה לזו:
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.
מסמנים את התיבה לצד המשרה.
לוחצים על מחיקה ומאשרים את המחיקה.
נכנסים לדף Cloud Run > Services במסוף Google Cloud .
מסמנים את התיבה לצד השירות.
לוחצים על מחיקה ומאשרים את המחיקה.
נכנסים לדף Service Accounts במסוף Google Cloud .
מסמנים את התיבה ליד חשבון השירות שיצרתם.
לוחצים על מחיקה ומאשרים את המחיקה.
המאמרים הבאים
- שימוש ב-Cloud Scheduler כדי להפעיל את Cloud Run לפי לוח זמנים
- כתיבה של פונקציות CloudEvents כדי לתזמן את הפונקציה כך שתגיב לאירועים בפרויקט.