本頁面說明如何使用 Cloud Build 私人集區,從私人虛擬私有雲網路存取資源。
在本教學課程中,您會在私有虛擬私有雲網路中建立 Compute Engine 代管的 JFrog Artifactory,然後設定在私有集區中執行的建構作業,從該 Artifactory 存取資料。JFrog Artifactory 是開放原始碼的二進位檔存放區管理工具。
建立私有 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
執行下列指令來測試連線。容器準備就緒後,會先傳回
200
HTTP 代碼,然後傳回 HTML 網頁。curl -i http://localhost:8081
如要在 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
建立要上傳至 Artifactory 的 txt 檔案:
echo "Hello world" >> helloworld.txt
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" }
輸入
exit
結束 SSH 工作階段。移除外部 IP 位址,這樣一來,Artifactory 就只能從私人內部來源存取。
gcloud compute instances delete-access-config --zone us-central1-a jfrog
嘗試從 Artifactory 存取資料
設定環境變數,儲存專案 ID 和專案編號:
PROJECT_ID=$(gcloud config list --format='value(core.project)') PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
將 Compute Engine 檢視者角色授予用於建構的服務帳戶,以便查看 JFrog 執行個體的內部 IP 位址:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT \ --role=roles/compute.viewer
其中 SERVICE_ACCOUNT 是服務帳戶電子郵件地址。
建立名為
cloudbuild.yaml
的檔案,其中包含下列程式碼,可從 Artifactory 讀取資料。這是建構設定檔。第一步是從您建立的 Artifactory 擷取內部 IP 位址。第二個步驟會將要求傳送至該地址,以讀取您建立的
helloworld.txt
檔案。這些步驟分開列出,方便您找出權限和網路錯誤。如果第一個步驟失敗,表示發生權限錯誤,您需要確認建構服務帳戶是否能存取 Compute Engine 資源,如上一個步驟所示。如果第二個步驟失敗,表示網路發生錯誤。本教學課程的其餘部分會說明網路設定。使用建構設定檔啟動建構作業。
根據預設,在 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 的虛擬私有雲網路與服務供應商網路之間建立私人連線
首先,請確認虛擬私有雲網路允許輸入。建立防火牆規則,允許傳入的內部流量進入含有
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
為 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
將虛擬私有雲網路與 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
建立私人集區
default
虛擬私有雲網路現在可與 Cloud Build 私人集區搭配使用。建立私人集區,並與虛擬私有雲網路對等互連。gcloud builds worker-pools create jfrog-pool --region us-central1 \ --peered-network=projects/${PROJECT_ID}/global/networks/default
如要使用新的私人集區執行建構作業,您可以傳遞
--worker-pool
標記和gcloud
指令,或更新cloudbuild.yaml
設定,確保系統一律使用私人集區。在本教學課程中,請新增下列選項來更新cloudbuild.yaml
:完整檔案如下所示:
啟動建構:
gcloud builds submit --no-source
建構作業會使用與虛擬私有雲網路對等互連的新私人集區,因此可以存取 Artifactory 的內部 IP 位址。輸出結果會顯示成功,且
Step #1
應會列印「Hello world」。