你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

在 Azure DocumentDB 中启用专用访问

Azure 专用链接是一项功能强大的服务,允许用户通过指定的专用终结点连接到 Azure DocumentDB。 此专用终结点由位于自己的虚拟网络中的子网中的专用 IP 地址组成。 终结点使您可以仅通过专用 IP 限制访问 Azure DocumentDB 产品。 通过将专用链接与严格的 NSG 策略集成,数据外泄的风险会大幅减少。 若要更深入地了解专用终结点,请考虑查看什么是 Azure 专用链接?

注释

专用链接保护您的连接,但不会阻止 Azure DocumentDB 终结点通过公共 DNS 解析。 传入请求的过滤在应用程序级别处理,而不是在传输或网络级别进行处理。

专用链接提供从虚拟网络内部或任何连接的对等虚拟网络访问 Azure DocumentDB 的灵活性。 此外,通过专用对等互连、VPN 或 Azure ExpressRoute,可以本地访问与“Private Link”相关联的资源。

若要建立连接,具有专用链接的 Azure DocumentDB 支持自动和手动审批方法。

先决条件

  • 现有的 Azure DocumentDB 群集

使用 Azure 门户创建具有专用终结点的群集

按照以下步骤使用 Azure 门户创建具有专用终结点的新 Azure DocumentDB 群集:

  1. 登录到 Azure 门户,然后选择“在 Azure 门户的左上角 创建资源 ”。

  2. 在“ 创建资源 ”页上,选择“ 数据库 ”,然后选择 “Azure DocumentDB”。

  3. 在“选择 API”选项页上的 “MongoDB ”磁贴上,选择“创建”。

  4. 选择 群集 资源类型。

  5. 在“ 创建 Azure DocumentDB 群集”页上,选择或创建 资源组,输入 群集名称和 位置,然后输入并确认管理员密码。

  6. 选择“下一步: 网络”。

  7. 对于“连接”方法,选择“ 网络 ”选项卡,选择 “专用访问”。

  8. 在“创建专用终结点”屏幕上,为以下项输入或选择适当的值:

    设置 价值
    资源组 选择资源组。
    Name 输入专用终结点的任何名称。 如果采用此名称,请创建唯一的名称。
    网络接口名称 输入网络接口的任何名称。 如果采用此名称,请创建唯一的名称。
    位置 选择要在其中部署专用链接的区域。 在虚拟网络所在的同一位置创建专用终结点。
    资源类型 选择 Microsoft.DocumentDB/mongoClusters
    目标资源 选择创建的 Azure DocumentDB 资源。
    目标子资源 选择之前选定的且你的专用终结点可访问的资源的子资源类型。
    虚拟网络 选择虚拟网络。
    子网 选择子网。
    与专用 DNS 区域集成 选择 “是”。 若要与专用终结点建立专用连接,则需要 DNS 记录。 建议将专用终结点与专用 DNS 区域集成。 还可以使用自己的 DNS 服务器,或使用虚拟机上的主机文件创建 DNS 记录。 为此选项选择“是”时,还会创建专用 DNS 区域组。 DNS 区域组是专用 DNS 区域与专用终结点之间的链接。 当专用终结点更新时,此链接可帮助你自动更新专用 DNS 区域。 例如,添加或删除区域时,专用 DNS 区域会自动更新。
    配置名称 选择订阅和资源组。 专用 DNS 区域会自动确定。 无法使用 Azure 门户更改它。
  9. 选择“确定”。

  10. 选择 “下一步:标记>审阅 + 创建”。 在 “审阅 + 创建 ”页上,选择“ 创建”。

在现有群集上启用专用访问

若要为现有群集中的节点创建专用终结点,请打开该群集的“网络”页

  1. 选择“添加专用终结点”

    在“网络”屏幕上选择“添加专用终结点”的屏幕截图。

  2. 在“创建专用终结点”屏幕的“基本信息”选项卡上,确认订阅、资源组和区域。 输入终结点的名称(例如 my-cluster-1),并输入网络接口名(例如 my-cluster-1-nic)

    注释

    除非你有合理的原因需要选择其他设置,否则我们建议选择与你的群集相匹配的订阅和区域。 表单域的默认值可能不正确。 进行检查,必要时更新。

  3. 选择下一步:资源。 为 资源类型选择“Microsoft.DocumentDB/mongoClusters”,然后选择 资源的目标群集。 对于 目标子资源,请选择“MongoCluster”。

  4. 选择“下一步: 虚拟网络”。 选择所需的“虚拟网络”和“子网”。 在“专用 IP 配置”下选择“静态分配 IP 地址”,或者保留默认选项(即“动态分配 IP 地址”)

  5. 选择“下一步: DNS”。

  6. 在“专用 DNS 集成”下,对于“与专用 DNS 区域集成”,保留默认选项“是”或选择“否”

注释

需要在群集上启用专用 DNS 集成才能进行适当的 DNS 解析。

  1. 选择“下一步: 标记”,并添加任何所需标记

  2. 选择“查看 + 创建”。 查看设置,如果对设置感到满意,请选择“创建”

使用 Azure CLI 创建专用终结点

运行以下 Azure CLI 脚本,为现有 Azure DocumentDB 帐户创建名为 myPrivateEndpoint 的专用终结点。 将变量值替换为环境的详细信息。

# Resource group where the Azure DocumentDB account and virtual network resources are located  
ResourceGroupName="myResourceGroup" 

# Name of the existing Azure DocumentDB account  
ClusterName="myMongoCluster" 

# Subscription ID where the Azure DocumentDB account and virtual network resources are located  
SubscriptionId="<your Azure subscription ID>"  

# API type of your Azure DocumentDB account: Sql, SqlDedicated, MongoCluster, Cassandra, Gremlin, or Table 
SubResourceType="MongoCluster"  

# Name of the virtual network to create  
VNetName="myVnet"  

# Name of the subnet to create  
SubnetName="mySubnet"  

# Name of the private endpoint to create  
PrivateEndpointName="myPrivateEndpoint"  

# Name of the private endpoint connection to create 
PrivateConnectionName="myConnection" 

az network vnet create \
  --name $VNetName \
  --resource-group $ResourceGroupName \
  --subnet-name $SubnetName 

az network vnet subnet update \
  --name <name> \
  --resource-group $ResourceGroupName \
  --vnet-name $VNetName \
  --disable-private-endpoint-network-policies true 

az network private-endpoint create \
  --name $PrivateEndpointName \
  --resource-group $ResourceGroupName \
  --vnet-name $VNetName \
  --subnet $SubnetName \
  --private-connection-resource-id "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.DocumentDB/mongoClusters/$ClusterName" \
  --group-ids MongoCluster --connection-name $PrivateConnectionName 

将专用终结点与专用 DNS 区域集成

创建专用终结点后,应使用以下 Azure CLI 脚本将其与专用 DNS 区域集成:

注释

需要在群集上启用专用 DNS 集成才能进行适当的 DNS 解析。

#Zone name differs based on the API type and group ID you are using. 
zoneName="privatelink.mongocluster.cosmos.azure.com" 

az network private-dns zone create \
  --resource-group $ResourceGroupName \
  --name $zoneName 

az network private-dns link vnet create --resource-group $ResourceGroupName \
  --zone-name $zoneName \
  --name <dns-link-name> \
  --virtual-network $VNetName \
  --registration-enabled false 

#Create a DNS zone group
az network private-endpoint dns-zone-group create \
  --resource-group $ResourceGroupName \
  --endpoint-name <pe-name> \
  --name <zone-group-name> \
  --private-dns-zone $zoneName \
  --zone-name mongocluster 

获取用于连接的连接字符串

始终使用 mongodb+srv 连接字符串从具有有权访问分配给 Azure DocumentDB 群集的专用终结点的专用 IP 地址的客户端连接到数据库。

  1. 选择现有的 Azure DocumentDB 群集。
  2. 在群集边栏的“设置”下选择“连接字符串”
  3. 复制用于本机 DocumentDB 身份验证或 Microsoft Entra ID 身份验证的全局读写或 self 连接字符串。

注释

在副本群集上,仅提供自连接字符串。

  1. 在应用程序或管理工具中使用复制的连接字符串连接到群集。
az network private-link-resource list \
  -g <rg-name> \
  -n <resource-name> \
  --type Microsoft.DocumentDB/mongoClusters