快速入門:使用 Azure PowerShell 建立 Private Link 服務

開始建立參考您服務的 Private Link 服務。 針對在 Azure Standard Load Balancer 後方部署的服務或資源,授與 Private Link 存取權限。 您服務的使用者可以從其虛擬網路進行私人存取。

Diagram of resources created in private endpoint quickstart.

必要條件

  • 具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶

  • Azure Cloud Shell 或 Azure PowerShell。

    本快速入門中的步驟會在 Azure Cloud Shell 中以互動方式執行 Azure PowerShell Cmdlet。 若要在 Cloud Shell 中執行命令,請選取程式碼區塊右上角的 [開啟 Cloudshell]。 選取 [複製] 以複製程式碼,然後將其貼入 Cloud Shell 以執行。 您也可以從 Azure 入口網站內執行 Cloud Shell。

    您也可以在本機安裝 Azure PowerShell 來執行 Cmdlet。 本文中的步驟需要 Azure PowerShell 模組 5.4.1 版或更新版本。 執行 Get-Module -ListAvailable Az 來了解您安裝的版本。 如果您需要升級,請參閱更新 Azure PowerShell 模組

    如果您在本機執行 PowerShell,請執行 Connect-AzAccount 以連線至 Azure。

建立資源群組

Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。

使用 New-AzResourceGroup 建立資源群組:

New-AzResourceGroup -Name 'test-rg' -Location 'eastus2'

建立內部負載平衡器

在本節中,您會建立虛擬網路和內部 Azure Load Balancer。

虛擬網路

在本節中,您會建立虛擬網路和子網路,以裝載可存取 Private Link 服務的負載平衡器。

## Create backend subnet config ##
$subnet = @{
    Name = 'subnet-1'
    AddressPrefix = '10.0.0.0/24'
}
$subnetConfig = New-AzVirtualNetworkSubnetConfig @subnet 

## Create the virtual network ##
$net = @{
    Name = 'vnet-1'
    ResourceGroupName = 'test-rg'
    Location = 'eastus2'
    AddressPrefix = '10.0.0.0/16'
    Subnet = $subnetConfig
}
$vnet = New-AzVirtualNetwork @net

建立標準負載平衡器

本節將詳細說明如何建立及設定下列負載平衡器元件:

## Place virtual network created in previous step into a variable. ##
$vnet = Get-AzVirtualNetwork -Name 'vnet-1' -ResourceGroupName 'test-rg'

## Create load balancer frontend configuration and place in variable. ##
$lbip = @{
    Name = 'frontend'
    PrivateIpAddress = '10.0.0.4'
    SubnetId = $vnet.subnets[0].Id
}
$feip = New-AzLoadBalancerFrontendIpConfig @lbip

## Create backend address pool configuration and place in variable. ##
$bepool = New-AzLoadBalancerBackendAddressPoolConfig -Name 'backend-pool'

## Create the health probe and place in variable. ##
$probe = @{
    Name = 'health-probe'
    Protocol = 'http'
    Port = '80'
    IntervalInSeconds = '360'
    ProbeCount = '5'
    RequestPath = '/'
}
$healthprobe = New-AzLoadBalancerProbeConfig @probe

## Create the load balancer rule and place in variable. ##
$lbrule = @{
    Name = 'http-rule'
    Protocol = 'tcp'
    FrontendPort = '80'
    BackendPort = '80'
    IdleTimeoutInMinutes = '15'
    FrontendIpConfiguration = $feip
    BackendAddressPool = $bePool
}
$rule = New-AzLoadBalancerRuleConfig @lbrule -EnableTcpReset

## Create the load balancer resource. ##
$loadbalancer = @{
    ResourceGroupName = 'test-rg'
    Name = 'load-balancer'
    Location = 'eastus2'
    Sku = 'Standard'
    FrontendIpConfiguration = $feip
    BackendAddressPool = $bePool
    LoadBalancingRule = $rule
    Probe = $healthprobe
}
New-AzLoadBalancer @loadbalancer

停用網路原則

在虛擬網路中建立私人連結服務之前,必須先停用設定 privateLinkServiceNetworkPolicies

## Place the subnet name into a variable. ##
$subnet = 'subnet-1'

## Place the virtual network configuration into a variable. ##
$net = @{
    Name = 'vnet-1'
    ResourceGroupName = 'test-rg'
}
$vnet = Get-AzVirtualNetwork @net

## Set the policy as disabled on the virtual network. ##
($vnet | Select -ExpandProperty subnets | Where-Object {$_.Name -eq $subnet}).privateLinkServiceNetworkPolicies = "Disabled"

## Save the configuration changes to the virtual network. ##
$vnet | Set-AzVirtualNetwork

在本節中,建立使用上一個步驟中所建立 Standard Azure Load Balancer 的私人連結服務。

## Place the virtual network into a variable. ##
$vnet = Get-AzVirtualNetwork -Name 'vnet-1' -ResourceGroupName 'test-rg'

## Create the IP configuration for the private link service. ##
$ipsettings = @{
    Name = 'ipconfig-1'
    PrivateIpAddress = '10.0.0.5'
    Subnet = $vnet.subnets[0]
}
$ipconfig = New-AzPrivateLinkServiceIpConfig @ipsettings

## Place the load balancer frontend configuration into a variable. ##
$par = @{
    Name = 'load-balancer'
    ResourceGroupName = 'test-rg'
}
$fe = Get-AzLoadBalancer @par | Get-AzLoadBalancerFrontendIpConfig

## Create the private link service for the load balancer. ##
$privlinksettings = @{
    Name = 'private-link-service'
    ResourceGroupName = 'test-rg'
    Location = 'eastus2'
    LoadBalancerFrontendIpConfiguration = $fe
    IpConfiguration = $ipconfig
}
New-AzPrivateLinkService @privlinksettings

您的私人連結服務已建立,且可以接收流量。 如果您想要查看流量,請在標準負載平衡器後方設定您的應用程式。

建立私人端點

在本節中,您會將私人連結服務對應至私人端點。 虛擬網路包含私人連結服務的私人端點。 此虛擬網路包含將存取私人連結服務的資源。

建立私人端點虛擬網路

## Create backend subnet config ##
$subnet = @{
    Name = 'subnet-pe'
    AddressPrefix = '10.1.0.0/24'
}
$subnetConfig = New-AzVirtualNetworkSubnetConfig @subnet 

## Create the virtual network ##
$net = @{
    Name = 'vnet-pe'
    ResourceGroupName = 'test-rg'
    Location = 'eastus2'
    AddressPrefix = '10.1.0.0/16'
    Subnet = $subnetConfig
}
$vnetpe = New-AzVirtualNetwork @net

建立端點和連線

## Place the private link service configuration into variable. ##
$par1 = @{
    Name = 'private-link-service'
    ResourceGroupName = 'test-rg'
}
$pls = Get-AzPrivateLinkService @par1

## Create the private link configuration and place in variable. ##
$par2 = @{
    Name = 'connection-1'
    PrivateLinkServiceId = $pls.Id
}
$plsConnection = New-AzPrivateLinkServiceConnection @par2

## Place the virtual network into a variable. ##
$par3 = @{
    Name = 'vnet-pe'
    ResourceGroupName = 'test-rg'
}
$vnetpe = Get-AzVirtualNetwork @par3

## Create private endpoint ##
$par4 = @{
    Name = 'private-endpoint'
    ResourceGroupName = 'test-rg'
    Location = 'eastus2'
    Subnet = $vnetpe.subnets[0]
    PrivateLinkServiceConnection = $plsConnection
}
New-AzPrivateEndpoint @par4 -ByManualRequest

核准私人端點連線

在本節中,您會核准您在先前步驟中建立的連線。

## Place the private link service configuration into variable. ##
$par1 = @{
    Name = 'private-link-service'
    ResourceGroupName = 'test-rg'
}
$pls = Get-AzPrivateLinkService @par1

$par2 = @{
    Name = $pls.PrivateEndpointConnections[0].Name
    ServiceName = 'private-link-service'
    ResourceGroupName = 'test-rg'
    Description = 'Approved'
    PrivateLinkResourceType = 'Microsoft.Network/privateLinkServices'
}
Approve-AzPrivateEndpointConnection @par2

私人端點的 IP 位址

在本節中,您將尋找與負載平衡器和私人連結服務對應的私人端點 IP 位址。

## Get private endpoint and the IP address and place in a variable for display. ##
$par1 = @{
    Name = 'private-endpoint'
    ResourceGroupName = 'test-rg'
    ExpandResource = 'networkinterfaces'
}
$pe = Get-AzPrivateEndpoint @par1

## Display the IP address by expanding the variable. ##
$pe.NetworkInterfaces[0].IpConfigurations[0].PrivateIpAddress
❯ $pe.NetworkInterfaces[0].IpConfigurations[0].PrivateIpAddress
10.1.0.4

清除資源

當不再需要時,您可以使用 Remove-AzResourceGroup 命令來移除資源群組、負載平衡器和其餘資源。

Remove-AzResourceGroup -Name 'test-rg'

下一步

在本快速入門中,您將:

  • 建立虛擬網路和內部 Azure Load Balancer。

  • 建立私人連結服務

若要深入了解 Azure 私人端點,請繼續: