גישה למשאבים ב-JFrog Artifactory פרטי עם מאגרי IP פרטיים

בדף הזה נדגים איך להשתמש במאגרי פרטיים של 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 ? יכול להיות שאתם זכאים לתקופת ניסיון בחינם.

לפני שמתחילים

  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. Verify that billing is enabled for your Google Cloud project.

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

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

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

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

אפשרות א': שימוש ב-Cloud Shell

אפשר לפעול לפי ההוראות במדריך הזה באמצעות Cloud Shell, ש-Google Cloud CLI שמשמש במדריך הזה מותקן בו מראש. אם אתם משתמשים ב-Cloud Shell, אתם לא צריכים להתקין את כלי שורת הפקודה האלה בתחנת העבודה.

כדי להשתמש ב-Cloud Shell:

  1. עוברים אל Google Cloud המסוף.

    Google Cloud console

  2. לוחצים על הלחצן Activate Cloud Shell כפתור הפעלת Shell בחלק העליון של חלון המסוף. Google Cloud

    בחלק התחתון של מסוף Google Cloud ייפתח סשן של Cloud Shell בתוך מסגרת חדשה, ותופיע הודעה של שורת הפקודה.

    סשן Cloud Shell

אפשרות ב': שימוש בכלי שורת פקודה באופן מקומי

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

  1. מתקינים את Google Cloud CLI.

יצירת Artifactory פרטי

  1. יצירת מכונה של Compute Engine מתוך קונטיינר:

    gcloud compute instances create-with-container jfrog \
    --container-image docker.bintray.io/jfrog/artifactory-jcr:latest \
    --zone us-central1-a
    
  2. מתחברים למופע באמצעות SSH. יכול להיות שיחלפו כמה דקות עד שהקונטיינר יאותחל.

    gcloud compute ssh --zone us-central1-a jfrog
    
  3. מריצים את הפקודה הבאה כדי לבדוק את החיבור. אחרי שהקונטיינר יהיה מוכן, הוא יגיב עם קוד HTTP‏ 200, ואחריו דף HTML.

    curl -i http://localhost:8081
    
  4. כדי ליצור מאגר ב-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

  1. יוצרים קובץ txt להעלאה ל-Artifactory:

    echo "Hello world" >> helloworld.txt
    
  2. ‫JFrog מגיע עם מאגר לדוגמה שמוגדר כברירת מחדל. מעלים למאגר באמצעות פרטי הכניסה שמוגדרים כברירת מחדל:

    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"
        }
    
  3. מזינים exit כדי לסיים את סשן ה-SSH.

  4. מסירים את כתובת ה-IP החיצונית, כדי שיהיה אפשר לגשת ל-Artifactory רק ממקורות פנימיים פרטיים.

    gcloud compute instances delete-access-config --zone us-central1-a jfrog
    

ניסיון לגשת לנתונים מ-Artifactory

  1. מגדירים משתני סביבה כדי לאחסן את מזהה הפרויקט ואת מספר הפרויקט:

    PROJECT_ID=$(gcloud config list --format='value(core.project)')
    PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
    
  2. מקצים לחשבון השירות שבו אתם משתמשים לבנייה את התפקיד Compute Engine Viewer כדי שתוכלו לראות את כתובת ה-IP הפנימית של מופע JFrog:

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:SERVICE_ACCOUNT \
        --role=roles/compute.viewer
    

    כאשר SERVICE_ACCOUNT היא כתובת האימייל בחשבון השירות.

  3. יוצרים קובץ בשם cloudbuild.yaml עם הקוד הבא כדי לקרוא מ-Artifactory. זהו קובץ תצורת ה-build.

    בשלב הראשון מאחזרים את כתובת ה-IP הפנימית מ-Artifactory שיצרתם. בשלב השני נשלחת בקשה לכתובת הזו לקרוא את הקובץ helloworld.txt שיצרתם. השלבים מופרדים כדי להקל על זיהוי שגיאות בהרשאות וב-Networking. אם השלב הראשון נכשל, זה קורה בגלל שגיאת הרשאות. צריך לוודא שלחשבון השירות של ה-Build יש גישה למשאבים של Compute Engine, כמו שמוצג בשלב הקודם. אם השלב השני נכשל, זה קורה בגלל שגיאה בחיבור לרשת. בהמשך המדריך הזה נסביר על הגדרות הרשת.

    steps:
      - id: Get Private Artifactory Address
        name: gcr.io/cloud-builders/gcloud
        entrypoint: /bin/bash
        args: 
          - -c
          - |
            gcloud compute instances describe jfrog \
            --zone us-central1-a \
            --format="value(networkInterfaces.networkIP)" >> _INTERNAL_IP_ADDRESS
    
      - id: Pull from Private Artifactory
        name: gcr.io/cloud-builders/curl
        entrypoint: /bin/bash
        args:
          - -c
          - |
            curl -u admin:password --connect-timeout 10.00 \
            http://$(cat _INTERNAL_IP_ADDRESS):8081/artifactory/example-repo-local/helloworld.txt
  4. מפעילים 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 לבין הרשת של בעלים של שירות מנוהל

  1. קודם כול, מוודאים שרשת ה-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
    
  2. יוצרים טווח שמור למאגר הפרטי של Cloud Build כדי להשתמש בו עבור העובדים. הטווח השמור חייב להיות ברשת שבה נמצא Artifactory. במקרה הזה, השדה הוא default compute 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
    
  3. מבצעים קישור בין רשת ה-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
    

יצירת המאגר הפרטי

  1. רשת ה-VPC‏ default מוכנה עכשיו לשימוש עם מאגרי Cloud Build פרטיים. יוצרים את המאגר הפרטי ומקשרים אותו לרשת ה-VPC.

     gcloud builds worker-pools create jfrog-pool --region us-central1 \
     --peered-network=projects/${PROJECT_ID}/global/networks/default
    
  2. כדי להריץ את ה-build עם המאגר הפרטי החדש, אפשר להעביר את הדגל --worker-pool עם הפקודה gcloud או לעדכן את ההגדרה cloudbuild.yaml כדי לוודא שהמאגר הפרטי תמיד נמצא בשימוש. במדריך הזה, מעדכנים את cloudbuild.yaml על ידי הוספת האפשרות הבאה:

    options:
      pool:
        name: 'projects/${PROJECT_ID}/locations/us-central1/workerPools/jfrog-pool'
  3. הקובץ המלא ייראה כך:

    steps:
      - id: Get Private Artifactory Address
        name: gcr.io/cloud-builders/gcloud
        entrypoint: /bin/bash
        args: 
          - -c
          - |
            gcloud compute instances describe jfrog \
            --zone us-central1-a \
            --format="value(networkInterfaces.networkIP)" >> _INTERNAL_IP_ADDRESS
    
      - id: Pull from Private Artifactory
        name: gcr.io/cloud-builders/curl
        entrypoint: /bin/bash
        args:
          - -c
          - |
            curl -u admin:password --connect-timeout 10.00 \
            http://$(cat _INTERNAL_IP_ADDRESS):8081/artifactory/example-repo-local/helloworld.txt
    
    options:
      pool:
        name: 'projects/${PROJECT_ID}/locations/us-central1/workerPools/jfrog-pool'
  4. מתחילים את הבנייה:

     gcloud builds submit --no-source
    
  5. ה-build ישתמש במאגר הפרטי החדש, שמוצמד לרשת ה-VPC, וכך תהיה לו גישה לכתובת ה-IP הפנימית של Artifactory. הפלט יהיה מוצלח והפונקציה Step #1 תדפיס 'Hello world'.

הסרת המשאבים

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

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

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

הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך.

כדי למחוק את הפרויקט:

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

    כניסה לדף Manage resources

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

מחיקת משאבי הדרכה

  1. מוחקים את שירות Compute Engine שפרסתם במדריך הזה:

     gcloud compute instances delete jfrog
    
  2. מוחקים את כלל חומת האש:

     gcloud compute firewall-rules delete allow-private-pools --network=default
    
  3. מסירים את הטווח השמור:

     gcloud compute addresses delete jfrog-ranges --global
    
  4. מחיקת המאגר הפרטי של Cloud Build:

     gcloud builds worker-pools delete jfrog-pool
    

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