使用 Azure CLI 为 Azure Database for PostgreSQL 创建和管理 VNet 服务终结点 - 单一服务器Create and manage VNet service endpoints for Azure Database for PostgreSQL - Single Server using Azure CLI

虚拟网络 (VNet) 服务终结点和规则将虚拟网络的专用地址空间扩展到你的 Azure Database for PostgreSQL 服务器。Virtual Network (VNet) services endpoints and rules extend the private address space of a Virtual Network to your Azure Database for PostgreSQL server. 使用便捷的 Azure 命令行接口 (CLI) 命令,可创建、更新、删除、列出和显示 VNet 服务终结点和规则,用于管理服务器。Using convenient Azure Command Line Interface (CLI) commands, you can create, update, delete, list, and show VNet service endpoints and rules to manage your server. 有关 Azure Database for PostgreSQL VNet 服务终结点(包括限制)的概述,请参阅 Azure Database for PostgreSQL Server VNet 服务终结点For an overview of Azure Database for PostgreSQL VNet service endpoints, including limitations, see Azure Database for PostgreSQL Server VNet service endpoints. 在 Azure Database for PostgreSQL 的所有支持区域中,VNet 服务终结点均可用。VNet service endpoints are available in all supported regions for Azure Database for PostgreSQL.

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅If you don't have an Azure trail subscription, create a trial subscription before you begin.

先决条件Prerequisites

若要逐步执行本操作指南:To step through this how-to guide:

  • 安装 Azure CLIInstall the Azure CLI.

  • 创建 Azure Database for PostgreSQL 服务器和数据库Create an Azure Database for PostgreSQL server and database.

    备注

    只有常规用途和内存优化服务器才支持 VNet 服务终结点。Support for VNet service endpoints is only for General Purpose and Memory Optimized servers. 在 VNet 对等互连的情况下,如果流量通过具有服务终结点的公共 VPN 网关流动,并且应该流向对等机,请创建 ACL/VNet 规则,以便网关 VNet 中的 Azure 虚拟机能够访问 Azure Database for PostgreSQL 服务器。In case of VNet peering, if traffic is flowing through a common VPN Gateway with service endpoints and is supposed to flow to the peer, please create an ACL/VNet rule to allow Azure Virtual Machines in the Gateway VNet to access the Azure Database for PostgreSQL server.

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.
    • 如果使用的是本地安装,请通过 Azure CLI 使用 az login 命令登录。If you're using a local install, sign in with Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅使用 Azure CLI 登录See Sign in with Azure CLI for additional sign-in options.
    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with Azure CLI.
    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.
  • 本文需要 Azure CLI 版本 2.0 或更高版本。This article requires version 2.0 or later of the Azure CLI.

配置 Azure Database for PostgreSQL Vnet 服务终结点Configure Vnet service endpoints for Azure Database for PostgreSQL

az network vnet 命令用于配置虚拟网络。The az network vnet commands are used to configure Virtual Networks.

如果有多个订阅,请选择应计费的资源所在的相应订阅。If you have multiple subscriptions, choose the appropriate subscription in which the resource should be billed. 使用 az account set 命令选择帐户下的特定订阅 ID。Select the specific subscription ID under your account using az account set command. 用订阅的 az login 输出中的 id 属性代替订阅 id 占位符。Substitute the id property from the az login output for your subscription into the subscription id placeholder.

  • 该帐户必须拥有创建虚拟网络和服务终结点所需的必要权限。The account must have the necessary permissions to create a virtual network and service endpoint.

对虚拟网络拥有写入访问权限的用户可在虚拟网络上单独配置服务终结点。Service endpoints can be configured on virtual networks independently, by a user with write access to the virtual network.

若要在 VNet 中保护 Azure 服务资源,用户必须对所添加的子网拥有“Microsoft.Network/virtualNetworks/subnets/joinViaServiceEndpoint/”权限。To secure Azure service resources to a VNet, the user must have permission to "Microsoft.Network/virtualNetworks/subnets/joinViaServiceEndpoint/" for the subnets being added. 此权限默认包含在内置的服务管理员角色中,可以通过创建自定义角色进行修改。This permission is included in the built-in service administrator roles, by default and can be modified by creating custom roles.

详细了解内置角色以及将特定的权限分配到自定义角色Learn more about built-in roles and assigning specific permissions to custom roles.

VNet 和 Azure 服务资源可以位于相同或不同的订阅中。VNets and Azure service resources can be in the same or different subscriptions. 如果 VNet 和 Azure 服务资源位于不同的订阅中,资源应在相同的 Active Directory (AD) 租户下。If the VNet and Azure service resources are in different subscriptions, the resources should be under the same Active Directory (AD) tenant. 确保两个订阅都注册了 Microsoft.Sql 资源提供程序。Ensure that both the subscriptions have the Microsoft.Sql resource provider registered. 有关详细信息,请参阅资源管理器注册For more information, see resource-manager-registration.

重要

强烈建议在运行下面的示例脚本或配置服务终结点前先阅读本文有关服务终结点配置和注意事项的内容。It is highly recommended to read this article about service endpoint configurations and considerations before running the sample script below, or configuring service endpoints. 虚拟网络服务终结点: 虚拟网络服务终结点是一个子网,其属性值包括一个或多个正式的 Azure 服务类型名称。Virtual Network service endpoint: A Virtual Network service endpoint is a subnet whose property values include one or more formal Azure service type names. VNet 服务终结点使用服务类型名称 Microsoft.Sql,可引用名为“SQL 数据库”的 Azure 服务。VNet services endpoints use the service type name Microsoft.Sql, which refers to the Azure service named SQL Database. 此服务标记也适用于 Azure SQL 数据库、Azure Database for PostgreSQL 和 MySQL 服务。This service tag also applies to the Azure SQL Database, Azure Database for PostgreSQL and MySQL services. 请务必要注意,对 VNet 服务终结点应用 Microsoft.Sql 服务标记时,它会为所有 Azure 数据库服务配置服务终结点流量,其中包括 Azure SQL 数据库、Azure Database for PostgreSQL 和子网上的 Azure Database for MySQL 服务器。It is important to note when applying the Microsoft.Sql service tag to a VNet service endpoint it configures service endpoint traffic for all Azure Database services, including Azure SQL Database, Azure Database for PostgreSQL and Azure Database for MySQL servers on the subnet.

示例脚本演示了如何创建 Azure Database for PostgreSQL 数据库,创建 VNet 和 VNet 服务终结点,以及如何使用 VNet 规则在子网中保护服务器Sample script to create an Azure Database for PostgreSQL database, create a VNet, VNet service endpoint and secure the server to the subnet with a VNet rule

在此示例脚本中,更改突出显示的行,以自定义管理员用户名和密码。In this sample script, change the highlighted lines to customize the admin username and password. az account set --subscription 命令中使用的 SubscriptionID 替换为你自己的订阅标识符。Replace the SubscriptionID used in the az account set --subscription command with your own subscription identifier.

#!/bin/bash

# To find the name of an Azure region in the CLI run this command: az account list-locations
# Substitute  <subscription id> with your identifier
az account set --subscription <subscription id>

# Create a resource group
az group create \
--name myresourcegroup \
--location chinaeast

# Create a PostgreSQL server in the resource group
# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
# Substitute the <server_admin_password> with your own value.
az postgres server create \
--name mydemoserver \
--resource-group myresourcegroup \
--location chinaeast \
--admin-user mylogin \
--admin-password <server_admin_password> \
--sku-name GP_Gen4_2

# Get available service endpoints for Azure region output is JSON
# Use the command below to get the list of services supported for endpoints, for an Azure region, say "chinaeast".
az network vnet list-endpoint-services \
-l chinaeast

# Add Azure SQL service endpoint to a subnet *mySubnet* while creating the virtual network *myVNet* output is JSON
az network vnet create \
-g myresourcegroup \
-n myVNet \
--address-prefixes 10.0.0.0/16 \
-l chinaeast

# Creates the service endpoint
az network vnet subnet create \
-g myresourcegroup \
-n mySubnet \
--vnet-name myVNet \
--address-prefix 10.0.1.0/24 \
--service-endpoints Microsoft.SQL

# View service endpoints configured on a subnet
az network vnet subnet show \
-g myresourcegroup \
-n mySubnet \
--vnet-name myVNet

# Create a VNet rule on the sever to secure it to the subnet. Note: resource group (-g) parameter is where the database exists. VNet resource group if different should be specified using subnet id (URI) instead of subnet, VNet pair.
az postgres server vnet-rule create \
-n myRule \
-g myresourcegroup \
-s mydemoserver \
--vnet-name myVNet \
--subnet mySubnet

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组以及与其关联的所有资源。After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.

#!/bin/bash
az group delete --name myresourcegroup