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

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

  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.

    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

  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.

    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. 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 Invoker כי בפונקציות 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. מאשרים את שאר הגדרות ברירת המחדל ולוחצים על יצירה.

  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. ברשימה אזור זמן, בוחרים אזור זמן.

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

  8. ברשימה סוג היעד, בוחרים באפשרות HTTP.

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

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

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

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

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

יצרתם משימת cron שמריצה את פונקציית Cloud Run בשעה 16:30 בימי ראשון.

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

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

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

    מעבר אל Jobs

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

  3. לוחצים על הפעלת הרצה בכוח.

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

    אחרי שהעבודה תפעל, בסטטוס של ההרצה האחרונה יופיע Success.

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

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

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

    כניסה לדף Services

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

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

    ייפתח הדף Service details עם מדדי השירות.

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

    אמורה להופיע רשומה ביומן שדומה לזו: 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. לוחצים על מחיקה ומאשרים את המחיקה.

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

    כניסה לדף Services

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

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

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

    לדף Service accounts

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

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

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