בדף הזה נדגים איך להשתמש במאגרי פרטיים של Cloud Build כדי לגשת למשאבים מרשת פרטית של ענן וירטואלי פרטי (VPC).
במדריך הזה תיצרו JFrog Artifactory ב-Compute Engine שמתארח ברשת VPC פרטית, ואז תגדירו build שפועל במאגר פרטי כדי לגשת לנתונים מ-Artifactory. Jfrog Artifactory הוא מנהל מאגר בינארי בקוד פתוח.
מטרות
- הגדרה של Jfrog Artifactory ב-Compute Engine
- העלאת קובץ ל-Artifactory
- יצירת מאגר פרטי
- קישור של רשת הבעלים של שירות מנוהל שמארחת את המאגר הפרטי לרשת הענן הווירטואלי הפרטי (VPC) של Artifactory
- כתיבת קובץ תצורת build כדי לגשת לנתונים ב-Artifactory
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:
- Compute Engine
- Cloud Build
כדי להעריך את ההוצאות בהתאם לתחזית השימוש שלכם, אתם יכולים להיעזר במחשבון העלויות.
לפני שמתחילים
- נכנסים לחשבון 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 Compute Engine, Cloud Build, Service Networking 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.-
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 Compute Engine, Cloud Build, Service Networking 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.
אפשרות א': שימוש ב-Cloud Shell
אפשר לפעול לפי ההוראות במדריך הזה באמצעות Cloud Shell, ש-Google Cloud CLI שמשמש במדריך הזה מותקן בו מראש. אם אתם משתמשים ב-Cloud Shell, אתם לא צריכים להתקין את כלי שורת הפקודה האלה בתחנת העבודה.
כדי להשתמש ב-Cloud Shell:
עוברים אל Google Cloud המסוף.
לוחצים על הלחצן Activate Cloud Shell
בחלק העליון של חלון המסוף. Google Cloud בחלק התחתון של מסוף Google Cloud ייפתח סשן של Cloud Shell בתוך מסגרת חדשה, ותופיע הודעה של שורת הפקודה.
אפשרות ב': שימוש בכלי שורת פקודה באופן מקומי
אם אתם מעדיפים לפעול לפי ההדרכה הזו בתחנת העבודה שלכם, אתם צריכים להתקין את הכלים הנדרשים.
יצירת Artifactory פרטי
יצירת מכונה של Compute Engine מתוך קונטיינר:
gcloud compute instances create-with-container jfrog \ --container-image docker.bintray.io/jfrog/artifactory-jcr:latest \ --zone us-central1-aמתחברים למופע באמצעות SSH. יכול להיות שיחלפו כמה דקות עד שהקונטיינר יאותחל.
gcloud compute ssh --zone us-central1-a jfrogמריצים את הפקודה הבאה כדי לבדוק את החיבור. אחרי שהקונטיינר יהיה מוכן, הוא יגיב עם קוד HTTP
200, ואחריו דף HTML.curl -i http://localhost:8081כדי ליצור מאגר ב-Artifactory, צריך לחתום על הסכם הרישיון עם משתמש הקצה (EULA) של JFrog:
curl -XPOST -vu admin:password http://localhost:8081/artifactory/ui/jcr/eula/acceptהפלט אמור להיראות כך:
* Trying 127.0.0.1:8081... * Connected to localhost (127.0.0.1) port 8081 (#0) * Server auth using Basic with user 'admin' > POST /artifactory/ui/jcr/eula/accept HTTP/1.1 > Host: localhost:8081 > Authorization: Basic …. > User-Agent: curl/7.74.0 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < X-JFrog-Version: Artifactory/7.19.9 71909900 < X-Artifactory-Id: …. < X-Artifactory-Node-Id: jfrog2 < SessionValid: false < Content-Length: 0 < Date: Fri, 25 Jun 2021 19:08:10 GMT * Connection #0 to host localhost left intact
העלאת קובץ ל-Artifactory
יוצרים קובץ txt להעלאה ל-Artifactory:
echo "Hello world" >> helloworld.txtJFrog מגיע עם מאגר לדוגמה שמוגדר כברירת מחדל. מעלים למאגר באמצעות פרטי הכניסה שמוגדרים כברירת מחדל:
curl -u admin:password -X PUT \ "http://localhost:8081/artifactory/example-repo-local/helloworld.txt" \ -T helloworld.txtהפלט צריך להיות:
{ "repo" : "example-repo-local", "path" : "/helloworld.txt", "created" : "2021-06-25T19:08:24.176Z", "createdBy" : "admin", "downloadUri" : "http://localhost:8081/artifactory/example-repo-local/helloworld.txt", "mimeType" : "text/plain", "size" : "12", "checksums" : { "sha1" : "...", "md5" : "...", "sha256" : "..." }, "originalChecksums" : { "sha256" : "..." }, "uri" : "http://localhost:8081/artifactory/example-repo-local/helloworld.txt" }מזינים
exitכדי לסיים את סשן ה-SSH.מסירים את כתובת ה-IP החיצונית, כדי שיהיה אפשר לגשת ל-Artifactory רק ממקורות פנימיים פרטיים.
gcloud compute instances delete-access-config --zone us-central1-a jfrog
ניסיון לגשת לנתונים מ-Artifactory
מגדירים משתני סביבה כדי לאחסן את מזהה הפרויקט ואת מספר הפרויקט:
PROJECT_ID=$(gcloud config list --format='value(core.project)') PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')מקצים לחשבון השירות שבו אתם משתמשים לבנייה את התפקיד Compute Engine Viewer כדי שתוכלו לראות את כתובת ה-IP הפנימית של מופע JFrog:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT \ --role=roles/compute.viewerכאשר SERVICE_ACCOUNT היא כתובת האימייל בחשבון השירות.
יוצרים קובץ בשם
cloudbuild.yamlעם הקוד הבא כדי לקרוא מ-Artifactory. זהו קובץ תצורת ה-build.בשלב הראשון מאחזרים את כתובת ה-IP הפנימית מ-Artifactory שיצרתם. בשלב השני נשלחת בקשה לכתובת הזו לקרוא את הקובץ
helloworld.txtשיצרתם. השלבים מופרדים כדי להקל על זיהוי שגיאות בהרשאות וב-Networking. אם השלב הראשון נכשל, זה קורה בגלל שגיאת הרשאות. צריך לוודא שלחשבון השירות של ה-Build יש גישה למשאבים של Compute Engine, כמו שמוצג בשלב הקודם. אם השלב השני נכשל, זה קורה בגלל שגיאה בחיבור לרשת. בהמשך המדריך הזה נסביר על הגדרות הרשת.מפעילים build באמצעות קובץ תצורת ה-build.
כברירת מחדל, כשמריצים build ב-Cloud Build, ה-build רץ בסביבה מאובטחת ומארחת עם גישה לאינטרנט הציבורי. כל בנייה פועלת על עובד משלה ומבודדת מעומסי עבודה אחרים. לבחירת ברירת המחדל יש מגבלות על מידת ההתאמה האישית של הסביבה, במיוחד בנוגע לגישה לרשת פרטית. בדוגמה הזו, אתם מנסים לגשת לרשת פרטית מעובד ציבורי.
מריצים את הפקודה
cloudbuild.yaml. הפעולה אמורה להיכשל.gcloud builds submit --no-sourceהפלט אמור להיראות ככה:
BUILD Starting Step #0 - "Get Private Artifactory Address" Step #0 - "Get Private Artifactory Address": Already have image (with digest): gcr.io/cloud-builders/gcloud Finished Step #0 - "Get Private Artifactory Address" Starting Step #1 - "Pull from Private Artifactory" Step #1 - "Pull from Private Artifactory": Already have image (with digest): gcr.io/cloud-builders/curl Step #1 - "Pull from Private Artifactory": % Total % Received % Xferd Average Speed Time Time Time Current Step #1 - "Pull from Private Artifactory": Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- 0:02:09 --:--:-- 0curl: (7) Failed to connect to 10.128.0.2 port 8081: Connection timed out Finished Step #1 - "Pull from Private Artifactory" ERROR ERROR: build step 1 "gcr.io/cloud-builders/curl" failed: step exited with non-zero status: 7אפשר לראות לפי פסק הזמן הקצוב לתפוגת החיבור ש-Cloud Build לא מצליח להגיע לכתובת ה-IP הפנימית. כדי לגשת למשאב הפרטי הזה, צריך להשתמש במאגרי Cloud Build פרטיים.
יצירת חיבור פרטי בין רשת ה-VPC של Artifactory לבין הרשת של בעלים של שירות מנוהל
קודם כול, מוודאים שרשת ה-VPC מאפשרת תעבורת נכנסת. יוצרים כלל חומת אש כדי לאפשר תעבורת נתונים פנימית נכנסת לרשת עם
jfrogמכונה. הטווח10.0.0.0/16נמצא במרחב כתובות פרטי, שבו תשתמשו במאגרי ה-IP הפרטיים של Cloud Build בשלבים הבאים.gcloud compute firewall-rules create allow-private-pools --direction=INGRESS \ --priority=1000 --network=default --action=ALLOW --rules=all --source-ranges=10.0.0.0/16יוצרים טווח שמור למאגר הפרטי של Cloud Build כדי להשתמש בו עבור העובדים. הטווח השמור חייב להיות ברשת שבה נמצא Artifactory. במקרה הזה, השדה הוא
defaultcompute network.יש שתי אפשרויות להגדרת טווחי כתובות שמורות. אפשר לציין את הטווח באופן מפורש על ידי ציון
--addressesו---prefix-length, או לאפשר ל- Google Cloud להקצות טווח זמין על סמךprefix-lengthשצוין.בדוגמה שלמטה, הגדרתם במפורש את הכתובות כך שיתאימו לכלל חומת האש שיצרתם. המאגר הפרטי ישתמש במרחב הכתובות הזה, והתנועה הנכנסת לא תיחסם.
gcloud compute addresses create jfrog-ranges --global --purpose=VPC_PEERING \ --addresses=10.0.0.0 --prefix-length=16 --network=defaultמבצעים קישור בין רשת ה-VPC לבין Service Networking API.
מאגרי Cloud Build פרטיים מפעילים עובדים באמצעות Service Networking API. כך תוכלו להציע את השירותים המנוהלים שלכם באמצעות כתובות IP פנימיות. החיבור מתבצע על ידי קישור בין ה-VPC שמנוהל על ידי Google ומריץ את העובדים של המאגר הפרטי של Cloud Build לבין ה-VPC שלכם. השלמת הפעולה עשויה להימשך כמה דקות.
gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \ --ranges=jfrog-ranges --network=default
יצירת המאגר הפרטי
רשת ה-VPC
defaultמוכנה עכשיו לשימוש עם מאגרי Cloud Build פרטיים. יוצרים את המאגר הפרטי ומקשרים אותו לרשת ה-VPC.gcloud builds worker-pools create jfrog-pool --region us-central1 \ --peered-network=projects/${PROJECT_ID}/global/networks/defaultכדי להריץ את ה-build עם המאגר הפרטי החדש, אפשר להעביר את הדגל
--worker-poolעם הפקודהgcloudאו לעדכן את ההגדרהcloudbuild.yamlכדי לוודא שהמאגר הפרטי תמיד נמצא בשימוש. במדריך הזה, מעדכנים אתcloudbuild.yamlעל ידי הוספת האפשרות הבאה:הקובץ המלא ייראה כך:
מתחילים את הבנייה:
gcloud builds submit --no-sourceה-build ישתמש במאגר הפרטי החדש, שמוצמד לרשת ה-VPC, וכך תהיה לו גישה לכתובת ה-IP הפנימית של Artifactory. הפלט יהיה מוצלח והפונקציה
Step #1תדפיס 'Hello world'.
הסרת המשאבים
כדי להימנע מחיובים בחשבון Google Cloud בגלל השימוש במשאבים שנעשה במסגרת המדריך הזה, אפשר למחוק את הפרויקט שמכיל את המשאבים, או להשאיר את הפרויקט ולמחוק את המשאבים בנפרד.
אם יצרתם פרויקט חדש בשביל המדריך הזה, תמחקו את הפרויקט. אם השתמשתם בפרויקט קיים ואתם רוצים לשמור אותו בלי השינויים שנוספו במדריך הזה, תצטרכו למחוק את המשאבים שנוצרו לצורך המדריך.
מחיקת הפרויקט
הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך.
כדי למחוק את הפרויקט:
- במסוף Google Cloud , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.
מחיקת משאבי הדרכה
מוחקים את שירות Compute Engine שפרסתם במדריך הזה:
gcloud compute instances delete jfrogמוחקים את כלל חומת האש:
gcloud compute firewall-rules delete allow-private-pools --network=defaultמסירים את הטווח השמור:
gcloud compute addresses delete jfrog-ranges --globalמחיקת המאגר הפרטי של Cloud Build:
gcloud builds worker-pools delete jfrog-pool