例: MongoDB Atlas クラスタのプライベート接続性
このページでは、Integration Connectors から Google Cloud 内のプライベート ネットワークで実行されている MongoDB Atlas クラスタへの接続性を有効にする方法について説明します。
Atlas クラスタのプライベート接続性を構成する
Atlas クラスタのプライベート接続性を構成する手順は次のとおりです。
- Atlas 管理コンソールにログインします。
- ネットワーク接続性を確立する必要がある Atlas プロジェクトを開きます。
- プロジェクトの [ネットワーク アクセス] ページを開いてから、[プライベート エンドポイント] を選択します。
- [専用クラスタ] を選択してから、[プライベート エンドポイントを追加] をクリックします。
これによってダイアログが開くので、クラウド プロバイダを選択します。
- [Google Cloud] を選択してから、[Next] をクリックします。
- [Atlas リージョン] を選択してから、[Next] をクリックします。
これは、Atlas クラスタが作成される Google Cloud リージョンで、接続を作成する Google Cloud プロジェクトのものと同じである必要があります。サポートされているすべての Integration Connectors リージョンの一覧については、ロケーションをご覧ください。
- Google Cloud プロジェクト ID を入力します。これは、Integration Connectors ランタイムのサービス ディレクトリのプロジェクト ID です。詳細については、Service Directory のプロジェクト ID を取得するをご覧ください。
- [VPC 名] フィールドに
defaultと入力します。 - [サブネット名] フィールドに
defaultと入力します。 - [プライベート サービス接続エンドポイントの接頭辞] フィールドに接頭辞を入力します。
このテキストはすべてのエンドポイント名の接頭辞として使用され、作成したプライベート エンドポイントの識別に使用できます。
gcloudCLI(コマンドライン インターフェース)をインストールします。詳細については、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"
- シェル スクリプトを実行します。シェル スクリプトを実行するには、スクリプトを保存したディレクトリに移動してから、次のコマンドを実行します。
sh setup_psc.sh
このスクリプトは、IP アドレスとそれらに対応する PSC エンドポイント名のリストを含む JSON ファイルを生成します。JSON ファイルは、シェル スクリプトが実行されるディレクトリに保存されます。
- Atlas 管理コンソールで [次へ] をクリックします。
- [JSON ファイルをアップロードする] をクリックしてから、手順 13 で作成した JSON ファイルをアップロードします。
- [作成] をクリックします。
エンドポイントの設定プロセスが完了するには最大 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 管理コンソールで、[データベースのデプロイ] ページを開きます。
- 必要なクラスタまでスクロールしてから、[接続] をクリックします。これによって、[Connect to <CLUSTER_NAME>] ダイアログが開きます。
- 接続タイプの [プライベート エンドポイント] をクリックします。
- [プライベート エンドポイントを選択する] プルダウンから、Atlas UI に入力したプライベート エンドポイント接頭辞を選択します。
- [接続方法を選択する] をクリックします。
- [アプリケーションに接続する] セクションで [ドライバ] をクリックします。この手順では、
srv接続文字列を取得します。 - [アプリケーション コードに接続文字列を追加する] セクションから
srv接続文字列をコピーします。srv文字列の形式:mongodb+srv://{cluster-name}-pl-{index}.{unique-pin}.mongodb.net。 - ホスト名としてこの
srv文字列を使用して、MongoDB 接続を作成します。詳しくは、MongoDB コネクタを構成するをご覧ください。接続は正常に作成される必要があります。