快速入门:使用 Azure PowerShell 诊断虚拟机网络流量筛选器问题

在本快速入门中,你要部署虚拟机并使用网络观察程序 IP 流验证来测试与不同 IP 地址的连接。 使用 IP 流验证功能得到的结果,可以确定阻止流量并导致通信失败的安全规则,并了解如何解决该问题。 你还将了解如何使用网络接口的有效安全规则来确定安全规则允许或拒绝流量的原因。

关系图显示了在网络观察程序快速入门中创建的资源。

如果没有 Azure 订阅,请在开始前创建一个试用版订阅

先决条件

创建虚拟机

在本部分中,你将在中国东部区域创建虚拟网络和子网。 然后,将在子网中创建具有默认网络安全组的虚拟机。

  1. 使用 New-AzResourceGroup 创建资源组。 Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。

    # Create a resource group.
    New-AzResourceGroup -Name 'myResourceGroup' -Location 'chinaeast' 
    
  2. 使用 New-AzVirtualNetworkSubnetConfig 为虚拟机子网和 Bastion 主机子网创建子网配置。

    # Create subnets configuration.
    $Subnet = New-AzVirtualNetworkSubnetConfig -Name 'mySubnet' -AddressPrefix '10.0.0.0/24'
    
  3. 使用 New-AzVirtualNetwork 创建虚拟网络。

    # Create a virtual network.
    New-AzVirtualNetwork -Name 'myVNet' -ResourceGroupName 'myResourceGroup' -Location 'chinaeast' -AddressPrefix '10.0.0.0/16' -Subnet $Subnet
    
  4. 使用 New-AzNetworkSecurityGroup 创建默认网络安全组。

    # Create a network security group. 
    New-AzNetworkSecurityGroup -Name 'myVM-nsg' -ResourceGroupName 'myResourceGroup' -Location  'chinaeast'
    
  5. 使用 New-AzVM 创建虚拟机。 出现提示时,输入用户名和密码。

    # Create a Linux virtual machine using the latest Ubuntu 20.04 LTS image.
    New-AzVm -ResourceGroupName 'myResourceGroup' -Name 'myVM' -Location 'chinaeast' -VirtualNetworkName 'myVNet' -SubnetName 'mySubnet' -SecurityGroupName 'myVM-nsg' -Image 'Canonical:0001-com-ubuntu-server-focal:20_04-lts-gen2:latest'
    

使用 IP 流验证测试网络通信

在本部分中,你要使用网络观察程序的 IP 流验证功能来测试传入和来自虚拟机的网络通信。

  1. 通过使用 IP 流验证(13.107.21.200www.bing.com 使用的公共 IP 地址之一)以下项来使用 Test-AzNetworkWatcherIPFlow 测试从 myVM13.107.21.200 的出站通信:

    # Place myVM configuration into a variable.
    $vm = Get-AzVM -ResourceGroupName 'myResourceGroup' -Name 'myVM'
    
    # Start the IP flow verify session to test outbound flow to www.bing.com.
    Test-AzNetworkWatcherIPFlow -Location 'chinaeast' -TargetVirtualMachineId $vm.Id -Direction 'Outbound' -Protocol 'TCP' -RemoteIPAddress '13.107.21.200' -RemotePort '80' -LocalIPAddress '10.0.0.4' -LocalPort '60000'
    

    几秒钟后,将会收到与以下示例类似的输出:

    Access RuleName
    ------ --------
    Allow  defaultSecurityRules/AllowInternetOutBound
    

    测试结果指示由于默认安全规则 AllowInternetOutBound 允许访问 13.107.21.200。 默认情况下,Azure 虚拟机可以访问 Internet。

  2. RemoteIPAddress 更改为 10.0.1.10 并重复测试。 10.0.1.10myVNet 地址空间中的专用 IP 地址。

    # Start the IP flow verify session to test outbound flow to 10.0.1.10.
    Test-AzNetworkWatcherIPFlow -Location 'chinaeast' -TargetVirtualMachineId $vm.Id -Direction 'Outbound' -Protocol 'TCP' -RemoteIPAddress '10.0.1.10' -RemotePort '80' -LocalIPAddress '10.0.0.4' -LocalPort '60000'
    

    几秒钟后,将会收到与以下示例类似的输出:

    Access RuleName
    ------ --------
    Allow  defaultSecurityRules/AllowVnetOutBound
    

    第二次测试的结果指示,由于默认安全规则 AllowVnetOutBound,允许访问 10.0.1.10。 默认情况下,Azure 虚拟机可以访问其虚拟网络地址空间中的所有 IP 地址。

  3. RemoteIPAddress 更改为 10.10.10.10 并重复测试。 10.10.10.10 是不在 myVNet 地址空间中的专用 IP 地址。

    # Start the IP flow verify session to test outbound flow to 10.10.10.10.
    Test-AzNetworkWatcherIPFlow -Location 'chinaeast' -TargetVirtualMachineId $vm.Id -Direction 'Outbound' -Protocol 'TCP' -RemoteIPAddress '10.10.10.10' -RemotePort '80' -LocalIPAddress '10.0.0.4' -LocalPort '60000'
    

    几秒钟后,将会收到与以下示例类似的输出:

    Access RuleName
    ------ --------
    Allow  defaultSecurityRules/DenyAllOutBound
    

    第三次测试的结果表明,由于默认安全规则 DenyAllOutBound,拒绝访问 10.10.10.10

  4. 将“方向”更改为“入站”,将“LocalPort”更改为“80”,并将“RemotePort”更改为“60000”,然后重复测试。

    # Start the IP flow verify session to test inbound flow from 10.10.10.10.
    Test-AzNetworkWatcherIPFlow -Location 'chinaeast' -TargetVirtualMachineId $vm.Id -Direction 'Inbound' -Protocol 'TCP' -RemoteIPAddress '10.10.10.10' -RemotePort '60000' -LocalIPAddress '10.0.0.4' -LocalPort '80'
    

    几秒钟后,将会收到与以下示例类似的输出:

    Access RuleName
    ------ --------
    Allow  defaultSecurityRules/DenyAllInBound
    

    第四次测试的结果表明,由于默认安全规则 DenyAllInBound,拒绝来自 10.10.10.10 的访问。 默认情况下,将会拒绝从虚拟网络外部对 Azure 虚拟机的所有访问。

查看安全规则的详细信息

要确定上一部分中的规则允许或拒绝通信的原因,请使用 Get-AzEffectiveNetworkSecurityGroup cmdlet 查看 myVM 虚拟机网络接口的有效安全规则:

# Get the effective security rules for the network interface of myVM.
Get-AzEffectiveNetworkSecurityGroup -NetworkInterfaceName 'myVM' -ResourceGroupName 'myResourceGroup'

返回的输出包含 AllowInternetOutbound 规则的以下信息,该规则允许对 www.bing.com 进行出站访问:

{
    "Name": "defaultSecurityRules/AllowInternetOutBound",
    "Protocol": "All",
    "SourcePortRange": [
        "0-65535"
    ],
    "DestinationPortRange": [
        "0-65535"
    ],
    "SourceAddressPrefix": [
        "0.0.0.0/0",
        "0.0.0.0/0"
    ],
    "DestinationAddressPrefix": [
        "Internet"
    ],
    "ExpandedSourceAddressPrefix": [],
    "ExpandedDestinationAddressPrefix": [
        "1.0.0.0/8",
        "2.0.0.0/7",
        "4.0.0.0/9",
        "4.144.0.0/12",
        "4.160.0.0/11",
        "4.192.0.0/10",
        "5.0.0.0/8",
        "6.0.0.0/7",
        "8.0.0.0/7",
        "11.0.0.0/8",
        "12.0.0.0/8",
        "13.0.0.0/10",
        "13.64.0.0/11",
        "13.104.0.0/13",
        "13.112.0.0/12",
        "13.128.0.0/9",
        "14.0.0.0/7",
        ...
        ...
        ...
        "200.0.0.0/5",
        "208.0.0.0/4"
    ],
    "Access": "Allow",
    "Priority": 65001,
    "Direction": "Outbound"
},

可以在输出中看到地址前缀 13.104.0.0/13AllowInternetOutBound 规则的地址前缀之一。 此前缀包含用于测试与 www.bing.com 的出站通信的 IP 地址 13.107.21.200

同样,可以检查其他规则来查看每个规则下的源和目标 IP 地址前缀。

清理资源

如果不再需要,请使用 Remove-AzResourcegroup 删除资源组以及其包含的所有资源:

# Delete the resource group and all resources it contains.
Remove-AzResourceGroup -Name 'myResourceGroup' -Force

后续步骤

在本快速入门中,你已创建 VM 并对入站和出站网络流量筛选器进行诊断。 你已了解了如何通过网络安全组规则来允许或拒绝出入 VM 的流量。 请详细了解安全规则以及如何创建安全规则

即使相应的网络流量筛选器已就位,与虚拟机的通信仍可能因路由配置问题而失败。 若要了解如何诊断虚拟机路由问题,请参阅诊断虚拟机网络路由问题。 若要使用一个工具诊断出站路由、延迟和流量筛选问题,请参阅排查与 Azure 网络观察程序的连接问题