תזמון של פונקציית Cloud Run מסוג HTTP

במדריך הזה נסביר איך לתזמן פונקציית 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. במדריך הזה תלמדו:

  1. יוצרים פונקציית Cloud Run מסוג HTTP.
  2. יצירת משימה ב-Cloud Scheduler.
  3. מריצים את המשימה ב-Cloud Scheduler.
  4. אימות התוצאות בפונקציות Cloud Run.

עלויות

במסמך הזה משתמשים ברכיבים הבאים של 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. 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.

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

  5. 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.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

  6. Create a service account:

    1. 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.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. 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.

    5. Click Create and continue.
    6. 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.

    7. Click Continue.
    8. Click Done to finish creating the service account.

  7. 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

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

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

  10. 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.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. Create a service account:

    1. 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.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. 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.

    5. Click Create and continue.
    6. 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.

    7. Click Continue.
    8. Click Done to finish creating the service account.

שימו לב שצריך להקצות את התפקיד "הפעלת Cloud Run", כי בפונקציות Cloud Run, הרשאות ההפעלה זמינות באמצעות ניהול שירות Cloud Run הבסיסי.

התפקידים הנדרשים

כדי לקבל את ההרשאות שדרושות להשלמת המדריך, בקשו מהאדמין לתת לכם את התפקידים הבאים ב-IAM ברמת הפרויקט:

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

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

מתן התפקידים

המסוף

  1. נכנסים לדף IAM במסוף Google Cloud .

    כניסה לדף IAM
  2. בוחרים את הפרויקט.
  3. לוחצים על Grant access.
  4. בשדה New principals, מזינים את מזהה המשתמש. בדרך כלל זו כתובת האימייל שמשמשת לפריסת השירות של Cloud Run.

  5. בוחרים תפקיד מהרשימה Select a role.
  6. כדי לתת עוד תפקידים, לוחצים על Add another role ומוסיפים אותם.
  7. לוחצים על 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.

  1. במסוף Google Cloud , נכנסים לדף Cloud Run >‏ Services.

    לדף Services

  2. לוחצים על כתיבת פונקציה.

  3. בקטע Configure, מבצעים את הפעולות הבאות:

    1. בשדה Service name, נותנים שם לפונקציה.
    2. ברשימה Region, בוחרים אזור.
    3. אל תשנו את שפת ברירת המחדל של סביבת זמן הריצה.
    4. לאימות, בוחרים באפשרות נדרש אימות.

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

      למידע נוסף, קראו את הסקירה הכללית על אימות.

  4. מאשרים את שאר ברירות המחדל ולוחצים על Create.

  5. מעתיקים את כתובת ה-URL מסוג HTTP של הפונקציה.

מידע נוסף, כולל איך לפרוס פונקציית Cloud Run באמצעות Terraform, זמין במאמר פריסת פונקציית Cloud Run.

יצירת משימה ב-Cloud Scheduler

יוצרים משימה ב-Cloud Scheduler שמפעילה פעולה באמצעות HTTP.

  1. במסוף Google Cloud , נכנסים לדף Cloud Scheduler > Jobs.

    מעבר לדף Jobs

  2. לוחצים על ‎ Create job‎.

  3. נותנים שם למשימה.

  4. ברשימה Region, בוחרים אזור.

  5. מציינים את התדירות של המשימה, בפורמט unix-cron:

    30 16 * * 7
    

    מידע נוסף זמין במאמר פורמט ואזור זמן של משימת Cron.

  6. ברשימה Timezone, בוחרים אזור זמן.

  7. לוחצים על Continue.

  8. ברשימה Target type, בוחרים באפשרות HTTP.

  9. בשדה URL, מזינים את כתובת ה-URL מסוג HTTP של הפונקציה שהעתקתם קודם.

  10. ברשימה Auth header, בוחרים באפשרות Add OIDC token.

  11. ברשימה Service account, בוחרים את חשבון השירות שיצרתם קודם.

  12. בשדה Audience, מזינים את כתובת ה-URL מסוג HTTP של הפונקציה שהעתקתם קודם.

  13. לוחצים על יצירה.

יצרתם משימת cron שמריצה את פונקציית Cloud Run בימי ראשון בשעה 16:30. כדי להפוך את פונקציית ה-HTTP לאידמפוטנטית ולמנוע עיבוד כפול כש-Cloud Scheduler מנסה להפעיל מחדש משימה, קראו את המאמר ביטול כפילויות של בקשות למשימות.

הרצת המשימה ב-Cloud Scheduler

עכשיו אפשר להריץ את המשימה שיצרתם.

  1. במסוף Google Cloud , נכנסים לדף Cloud Scheduler > Jobs.

    מעבר לדף Jobs

  2. מסמנים את התיבה של המשימה שיצרתם.

  3. לוחצים על Force run.

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

    אחרי שהמשימה תרוץ, הערך של Status of last execution אמור להיות Success.

אימות התוצאות בפונקציות Cloud Run

אתם יכולים לוודא שפונקציית Cloud Run מופעלת ומבוצעת בהצלחה על ידי משימת ה-cron.

  1. במסוף Google Cloud , נכנסים לדף Cloud Run > Services.

    לדף Services

  2. כדי לסנן את רשימת השירותים לפונקציות, בוחרים באפשרות filter_list Filter > Deployment type > Function.

  3. לוחצים על שם הפונקציה.

    הדף Service details נפתח ומוצגים בו מדדי השירות.

  4. לוחצים על הכרטיסייה Logs.

    אמורה להופיע רשומת יומן דומה לזו: POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME

הסרת המשאבים

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

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

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

    כניסה לדף Manage resources

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

מחיקת המשאבים של המדריך

  1. במסוף Google Cloud , נכנסים לדף Cloud Scheduler > Jobs.

    מעבר לדף Jobs

  2. מסמנים את התיבה לצד המשימה.

  3. לוחצים על Delete ומאשרים את המחיקה.

  4. במסוף Google Cloud , נכנסים לדף Cloud Run > Services.

    לדף Services

  5. מסמנים את התיבה לצד השירות.

  6. לוחצים על Delete ומאשרים את המחיקה.

  7. נכנסים לדף Service Accounts במסוף Google Cloud .

    לדף Service accounts

  8. מסמנים את התיבה ליד חשבון השירות שיצרתם.

  9. לוחצים על Delete ומאשרים את המחיקה.

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