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

使用 Azure PowerShell 配置公共 IP 地址的路由首选项Configure routing preference for a public IP address using Azure PowerShell

本文介绍了如何使用 Azure PowerShell 为公共 IP 地址配置通过 ISP 网络路由流量的路由首选项(即“Internet”选项)。This article shows you how to configure routing preference via ISP network (Internet option) for a public IP address using Azure PowerShell. 创建公共 IP 地址后,可以将它与以下 Azure 资源关联,以用于 Internet 的入站和出站流量:After creating the public IP address, you can associate it with the following Azure resources for inbound and outbound traffic to the internet:

  • 虚拟机Virtual machine
  • 虚拟机规模集Virtual machine scale set
  • Azure Kubernetes 服务 (AKS)Azure Kubernetes Service (AKS)
  • 面向 Internet 的负载均衡器Internet-facing load balancer
  • 应用程序网关Application Gateway
  • Azure 防火墙Azure Firewall

默认情况下,对于所有 Azure 服务,公共 IP 地址的路由首选项设置为“Microsoft 全球网络”,并且可以与任何 Azure 服务相关联。By default, the routing preference for public IP address is set to the Microsoft global network for all Azure services and can be associated with any Azure service.

重要

路由首选项目前为公共预览版。Routing preference is currently in public preview. 此预览版在提供时没有附带服务级别协议,不建议将其用于生产工作负荷。This preview version is provided without a service level agreement, and it's not recommended for production workloads. 某些功能可能不受支持或者受限。Certain features might not be supported or might have constrained capabilities. 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款For more information, see Supplemental Terms of Use for Microsoft Azure Previews.

如果还没有 Azure 订阅,请现在就创建一个免费帐户If you don't have an Azure subscription, create a free account now.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。You can use either Bash or PowerShell with Cloud Shell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To start Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。Select Try It in the upper-right corner of a code block. 选择“试用”不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell”按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Start Cloud Shell.

  2. 选择代码块上的“复制”按钮以复制代码。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上选择 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. 选择 Enter 运行此代码。Select Enter to run the code.

如果选择在本地安装和使用 PowerShell,则本文需要 Azure PowerShell 模块 6.9.0 或更高版本。If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 6.9.0 or later. 运行 Get-Module -ListAvailable Az 查找已安装的版本。Run Get-Module -ListAvailable Az to find the installed version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount 来创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.

创建资源组Create a resource group

使用 New-AzResourceGroup 创建资源组。Create a resource group with New-AzResourceGroup. 此示例在 eastus 位置中创建一个名为 myResourceGroup 的资源组:This example creates a resource group named myResourceGroup in the eastus location:

$rg = New-AzResourceGroup -Name myResourceGroup -Location EastUS

创建具有“Internet”路由首选项的公共 IPCreate a Public IP with Internet routing preference

以下命令在美国东部 Azure 区域新建路由首选项类型为“Internet”的公共 IP:The following command creates a new public IP with a routing preference type as Internet in the East US Azure region:

$iptagtype="RoutingPreference"
$tagName = "Internet"
$ipTag = New-AzPublicIpTag -IpTagType $iptagtype -Tag $tagName 
# attach the tag
$publicIp = New-AzPublicIpAddress  `
-Name "MyPublicIP" `
-ResourceGroupName $rg.ResourceGroupName `
-Location $rg.Location `
-IpTag $ipTag `
-AllocationMethod Static `
-Sku Standard `
-IpAddressVersion IPv4

可以将上面创建的公共 IP 地址与 WindowsLinux 虚拟机相关联。You can associate the above created public IP address with a Windows or Linux virtual machine. 请参阅“教程”页上的“CLI”部分:将公共 IP 地址与虚拟机相关联,以将公共 IP 关联到 VM。Use the CLI section on the tutorial page: Associate a public IP address to a virtual machine to associate the Public IP to your VM. 也可以将上面创建的公共 IP 地址与 Azure 负载均衡器相关联,具体方法为将它分配到负载均衡器前端配置。You can also associate the public IP address created above with with an Azure Load Balancer, by assigning it to the load balancer frontend configuration. 此公共 IP 地址充当负载均衡型虚拟 IP 地址 (VIP)。The public IP address serves as a load-balanced virtual IP address (VIP).

清理资源Clean up resources

如果不再需要资源组、VM 和所有相关资源,可以使用 Remove-AzResourceGroup 命令来删除它们。If no longer needed, you can use the Remove-AzResourceGroup command to remove the resource group, VM, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup

后续步骤Next steps