透過私人集區存取私人 JFrog Artifactory 中的資源


本頁面說明如何使用 Cloud Build 私人集區,從私人虛擬私有雲網路存取資源。

在本教學課程中,您會在私有虛擬私有雲網路中建立 Compute Engine 代管的 JFrog Artifactory,然後設定在私有集區中執行的建構作業,從該 Artifactory 存取資料。JFrog Artifactory 是開放原始碼的二進位檔存放區管理工具。

目標

  • 在 Compute Engine 上設定 JFrog Artifactory
  • 將檔案上傳至 Artifactory
  • 建立私人集區
  • 將代管私有集區的服務生產者網路,對等互連至 Artifactory 的虛擬私有雲網路
  • 編寫建構設定檔,存取 Artifactory 中的資料

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

  • Compute Engine
  • Cloud Build

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine, Cloud Build, Service Networking APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Compute Engine, Cloud Build, Service Networking APIs.

    Enable the APIs

  8. 選項 A:使用 Cloud Shell

    在進行本教學課程時,您可以使用 Cloud Shell,其中已預先安裝本教學課程所使用的 Google Cloud CLI。如果使用的是 Cloud Shell,則不需要在工作站上安裝這些指令列工具。

    如要使用 Cloud Shell:

    1. 前往Google Cloud 控制台

      Google Cloud 遊戲主機

    2. 按一下主控台視窗頂端的「啟用 Cloud Shell」啟動 Shell 按鈕 Google Cloud 按鈕。

      系統會在 Google Cloud 控制台底部的新頁框中開啟 Cloud Shell 工作階段,並顯示指令列提示。

      Cloud Shell 工作階段

    選項 B:在本機使用指令列工具

    如果您想在工作站上進行本教學課程,請按照下列步驟安裝必要工具。

    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. 執行下列指令來測試連線。容器準備就緒後,會先傳回 200 HTTP 代碼,然後傳回 HTML 網頁。

    curl -i http://localhost:8081
    
  4. 如要在 Artifactory 建立存放區,您必須簽署 JFrog 使用者授權協議 (EULA):

    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. 建立要上傳至 Artifactory 的 txt 檔案:

    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. 設定環境變數,儲存專案 ID 和專案編號:

    PROJECT_ID=$(gcloud config list --format='value(core.project)')
    PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
    
  2. Compute Engine 檢視者角色授予用於建構的服務帳戶,以便查看 JFrog 執行個體的內部 IP 位址:

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

    其中 SERVICE_ACCOUNT 是服務帳戶電子郵件地址。

  3. 建立名為 cloudbuild.yaml 的檔案,其中包含下列程式碼,可從 Artifactory 讀取資料。這是建構設定檔。

    第一步是從您建立的 Artifactory 擷取內部 IP 位址。第二個步驟會將要求傳送至該地址,以讀取您建立的 helloworld.txt 檔案。這些步驟分開列出,方便您找出權限和網路錯誤。如果第一個步驟失敗,表示發生權限錯誤,您需要確認建構服務帳戶是否具備 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. 使用建構設定檔啟動建構作業。

    根據預設,在 Cloud Build 上執行建構作業時,建構作業會在安全的託管環境中執行,並可存取公開網際網路。每項建構作業都會在專屬的 worker 上執行,並與其他工作負載隔離。預設集區的環境自訂程度有限,尤其是私人網路存取權。在本例中,您嘗試從公開工作站存取私人網路。

    使用下列指令執行 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 私人集區。

在 Artifactory 的虛擬私有雲網路與服務供應商網路之間建立私人連線

  1. 首先,請確認虛擬私有雲網路允許輸入。建立防火牆規則,允許傳入的內部流量進入含有 jfrog 執行個體的網路。範圍 10.0.0.0/16 位於私人位址空間中,您會在下列步驟中將其用於 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 計算網路。

    設定預留範圍時,有兩種做法。您可以提供 --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. 將虛擬私有雲網路與 Service Networking API 對等互連。

    Cloud Build 私人集區會使用 Service Networking API 執行工作站。這樣一來,您就能在內部 IP 位址上提供代管服務。方法是將執行 Cloud Build 私人集區工作站的 Google 管理虛擬私有雲,與您自己的虛擬私有雲對等互連。這項作業應該會在幾分鐘內完成。

    gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \
    --ranges=jfrog-ranges --network=default
    

建立私人集區

  1. default 虛擬私有雲網路現在可搭配 Cloud Build 私人集區使用。建立私人集區,並與虛擬私有雲網路對等互連。

     gcloud builds worker-pools create jfrog-pool --region us-central1 \
     --peered-network=projects/${PROJECT_ID}/global/networks/default
    
  2. 如要使用新的私人集區執行建構作業,您可以透過 gcloud 指令傳遞 --worker-pool 標記,也可以更新 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. 建構作業會使用與虛擬私有雲網路對等互連的新私人集區,因此可以存取 Artifactory 的內部 IP 位址。輸出作業會成功,且 Step #1 應會列印「Hello world」。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有專案,並想保留專案,但不要本教學課程新增的變更,請刪除為本教學課程建立的資源

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

如要刪除專案:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

刪除教學課程資源

  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
    

後續步驟