使用 Azure CLI 來建立及管理適用於 PostgreSQL 其 Azure 資料庫 - 單一伺服器的 VNet 服務端點和規則

適用於:適用於 PostgreSQL 的 Azure 資料庫 - 單一伺服器

重要

適用於 PostgreSQL 的 Azure 資料庫 - 單一伺服器位於淘汰路徑上。 強烈建議您升級至 適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器。 如需移轉至 適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器的詳細資訊,請參閱單一伺服器 適用於 PostgreSQL 的 Azure 資料庫 發生什麼事?

虛擬網絡 (VNet) 服務端點和規則會將 虛擬網絡 的私人地址空間延伸至您的 適用於 PostgreSQL 的 Azure 資料庫 伺服器。 使用方便的 Azure CLI 命令,您可以建立、更新、刪除、列出及顯示 VNet 服務端點和規則來管理您的伺服器。 如需 適用於 PostgreSQL 的 Azure 資料庫 VNet 服務端點的概觀,包括限制,請參閱 適用於 PostgreSQL 的 Azure 資料庫 伺服器 VNet 服務端點。 VNet 服務端點可在所有支持的區域中取得 適用於 PostgreSQL 的 Azure 資料庫。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

注意

VNet 服務端點的支援僅適用於一般用途伺服器和記憶體最佳化伺服器。 如果是 VNet 對等互連,如果流量流經具有服務端點的一般 VNet 閘道,且應該流向對等互連,請建立 ACL/VNet 規則,以允許閘道 VNet 中的 Azure 虛擬機器 存取 適用於 PostgreSQL 的 Azure 資料庫 伺服器。

設定 Vnet 服務端點

az network vnet 命令可用來設定虛擬網路。 擁有虛擬網路寫入權的使用者可以任意地在虛擬網路上設定服務端點。

若要將 Azure 服務資源放到 VNet 保護,使用者必須擁有所要新增之子網路的 "Microsoft.Network/virtualNetworks/subnets/joinViaServiceEndpoint/" 權限。 此權限預設會隨附在內建的服務管理員角色中,可藉由建立自訂角色加以修改。

深入了解內建角色以及如何將特定權限指派給自訂角色

VNet 和 Azure 服務資源不一定要位於相同訂用帳戶中。 如果 VNet 和 Azure 服務資源位於不同的訂用帳戶中,則資源應該位於相同的 Active Directory (AD) 租用戶底下。 請確定這兩個訂用帳戶都 已註冊 Microsoft.Sql 資源提供者。 如需詳細資訊,請參閱 resource-manager-registration

重要

強烈建議先閱讀本文,以瞭解服務端點組態和考慮,再執行下列範例腳本,或設定服務端點。 虛擬網路服務端點:虛擬網路服務端點是一個子網路,其屬性值包含一或多個正式的 Azure 服務類型名稱。 VNet 服務端點使用 Microsoft.Sql 服務類型名稱,它參考名為 SQL Database 的 Azure 服務。 此服務標籤也會套用到 Azure SQL Database、適用於 PostgreSQL 和 MySQL 的 Azure 資料庫服務。 請務必注意,當您將 Microsoft.Sql 服務標籤套用到 VNet 服務端點時,它會設定所有 Azure 資料庫服務的服務端點流量,包括子網路上的 Azure SQL Database、適用於 PostgreSQL 的 Azure 資料庫和適用於 MySQL 的 Azure 資料庫伺服器。

範例指令碼

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com ,從另一個瀏覽器索引標籤啟動 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼透過不同的訂閱登入,並將 <Subscription ID> 取代為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

執行指令碼

# Create a PostgreSQL server and configure a vNet rule

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-postgresql-rg-$randomIdentifier"
tag="create-postgresql-server"
server="msdocs-postgresql-server-$randomIdentifier"
sku="GP_Gen5_2"
vNet="vNet-$randomIdentifier"
vNetAddressPrefix="10.0.0.0/16"
subnet="subnet-$randomIdentifier"
subnetAddressPrefix="10.0.1.0/24"
rule="rule-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"

echo "Using resource group $resourceGroup with login: $login, password: $password..."

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# 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.
echo "Creating $server in $location..."
az postgres server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku

# Get available service endpoints for Azure region output is JSON
echo "List of available service endpoints for $location"
az network vnet list-endpoint-services --location "$location"

# Add Azure SQL service endpoint to a subnet while creating the virtual network
echo "Adding service endpoint to $subnet in $vNet"
az network vnet create --resource-group $resourceGroup --name $vNet --address-prefixes $vNetAddressPrefix --location "$location"

# Creates the service endpoint
echo "Creating a service endpoint to $subnet in $vNet"
az network vnet subnet create --resource-group $resourceGroup --name $subnet --vnet-name $vNet --address-prefix $subnetAddressPrefix --service-endpoints Microsoft.SQL

# View service endpoints configured on a subnet
echo "Viewing the service endpoint to $subnet in $vNet"
az network vnet subnet show --resource-group $resourceGroup --name $subnet --vnet-name $vNet

# Create a VNet rule on the server 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.
echo "Creating a VNet rule on $server to secure it to $subnet in $vNet"
az postgres server vnet-rule create --name $rule --resource-group $resourceGroup --server $server --vnet-name $vNet --subnet $subnet

清除部署

您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。

echo "Cleaning up resources by removing the resource group..."
az group delete --name $resourceGroup -y