您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

保留 IP 地址(经典部署)Reserved IP addresses (classic deployment)

Azure 中的 IP 地址分为两类:动态 IP 地址和保留 IP 地址。IP addresses in Azure fall into two categories: dynamic and reserved. 由 Azure 管理的公共 IP 地址默认为动态 IP 地址。Public IP addresses managed by Azure are dynamic by default. 这意味着,用于给定云服务的 IP 地址 (VIP) 或用于直接访问 VM 或角色实例的 IP 地址 (ILPIP) 可能会在关闭资源或停止(释放)资源的情况下不时进行更改。That means that the IP address used for a given cloud service (VIP) or to access a VM or role instance directly (ILPIP) can change from time to time, when resources are shut down or stopped (deallocated).

要防止 IP 地址更改,可将其设置为保留 IP 地址。To prevent IP addresses from changing, you can reserve an IP address. 保留 IP 只能用作 VIP,可确保云服务的 IP 地址即使在关闭资源或停止(释放)资源的情况下也保持不变。Reserved IPs can be used only as a VIP, ensuring that the IP address for the cloud service remains the same, even as resources are shut down or stopped (deallocated). 此外,还可以将用作 VIP 的现有动态 IP 转换为保留 IP 地址。Furthermore, you can convert existing dynamic IPs used as a VIP to a reserved IP address.

重要

Azure 具有用于创建和处理资源的两个不同的部署模型:Resource Manager 和经典Azure has two different deployment models for creating and working with resources: Resource Manager and classic. 本文介绍使用经典部署模型。This article covers using the classic deployment model. Microsoft 建议大多数新部署使用资源管理器模型。Microsoft recommends that most new deployments use the Resource Manager model. 了解如何使用 Resource Manager 部署模型保留静态公共 IP 地址。Learn how to reserve a static public IP address using the Resource Manager deployment model.

若要详细了解 Azure 中的 IP 地址,请阅读 IP 地址一文。To learn more about IP addresses in Azure, read the IP addresses article.

何时需要保留 IP?When do I need a reserved IP?

  • 想要确保将 IP 保留在订阅中You want to ensure that the IP is reserved in your subscription. 如果想要保留一个 IP 地址,使得该 IP 地址在任何情况下都不会从订阅中释放,则应使用保留的公共 IP。If you want to reserve an IP address that is not released from your subscription under any circumstance, you should use a reserved public IP.
  • 想要 IP 始终与云服务相关联,即使 VM 处于停止或释放状态下You want your IP to stay with your cloud service even across stopped or deallocated state (VMs). 如果想要通过即使在云服务中的 VM 处于关闭或停止(释放)状态下也不会更改的 IP 地址来访问服务。If you want your service to be accessed by using an IP address that doesn't change, even when VMs in the cloud service are shut down or stop (deallocated).
  • 想要确保 Azure 的出站流量使用可预测的 IP 地址You want to ensure that outbound traffic from Azure uses a predictable IP address. 可以将本地防火墙配置为仅允许来自特定 IP 地址的流量。You may have your on-premises firewall configured to allow only traffic from specific IP addresses. 通过保留 IP,可以了解源 IP 地址,无需因为 IP 更改而更新防火墙规则。By reserving an IP, you know the source IP address, and don't need to update your firewall rules due to an IP change.

常见问题解答FAQs

  • 可以将保留 IP 用于所有 Azure 服务吗?Can I use a reserved IP for all Azure services? 不是。No. 保留 IP 只能用于通过 VIP 公开的 VM 和云服务实例角色。Reserved IPs can only be used for VMs and cloud service instance roles exposed through a VIP.
  • 我可以有多少个保留 IP?How many reserved IPs can I have? 有关详细信息,请参阅 Azure 限制一文。For details, see the Azure limits article.
  • 保留 IP 是否收费?Is there a charge for reserved IPs? 有时。Sometimes. 有关定价详细信息,请参阅保留 IP 地址定价详细信息For pricing details, see the Reserved IP Address Pricing Details page.
  • 如何保留某个 IP 地址?How do I reserve an IP address? 可以使用 PowerShell、Azure 管理 REST APIAzure 门户在 Azure 区域中保留 IP 地址。You can use PowerShell, the Azure Management REST API, or the Azure portal to reserve an IP address in an Azure region. 保留 IP 地址将关联到订阅。A reserved IP address is associated to your subscription.
  • 我是否可将保留 IP 用于基于地缘组的 VNet?Can I use a reserved IP with affinity group-based VNets? 不是。No. 仅区域 VNet 支持保留 IP。Reserved IPs are only supported in regional VNets. 与地缘组关联的 VNet 不支持保留 IP。Reserved IPs are not supported for VNets that are associated with affinity groups. 有关如何将 VNet 与区域或地缘组关联的详细信息,请参阅关于区域 VNet 和地缘组一文。For more information about associating a VNet with a region or affinity group, see the About Regional VNets and Affinity Groups article.

管理保留 VIPManage reserved VIPs

使用 Azure PowerShell(经典)Using Azure PowerShell (classic)

在使用保留 IP 之前,必须先将其添加到订阅。Before you can use reserved IPs, you must add it to your subscription. 从美国中部位置提供的公共 IP 地址池创建保留 IP,如下所示:Create a reserved IP from the pool of public IP addresses available in the Central US location as follows:

备注

对于经典部署模型,必须安装 Azure PowerShell 的服务管理版本。For classic deployment model, you must install the Service Management version of Azure PowerShell. 有关详细信息,请参阅安装 Azure PowerShell 服务管理模块For more information, see Install the Azure PowerShell Service Management module.

  New-AzureReservedIP –ReservedIPName MyReservedIP –Location "Central US"

但请注意,不能指定要保留的具体 IP。Notice, however, that you cannot specify what IP is being reserved. 要查看订阅中哪些 IP 地址为保留 IP 地址,请运行以下 PowerShell 命令,并查看 ReservedIPNameAddress 的值:To view what IP addresses are reserved in your subscription, run the following PowerShell command, and notice the values for ReservedIPName and Address:

Get-AzureReservedIP

预期输出:Expected output:

ReservedIPName       : MyReservedIP
Address              : 23.101.114.211
Id                   : d73be9dd-db12-4b5e-98c8-bc62e7c42041
Label                :
Location             : Central US
State                : Created
InUse                : False
ServiceName          :
DeploymentName       :
OperationDescription : Get-AzureReservedIP
OperationId          : 55e4f245-82e4-9c66-9bd8-273e815ce30a
OperationStatus      : Succeeded

备注

使用 PowerShell 创建保留 IP 地址时,不能指定要在其中创建保留 IP 的资源组。When you create a reserved IP address with PowerShell, you cannot specify a resource group to create the reserved IP in. Azure 将其自动放置于名为默认网络的资源组中。Azure places it into a resource group named Default-Networking automatically. 如果使用 Azure 门户创建保留 IP,可指定所选的任何资源组。If you create the reserved IP using the Azure portal, you can specify any resource group you choose. 但是,如果在默认网络以外的资源组中创建保留 IP,每当使用 Get-AzureReservedIPRemove-AzureReservedIP 等命令引用保留 IP 时,必须引用名称“Group resource-group-name reserved-ip-name”。If you create the reserved IP in a resource group other than Default-Networking however, whenever you reference the reserved IP with commands such as Get-AzureReservedIP and Remove-AzureReservedIP, you must reference the name Group resource-group-name reserved-ip-name. 例如,如果在名为 myResourceGroup 的资源组中创建名为 myReservedIP 的保留 IP,必须将保留 IP 的名称引用为“Group myResourceGroup myReservedIP”。For example, if you create a reserved IP named myReservedIP in a resource group named myResourceGroup, you must reference the name of the reserved IP as Group myResourceGroup myReservedIP.

某个 IP 成为保留 IP 后,它就会始终与订阅相关联,直至将它删除。Once an IP is reserved, it remains associated to your subscription until you delete it. 删除保留 IP,如下所示:Delete a reserved IP as follows:

Remove-AzureReservedIP -ReservedIPName "MyReservedIP"

使用 Azure CLI(经典)Using Azure CLI (classic)

使用 Azure 经典 CLI 从美国中部位置提供的公共 IP 地址池创建保留 IP,如下所示:Create a reserved IP from the pool of public IP addresses available in the Central US location as Using Azure classic CLI follows:

备注

对于经典部署,必须使用 Azure 经典 CLI。For classic deployment, you must use Azure classic CLI. 有关安装 Azure 经典 CLI 的信息,请参阅安装 Azure 经典 CLIFor information about installing Azure classic CLI, see Install the Azure classic CLI

命令:Command:

azure network reserved-ip create <name> <location>

示例:Example:

azure network reserved-ip create MyReservedIP centralus

可以使用 Azure CLI 查看订阅中保留的 IP 地址,如下所示:You can view what IP addresses are reserved in your subscription using Azure CLI as follows:

命令:Command:

azure network reserved-ip list

某个 IP 成为保留 IP 后,它就会始终与订阅相关联,直至将它删除。Once an IP is reserved, it remains associated to your subscription until you delete it. 删除保留 IP,如下所示:Delete a reserved IP as follows:

命令:Command:

azure network reserved-ip delete <name>

示例:Example:

azure network reserved-ip delete MyReservedIP

保留现有云服务的 IP 地址Reserve the IP address of an existing cloud service

添加 -ServiceName 参数即可保留现有云服务的 IP 地址。You can reserve the IP address of an existing cloud service by adding the -ServiceName parameter. 保留美国中部位置中 TestService 云服务的 IP 地址,如下所示:Reserve the IP address of a cloud service TestService in the Central US location as follows:

  • 使用 Azure PowerShell(经典):Using Azure PowerShell (classic):

    New-AzureReservedIP –ReservedIPName MyReservedIP –Location "Central US" -ServiceName TestService
    
  • 使用 Azure CLI(经典):Using Azure CLI (classic):

    命令:Command:

     azure network reserved-ip create <name> <location> -r <service-name> -d <deployment-name>
    

    示例:Example:

      azure network reserved-ip create MyReservedIP centralus -r TestService -d asmtest8942
    

将保留 IP 关联到新的云服务Associate a reserved IP to a new cloud service

下面的脚本将创建新的保留 IP,然后将其关联到名为 TestService 的新云服务。The following script creates a new reserved IP, then associates it to a new cloud service named TestService.

使用 Azure PowerShell(经典)Using Azure PowerShell (classic)

New-AzureReservedIP –ReservedIPName MyReservedIP –Location "Central US"

$image = Get-AzureVMImage|?{$_.ImageName -like "*RightImage-Windows-2012R2-x64*"}

New-AzureVMConfig -Name TestVM -InstanceSize Small -ImageName $image.ImageName `
| Add-AzureProvisioningConfig -Windows -AdminUsername adminuser -Password MyP@ssw0rd!! `
| New-AzureVM -ServiceName TestService -ReservedIPName MyReservedIP -Location "Central US"

备注

创建用于云服务的保留 IP 时,仍需使用 VIP:<端口号> 来引用 VM,以便进行入站通信。When you create a reserved IP to use with a cloud service, you still refer to the VM by using VIP:<port number> for inbound communication. 使用保留 IP 并不意味着可以直接连接到 VM。Reserving an IP does not mean you can connect to the VM directly. 保留 IP 将分配给 VM 所部署到的云服务。The reserved IP is assigned to the cloud service that the VM has been deployed to. 如果想要直接通过 IP 连接到 VM,则必须配置实例层级公共 IP。If you want to connect to a VM by IP directly, you have to configure an instance-level public IP. 实例层级公共 IP 是一类可直接分配给 VM 的公共 IP(称为 ILPIP)。An instance-level public IP is a type of public IP (called an ILPIP) that is assigned directly to your VM. 它不能保留。It cannot be reserved. 有关详细信息,请参阅实例层级公共 IP (ILPIP) 一文。For more information, read the Instance-level Public IP (ILPIP) article.

从正在运行的部署中删除保留 IPRemove a reserved IP from a running deployment

删除添加到新的云服务的保留 IP,如下所示:Remove a reserved IP added to a new cloud service as follows:

使用 Azure PowerShell(经典)Using Azure PowerShell (classic)

Remove-AzureReservedIPAssociation -ReservedIPName MyReservedIP -ServiceName TestService

使用 Azure CLI(经典)Using Azure CLI (classic)

命令:Command:

azure network reserved-ip disassociate <name> <service-name> <deployment-name>

示例:Example:

azure network reserved-ip disassociate MyReservedIP TestService asmtest8942

备注

从正在运行的部署中删除保留 IP 并不会从订阅中删除保留 IP。Removing a reserved IP from a running deployment does not remove the reservation from your subscription. 它只是释放该 IP,以供订阅中的其他资源使用。It simply frees the IP to be used by another resource in your subscription.

若要从订阅中完全删除保留 IP,请运行以下命令:To remove a reserved IP completely from a subscription, run the following command:

命令:Command:

azure network reserved-ip delete <name>

示例:Example:

azure network reserved-ip delete MyReservedIP

将保留 IP 关联到正在运行的部署Associate a reserved IP to a running deployment

使用 Azure PowerShell(经典)Using Azure PowerShell (classic)

以下命令将使用名为 TestVM2 的新 VM 创建名为 TestService2 的云服务。The following commands create a cloud service named TestService2 with a new VM named TestVM2. 然后,名为 MyReservedIP 的现有保留 IP 将关联到云服务。The existing reserved IP named MyReservedIP is then associated to the cloud service.

$image = Get-AzureVMImage|?{$_.ImageName -like "*RightImage-Windows-2012R2-x64*"}

New-AzureVMConfig -Name TestVM2 -InstanceSize Small -ImageName $image.ImageName `
| Add-AzureProvisioningConfig -Windows -AdminUsername adminuser -Password MyP@ssw0rd!! `
| New-AzureVM -ServiceName TestService2 -Location "Central US"

Set-AzureReservedIPAssociation -ReservedIPName MyReservedIP -ServiceName TestService2

使用 Azure CLI(经典)Using Azure CLI (classic)

可以使用 Azure CLI 将新的保留 IP 关联到正在运行的云服务部署,如下所示:You can associate a new reserved IP to your running cloud service deployment using Azure CLI as follows:

命令:Command:

azure network reserved-ip associate <name> <service-name> <deployment-name>

示例:Example:

azure network reserved-ip associate MyReservedIP TestService asmtest8942

使用服务配置文件将保留 IP 关联到云服务Associate a reserved ip to a cloud service by using a service configuration file

也可以使用服务配置 (CSCFG) 文件将保留 IP 关联到云服务。You can also associate a reserved IP to a cloud service by using a service configuration (CSCFG) file. 下面的示例 xml 演示如何将云服务配置为使用名为 MyReservedIP 的保留 VIP:The following sample xml shows how to configure a cloud service to use a reserved VIP named MyReservedIP:

   <?xml version="1.0" encoding="utf-8"?>
    <ServiceConfiguration serviceName="ReservedIPSample" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2014-01.2.3">
      <Role name="WebRole1">
        <Instances count="1" />
        <ConfigurationSettings>
          <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
        </ConfigurationSettings>
      </Role>
      <NetworkConfiguration>
        <AddressAssignments>
          <ReservedIPs>
           <ReservedIP name="MyReservedIP"/>
          </ReservedIPs>
        </AddressAssignments>
      </NetworkConfiguration>
    </ServiceConfiguration>

后续步骤Next steps