Share via


快速入門:使用 Bicep 建立流量管理員設定檔

本快速入門說明如何使用 Bicep 來建立具有外部端點並使用效能路由方法的流量管理員設定檔。

Bicep 是使用宣告式語法來部署 Azure 資源的特定領域語言 (DSL)。 其提供簡潔的語法、可靠的類型安全,並支援程式碼重複使用。 Bicep 能夠為您在 Azure 中的基礎結構即程式碼解決方案,提供最佳的製作體驗。

必要條件

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

檢閱 Bicep 檔案

此快速入門中使用的 Bicep 檔案是來自 Azure 快速入門範本

@description('Relative DNS name for the traffic manager profile, must be globally unique.')
param uniqueDnsName string

resource ExternalEndpointExample 'Microsoft.Network/trafficmanagerprofiles@2022-04-01' = {
  name: 'ExternalEndpointExample'
  location: 'global'
  properties: {
    profileStatus: 'Enabled'
    trafficRoutingMethod: 'Performance'
    dnsConfig: {
      relativeName: uniqueDnsName
      ttl: 30
    }
    monitorConfig: {
      protocol: 'HTTPS'
      port: 443
      path: '/'
      expectedStatusCodeRanges: [
        {
          min: 200
          max: 202
        }
        {
          min: 301
          max: 302
        }
      ]
    }
    endpoints: [
      {
        type: 'Microsoft.Network/TrafficManagerProfiles/ExternalEndpoints'
        name: 'endpoint1'
        properties: {
          target: 'www.microsoft.com'
          endpointStatus: 'Enabled'
          endpointLocation: 'northeurope'
        }
      }
      {
        type: 'Microsoft.Network/TrafficManagerProfiles/ExternalEndpoints'
        name: 'endpoint2'
        properties: {
          target: 'docs.microsoft.com'
          endpointStatus: 'Enabled'
          endpointLocation: 'southcentralus'
        }
      }
    ]
  }
}
output name string = ExternalEndpointExample.name
output resourceGroupName string = resourceGroup().name
output resourceId string = ExternalEndpointExample.id

Bicep 檔案中定義了一項 Azure 資源:

部署 Bicep 檔案

  1. 將 Bicep 檔案以 main.bicep 儲存至本機電腦。

  2. 使用 Azure CLI 或 Azure PowerShell 部署 Bicep 檔案。

    az group create --name exampleRG --location eastus
    az deployment group create --resource-group exampleRG --template-file main.bicep --parameters uniqueDnsName=<dns-name>
    

    Bicep 檔案部署會建立具有兩個外部端點的設定檔。 Endpoint1 會使用 www.microsoft.com 的目標端點搭配北歐的位置。 Endpoint2 會使用 learn.microsoft.com 的目標端點搭配美國中南部的位置。

    注意

    uniqueDNSname 必須是全域唯一的名稱,才能成功部署 Bicep 檔案。

    當部署完成時,您會看到指出部署成功的訊息。

驗證部署

使用 Azure CLI 或 Azure PowerShell 驗證部署。

  1. 決定流量管理員設定檔的 DNS 名稱。

    az network traffic-manager profile show --name ExternalEndpointExample --resource-group exampleRG 
    

    從輸出中複製 fqdn 值。 值的格式如下:<relativeDnsName>.trafficmanager.net。 此值也是流量管理員設定檔的 DNS 名稱。

  2. 執行下列命令,方法是將 {relativeDnsName} 變數取代為 <relativeDnsName>.trafficmanager.net

    nslookup -type=cname {relativeDnsName}
    

    您應會取得 www.microsoft.comlearn.microsoft.com 的正式名稱,這取決於哪個區域較接近您。

  3. 若要檢查您是否可解析為其他端點,請針對您在上一個步驟中取得的目標停用端點。 以 endpoint1endpoint2 取代 {endpointName},分別停用 www.microsoft.comlearn.microsoft.com 的目標。

    az network traffic-manager endpoint update --name {endpointName} --type externalEndpoints --profile-name ExternalEndpointExample --resource-group exampleRG --endpoint-status "Disabled"
    
  4. 在 Azure CLI 或 Azure PowerShell 中再次從步驟 2 執行命令。 這次,您應該會取得其他端點的其他正式名稱/NameHost。

清除資源

不再需要流量管理員設定檔時,請使用 Azure 入口網站、Azure CLI 或 Azure PowerShell 來刪除資源群組。 這會移除流量管理員設定檔和所有相關資源。

az group delete --name exampleRG

下一步

在本快速入門中,您使用 Bicep 建立了流量管理員設定檔。

若要深入了解如何路由傳送流量,請繼續進行流量管理員的教學課程。