创建并管理具有多个 NIC 的 Windows 虚拟机Create and manage a Windows virtual machine that has multiple NICs

Azure 中的虚拟机 (VM) 可附有多个虚拟网络接口卡 (NIC)。Virtual machines (VMs) in Azure can have multiple virtual network interface cards (NICs) attached to them. 一种常见方案是为前端和后端连接设置不同的子网。A common scenario is to have different subnets for front-end and back-end connectivity. 可以将 VM 上的多个 NIC 关联到多个子网,但这些子网必须全都位于同一个虚拟网络 (vNet) 中。You can associate multiple NICs on a VM to multiple subnets, but those subnets must all reside in the same virtual network (vNet). 本文详述了如何创建附有多个 NIC 的 VM。This article details how to create a VM that has multiple NICs attached to it. 还可以了解如何从现有 VM 中添加或删除 NIC。You also learn how to add or remove NICs from an existing VM. 不同的 VM 大小支持不同数目的 NIC,因此请相应地调整 VM 的大小。Different VM sizes support a varying number of NICs, so size your VM accordingly.

先决条件Prerequisites

在以下示例中,请将示例参数名称替换成自己的值。In the following examples, replace example parameter names with your own values. 示例参数名称包括 myResourceGroupmyVnetmyVMExample parameter names include myResourceGroup, myVnet, and myVM.

创建具有多个 NIC 的 VMCreate a VM with multiple NICs

首先创建一个资源组。First, create a resource group. 以下示例在“chinaeast”位置创建名为“myResourceGroup”的资源组:The following example creates a resource group named myResourceGroup in the chinaeast location:

New-AzResourceGroup -Name "myResourceGroup" -Location "chinaeast"

创建虚拟网络和子网Create virtual network and subnets

虚拟网络的一种常见方案是具有两个或多个子网。A common scenario is for a virtual network to have two or more subnets. 一个子网可能用于前端流量,另一个用于后端流量。One subnet may be for front-end traffic, the other for back-end traffic. 若要连接两个子网,可在 VM 上使用多个 NIC。To connect to both subnets, you then use multiple NICs on your VM.

  1. 通过 New-AzVirtualNetworkSubnetConfig 定义两个虚拟网络子网。Define two virtual network subnets with New-AzVirtualNetworkSubnetConfig. 以下示例分别定义 mySubnetFrontEndmySubnetBackEnd 的子网:The following example defines the subnets for mySubnetFrontEnd and mySubnetBackEnd:

    $mySubnetFrontEnd = New-AzVirtualNetworkSubnetConfig -Name "mySubnetFrontEnd" `
        -AddressPrefix "192.168.1.0/24"
    $mySubnetBackEnd = New-AzVirtualNetworkSubnetConfig -Name "mySubnetBackEnd" `
        -AddressPrefix "192.168.2.0/24"
    
  2. 通过 New-AzVirtualNetwork 创建虚拟网络和子网。Create your virtual network and subnets with New-AzVirtualNetwork. 以下示例创建一个名为 myVnet 的虚拟网络:The following example creates a virtual network named myVnet:

    $myVnet = New-AzVirtualNetwork -ResourceGroupName "myResourceGroup" `
        -Location "chinaeast" `
        -Name "myVnet" `
        -AddressPrefix "192.168.0.0/16" `
        -Subnet $mySubnetFrontEnd,$mySubnetBackEnd
    

创建多个 NICCreate multiple NICs

通过 New-AzNetworkInterface 创建两个 NIC。Create two NICs with New-AzNetworkInterface. 将其中一个 NIC 附加到前端子网,将另一个 NIC 附加到后端子网。Attach one NIC to the front-end subnet and one NIC to the back-end subnet. 以下示例创建名为 myNic1myNic2 的 NIC:The following example creates NICs named myNic1 and myNic2:

$frontEnd = $myVnet.Subnets|?{$_.Name -eq 'mySubnetFrontEnd'}
$myNic1 = New-AzNetworkInterface -ResourceGroupName "myResourceGroup" `
    -Name "myNic1" `
    -Location "chinaeast" `
    -SubnetId $frontEnd.Id

$backEnd = $myVnet.Subnets|?{$_.Name -eq 'mySubnetBackEnd'}
$myNic2 = New-AzNetworkInterface -ResourceGroupName "myResourceGroup" `
    -Name "myNic2" `
    -Location "chinaeast" `
    -SubnetId $backEnd.Id

通常还会创建用于筛选流向 VM 的网络流量的网络安全组和用于在多个 VM 间分配流量的负载均衡器Typically you also create a network security group to filter network traffic to the VM and a load balancer to distribute traffic across multiple VMs.

创建虚拟机Create the virtual machine

立即开始构建 VM 配置。Now start to build your VM configuration. 每种 VM 大小限制了可添加到 VM 的 NIC 数目。Each VM size has a limit for the total number of NICs that you can add to a VM. 有关详细信息,请参阅 Windows VM 大小For more information, see Windows VM sizes.

  1. 将 VM 凭据设置为 $cred 变量,如下所示:Set your VM credentials to the $cred variable as follows:

    $cred = Get-Credential
    
  2. 通过 New-AzVMConfig 定义你的 VM。Define your VM with New-AzVMConfig. 以下示例定义名为 myVM 的 VM,并使用支持两个以上 NIC 的 VM 大小(Standard_DS3_v2):The following example defines a VM named myVM and uses a VM size that supports more than two NICs (Standard_DS3_v2):

    $vmConfig = New-AzVMConfig -VMName "myVM" -VMSize "Standard_DS3_v2"
    
  3. 通过 Set-AzVMOperatingSystemSet-AzVMSourceImage 创建 VM 配置的其余部分。Create the rest of your VM configuration with Set-AzVMOperatingSystem and Set-AzVMSourceImage. 以下示例创建一个 Windows Server 2016 Datacenter:The following example creates a Windows Server 2016 Datacenter:

    $vmConfig = Set-AzVMOperatingSystem -VM $vmConfig `
        -Windows `
        -ComputerName "myVM" `
        -Credential $cred `
        -ProvisionVMAgent `
        -EnableAutoUpdate
    $vmConfig = Set-AzVMSourceImage -VM $vmConfig `
        -PublisherName "MicrosoftWindowsServer" `
        -Offer "WindowsServer" `
        -Skus "2016-Datacenter" `
        -Version "latest"
    
  4. 通过 Add-AzVMNetworkInterface 附加之前创建的两个 NIC:Attach the two NICs that you previously created with Add-AzVMNetworkInterface:

    $vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $myNic1.Id -Primary
    $vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $myNic2.Id
    
  5. 使用 New-AzVM 创建 VM:Create your VM with New-AzVM:

    New-AzVM -VM $vmConfig -ResourceGroupName "myResourceGroup" -Location "chinaeast"
    
  6. 通过完成为多个 NIC 配置操作系统中的步骤,将辅助 NIC 的路由添加到 OS。Add routes for secondary NICs to the OS by completing the steps in Configure the operating system for multiple NICs.

向现有 VM 添加 NICAdd a NIC to an existing VM

若要向现有 VM 添加虚拟 NIC,解除分配 VM,添加虚拟 NIC,并启动 VM。To add a virtual NIC to an existing VM, you deallocate the VM, add the virtual NIC, then start the VM. 不同的 VM 大小支持不同数目的 NIC,因此请相应地调整 VM 的大小。Different VM sizes support a varying number of NICs, so size your VM accordingly. 如果需要,可调整 VM 的大小If needed, you can resize a VM.

  1. 通过 Stop-AzVM 解除分配 VM。Deallocate the VM with Stop-AzVM. 以下示例解除分配 myResourceGroup 中名为 myVM 的 VM:The following example deallocates the VM named myVM in myResourceGroup:

    Stop-AzVM -Name "myVM" -ResourceGroupName "myResourceGroup"
    
  2. 通过 Get-AzVm 获取 VM 的现有配置。Get the existing configuration of the VM with Get-AzVm. 以下示例从 myResourceGroup 中获取名为 myVM 的 VM 的信息:The following example gets information for the VM named myVM in myResourceGroup:

    $vm = Get-AzVm -Name "myVM" -ResourceGroupName "myResourceGroup"
    
  3. 以下示例通过 New-AzNetworkInterface 创建附加到 mySubnetBackEnd 的名为 myNic3 的虚拟 NIC。The following example creates a virtual NIC with New-AzNetworkInterface named myNic3 that is attached to mySubnetBackEnd. 然后,通过 Add-AzVMNetworkInterface 将该虚拟 NIC 附加到 myResourceGroup 中名为 myVM 的 VM:The virtual NIC is then attached to the VM named myVM in myResourceGroup with Add-AzVMNetworkInterface:

    # Get info for the back end subnet
    $myVnet = Get-AzVirtualNetwork -Name "myVnet" -ResourceGroupName "myResourceGroup"
    $backEnd = $myVnet.Subnets|?{$_.Name -eq 'mySubnetBackEnd'}
    
    # Create a virtual NIC
    $myNic3 = New-AzNetworkInterface -ResourceGroupName "myResourceGroup" `
        -Name "myNic3" `
        -Location "chinaeast" `
        -SubnetId $backEnd.Id
    
    # Get the ID of the new virtual NIC and add to VM
    $nicId = (Get-AzNetworkInterface -ResourceGroupName "myResourceGroup" -Name "MyNic3").Id
    Add-AzVMNetworkInterface -VM $vm -Id $nicId | Update-AzVm -ResourceGroupName "myResourceGroup"
    

    主虚拟 NICPrimary virtual NICs

    具有多个 NIC 的 VM 上其中一个需为主 NIC。One of the NICs on a multi-NIC VM needs to be primary. 如果 VM 上现有虚拟 NIC 之一已设置为主 NIC,则可跳过此步骤。If one of the existing virtual NICs on the VM is already set as primary, you can skip this step. 以下示例假设 VM 上现在存在两个虚拟NIC,并且想要将第一个 NIC ([0]) 添加为主 NIC:The following example assumes that two virtual NICs are now present on a VM and you wish to add the first NIC ([0]) as the primary:

    # List existing NICs on the VM and find which one is primary
    $vm.NetworkProfile.NetworkInterfaces
    
    # Set NIC 0 to be primary
    $vm.NetworkProfile.NetworkInterfaces[0].Primary = $true
    $vm.NetworkProfile.NetworkInterfaces[1].Primary = $false
    
    # Update the VM state in Azure
    Update-AzVM -VM $vm -ResourceGroupName "myResourceGroup"
    
  4. 通过 Start-AzVm 启动 VM:Start the VM with Start-AzVm:

    Start-AzVM -ResourceGroupName "myResourceGroup" -Name "myVM"
    
  5. 通过完成为多个 NIC 配置操作系统中的步骤,将辅助 NIC 的路由添加到 OS。Add routes for secondary NICs to the OS by completing the steps in Configure the operating system for multiple NICs.

从现有 VM 中删除 NICRemove a NIC from an existing VM

若要从现有 VM 中删除虚拟 NIC,解除分配 VM,删除虚拟 NIC,并启动 VM。To remove a virtual NIC from an existing VM, you deallocate the VM, remove the virtual NIC, then start the VM.

  1. 通过 Stop-AzVM 解除分配 VM。Deallocate the VM with Stop-AzVM. 以下示例解除分配 myResourceGroup 中名为 myVM 的 VM:The following example deallocates the VM named myVM in myResourceGroup:

    Stop-AzVM -Name "myVM" -ResourceGroupName "myResourceGroup"
    
  2. 通过 Get-AzVm 获取 VM 的现有配置。Get the existing configuration of the VM with Get-AzVm. 以下示例从 myResourceGroup 中获取名为 myVM 的 VM 的信息:The following example gets information for the VM named myVM in myResourceGroup:

    $vm = Get-AzVm -Name "myVM" -ResourceGroupName "myResourceGroup"
    
  3. 通过 Get-AzNetworkInterface 获取有关删除 NIC 的信息。Get information about the NIC remove with Get-AzNetworkInterface. 以下示例获取有关“myNic3”的信息:The following example gets information about myNic3:

    # List existing NICs on the VM if you need to determine NIC name
    $vm.NetworkProfile.NetworkInterfaces
    
    $nicId = (Get-AzNetworkInterface -ResourceGroupName "myResourceGroup" -Name "myNic3").Id   
    
  4. 通过 Remove-AzVMNetworkInterface 删除 NIC,然后通过 Update-AzVm 更新 VM。Remove the NIC with Remove-AzVMNetworkInterface and then update the VM with Update-AzVm. 以下示例删除上一步中由 $nicId 获得的“myNic3”:The following example removes myNic3 as obtained by $nicId in the preceding step:

    Remove-AzVMNetworkInterface -VM $vm -NetworkInterfaceIDs $nicId | `
        Update-AzVm -ResourceGroupName "myResourceGroup"
    
  5. 通过 Start-AzVm 启动 VM:Start the VM with Start-AzVm:

    Start-AzVM -Name "myVM" -ResourceGroupName "myResourceGroup"
    

使用模板创建多个 NICCreate multiple NICs with templates

使用 Azure 资源管理器模板可在部署期间创建资源的多个实例,例如,创建多个 NIC。Azure Resource Manager templates provide a way to create multiple instances of a resource during deployment, such as creating multiple NICs. 资源管理器模板使用声明性 JSON 文件来定义环境。Resource Manager templates use declarative JSON files to define your environment. 有关详细信息,请参阅 Azure 资源管理器概述For more information, see overview of Azure Resource Manager. 使用“copy”指定要创建的实例数:You can use copy to specify the number of instances to create:

"copy": {
    "name": "multiplenics",
    "count": "[parameters('count')]"
}

有关详细信息,请参阅使用“copy”创建多个实例For more information, see creating multiple instances by using copy.

还可以使用 copyIndex() 在资源名称后面追加一个数字。You can also use copyIndex() to append a number to a resource name. 然后可创建“myNic1”、“MyNic2”等 。You can then create myNic1, MyNic2 and so on. 以下代码演示了追加索引值的示例:The following code shows an example of appending the index value:

"name": "[concat('myNic', copyIndex())]", 

可以阅读有关使用 Resource Manager 模板创建多个 NIC 的完整示例。You can read a complete example of creating multiple NICs by using Resource Manager templates.

通过完成为多个 NIC 配置操作系统中的步骤,将辅助 NIC 的路由添加到 OS。Add routes for secondary NICs to the OS by completing the steps in Configure the operating system for multiple NICs.

为多个 NIC 配置来宾 OSConfigure guest OS for multiple NICs

Azure 会将默认网关分配给附加到虚拟机的第一个(主)网络接口。Azure assigns a default gateway to the first (primary) network interface attached to the virtual machine. Azure 不会将默认网关分配给附加到虚拟机的其他(辅助)网络接口。Azure does not assign a default gateway to additional (secondary) network interfaces attached to a virtual machine. 因此,默认情况下无法与辅助网络接口所在子网的外部资源进行通信。Therefore, you are unable to communicate with resources outside the subnet that a secondary network interface is in, by default. 但是,辅助网络接口可以与子网外部的资源进行通信,尽管对不同操作系统而言,启用通信的步骤有所不同。Secondary network interfaces can, however, communicate with resources outside their subnet, though the steps to enable communication are different for different operating systems.

  1. 从 Windows 命令提示符下,运行 route print 命令,这将返回类似于以下虚拟机输出的输出,该虚拟机包含两个附加的网络接口:From a Windows command prompt, run the route print command, which returns output similar to the following output for a virtual machine with two attached network interfaces:

    ===========================================================================
    Interface List
    3...00 0d 3a 10 92 ce ......Microsoft Hyper-V Network Adapter #3
    7...00 0d 3a 10 9b 2a ......Microsoft Hyper-V Network Adapter #4
    ===========================================================================
    

    在本例中,Microsoft Hyper-V 网络适配器 #4(接口 7)是辅助网络接口,系统不会向其分配默认网关。In this example, Microsoft Hyper-V Network Adapter #4 (interface 7) is the secondary network interface that doesn't have a default gateway assigned to it.

  2. 从命令提示符处,运行 ipconfig 命令查看分配给辅助网络接口的 IP 地址。From a command prompt, run the ipconfig command to see which IP address is assigned to the secondary network interface. 在本例中,192.168.2.4 被分配到接口 7。In this example, 192.168.2.4 is assigned to interface 7. 辅助网络接口没有返回任何默认网关地址。No default gateway address is returned for the secondary network interface.

  3. 若要将发往辅助网络接口子网外部地址的所有流量路由到子网网关,请运行以下命令:To route all traffic destined for addresses outside the subnet of the secondary network interface to the gateway for the subnet, run the following command:

    route add -p 0.0.0.0 MASK 0.0.0.0 192.168.2.1 METRIC 5015 IF 7
    

    子网的网关地址是为该子网定义的地址范围中的第一个 IP 地址(以 .1 结尾)。The gateway address for the subnet is the first IP address (ending in .1) in the address range defined for the subnet. 如果不想路由子网外部的所有流量,可改为向特定目标添加单独的路由。If you don't want to route all traffic outside the subnet, you could add individual routes to specific destinations, instead. 例如,如果只想将流量从辅助网络接口路由到 192.168.3.0 网络,请输入以下命令:For example, if you only wanted to route traffic from the secondary network interface to the 192.168.3.0 network, you enter the command:

    route add -p 192.168.3.0 MASK 255.255.255.0 192.168.2.1 METRIC 5015 IF 7
    
  4. 例如,若要确认与 192.168.3.0 网络中资源的通信是否成功,请输入以下命令使用接口 7 (192.168.2.4) 对 192.168.3.4 执行 ping 操作:To confirm successful communication with a resource on the 192.168.3.0 network, for example, enter the following command to ping 192.168.3.4 using interface 7 (192.168.2.4):

    ping 192.168.3.4 -S 192.168.2.4
    

    可能需要使用以下命令通过正在 ping 的设备的 Windows 防火墙打开 ICMP:You may need to open ICMP through the Windows firewall of the device you're pinging with the following command:

    netsh advfirewall firewall add rule name=Allow-ping protocol=icmpv4 dir=in action=allow
    
  5. 若要确认已添加的路由是否在路由表中,请输入 route print 命令,它将返回一个类似于以下文本的输出:To confirm the added route is in the route table, enter the route print command, which returns output similar to the following text:

    ===========================================================================
    Active Routes:
    Network Destination        Netmask          Gateway       Interface  Metric
              0.0.0.0          0.0.0.0      192.168.1.1      192.168.1.4     15
              0.0.0.0          0.0.0.0      192.168.2.1      192.168.2.4   5015
    

    “网关”下列出的路由 192.168.1.1 是主网络接口的默认路由。The route listed with 192.168.1.1 under Gateway, is the route that is there by default for the primary network interface. “网关”下列出的路由 192.168.2.1 是你所添加的路由。The route with 192.168.2.1 under Gateway, is the route you added.

后续步骤Next steps

尝试创建具有多个 NIC 的 VM 时,请查看 Windows VM 大小Review Windows VM sizes when you're trying to create a VM that has multiple NICs. 注意每个 VM 大小支持的 NIC 数目上限。Pay attention to the maximum number of NICs that each VM size supports.