使用 VPC 对等互连配置 MongoDB Atlas

本页面介绍了如何配置 MongoDB Atlas 源数据库,以便通过虚拟私有云对等互连与 Datastream 搭配使用。请注意,上述过程并非高可用性解决方案。如果 MongoDB 节点未通过连接配置文件测试,您需要在网络地址转换 (NAT) 虚拟机 (VM) 上手动运行本页后面部分中引用的启动脚本。

由于虚拟私有云对等互连不具有传递性,因此您必须使用 Compute Engine 虚拟机配置 NAT 网关,以将流量从 Datastream 路由到 MongoDB Atlas。

配置 MongoDB 数据库用户

如需将 Datastream 与 MongoDB Atlas 实例搭配使用,首先需要创建数据库用户并向其授予访问权限:

  1. MongoDB Atlas 控制台中,在安全下,点击数据库访问权限
  2. 点击新建数据库用户,然后为您的用户选择密码身份验证方法。
  3. 输入 Datastream 用户的用户名和密码。
  4. 数据库用户权限下,选择授予特定用户权限
  5. 特定权限下,添加以下角色:
    • readAnyDatabase
  6. 点击添加用户

设置虚拟私有云网络

确定 Google Cloud 项目中要与 Datastream 和 MongoDB Atlas 建立对等互连的虚拟私有云网络。此页面假设您使用默认 VPC 及其子网,例如 us-central1 区域中的默认子网。

  1. 确保您确定的网络与 Datastream 或 MongoDB Atlas 网络没有重叠的 IP 地址范围。
  2. 创建专用连接配置,以便将 Datastream 与您的 Virtual Private Cloud 网络对等互连:
    1. 请务必从专用连接方法下拉菜单中选择 VPC 对等互连
    2. 选择您的 default VPC 网络。
    3. 提供未使用的 IP 地址范围(例如 10.0.0.0/29),供 Datastream 使用。
    4. 记下入站流量防火墙规则的分配 IP 范围。
  3. 在 MongoDB Atlas 中设置网络对等互连连接,以将您的虚拟私有云网络与您的 MongoDB Atlas 网络对等互连。如需了解详情,请参阅 MongoDB 文档中的设置网络对等互连连接
    1. 在 MongoDB Atlas 中提供您的 Google Cloud 项目 ID 和默认 VPC 名称。
    2. 记下 MongoDB Atlas 为您提供的项目 ID 和 VPC 名称,以便在 Google Cloud 端完成对等互连。
  4. 创建连接后,在 MongoDB Atlas 项目中,前往网络访问 > VPC 对等互连标签页,并记下出站防火墙规则的无类别域间路由 (CIDR) 块。

创建 NAT 虚拟机

  1. 在 Google Cloud 控制台中,前往虚拟机实例页面。

    转到虚拟机实例

  2. 点击创建实例

  3. 名称字段中,输入虚拟机的名称,例如 mongo-nat-gateway

  4. 区域字段中,选择默认 VPC 中的一个区域,例如 us-central1

  5. 在导航菜单中,点击网络

  6. 网络标记字段中,添加一个标记,例如 mongo-nat-vm。此标记由防火墙规则使用。

  7. IP 转发下,选中启用复选框。

  8. 网络接口下,选择您的默认 VPC 及其子网,例如 us-central1 中的 default

  9. 在导航菜单中,点击高级

  10. 自动化部分中,将以下脚本粘贴到启动脚本字段中。将 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. 点击创建以启动虚拟机。

  12. 虚拟机运行后,记下其内部 IP 地址,例如 10.128.0.2

创建入站和出站防火墙规则

您必须在 VPC 网络中创建两条防火墙规则。

  1. 创建具有以下特征的入站防火墙规则

    • 名称INGRESS_RULE_NAME
    • 方向:入站
    • 操作:允许
    • 目标参数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 虚拟机的内部 IP 地址:

  1. 登录您的 MongoDB Atlas 账号
  2. 在导航菜单中,点击安全,然后点击网络访问权限
  3. 点击添加 IP 地址
  4. 访问列表条目字段中,输入您在 VPC 网络中创建的 NAT 虚拟机实例的内部 IP 地址。
  5. 点击确认,然后等待状态变为有效

创建连接配置文件

为数据库创建 Datastream 连接配置文件。

  1. 前往 Google Cloud 控制台中的连接配置文件页面。

    转到“连接配置文件”页面

  2. 点击 Create profile(创建配置文件),然后选择 MongoDB

  3. 主机名字段中,输入您在 VPC 网络中创建的 NAT 虚拟机实例的内部 IP 地址。

  4. 端口字段中,输入 27017

  5. 输入数据库用户的用户名和密码。

  6. 添加 tlstls_allow_invalid_hostnames 标签,并将其值设置为 true。如需详细了解如何设置标签,请参阅为 MongoDB 数据库创建连接配置文件

  7. 选择专用连接作为连接方法。

  8. 选择您创建的专用连接配置。

  9. 点击创建以保存连接配置文件。

  10. 运行测试以验证与数据库的连接。