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

教程:在虚拟网络中创建具有 App 服务s Web 应用的 Azure Database for PostgreSQL 灵活服务器实例

适用于:Azure Database for PostgreSQL 灵活服务器

本教程介绍如何在虚拟网络中使用 Azure Database for PostgreSQL 灵活服务器创建Azure App 服务 Web 应用。

在本教程中,将了解如何:

  • 在虚拟网络中创建 Azure Database for PostgreSQL 灵活服务器实例
  • 创建 Web 应用
  • 将 Web 应用添加到虚拟网络
  • 从 Web 应用连接 Azure Database for PostgreSQL 灵活服务器

先决条件

  • 如果没有 Azure 订阅,请在开始之前创建一个免费帐户。

  • 在本地安装 Azure CLI 2.0 或更高版本。 若要查看安装的版本,请运行 az --version 命令。

  • 使用 az login 命令登录到你的帐户。 记下与订阅名称相对应的命令输出中的 id 属性。

    az login
    
  • 如果有多个订阅,请选择应计费的资源所在的相应订阅。 使用 az account set 命令选择帐户下的特定订阅 ID。

    az account set --subscription <subscription ID>
    

在新虚拟网络中创建 Azure Database for PostgreSQL 灵活服务器实例

使用以下命令在虚拟网络(VNET)中创建专用 Azure Database for PostgreSQL 灵活服务器实例:

az postgres flexible-server create --resource-group demoresourcegroup --name demoserverpostgres --vnet demoappvnet --location westus2

此命令将执行以下操作,可能需要花几分钟的时间:

  • 创建资源组(如果尚未存在)。
  • 如果未提供服务器名称,则生成服务器名称。
  • 为此虚拟网络中的 Azure Database for PostgreSQL 灵活服务器实例和子网创建新的虚拟网络,以便为 Azure Database for PostgreSQL 灵活服务器实例创建一个新的虚拟网络。
  • 创建服务器的管理员用户名和密码(如果未提供)。
  • 创建一个名为 postgres 的空数据库

下面是示例输出。

Local context is turned on. Its information is saved in working directory /home/jane. You can run `az local-context off` to turn it off.
Command argument values from local context: --resource-group demoresourcegroup, --location: eastus
Checking the existence of the resource group ''...
Creating Resource group 'demoresourcegroup ' ...
Creating new vnet "demoappvnet" in resource group "demoresourcegroup" ...
Creating new subnet "Subnet095447391" in resource group "demoresourcegroup " and delegating it to "Microsoft.DBforPostgreSQL/flexibleServers"...
Creating Azure Database for PostgreSQL flexible server instance 'demoserverpostgres' in group 'demoresourcegroup'...
Your server 'demoserverpostgres' is using sku 'Standard_D2s_v3' (Paid Tier). Please refer to https://aka.ms/postgres-pricing for pricing details
Make a note of your password. If you forget, you have to reset your password with 'az postgres flexible-server update -n demoserverpostgres --resource-group demoresourcegroup -p <new-password>'.
{
  "connectionString": "postgresql://generated-username:generated-password@demoserverpostgres.postgres.database.azure.com/postgres?sslmode=require",
  "host": "demoserverpostgres.postgres.database.azure.com",
  "id": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/demoresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/demoserverpostgres",
  "location": "East US",
  "password": "generated-password",
  "resourceGroup": "demoresourcegroup",
  "skuname": "Standard_D2s_v3",
  "subnetId": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/demoresourcegroup/providers/Microsoft.Network/virtualNetworks/VNET095447391/subnets/Subnet095447391",
  "username": "generated-username",
  "version": "12"
}

创建 Web 应用

在本部分中,将在 App 服务 应用中创建应用主机,将此应用连接到 Azure Database for PostgreSQL 灵活服务器数据库,然后将代码部署到该主机。 在终端中,请确保你位于应用程序代码的存储库根路径。 请注意,基本计划不支持 VNET 集成。 请使用“标准”或“高级”。

使用 az webapp up 命令创建应用服务应用(主机进程)

az webapp up --resource-group demoresourcegroup --location westus2 --plan testappserviceplan --sku P2V2 --name mywebapp

注意

  • 对于 --location 参数,请使用与上一节中的数据库相同的位置。
  • 将 <app-name> 替换为在整个 Azure 中唯一的名称。 <app-name> 允许的字符包括“A-Z”、“0-9”和“-”。 良好的模式是结合使用公司名称和应用标识符。

此命令将执行以下操作,可能需要花几分钟的时间:

  • 创建资源组(如果尚未存在)。 (在此命令中,你将使用之前在其中创建数据库的同一资源组。)
  • 创建应用服务应用(如果不存在)。
  • 为应用启用默认日志记录(如果尚未启用)。
  • 在启用了生成自动化的情况下,使用 ZIP 部署上传存储库。

为 Web 应用创建子网

在启用 VNET 集成之前,需要具备委托给应用服务 Web 应用的子网。 创建子网前,请查看数据库子网地址以避免对 Web 应用子网使用相同的地址前缀。

az network vnet show --resource-group demoresourcegroup -n demoappvnet

运行以下命令,在创建 Azure Database for PostgreSQL 灵活服务器实例的同一虚拟网络中创建新子网。 更新地址前缀以避免与 Azure Database for PostgreSQL 灵活服务器子网冲突。

az network vnet subnet create --resource-group demoresourcegroup --vnet-name demoappvnet --name webappsubnet  --address-prefixes 10.0.1.0/24  --delegations Microsoft.Web/serverFarms

将 Web 应用添加到虚拟网络

使用 az webapp vnet-integration 命令向 webapp 添加区域虚拟网络集成。

az webapp vnet-integration add --resource-group demoresourcegroup -n  mywebapp --vnet demoappvnet --subnet webappsubnet

配置环境变量以连接数据库

现在将代码部署到App 服务,下一步是将应用连接到 Azure 中的 Azure Database for PostgreSQL 灵活服务器实例。 应用代码需要在多个环境变量中查找数据库信息。 若要在应用服务中设置环境变量,需要使用 az webapp config appsettings set 命令。

  
az webapp config appsettings set  --name mywebapp --settings DBHOST="<postgres-server-name>.postgres.database.azure.com" DBNAME="postgres" DBUSER="<username>" DBPASS="<password>" 
  • 替换 新创建的 Azure Database for PostgreSQL 灵活服务器实例命令的 postgres-server-nameusernamepassword
  • 将 <username> 和 <password> 替换为命令也为你生成的凭据。
  • 资源组和应用名称是从“.azure/config”文件中的缓存值中提取的。
  • 此命令会创建名为 DBHOST、DBNAME、DBUSER* 和 DBPASS 的设置。 如果应用程序代码对数据库信息使用了不同的名称,则如代码中所述,对应用设置使用这些名称。

将 Web 应用配置为允许来自虚拟网络中的所有出站连接。

az webapp config set --name mywebapp --resource-group demoresourcegroup --generic-configurations '{"vnetRouteAllEnabled": true}'

清理资源

使用以下命令清除在此教程中创建的所有资源。 此命令将删除这一资源组中的所有资源。

az group delete -n demoresourcegroup

后续步骤