使用网络配置文件配置虚拟网络(经典)Configure a virtual network (classic) using a network configuration file

Important

Azure 具有用于创建和处理资源的两个不同的部署模型:资源管理器部署模型和经典部署模型Azure has two different deployment models for creating and working with resources: Resource Manager and classic. 本文介绍使用经典部署模型的情况。This article covers using the classic deployment model. Azure 建议大多数新部署使用 Resource Manager 部署模型。Azure recommends that most new deployments use the Resource Manager deployment model.

可以在 Azure 经典命令行接口 (CLI) 或 Azure PowerShell 中使用网络配置文件来创建和配置虚拟网络(经典)。You can create and configure a virtual network (classic) with a network configuration file using the Azure classic command-line interface (CLI) or Azure PowerShell. 无法使用网络配置文件通过 Azure Resource Manager 部署模型创建或修改虚拟网络。You cannot create or modify a virtual network through the Azure Resource Manager deployment model using a network configuration file. 无法在 Azure 门户中使用网络配置文件来创建或修改虚拟网络(经典),但可以直接在 Azure 门户中创建虚拟网络(经典),而无需使用网络配置文件。You cannot use the Azure portal to create or modify a virtual network (classic) using a network configuration file, however you can use the Azure portal to create a virtual network (classic), without using a network configuration file.

使用网络配置文件创建和配置虚拟网络(经典)需要导出、更改和导入该文件。Creating and configuring a virtual network (classic) with a network configuration file requires exporting, changing, and importing the file.

导出网络配置文件Export a network configuration file

可以使用 PowerShell 或 Azure 经典 CLI 导出网络配置文件。You can use PowerShell or the Azure classic CLI to export a network configuration file. PowerShell 导出 XML 文件,而 Azure 经典 CLI 导出 json 文件。PowerShell exports an XML file, while the Azure classic CLI exports a json file.

PowerShellPowerShell

  1. 安装 Azure PowerShell 并登录到 AzureInstall Azure PowerShell and sign in to Azure.

  2. 在以下命令中根据需要更改目录(并确保它存在)和文件名,然后运行该命令导出网络配置文件:Change the directory (and ensure it exists) and filename in the following command as desired, then run the command to export the network configuration file:

    Get-AzureVNetConfig -ExportToFile c:\azure\networkconfig.xml
    

Azure 经典 CLIAzure classic CLI

  1. 安装 Azure 经典 CLIInstall the Azure classic CLI. 通过经典 CLI 命令提示符完成剩余的步骤。Complete the remaining steps from a classic CLI command prompt.

  2. 输入 azure login -e AzureChinaCloud 命令登录到 Azure。Log in to Azure by entering the azure login -e AzureChinaCloud command.

  3. 输入 azure config mode asm 命令确保处于 asm 模式。Ensure you're in asm mode by entering the azure config mode asm command.

  4. 在以下命令中根据需要更改目录(并确保它存在)和文件名,然后运行该命令导出网络配置文件:Change the directory (and ensure it exists) and filename in the following command as desired, then run the command to export the network configuration file:

    azure network export c:\azure\networkconfig.json
    

创建或修改网络配置文件Create or modify a network configuration file

网络配置文件是一个 XML 文件(使用 PowerShell 时)或一个 json 文件(使用经典 CLI 时)。A network configuration file is an XML file (when using PowerShell) or a json file (when using the classic CLI). 可在任何文本或 XML/json 编辑器中编辑该文件。You can edit the file in any text, or XML/json editor. 网络配置文件架构设置一文中提供了所有设置的详细信息。The Network configuration file schema settings article includes details for all settings. 有关设置的更多说明,请参阅查看虚拟网络和设置For additional explanation of the settings, see View virtual networks and settings. 对文件所做的更改:The changes you make to the file:

  • 必须符合架构,否则导入网络配置文件将会失败。Must comply with the schema, or importing the network configuration file will fail.
  • 覆盖订阅的所有现有网络设置,因此,在修改时请格外小心。Overwrite any existing network settings for your subscription, so use extreme caution when making modifications. 例如,请参考以下示例网络配置文件。For example, reference the example network configuration files that follow. 假设原始文件包含两个 VirtualNetworkSite 实例,而我们已按示例中所示更改了此文件。Say the original file contained two VirtualNetworkSite instances, and you changed it, as shown in the examples. 导入文件时,Azure 会删除我们在该文件中删除的 VirtualNetworkSite 的虚拟网络实例。When you import the file, Azure deletes the virtual network for the VirtualNetworkSite instance you removed in the file. 这种简化方案假设虚拟网络中没有任何资源,如果有,则无法删除虚拟网络,并且导入将会失败。This simplified scenario assumes no resources were in the virtual network, as if there were, the virtual network could not be deleted, and the import would fail.

Important

Azure 会将部署有项目的子网视为“使用中”。Azure considers a subnet that has something deployed to it as in use. 当某个子网处于“使用中”状态时,不能对其进行修改。When a subnet is in use, it cannot be modified. 在修改网络配置文件中的子网信息之前,请将已部署到子网的任何内容移到不会进行修改的其他子网。Before modifying subnet information in a network configuration file, move anything that you have deployed to the subnet to a different subnet that isn't being modified. 有关详细信息,请参阅将 VM 或角色实例移到其他子网See Move a VM or Role Instance to a Different Subnet for details.

用于 PowerShell 的示例 XMLExample XML for use with PowerShell

以下示例网络配置文件在“中国东部”Azure 区域创建名为 myVirtualNetwork、地址空间为 10.0.0.0/16 的虚拟网络。 The following example network configuration file creates a virtual network named myVirtualNetwork with an address space of 10.0.0.0/16 in the China East Azure region. 该虚拟网络包含一个名为 mySubnet、地址前缀为 10.0.0.0/24 的子网。The virtual network contains one subnet named mySubnet with an address prefix of 10.0.0.0/24.

<?xml version="1.0" encoding="utf-8"?>
<NetworkConfiguration xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/ServiceHosting/2011/07/NetworkConfiguration">
  <VirtualNetworkConfiguration>
    <Dns />
    <VirtualNetworkSites>
      <VirtualNetworkSite name="myVirtualNetwork" Location="China East">
        <AddressSpace>
          <AddressPrefix>10.0.0.0/16</AddressPrefix>
        </AddressSpace>
        <Subnets>
          <Subnet name="mySubnet">
            <AddressPrefix>10.0.0.0/24</AddressPrefix>
          </Subnet>
        </Subnets>
      </VirtualNetworkSite>
    </VirtualNetworkSites>
  </VirtualNetworkConfiguration>
</NetworkConfiguration>

如果导出的网络配置文件不包含任何内容,则可以复制上述示例中的 XML,并将其粘贴到新文件中。If the network configuration file you exported contains no contents, you can copy the XML in the previous example, and paste it into a new file.

用于经典 CLI 的示例 JSONExample JSON for use with the classic CLI

以下示例网络配置文件在“中国东部”Azure 区域创建名为 myVirtualNetwork、地址空间为 10.0.0.0/16 的虚拟网络。 The following example network configuration file creates a virtual network named myVirtualNetwork with an address space of 10.0.0.0/16 in the China East Azure region. 该虚拟网络包含一个名为 mySubnet、地址前缀为 10.0.0.0/24 的子网。The virtual network contains one subnet named mySubnet with an address prefix of 10.0.0.0/24.

{
   "VirtualNetworkConfiguration" : {
      "Dns" : "",
      "VirtualNetworkSites" : [
         {
            "AddressSpace" : [ "10.0.0.0/16" ],
            "Location" : "China East",
            "Name" : "myVirtualNetwork",
            "Subnets" : [
               {
                  "AddressPrefix" : "10.0.0.0/24",
                  "Name" : "mySubnet"
               }
            ]
         }
      ]
   }
}

如果导出的网络配置文件不包含任何内容,则可以复制上述示例中的 json,并将其粘贴到新文件中。If the network configuration file you exported contains no contents, you can copy the json in the previous example, and paste it into a new file.

导入网络配置文件Import a network configuration file

可以使用 PowerShell 或经典 CLI 导入网络配置文件。You can use PowerShell or the classic CLI to import a network configuration file. PowerShell 导入 XML 文件,而经典 CLI 导入 json 文件。PowerShell imports an XML file, while the classic CLI imports a json file. 如果导入失败,请确认该文件是否符合网络配置架构If the import fails, confirm that the file complies with the network configuration schema.

PowerShellPowerShell

  1. 安装 Azure PowerShell 并登录到 AzureInstall Azure PowerShell and sign in to Azure.

  2. 在以下命令中根据需要更改目录和文件名,然后运行该命令导入网络配置文件:Change the directory and filename in the following command as necessary, then run the command to import the network configuration file:

    Set-AzureVNetConfig  -ConfigurationPath c:\azure\networkconfig.xml
    

Azure 经典 CLIAzure classic CLI

  1. 安装 Azure 经典 CLIInstall the Azure classic CLI. 通过经典 CLI 命令提示符完成剩余的步骤。Complete the remaining steps from a classic CLI command prompt.

  2. 输入 azure login -e AzureChinaCloud 命令登录到 Azure。Log in to Azure by entering the azure login -e AzureChinaCloud command.

  3. 输入 azure config mode asm 命令确保处于 asm 模式。Ensure you're in asm mode by entering the azure config mode asm command.

  4. 在以下命令中根据需要更改目录和文件名,然后运行该命令导入网络配置文件:Change the directory and filename in the following command as necessary, then run the command to import the network configuration file:

    azure network import c:\azure\networkconfig.json