例: Atlas クラスタのプライベート接続性

このページでは、Integration Connectors から Google Cloud 内のプライベート ネットワークで実行されている MongoDB Atlas クラスタへの接続性を有効にする方法について説明します。

Atlas クラスタのプライベート接続性を構成する

Atlas クラスタのプライベート接続性を構成する手順は次のとおりです。

  1. Atlas 管理コンソールにログインします。
  2. ネットワーク接続性を確立する必要がある Atlas プロジェクトを開きます。
  3. プロジェクトの [ネットワーク アクセス] ページを開いてから、[プライベート エンドポイント] を選択します。
  4. [専用クラスタ] を選択してから、[プライベート エンドポイントを追加] をクリックします。

    これによってダイアログが開くので、クラウド プロバイダを選択します。

  5. [Google Cloud] を選択してから、[Next] をクリックします。
  6. [Atlas リージョン] を選択してから、[Next] をクリックします。

    これは、Atlas クラスタが作成される Google Cloud リージョンで、接続を作成する Google Cloud プロジェクトのものと同じである必要があります。サポートされているすべての Integration Connector のリージョンの一覧については、ロケーションをご覧ください。

  7. Google Cloud プロジェクト ID を入力します。これは、Integration Connectors ランタイムのサービス ディレクトリのプロジェクト ID です。詳細については、サービス ディレクトリのプロジェクト ID を取得するをご覧ください。
  8. [VPC 名] フィールドに default と入力します。
  9. [サブネット名] フィールドに default と入力します。
  10. [プライベート サービス接続エンドポイントの接頭辞] フィールドに接頭辞を入力します。

    このテキストはすべてのエンドポイント名の前に付けられ、作成したプライベート エンドポイントを識別するために使用できます。

  11. gcloud CLI(コマンドライン インターフェース)をインストールします。詳細については、gcloud CLI をインストールするをご覧ください。
  12. 次のシェル スクリプトをコピーして、ローカル システムに 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
    
    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"'"}' "${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
      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' | sed '2 s/"/"},/4' >> atlasEndpoints-${PREFIX}.json
    done
    
    sed -i '$s/,/]/' atlasEndpoints-${PREFIX}.json
    
    echo "Created atlasEndpoints-${PREFIX}.json"
  13. シェル スクリプトを実行します。シェル スクリプトを実行するには、スクリプトを保存したディレクトリに移動してから、次のコマンドを実行します。
    sh setup_psc.sh

    このスクリプトは、IP アドレスとそれらに対応する PSC エンドポイント名のリストを含む JSON ファイルを生成します。JSON ファイルは、シェル スクリプトが実行されるディレクトリに保存されます。

  14. Atlas 管理コンソールで [次へ] をクリックします。
  15. [JSON ファイルをアップロードする] をクリックしてから、手順 13 で作成した JSON ファイルをアップロードします。
  16. [作成] をクリックします。

    エンドポイントの設定プロセスが完了するには最大 25 分かかります。設定が成功した場合、エンドポイントは Available 状態になります。

  17. プライベート接続性を確認します。詳細については、プライベート接続性を確認するをご覧ください。

サービス ディレクトリのプロジェクト 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 クラスタへのプライベート接続性を確認するには、次のようにします。

  1. Atlas 管理コンソールで、[データベースのデプロイ] ページを開きます。
  2. 必要なクラスタまでスクロールしてから、[接続] をクリックします。これによって、[Connect to <CLUSTER_NAME>] ダイアログが開きます。
  3. 接続タイプの [プライベート エンドポイント] をクリックします。
  4. [プライベート エンドポイントを選択する] プルダウンから、Atlas UI に入力したプライベート エンドポイント接頭辞を選択します。
  5. [接続方法を選択する] をクリックします。
  6. [アプリケーションに接続する] セクションで [ドライバ] をクリックします。このステップでは、srv 接続文字列を取得します。
  7. [アプリケーション コードに接続文字列を追加する] セクションから srv 接続文字列をコピーします。

    srv 文字列の形式: mongodb+srv://{cluster-name}-pl-{index}.{unique-pin}.mongodb.net

  8. ホスト名としてこの srv 文字列を使用して、MongoDB 接続を作成します。詳しくは、MongoDB コネクタを構成するをご覧ください。

    接続が正常に作成されるはずです。