使用虛擬私有雲對等互連設定 MongoDB Atlas

本頁面說明如何設定 MongoDB Atlas 來源資料庫,以便透過虛擬私有雲對等互連與 Datastream 搭配使用。請注意,上述程序並非高可用性解決方案。如果 MongoDB 節點未通過連線設定檔測試,您需要在網路位址轉譯 (NAT) 虛擬機器 (VM) 上,手動執行本頁稍後提及的開機指令碼。

由於虛擬私有雲對等互連不具遞移性,您必須使用 Compute Engine VM 設定 NAT 閘道,才能將流量從 Datastream 轉送至 MongoDB Atlas。

設定 MongoDB 資料庫使用者

如要搭配使用 Datastream 與 MongoDB Atlas 執行個體,請先建立資料庫使用者並授予存取權:

  1. MongoDB Atlas 資訊主頁的「Security」下方,點選「Database access」
  2. 點選「New database user」,然後選取使用者的密碼驗證方式。
  3. 輸入 Datastream 使用者的使用者名稱和密碼。
  4. 在「Database user privileges」下方,選取「Grant specific user privileges」
  5. 在「Specific privileges」下方新增下列角色:
    • readAnyDatabase
  6. 點選「Add user」

設定虛擬私有雲網路

找出專案中的虛擬私有雲網路,與 Datastream 和 MongoDB Atlas 對等互連。 Google Cloud 本頁假設您使用預設 VPC 及其子網路,例如 us-central1 區域中的預設子網路。

  1. 確認您識別的網路與 Datastream 或 MongoDB Atlas 網路沒有重疊的 IP 位址範圍。
  2. 建立私人連線設定,將 Datastream 與虛擬私有雲網路對等互連:
    1. 請務必從「私人連線方式」下拉式選單中選取「虛擬私有雲對等互連」
    2. 選取 default 虛擬私有雲網路。
    3. 提供未使用的 IP 位址範圍,例如 10.0.0.0/29,供 Datastream 使用。
    4. 記下輸入防火牆規則的已分配 IP 範圍。
  3. 在 MongoDB Atlas 中設定網路對等互連連線,將虛擬私有雲網路與 MongoDB Atlas 網路對等互連。詳情請參閱 MongoDB 說明文件中的「設定網路對等互連連線」:
    1. 在 MongoDB Atlas 中提供 Google Cloud 專案 ID 和預設虛擬私有雲名稱。
    2. 請記下 MongoDB Atlas 提供的專案 ID 和 VPC 名稱,以便在 Google Cloud 端完成對等互連。
  4. 建立連線後,請前往 MongoDB Atlas 專案的「Network Access」 >「VPC Peering」分頁,並記下輸出防火牆規則的無類別跨網域路由 (CIDR) 區塊。

建立 NAT VM

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 點選「建立執行個體」

  3. 在「Name」(名稱) 欄位,輸入 VM 的名稱,例如 mongo-nat-gateway

  4. 在「Region」(區域) 欄位中,選取預設虛擬私有雲內的區域,例如 us-central1

  5. 在導覽選單中,按一下「Networking」(網路)

  6. 在「Network tags」(網路標記) 欄位中新增標記,例如 mongo-nat-vm。防火牆規則會使用這個標記。

  7. 在「IP forwarding」(IP 轉送) 下方,勾選「Enable」(啟用) 核取方塊。

  8. 在「網路介面」下方,選取預設 VPC 及其子網路,例如 default 中的 us-central1

  9. 在導覽選單中,按一下「進階」

  10. 在「Automation」(自動化) 區段中,將下列指令碼貼到「Startup script」(開機指令碼) 欄位。將 PRIVATE_SRV_RECORD 替換為 MongoDB Atlas 叢集的私有 SRV 記錄,例如 my-cluster-pri.abcde.mongodb.net

    #!/bin/bash
    
    # --- Startup script for MongoDB Atlas NAT gateway ---
    # This script has two main functions:
    # 1. Resolves the IP address of a MongoDB Atlas node from its SRV record.
    # 2. Configures iptables to forward traffic to the resolved MongoDB IP.
    # --------------------------------------------------------------------------
    
    # --- Part 1: Resolve MongoDB node IP address ---
    
    #
    # EDIT THIS LINE to match your Mongo Atlas private SRV record
    #
    export SRV_RECORD=PRIVATE_SRV_RECORD
    
    echo "Resolving SRV record for: $SRV_RECORD"
    
    # Function to install DNS utilities if needed
    install_dns_tools() {
        echo "'dig' not found. Attempting to install DNS utilities..."
        if command -v apt-get &> /dev/null; then
            # Wait for any existing apt processes to finish before trying to install packages.
            # This is common on VM startup where the system might be running automatic updates.
            echo "Checking for and waiting on existing apt-get processes..."
            while fuser /var/lib/apt/lists/lock /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do
                echo "Another apt process is running. Waiting 10 seconds..."
                sleep 10
            done
            echo "Apt lock is free. Proceeding with installation."
    
            apt-get update && apt-get install -y dnsutils
        else
            echo "Error: Could not find apt-get to install DNS utilities."
            exit 1
        fi
    
        # Verify that dig is now available
        if ! command -v dig &> /dev/null; then
            echo "Error: Failed to install DNS utilities."
            exit 1
        fi
    }
    
    # Check if 'dig' is installed. If not, install it.
    if ! command -v dig &> /dev/null; then
        install_dns_tools
    fi
    
    echo "Using 'dig' for DNS resolution."
    # The `+short` option provides a concise output.
    # We use `awk` to grab the 4th column (the hostname) and `head` to get the first one.
    NODE_HOSTNAME=$(dig +short SRV "_mongodb._tcp.${SRV_RECORD}" | awk '{print $4}' | head -n 1)
    if [ -n "$NODE_HOSTNAME" ]; then
        NODE_IP=$(dig +short A "$NODE_HOSTNAME")
    fi
    
    # Check if the SRV lookup was successful and we got a hostname.
    if [ -z "$NODE_HOSTNAME" ]; then
        echo "Error: Could not resolve SRV record. Check the hostname and your
        network connection."
        exit 1
    fi
    
    echo "Found node hostname: $NODE_HOSTNAME"
    
    # Check if the A record lookup was successful.
    if [ -z "$NODE_IP" ]; then
        echo "Error: Could not resolve the IP address for node: $NODE_HOSTNAME"
        exit 1
    fi
    
    # 3. Print the final result of the lookup.
    echo "Successfully resolved IP address of a node: $NODE_IP"
    
    # --- Part 2: Configure iptables for NAT ---
    
    # Substitute the resolved IP address into the DB_ADDR variable.
    export DB_ADDR=$NODE_IP
    export DB_PORT=27017
    
    # Enable the VM to receive packets whose destinations do
    # not match any running process local to the VM
    echo 1 > /proc/sys/net/ipv4/ip_forward
    
    # Ask the Metadata server for the IP address of the VM nic0
    # network interface:
    md_url_prefix="http://169.254.169.254/computeMetadata/v1/instance"
    vm_nic_ip="$(curl -H "Metadata-Flavor: Google" ${md_url_prefix}/network-interfaces/0/ip)"
    
    # Clear any existing iptables NAT table entries (all chains):
    iptables -t nat -F
    
    # Create a NAT table entry in the prerouting chain, matching
    # any packets with destination database port, changing the destination
    # IP address of the packet to the MongoDB instance IP address:
    iptables -t nat -A PREROUTING \
         -p tcp --dport $DB_PORT \
         -j DNAT \
         --to-destination $DB_ADDR
    
    # Create a NAT table entry in the postrouting chain, matching
    # any packets with destination database port, changing the source IP
    # address of the packet to the NAT VM's primary internal IPv4 address:
    iptables -t nat -A POSTROUTING \
         -p tcp --dport $DB_PORT \
         -j SNAT \
         --to-source $vm_nic_ip
    
    # Save iptables configuration:
    iptables-save
    
    echo "Startup script completed successfully. iptables rules are configured and saved."
  11. 按一下「建立」即可啟動 VM。

  12. VM 執行後,請記下其內部 IP 位址,例如 10.128.0.2

建立輸入和輸出防火牆規則

您必須在虛擬私有雲網路中建立兩項防火牆規則。

  1. 建立具有下列特性的輸入防火牆規則

    • 名稱:INGRESS_RULE_NAME
    • 方向:ingress
    • 動作:允許
    • 目標參數:mongo-nat-vm 目標標記
    • 來源參數:Datastream 私人連線設定使用的 IP 位址範圍,例如 10.0.0.0/29
    • 通訊協定:TCP
    • 通訊埠: 27017
  2. 建立具有下列特性的輸出防火牆規則

    • 名稱:EGRESS_RULE_NAME
    • 方向:輸出
    • 動作:允許
    • 目標參數:mongo-nat-vm 目標標記
    • 目的地參數:MongoDB Atlas CIDR 區塊,例如 192.168.240.0/21
    • 通訊協定:TCP
    • 通訊埠: 27017

在 MongoDB Atlas 中設定 IP 許可清單

在 MongoDB Atlas 安全性設定中,允許 NAT VM 的內部 IP 位址:

  1. 登入 MongoDB Atlas 帳戶
  2. 依序點選導覽選單中的「安全性」和「網路存取」
  3. 按一下「新增 IP 位址」
  4. 在「Access list entry」(存取清單項目) 欄位中,輸入您在虛擬私有雲網路中建立的 NAT VM 執行個體內部 IP 位址。
  5. 按一下「確認」,等待狀態變更為「有效」

建立連線設定檔

為資料庫建立 Datastream 連線設定檔。

  1. 前往 Google Cloud 控制台的「連線設定檔」頁面。

    前往「連線設定檔」頁面

  2. 按一下「建立設定檔」,然後選取「MongoDB」

  3. 在「Hostname」(主機名稱) 欄位中,輸入您在虛擬私有雲網路中建立的 NAT VM 執行個體內部 IP 位址。

  4. 在「Port」(通訊埠) 欄位中輸入 27017

  5. 輸入資料庫使用者的使用者名稱和密碼。

  6. 新增 tlstls_allow_invalid_hostnames 標籤,並將值設為 true。如要進一步瞭解如何設定標籤,請參閱「為 MongoDB 資料庫建立連線設定檔」。

  7. 選取「私人連線」做為連線方法。

  8. 選取您建立的私人連線設定。

  9. 按一下「建立」即可儲存連線設定檔。

  10. 執行測試,確認與資料庫的連線。