教學課程:在虛擬網路中使用App Services Web 應用程式建立 適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器實例

適用於:適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器

本教學課程說明如何在虛擬網路建立具有 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器的 Azure App 服務 Web 應用程式。

在本教學課程中,您將了解如何:

  • 在虛擬網路中建立 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例
  • 建立 Web 應用程式
  • 將 Web 應用程式新增至虛擬網路
  • 從 Web 應用程式 連線 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

  • 在本機安裝 Azure CLI.2.0 版或更新版本。 若要查看已安裝的版本,請執行 az --version 命令。

  • 使用 az login 命令登入您的帳戶。 請記下對應訂用帳戶名稱之命令輸出中的 id 屬性。

    az login
    
  • 如果您有多個訂用帳戶,請選擇應該計費資源的適當訂用帳戶。 使用 az account set 命令,選取您的帳戶下的特定訂用帳戶標識碼。

    az account set --subscription <subscription ID>
    

在新虛擬網路中建立 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例

使用下列命令,在虛擬網路 (VNET) 內建立私人 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例:

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

此命令會執行下列動作,這可能需要幾分鐘的時間:

  • 如果資源群組不存在,請建立資源群組。
  • 如果未提供伺服器名稱,則會產生伺服器名稱。
  • 為 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例在此虛擬網路內的新 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例和子網建立新的虛擬網路。
  • 如果未提供,請為您的伺服器建立系統管理員使用者名稱、密碼。
  • 建立名為 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 Service 應用程式中建立應用程式主機,將此應用程式連線到 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器資料庫,然後將程式代碼部署至該主機。 請確定您位於終端機中應用程式程式代碼的存放庫根目錄。 注意 基本方案不支援 VNET 整合。 請使用標準或 進階版。

使用 az webapp up 命令建立 App Service 應用程式 (主機行程)

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

注意

  • 針對 --location 自變數,請使用與上一節中資料庫相同的位置。
  • 將所有 Azure 中唯一的名稱取代 <app-name> 。 應用程式名稱>允許的<字元為 A-Z、0-9 和 -。 良好的模式是使用公司名稱和應用程式識別碼的組合。

此命令會執行下列動作,這可能需要幾分鐘的時間:

  • 如果資源群組不存在,請建立資源群組。 (在此命令中,您會使用您稍早建立資料庫的相同資源群組。
  • 如果 App Service 應用程式不存在,請建立它。
  • 如果尚未啟用,請啟用應用程式的默認記錄。
  • 使用已啟用組建自動化的 ZIP 部署上傳存放庫。

建立 Web 應用程式的子網

啟用 VNET 整合之前,您必須有委派給 App Service Web 應用程式的子網。 建立子網之前,請先檢視資料庫子網位址,以避免針對 Web 應用程式子網使用相同的地址前綴。

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

執行下列命令,在與建立 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例相同的虛擬網路中建立新的子網。 更新位址前置詞,以避免與 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器子網發生衝突。

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 Service,下一個步驟是將應用程式連線到 Azure 中的 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例。 應用程式程式代碼預期會在許多環境變數中尋找資料庫資訊。 若要在 App Service 中設定環境變數,請使用 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>" 
  • 取代新建立 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例命令的 postgres-server-nameusernamepassword
  • 將使用者>名稱和<密碼>取代<為命令也為您產生的認證。
  • 資源群組和應用程式名稱是從 .azure/config 檔案中的快取值所繪製。
  • 此命令會建立名為 DBHOSTDBNAMEDBUSER*和 DBPASS設定。 如果您的應用程式程式代碼針對資料庫資訊使用不同的名稱,請使用這些名稱作為應用程式設定,如程式代碼中所述。

設定 Web 應用程式以允許虛擬網路內的所有輸出連線。

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

清除資源

使用下列命令清除您在教學課程中建立的所有資源。 此命令會刪除此資源群組中的所有資源。

az group delete -n demoresourcegroup

下一步