你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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

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

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

如果没有 Azure 订阅,请在开始之前创建一个免费帐户

先决条件

  • 具有活动订阅的 Azure 帐户。

  • Azure Cloud Shell 或 Azure CLI。

    本文中的步骤在 Azure Cloud Shell 中以交互方式运行 Azure CLI 命令。 要在 Cloud Shell 中运行命令,请选择代码块右上角的“打开 Cloudshell”。 选择“复制”以复制代码,并将其粘贴到 Cloud Shell 以运行。 也可以从 Azure 门户中运行 Cloud Shell。

    还可以 在本地安装 Azure CLI 以运行命令。 本快速入门需要 Azure CLI 版本 2.0 或更高版本。 如果在本地运行 Azure CLI,请使用 az login 命令登录到 Azure。

创建虚拟机

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

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

    # Create a resource group.
    az group create --name 'myResourceGroup' --location 'eastus'
    
  2. 使用 az network vnet create 创建虚拟网络。

    # Create a virtual network and a subnet.
    az network vnet create --resource-group 'myResourceGroup' --name 'myVNet' --subnet-name 'mySubnet' --subnet-prefixes 10.0.0.0/24 
    
  3. 使用 az network nsg create 创建默认网络安全组。

    # Create a default network security group.
    az network nsg create --name 'myVM-nsg' --resource-group 'myResourceGroup' --location 'eastus'
    
  4. 使用 az vm create 创建虚拟机。 出现提示时,输入用户名和密码。

    # Create a Linux virtual machine using the latest Ubuntu 20.04 LTS image.
    az vm create --resource-group 'myResourceGroup' --name 'myVM' --location 'eastus' --vnet-name 'myVNet' --subnet 'mySubnet' --public-ip-address '' --nsg '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 地址之一),通过 az network watcher test-ip-flow 命令来测试从 myVM 到 13.107.21.200 的出站通信:

    # Start the IP flow verify session to test outbound flow to www.bing.com.
    az network watcher test-ip-flow --direction 'outbound' --protocol 'TCP' --local '10.0.0.4:60000' --remote '13.107.21.200:80' --vm 'myVM' --nic 'myVmVMNic' --resource-group 'myResourceGroup' --out 'table'
    

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

    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.
    az network watcher test-ip-flow --direction 'outbound' --protocol 'TCP' --local '10.0.0.4:60000' --remote '10.0.1.10:80' --vm 'myVM' --nic 'myVmVMNic' --resource-group 'myResourceGroup' --out 'table'
    

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

    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.
    az network watcher test-ip-flow --direction 'outbound' --protocol 'TCP' --local '10.0.0.4:60000' --remote '10.10.10.10:80' --vm 'myVM' --nic 'myVmVMNic' --resource-group 'myResourceGroup' --out 'table'
    

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

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

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

  4. 将“方向”更改为“入站”,将本地端口更改为“80”,将远程端口更改为“60000”,然后重复测试。

    # Start the IP flow verify session to test inbound flow from 10.10.10.10.
    az network watcher test-ip-flow --direction 'inbound' --protocol 'TCP' --local '10.0.0.4:80' --remote '10.10.10.10:6000' --vm 'myVM' --nic 'myVmVMNic' --resource-group 'myResourceGroup' --out 'table'
    

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

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

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

查看安全规则的详细信息

若要确定上一部分中的规则允许或拒绝通信的原因,请使用 az network nic list-effective-nsg 命令来查看 myVM 虚拟机网络接口的有效安全规则:

# Get the effective security rules for the network interface of myVM.
az network nic list-effective-nsg --resource-group 'myResourceGroup' --name 'myVmVMNic'

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

{
  "access": "Allow",
  "destinationAddressPrefix": "Internet",
  "destinationAddressPrefixes": [
	"Internet"
  ],
  "destinationPortRange": "0-65535",
  "destinationPortRanges": [
	"0-65535"
  ],
  "direction": "Outbound",
  "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"
  ],
  "name": "defaultSecurityRules/AllowInternetOutBound",
  "priority": 65001,
  "protocol": "All",
  "sourceAddressPrefix": "0.0.0.0/0",
  "sourceAddressPrefixes": [
	"0.0.0.0/0",
	"0.0.0.0/0"
  ],
  "sourcePortRange": "0-65535",
  "sourcePortRanges": [
	"0-65535"
  ]
},

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

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

清理资源

不再需要 myResourceGroup 资源组时,请使用 az group delete 将其及其包含的所有资源删除:

# Delete the resource group and all resources it contains.
az group delete --name 'myResourceGroup' --yes

后续步骤

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

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