יש שתי שיטות ליצור קובצי אימג' בקונטיינר באמצעות buildpacks:
- פיתוח מקומי באמצעות ה-CLI
packכדי לבדוק באופן מקומי את הפונקציה ולשנות במהירות את האב טיפוס לפני ההפצה. - פיתוח מרחוק באמצעות Cloud Build. הפיתוח באמצעות Cloud Build טוב לפונקציות עם תהליכי build שצורכים הרבה משאבים, והוא גם יכול לעזור בהגנה על שרשרת האספקה של תוכנות.
הגדרת הפרויקט לצורך בניית פונקציות
כדי ליצור פונקציות עם buildpacks:
כוללים את ספריית Functions Framework.
מגדירים את הערך של משתנה הסביבה
GOOGLE_FUNCTION_TARGETלשם של הפונקציה שבה משתמשים כנקודת הכניסה. כדי לעשות את זה, צריך לכלולproject.tomlבאותה תיקייה של קוד המקור. קובץproject.tomlצריך לכלול את ההגדרות הבאות:
[[build.env]]
name = "GOOGLE_FUNCTION_TARGET"
value = "ENTRY_POINT"
מחליפים את ENTRY_POINT בשיטת הפונקציה.
במאמר הגדרת שירותי פונקציות Cloud Run מוסבר איך משתמשים במשתני סביבה בפונקציות Cloud Run.
גרסאות build מקומיות
Pack הוא כלי CLI שמתוחזק על ידי פרויקט CNB כדי לתמוך בשימוש ב-buildpacks. צריך להשתמש ב-CLI pack כדי לפתח גרסת build מקומית של הפונקציה בתוך קובץ אימג' של קונטיינר.
לפני שמתחילים
- מתקינים את Docker Community Edition (CE) בתחנת העבודה. Docker משמש את
packליצירת קובצי אימג' של OCI. - מתקינים את Pack CLI.
- מתקינים את הכלי Git source control כדי שתהיה אפשרות לטעון אפליקציות לדוגמה מ-GitHub.
פיתוח פונקציה באופן מקומי
משתמשים בפקודה pack build ומציינים את ה-builder שמוגדר כברירת המחדל --builder=gcr.io/buildpacks/builder, כדי לפתח גרסת build מקומית של קובצי אימג' בקונטיינר.
pack build --builder=gcr.io/buildpacks/builder IMAGE_NAME
מחליפים את IMAGE_NAME בשם של קובץ אימג' של קונטיינר.
אתם גם יכולים להתאים אישית את קובץ אימג' של קונטיינר באמצעות הרחבה של פיתוח גרסת ה-build והרצה של קובצי אימג'.
פיתוח פונקציה לדוגמה באופן מקומי
בדוגמאות הבאות מוסבר איך לפתח גרסת build מקומית לדוגמה.
- משכפלים את המאגר לדוגמה ומעבירים אותו למכונה המקומית:
git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
- עוברים לספרייה שמכילה את הקוד לדוגמה של האפליקציה:
Go
cd buildpack-samples/sample-functions-framework-go
Java
cd buildpack-samples/sample-functions-framework-java-mvn
Node.js
cd buildpack-samples/sample-functions-framework-node
Python
cd buildpack-samples/sample-functions-framework-python
Ruby
cd buildpack-samples/sample-functions-framework-ruby
- כדי ליצור את הפונקציה לדוגמה משתמשים ב-
pack:Go
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-go
Java
pack build --builder gcr.io/buildpacks/builder:v1 sample-functions-java-mvn
Node.js
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-node
Python
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-python
Ruby
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-ruby
- מריצים את קובץ האימג' באמצעות
docker:Go
docker run -p8080:8080 sample-functions-framework-go
Java
docker run -it -ePORT=8080 -p8080:8080 sample-functions-java-mvn
Node.js
docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-node
Python
docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-python
Ruby
docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-ruby
- עוברים אל localhost:8080 כדי להיכנס לפונקציה שפועלת.
פיתוח גרסאות build מרחוק
משתמשים ב-Cloud Build כדי לפתח גרסת build של הפונקציה בקובץ אימג' בקונטיינר וב-Artifact Registry בתור מאגר קונטיינרים לאחסון ולפריסה של כל קובץ אימג'.
לפני שמתחילים
- נכנסים לחשבון 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 and Artifact Registry 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 and Artifact Registry 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 - מוודאים שלפרויקט Google Cloud יש גישה למאגר קובצי אימג' של קונטיינר.
כדי להגדיר גישה למאגר Docker ב-Artifact Registry:
- יוצרים מאגר Docker חדש באותו מיקום של הפרויקט Google Cloud .
מחליפים את:gcloud artifacts repositories create REPO_NAME \ --repository-format=docker \ --location=REGION --description="DESCRIPTION"
REPO_NAMEבשם שבחרתם למאגר ב-Docker.-
REGIONעם המיקום בפרויקט או במיקום הקרוב ביותר למיקום הפרויקט. Google Cloud DESCRIPTIONבתיאור כלשהו.
לדוגמה, כדי ליצור מאגר
dockerב-us-west2שהתיאור שלו הוא 'Docker repository', מריצים את הפקודה:gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \ --location=us-west2 --description="Docker repository"
- מוודאים שהמאגר נוצר:
gcloud artifacts repositories list
השם שבחרתם למאגר ב-Docker אמור להופיע ברשימה.
- יוצרים מאגר Docker חדש באותו מיקום של הפרויקט Google Cloud .
פיתוח פונקציה מרחוק
משתמשים בפקודה gcloud builds submit כדי ליצור את קובץ האימג' של קונטיינר ולהעלות אותו למאגר.
אפשר לציין את קובץ האימג' של הקונטיינר בפקודה עצמה או להשתמש בקובץ הגדרה.
פיתוח באמצעות פקודה
כדי לבצע build בלי קובץ תצורה, מציינים את הדגל image:
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
מחליפים את:
LOCATIONבשם האזור של מאגר הקונטיינרים, לדוגמה,us-west2PROJECT_IDבמזהה הפרויקט. Google CloudREPO_NAMEבשם המאגר ב-Docker.IMAGE_NAMEבשם של קובץ האימג' בקונטיינר.
דוגמה:
gcloud builds submit --pack image=us-west2-docker.pkg.dev/my-project-id/my-buildpacks-docker-repo
יצירת build באמצעות קובצי תצורה
אתם יכולים להשתמש בקובץ הגדרות כדי להגדיר את פרטי ההגדרות של מאגר האימג'ים. קובץ התצורה משתמש בפורמט של קובץ YAML, והוא חייב לכלול שלב build שמשתמש ב-CLI של pack.
יוצרים קובץ YAML בשם
cloudbuild.yamlשכולל את ה-URI של מאגר קובצי אימג' של קונטיינרים.options: logging: CLOUD_LOGGING_ONLY pool: {} projectId: PROJECT_ID steps: - name: gcr.io/k8s-skaffold/pack entrypoint: pack args: - build - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME - --builder - gcr.io/buildpacks/builder:latest - --network - cloudbuild - --publish - name: gcr.io/cloud-builders/docker entrypoint: pack args: - pull - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME images: - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAMEמחליפים את:
LOCATIONבשם האזור של מאגר הקונטיינרים, לדוגמה,us-west2.PROJECT_IDבמזהה הפרויקט. Google CloudREPO_NAMEבשם המאגר ב-Docker.IMAGE_NAMEבשם של קובץ האימג' בקונטיינר.
מפתחים את האפליקציה.
אם קראתם לקובץ התצורה
cloudbuild.yaml, מריצים את הפקודה הבאה:gcloud builds submit .
דוגמה: פיתוח פונקציה לדוגמה מרחוק
בדוגמאות הבאות מוסבר איך לפתח מרחוק גרסת build לדוגמה ואז לאמת שקובץ אימג' של קונטיינר הועבר למאגר ב-Artifact Registry.
- משכפלים את המאגר לדוגמה ומעבירים אותו למכונה המקומית:
git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
- עוברים לספרייה שמכילה את הקוד לדוגמה של האפליקציה:
Go
cd buildpack-samples/sample-functions-framework-go
Java
cd buildpack-samples/sample-functions-framework-java-mvn
Node.js
cd buildpack-samples/sample-functions-framework-node
Python
cd buildpack-samples/sample-functions-framework-python
Ruby
cd buildpack-samples/sample-functions-framework-ruby
- שולחים את קוד המקור של האפליקציה ל-Cloud Build באמצעות
gcloud:Go
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-go
Java
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-gradle
Node.js
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-node
Python
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-python
Ruby
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-ruby
מחליפים את:
LOCATIONבשם האזור של מאגר הקונטיינרים. לדוגמה:us-west2-docker.pkg.devPROJECT_IDבמזהה הפרויקט. Google CloudREPO_NAMEבשם המאגר ב-Docker.
-
מוודאים שהפונקציה לדוגמה פורסמה בהצלחה ב-
REPO_NAME:gcloud artifacts docker images list LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME
מחליפים את:
LOCATIONבשם האזור של מאגר הקונטיינרים, לדוגמה,us-west2.PROJECT_IDבמזהה הפרויקט. Google CloudREPO_NAMEבשם המאגר ב-Docker.
המאמרים הבאים
- פריסת האימג' של הפונקציה ב-Cloud Run
- הגדרה של משתני סביבה
- הגדרה של קובצי אימג' בגרסת build
- האצה של גרסאות build באמצעות קובצי אימג' ששמורים במטמון