教程:使用 Azure PowerShell 创建流量管理器链接记录

在这个教程中,你用 Azure PowerShell 在 Azure DNS 中创建一个流量管理器链接记录。 流量管理器关联记录将一个DNS记录直接连接到Azure 流量管理器配置文件,向没有中间CNAME解析的客户端返回IP地址。

Important

流量管理器关联记录当前处于预览阶段。 有关适用于 beta 版、预览版或尚未正式发布的 Azure 功能的法律条款,请参阅 Azure 预览补充使用条款

关于基于门户的攻略,请参见使用 Azure 门户创建流量管理器链接记录。 有关概念概述,请参见 交通管理链接记录概述

本教程中,您将学习如何:

  • 建立网络和虚拟机基础设施。
  • 创建一个带端点的流量管理器配置文件。
  • 使用 Azure PowerShell 创建 Traffic Manager Linked Record。
  • 测试流量管理器的关联记录。

如果没有 Azure 订阅,可在开始前创建一个试用帐户

Prerequisites

  • 拥有有效订阅的 Azure 帐户。
  • 已在本地安装 Azure PowerShell 12.0.0 或更高版本。 请运行 Get-Module -ListAvailable Az 以查看版本。 若要进行安装或升级,请参阅安装 Azure PowerShell
  • 托管在 Azure DNS 中的域名。 如果你没有 Azure DNS 区域,创建一个委托你的域名给 Azure DNS。

Note

在本教程中,contoso.com 用作示例域名。 将 contoso.com 替换为你自己的域名。

Note

建议使用 Azure Az PowerShell 模块与 Azure 交互。 若要开始,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

设置变量

定义本教程中使用的资源名称和位置变量。

$ResourceGroup = "test-rg"
$Location = "chinanorth3"
$VNetName = "vnet-1"
$TmProfileName = "tm-profile"
$DnsZone = "contoso.com"
$DnsZoneResourceGroup = "<dns-zone-resource-group>"

Note

用包含你现有Azure DNS区域的资源组替换<dns-zone-resource-group>

创建资源组

New-AzResourceGroup -Name $ResourceGroup -Location $Location

创建虚拟网络

$subnet = @{
    Name          = "subnet-1"
    AddressPrefix = "10.10.0.0/24"
}
$SubnetConfig = New-AzVirtualNetworkSubnetConfig @subnet

$vnet = @{
    ResourceGroupName = $ResourceGroup
    Name              = $VNetName
    Location          = $Location
    AddressPrefix     = "10.10.0.0/16"
    Subnet            = $SubnetConfig
}
New-AzVirtualNetwork @vnet

创建 Web 服务器虚拟机

创建公共IP地址

$pip1 = @{
    ResourceGroupName = $ResourceGroup
    Name              = "public-ip-1"
    Location          = $Location
    Sku               = "Standard"
    AllocationMethod  = "Static"
    DomainNameLabel   = "vm-1-tmlink"
}
$Vm1Pip = New-AzPublicIpAddress @pip1

$pip2 = @{
    ResourceGroupName = $ResourceGroup
    Name              = "public-ip-2"
    Location          = $Location
    Sku               = "Standard"
    AllocationMethod  = "Static"
    DomainNameLabel   = "vm-2-tmlink"
}
$Vm2Pip = New-AzPublicIpAddress @pip2

创建网络安全组

$httpRule = @{
    Name                     = "AllowHTTP"
    Protocol                 = "Tcp"
    Direction                = "Inbound"
    Priority                 = 100
    SourceAddressPrefix      = "*"
    SourcePortRange          = "*"
    DestinationAddressPrefix = "*"
    DestinationPortRange     = 80
    Access                   = "Allow"
}
$HttpRule = New-AzNetworkSecurityRuleConfig @httpRule

$httpsRule = @{
    Name                     = "AllowHTTPS"
    Protocol                 = "Tcp"
    Direction                = "Inbound"
    Priority                 = 110
    SourceAddressPrefix      = "*"
    SourcePortRange          = "*"
    DestinationAddressPrefix = "*"
    DestinationPortRange     = 443
    Access                   = "Allow"
}
$HttpsRule = New-AzNetworkSecurityRuleConfig @httpsRule

$nsg = @{
    ResourceGroupName = $ResourceGroup
    Location          = $Location
    Name              = "nsg-1"
    SecurityRules     = $HttpRule, $HttpsRule
}
$Nsg = New-AzNetworkSecurityGroup @nsg

创建网络接口和虚拟机

$VNet = Get-AzVirtualNetwork -ResourceGroupName $ResourceGroup -Name $VNetName
$Subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $VNet -Name "subnet-1"

$nic1 = @{
    ResourceGroupName      = $ResourceGroup
    Name                   = "nic-1"
    Location               = $Location
    SubnetId               = $Subnet.Id
    PublicIpAddressId      = $Vm1Pip.Id
    NetworkSecurityGroupId = $Nsg.Id
}
$Vm1Nic = New-AzNetworkInterface @nic1

$nic2 = @{
    ResourceGroupName      = $ResourceGroup
    Name                   = "nic-2"
    Location               = $Location
    SubnetId               = $Subnet.Id
    PublicIpAddressId      = $Vm2Pip.Id
    NetworkSecurityGroupId = $Nsg.Id
}
$Vm2Nic = New-AzNetworkInterface @nic2

创建虚拟机配置并部署虚拟机。 当提示时,输入管理员账户的密码。

$Credential = Get-Credential -Message "Enter a username and password for the VMs"

$image = @{
    PublisherName = "Canonical"
    Offer         = "ubuntu-24_04-lts"
    Skus          = "server"
    Version       = "latest"
}

$Vm1Config = New-AzVMConfig -VMName "vm-1" -VMSize "Standard_B1s" |
    Set-AzVMOperatingSystem -Linux -ComputerName "vm-1" -Credential $Credential |
    Set-AzVMSourceImage @image |
    Add-AzVMNetworkInterface -Id $Vm1Nic.Id

New-AzVM -ResourceGroupName $ResourceGroup -Location $Location -VM $Vm1Config -AsJob

$Vm2Config = New-AzVMConfig -VMName "vm-2" -VMSize "Standard_B1s" |
    Set-AzVMOperatingSystem -Linux -ComputerName "vm-2" -Credential $Credential |
    Set-AzVMSourceImage @image |
    Add-AzVMNetworkInterface -Id $Vm2Nic.Id

New-AzVM -ResourceGroupName $ResourceGroup -Location $Location -VM $Vm2Config

在虚拟机上安装NGINX

$run1 = @{
    ResourceGroupName = $ResourceGroup
    VMName            = "vm-1"
    CommandId         = "RunShellScript"
    ScriptString      = "sudo apt-get update && sudo apt-get install -y nginx && echo 'Hello World from vm-1' | sudo tee /var/www/html/index.html"
}
Invoke-AzVMRunCommand @run1

$run2 = @{
    ResourceGroupName = $ResourceGroup
    VMName            = "vm-2"
    CommandId         = "RunShellScript"
    ScriptString      = "sudo apt-get update && sudo apt-get install -y nginx && echo 'Hello World from vm-2' | sudo tee /var/www/html/index.html"
}
Invoke-AzVMRunCommand @run2

检索公共IP地址

$Vm1IPAddress = (Get-AzPublicIpAddress -ResourceGroupName $ResourceGroup -Name "public-ip-1").IpAddress
$Vm2IPAddress = (Get-AzPublicIpAddress -ResourceGroupName $ResourceGroup -Name "public-ip-2").IpAddress

Write-Output "vm-1 IP: $Vm1IPAddress"
Write-Output "vm-2 IP: $Vm2IPAddress"

创建流量管理器配置文件

$tm = @{
    Name                 = $TmProfileName
    ResourceGroupName    = $ResourceGroup
    TrafficRoutingMethod = "Priority"
    RelativeDnsName      = "tm-profile-$(Get-Random)"
    Ttl                  = 30
    MonitorProtocol      = "HTTP"
    MonitorPort          = 80
    MonitorPath          = "/"
    RecordType           = "A"
}
$TmProfile = New-AzTrafficManagerProfile @tm

向流量管理器配置文件添加端点

$ep1 = @{
    EndpointName          = "tmendpoint-1"
    TrafficManagerProfile = $TmProfile
    Type                  = "ExternalEndpoints"
    Target                = $Vm1IPAddress
    EndpointStatus        = "Enabled"
    Priority              = 1
}
Add-AzTrafficManagerEndpointConfig @ep1

$ep2 = @{
    EndpointName          = "tmendpoint-2"
    TrafficManagerProfile = $TmProfile
    Type                  = "ExternalEndpoints"
    Target                = $Vm2IPAddress
    EndpointStatus        = "Enabled"
    Priority              = 2
}
Add-AzTrafficManagerEndpointConfig @ep2

Set-AzTrafficManagerProfile -TrafficManagerProfile $TmProfile

创建流量管理器关联记录

使用 New-AzDnsRecordSet 并配合 -TrafficManagementProfile 参数创建流量管理器链接记录。 以下命令在区域顶点()@创建一个A记录。

Note

-TrafficManagementProfile 参数可在Az.Dns 5.0.0及更高版本中提供,并且需要Traffic Manager Linked Records预览API(2024-06-01-preview 或更高版本)。

$TmProfileId = $TmProfile.Id

$apex = @{
    ResourceGroupName        = $DnsZoneResourceGroup
    ZoneName                 = $DnsZone
    Name                     = "@"
    RecordType               = "A"
    Ttl                      = 30
    TrafficManagementProfile = $TmProfileId
}
New-AzDnsRecordSet @apex

如果要为子域名而不是区域顶点创建记录,请用子域名名称替换 "@"。 例如:

$www = @{
    ResourceGroupName        = $DnsZoneResourceGroup
    ZoneName                 = $DnsZone
    Name                     = "www"
    RecordType               = "A"
    Ttl                      = 30
    TrafficManagementProfile = $TmProfileId
}
New-AzDnsRecordSet @www

核实链接的记录

$record = @{
    ResourceGroupName = $DnsZoneResourceGroup
    ZoneName          = $DnsZone
    Name              = "@"
    RecordType        = "A"
}
Get-AzDnsRecordSet @record

输出包含一个 TrafficManagementProfile 属性,用于显示 Traffic Manager 配置文件资源 ID,确认连接已建立。

Note

记录中的 TTL 值反映的是 Traffic Manager 配置文件的 TTL 值。 在记录创建过程中指定的任何TTL都会被流量管理器配置文件的DNSTTL值替代。

测试流量管理器关联记录

获取你DNS区域的名称服务器,直接查询其中一个。

$ns = @{
    ResourceGroupName = $DnsZoneResourceGroup
    ZoneName          = $DnsZone
    Name              = "@"
    RecordType        = "NS"
}
$NameServer = (Get-AzDnsRecordSet @ns).Records[0].Nsdname

Resolve-DnsName -Name $DnsZone -Server $NameServer -Type A

响应包含带有IP地址的A记录,确认流量管理器链接记录直接返回IP地址且无需CNAME跳转。

测试故障转移

  1. 访问您的域名。 你应该看看 VM-1 的 NGINX 页面。

  2. 停止 vm-1 虚拟机:

    Stop-AzVM -ResourceGroupName $ResourceGroup -Name "vm-1" -Force
    
  3. 等待几分钟,让 Traffic Manager 检测到该终结点处于不正常状态。

  4. 如果需要,先清除本地DNS缓存,然后再次浏览你的域名。 你现在应该能看到 vm-2的页面了。

  5. 重启 vm-1 以恢复原始配置:

    Start-AzVM -ResourceGroupName $ResourceGroup -Name "vm-1"
    

清理资源

当你不再需要资源时,移除资源组和DNS记录:

# Delete the resource group and all resources within it
Remove-AzResourceGroup -Name $ResourceGroup -Force -AsJob

# Delete the Traffic Manager Linked Record from the DNS zone
$cleanup = @{
    ResourceGroupName = $DnsZoneResourceGroup
    ZoneName          = $DnsZone
    Name              = "@"
    RecordType        = "A"
}
Remove-AzDnsRecordSet @cleanup

后续步骤

在这个教程中,你用 Azure PowerShell 创建了一个流量管理器链接记录。 该记录将你的DNS区域与流量管理器配置文件关联,直接返回IP地址给客户端。