使用 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 免費帳戶。
必要條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱 Azure Cloud Shell 中的 Bash 快速入門。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱使用 Azure CLI 登入。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能詳細資訊,請參閱使用 Azure CLI 擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
注意
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