虛擬私有雲 (VPC) 對等互連可讓您透過私人網路,將變更資料擷取 (CDC) 更新從 MongoDB Atlas 資料庫串流至 Datastream。由於虛擬私有雲對等互連不具遞移性,因此您必須透過 Compute Engine 網路位址轉譯 (NAT) 虛擬機器 (VM) 轉送流量,並設定防火牆規則。您也會建立 MongoDB 使用者、將 NAT VM IP 位址新增至 MongoDB Atlas 許可清單,以及建立連線設定檔。這項設定不提供高可用性。如果 MongoDB 節點未通過連線設定檔測試,您必須在 NAT VM 手動執行開機指令碼。
設定 MongoDB 資料庫使用者
如要搭配使用 Datastream 與 MongoDB Atlas 執行個體,請先建立資料庫使用者並授予存取權限:
- 在 MongoDB Atlas 資訊主頁的「Security」下方,點選「Database access」。
- 點選「New database user」,然後選取使用者的密碼驗證方式。
- 輸入 Datastream 使用者的使用者名稱和密碼。
- 在「Database user privileges」下方,選取「Grant specific user privileges」。
- 在「Specific privileges」下方新增下列角色:
readAnyDatabase
- 點選「Add user」。
設定虛擬私有雲網路
找出 Google Cloud 專案中的虛擬私有雲網路,與 Datastream 和 MongoDB Atlas 建立對等互連。本頁面假設您使用預設虛擬私有雲及其子網路,例如 us-central1 區域中的預設子網路。
- 確認您指定的網路與 Datastream 或 MongoDB Atlas 網路的 IP 位址範圍沒有重疊。
- 建立私人連線設定,將 Datastream 與虛擬私有雲網路對接:
- 請務必從「Private connectivity method」(私人連線方式) 下拉式選單中選取「VPC peering」(虛擬私有雲對接)。
- 選取
default虛擬私有雲網路。 - 提供未使用的 IP 位址範圍 (例如
10.0.0.0/29),供 Datastream 使用。 - 記下輸入防火牆規則的分配 IP 範圍。
- 在 MongoDB Atlas 中設定網路對接連線,將虛擬私有雲網路與 MongoDB Atlas 網路對接。詳情請參閱 MongoDB 說明文件中的「設定網路對等互連連線」:
- 提供您的 Google Cloud 專案 ID 和 MongoDB Atlas 中的預設虛擬私有雲名稱。
- 請記下 MongoDB Atlas 提供的專案 ID 和虛擬私有雲名稱,以便在 Google Cloud 端完成對接。
- 建立連線後,在 MongoDB Atlas 專案中,前往「Network Access」(網路存取權) >「VPC Peering」(虛擬私有雲對接) 分頁,並記下輸出防火牆規則的無類別跨網域路由 (CIDR) 區塊。
建立網路位址轉譯 VM
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
點選「建立執行個體」。
在「Name」(名稱) 欄位輸入 VM 名稱,例如
mongo-nat-gateway。在「Region」(區域) 欄位中,選取預設虛擬私有雲內的區域,例如
us-central1。點選導覽選單中的「Networking」(網路)。
在「Network tags」(網路標記) 欄位中新增標記,例如
mongo-nat-vm。防火牆規則會使用這個標記。在「IP forwarding」(IP 轉送) 下方,勾選「Enable」(啟用) 核取方塊。
在「Network interfaces」(網路介面) 下方,選取預設虛擬私有雲及其子網路,例如
us-central1中的default。在導覽選單中,按一下「Advanced」(進階)。
在「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."
點選「建立」來啟動 VM。
VM 執行後,請記下其內部 IP 位址,例如
10.128.0.2。
建立輸入和輸出防火牆規則
您必須在虛擬私有雲網路中建立兩項防火牆規則。
建立具備下列特性的輸入防火牆規則:
- 名稱:INGRESS_RULE_NAME
- 方向:輸入
- 動作:允許
- 目標參數:
mongo-nat-vm目標標記 - 來源參數:Datastream 私人連線設定使用的 IP 位址範圍,例如
10.0.0.0/29 - 通訊協定:TCP
- 通訊埠:
27017
建立具有下列特性的輸出防火牆規則:
- 名稱:EGRESS_RULE_NAME
- 方向:輸出
- 動作:允許
- 目標參數:
mongo-nat-vm目標標記 - 目的地參數:MongoDB Atlas CIDR 區塊,例如
192.168.240.0/21 - 通訊協定:TCP
- 通訊埠:
27017
在 MongoDB Atlas 設定 IP 許可清單
在 MongoDB Atlas 安全性設定中,允許 NAT VM 的內部 IP 位址:
- 登入 MongoDB Atlas 帳戶。
- 在導覽選單中,依序點選「Security」(安全性) 和「Network access」(網路存取權)。
- 按一下「新增 IP 位址」。
- 在「Access list entry」(存取清單項目) 欄位中,輸入您在虛擬私有雲網路中建立的 NAT VM 執行個體內部 IP 位址。
- 按一下「確認」,等待狀態變更為「有效」。
建立連線設定檔
為資料庫建立 Datastream 連線設定檔。
前往 Google Cloud 控制台的「Connection profiles」(連線設定檔) 頁面。
按一下「建立設定檔」,然後選取「MongoDB」。
在「Hostname」(主機名稱) 欄位中,輸入您在虛擬私有雲網路中建立的 NAT VM 執行個體內部 IP 位址。
在「Port」(通訊埠) 欄位中輸入
27017。輸入資料庫使用者的使用者名稱和密碼。
新增
tls和tls_allow_invalid_hostnames標籤,並將其值設為true。如要進一步瞭解如何設定標籤,請參閱「建立 MongoDB 資料庫的連線設定檔」。選取「私人連線」做為連線方式。
選取您建立的私人連線設定。
按一下「Create」(建立),儲存連線設定檔。
執行測試,確認與資料庫的連線。