範例:MongoDB Atlas 叢集的私人連線
本頁面說明如何啟用 Integration Connectors 與 Google Cloud 內私人網路上執行的 MongoDB Atlas 叢集之間的連線。
為 Atlas 叢集設定私人連線
如要為 Atlas 叢集設定私人連線,請按照下列步驟操作:
- 登入 Atlas 管理控制台。
- 開啟需要建立網路連線的 Atlas 專案。
- 開啟專案的「網路存取」頁面,然後選取「私人端點」。
- 選取「專屬叢集」,然後按一下「新增私人端點」。
系統會開啟對話方塊,讓您選擇雲端服務供應商。
- 選取「Google Cloud」,然後點選「下一步」。
- 選取 Atlas 區域,然後按一下「下一步」。
這是建立 Atlas 叢集的 Google Cloud 區域,且應與建立連線的 Google Cloud 專案所在區域相同。如需所有支援的 Integration Connectors 區域清單,請參閱「地區」。
- 輸入 Google Cloud 專案 ID。這是 Integration Connectors 執行階段的服務目錄專案 ID。詳情請參閱「取得 Service Directory 的專案 ID」。
- 在「VPC name」欄位中輸入
default
。 - 在「子網路名稱」欄位中輸入
default
。 - 在「Private service connect endpoint prefix」(Private Service Connect 端點前置字串) 欄位中輸入前置字串。
這段文字會加在所有端點名稱的前面,可用於識別您建立的私人端點。
- 安裝
gcloud
CLI (指令列介面)。詳情請參閱「安裝 gcloud CLI」一文。 - 複製並儲存下列殼層指令碼,做為本機系統的
setup_psc.sh
。在指令碼中為下列變數設定適當的值:
- PROJECT_ID
- 區域
- PREFIX
- SERVICE_ATTACHMENT_ID_PREFIX
#!/bin/bash alias gcurl='curl -H "Authorization: Bearer $(gcloud auth print-access-token)"' # Set PROJECT_ID to the ID of the Google Cloud project where you create your connections. export PROJECT_ID=PROJECT_ID # Set REGION to the Google Cloud region where Atlas cluster is installed. export REGION=REGION export HOSTNAME=https://connectors.googleapis.com/v1 # Set ENDPOINT_PREFIX to the prefix value that you entered for the # "Private service connect endpoint prefix" field (in step 10). export PREFIX=ENDPOINT_PREFIX # Set SERVICE_ATTACHMENT_ID_PREFIX to the prefix of the service attachments # created by Atlas. The service attachments are present in the script # displayed on the Atlas UI and have the following naming format: # projects/{google-cloud-project-id-of-atlas-cluster}/regions/{region}/serviceAttachment/{prefix}-{index} # For example, if the service attachment is # "projects/p-ditnerelrvndieqhe3kh9pop/regions/us-west1/serviceAttachments/sa-us-west1-37204mg5wr0j6-$i", # you must set the value of SERVICE_ATTACHMENT_ID_PREFIX to # "projects/p-ditnerelrvndieqhe3kh9pop/regions/us-west1/serviceAttachments/sa-us-west1-37204mg5wr0j6" export SERVICE_ATTACHMENT_ID_PREFIX=SERVICE_ATTACHMENT_ID_PREFIX # Set ENDPOINT_GLOBAL_ACCESS to true if you want to enable global access for the endpoint, else false. export ENDPOINT_GLOBAL_ACCESS=ENDPOINT_GLOBAL_ACCESS if [ "$ENDPOINT_GLOBAL_ACCESS" != "true" ] && [ "$ENDPOINT_GLOBAL_ACCESS" != "false" ]; then echo "Error: Variable ENDPOINT_GLOBAL_ACCESS must be either 'true' or 'false'." exit 1 fi function poll_operation() { OPERATION_ID="$1" INDEX="$2" RESULT='' while [[ "$RESULT" != *"true"* ]]; do RESULT=$(gcurl -s -X GET "${HOSTNAME}/${OPERATION_ID}" | grep '"done"') sleep 5 done echo "${PREFIX}-${INDEX} created" } echo "Creating Endpoint Attachments..." for i in {0..49} do export ENDPOINT=${HOSTNAME}/projects/${PROJECT_ID}/locations/${REGION}/endpointAttachments?endpointAttachmentId=${PREFIX}-$i OPERATION_ID=$(gcurl -s -X POST -H "Content-Type: application/json" -d '{"name": "'"projects/${PROJECT_ID}/locations/${REGION}/endpointAttachments/${PREFIX}-$i"'","serviceAttachment": "'"${SERVICE_ATTACHMENT_ID_PREFIX}-$i"'", "endpointGlobalAccess":'${ENDPOINT_GLOBAL_ACCESS}'}' "${ENDPOINT}" | sed 's/ //' | grep -E '"name"' | sed -E 's/"name": "//' | sed 's/",//') poll_operation "$OPERATION_ID" "$i" & done wait echo "All endpoint attachments created." echo "[" > atlasEndpoints-${PREFIX}.json echo "Fetching IP Addresses..." for i in {0..49} do ENDPOINT=${HOSTNAME}/projects/${PROJECT_ID}/locations/${REGION}/endpointAttachments/${PREFIX}-$i response=$(gcurl -s -X GET "${ENDPOINT}" | sed 's/ //g' | grep -E 'name|endpointIp' | sed 's/"endpointIp"/"IPAddress"/g' | sed -E 's/"name": "projects\/.+\/locations\/.+\/endpointAttachments\//"name": "/g' | sed '1 s/"/{"/1') if [ "$ENDPOINT_GLOBAL_ACCESS" = "true" ]; then echo "${response}" | sed '2 s/"/"}/4' >> atlasEndpoints-${PREFIX}.json else echo "${response}" | sed '2 s/"/"},/4' >> atlasEndpoints-${PREFIX}.json fi done sed -i '$s/,/]/' atlasEndpoints-${PREFIX}.json echo "Created atlasEndpoints-${PREFIX}.json"
- 執行殼層指令碼。如要執行 Shell 指令碼,請前往儲存指令碼的目錄,然後執行下列指令:
sh setup_psc.sh
這個指令碼會產生 JSON 檔案,其中包含 IP 位址清單及其對應的 PSC 端點名稱。JSON 檔案會儲存在執行殼層指令碼的目錄中。
- 在 Atlas 管理主控台中按一下「下一步」。
- 按一下「上傳 JSON 檔案」,然後上傳在步驟 13 中建立的 JSON 檔案。
- 按一下 [Create] (建立)。
端點設定程序最多可能需要 25 分鐘才能完成,如果設定成功,端點會處於
Available
狀態。 - 驗證私人連線。詳情請參閱「驗證私人連線」。
取得服務目錄的專案 ID
如要取得服務目錄的專案 ID,您可以使用 List Connections API,如以下範例所示。
語法
curl -X GET \ -H "authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://connectors.googleapis.com/v1/projects/CONNECTORS_PROJECT_ID/locations/-/connections"
取代下列內容:
- CONNECTORS_PROJECT_ID:您建立連線的 Google Cloud 專案 ID。
範例
這個範例會取得 connectors-test
Google Cloud 專案的服務目錄專案 ID。
curl -X GET \ -H "authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://connectors.googleapis.com/v1/projects/connectors-test/locations/-/connections"
在終端機上執行這個指令後,會顯示類似以下的輸出內容:
..... { "connections": [ { "name": "projects/connectors-test/locations/asia-northeast1/connections/big-query-iam-invalid-sa", "createTime": "2022-10-07T09:02:31.905048520Z", "updateTime": "2022-10-07T09:22:39.993778690Z", "connectorVersion": "projects/connectors-test/locations/global/providers/gcp/connectors/bigquery/versions/1", "status": { "state": "ACTIVE" }, "configVariables": [ { "key": "project_id", "stringValue": "connectors-test" }, { "key": "dataset_id", "stringValue": "testDataset" } ], "authConfig": {}, "serviceAccount": "564332356444-compute@developer.gserviceaccount.com", "serviceDirectory": "projects/abcdefghijk-tp/locations/asia-northeast1/namespaces/connectors/services/runtime", "nodeConfig": { "minNodeCount": 2, "maxNodeCount": 50 } }, ....
在輸出範例中,connectors-test
Google Cloud 專案的服務目錄專案 ID 為 abcdefghijk-tp
。
驗證私人連線
如要驗證 Atlas 叢集的私人連線,請按照下列步驟操作:
- 在 Atlas 管理控制台中,開啟「Database Deployments」頁面。
- 捲動至所需叢集,然後按一下「連線」。系統隨即開啟「Connect to <CLUSTER_NAME>」對話方塊。
- 按一下「Private Endpoint」,選擇連線類型。
- 從「Choose a private endpoint」下拉式選單中,選取您在 Atlas UI 中輸入的私人端點前置字串。
- 按一下「選擇連線方法」。
- 在「連線至應用程式」部分中,按一下「驅動程式」。這個步驟是取得
srv
連線字串。 - 從「將連線字串加入應用程式程式碼」專區複製
srv
連線字串。srv
字串的格式:mongodb+srv://{cluster-name}-pl-{index}.{unique-pin}.mongodb.net
。 - 使用這個
srv
字串做為主機名稱,建立 MongoDB 連線。詳情請參閱「設定 MongoDB 連接器」一文。連線應已成功建立。