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

保留 IP 地址(经典)Reserved IP addresses (Classic)

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 和经典。 本文介绍使用经典部署模型。 Microsoft 建议大多数新部署使用资源管理器模型。 了解如何使用 Resource Manager 部署模型保留静态公共 IP 地址。

若要详细了解 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.

常见问题FAQ

  1. 可以将保留 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.
  2. 我可以有多少个保留 IP?How many reserved IPs can I have?
    有关详细信息,请参阅 Azure 限制一文。For details, see the Azure limits article.
  3. 保留 IP 是否收费?Is there a charge for reserved IPs?
    有时。Sometimes. 有关定价详细信息,请参阅保留 IP 地址定价详细信息For pricing details, see the Reserved IP Address Pricing Details page.
  4. 如何保留某个 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.
  5. 我是否可将保留 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

确保已根据安装和配置 PowerShell 一文中的步骤安装和配置 PowerShell。Ensure you have installed and configured PowerShell by completing the steps in the Install and configure PowerShell article.

在使用保留 IP 之前,必须先将其添加到订阅。Before you can use reserved IPs, you must add it to your subscription. 若要从美国中部位置提供的公共 IP 地址池创建保留 IP,请运行以下命令:To create a reserved IP from the pool of public IP addresses available in the Central US location, run the following command:

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 的资源组。 Azure 将其自动放置于名为默认网络的资源组中。 如果使用 Azure 门户创建保留 IP,可指定所选的任何资源组。 但是,如果在默认网络以外的资源组中创建保留 IP,每当使用 Get-AzureReservedIPRemove-AzureReservedIP 等命令引用保留 IP 时,必须引用名称“Group resource-group-name reserved-ip-name”。 例如,如果在名为 myResourceGroup 的资源组中创建名为 myReservedIP 的保留 IP,必须将保留 IP 的名称引用为“Group myResourceGroup myReservedIP”。

某个 IP 成为保留 IP 后,它就会始终与订阅相关联,直至将它删除。Once an IP is reserved, it remains associated to your subscription until you delete it. 若要删除保留 IP,请运行以下 PowerShell 命令:To delete a reserved IP, run the following PowerShell command:

Remove-AzureReservedIP -ReservedIPName "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 地址,请运行以下 PowerShell 命令:To reserve the IP address of a cloud service TestService in the Central US location, run the following PowerShell command:

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

将保留 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.

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,以便进行入站通信。 使用保留 IP 并不意味着可以直接连接到 VM。 保留 IP 将分配给 VM 所部署到的云服务。 如果想要直接通过 IP 连接到 VM,则必须配置实例层级公共 IP。 实例层级公共 IP 是一类可直接分配给 VM 的公共 IP(称为 ILPIP)。 它不能保留。 有关详细信息,请参阅实例层级公共 IP (ILPIP) 一文。

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

若要删除添加到新云服务中的保留 IP,请运行下面的 PowerShell 命令:To remove a reserved IP added to a new cloud service, run the following PowerShell command:

Remove-AzureReservedIPAssociation -ReservedIPName MyReservedIP -ServiceName TestService

备注

从正在运行的部署中删除保留 IP 并不会从订阅中删除保留 IP。 它只是释放该 IP,以供订阅中的其他资源使用。

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

以下命令将使用名为 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

使用服务配置文件将保留 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