你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure PowerShell 创建和配置 Azure DDoS IP 保护

开始使用 Azure PowerShell 实现 Azure DDoS IP 保护。 在本快速入门中,你将使用 PowerShell 启用 DDoS IP 保护并将其链接到公共 IP 地址。

DDoS IP 保护在保护公共 IP 地址的示意图。

先决条件

  • 具有活动订阅的 Azure 帐户。 免费创建帐户
  • 本地安装的 Azure PowerShell 或 Azure Cloud Shell
  • 如果选择在本地安装和使用 PowerShell,则本文需要 Azure PowerShell 模块 9.0.0 或更高版本。 运行 Get-Module -ListAvailable Az 查找已安装的版本。 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount 以创建与 Azure 的连接。

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。 可以使用 Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。

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

选项 示例/链接
选择代码或命令块右上角的“试用”。 选择“试用”不会自动将代码或命令复制到 Cloud Shell。 显示 Azure Cloud Shell 的“试用”示例的屏幕截图。
转到 https://shell.azure.com 或选择“启动 Cloud Shell”按钮可在浏览器中打开 Cloud Shell。 用于启动 Azure Cloud Shell 的按钮。
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。 显示 Azure 门户中的 Cloud Shell 按钮的屏幕截图

若要使用 Azure Cloud Shell,请执行以下操作:

  1. 启动 Cloud Shell。

  2. 选择代码块(或命令块)上的“复制”按钮以复制代码或命令。

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V,或在 macOS 上选择 Cmd+Shift+V 将代码或命令粘贴到 Cloud Shell 会话中。

  4. 选择“Enter”运行代码或命令。

为公共 IP 地址启用 DDoS IP 保护

可在创建公共 IP 地址时启用 DDoS IP 保护。 在本示例中,我们将公共 IP 地址命名为 myStandardPublicIP:

#Creates the resource group
New-AzResourceGroup -Name MyResourceGroup -Location eastus

#Creates the IP address and enables DDoS IP Protection
New-AzPublicIpAddress -Name myStandardPublicIP -ResourceGroupName MyResourceGroup -Sku Standard -Location "East US" -AllocationMethod Static -DdosProtectionMode Enabled   

注意

DDoS IP 保护仅在公共 IP 标准 SKU 上启用。

为现有公共 IP 地址启用 DDoS IP 保护

可关联现有的公共 IP 地址:

#Gets the public IP address
$publicIp = Get-AzPublicIpAddress -Name myStandardPublicIP -ResourceGroupName MyResourceGroup 

#Enables DDoS IP Protection for the public IP address
$publicIp.DdosSettings.ProtectionMode = 'Enabled'

#Updates public IP address
Set-AzPublicIpAddress -PublicIpAddress $publicIp

验证并测试

检查公共 IP 地址的详细信息,并验证是否已启用 DDoS IP 保护。

#Gets the public IP address
$publicIp = Get-AzPublicIpAddress -Name myStandardPublicIP -ResourceGroupName MyResourceGroup 

#Checks the status of the public IP address
$protectionMode = $publicIp.DdosSettings.ProtectionMode

#Returns the status of the public IP address
$protectionMode

对现有公共 IP 地址禁用 DDoS IP 保护

$publicIp = Get-AzPublicIpAddress -Name myStandardPublicIP -ResourceGroupName MyResourceGroup 

$publicIp.DdosSettings.ProtectionMode = 'Disabled'

Set-AzPublicIpAddress -PublicIpAddress $publicIp 

注意

将 DDoS IP 保护从“已启用”更改为“已禁用”时,公共 IP 资源的遥测将不再活动。

清理资源

可保留资源以供下一教程使用。 如果不再需要,请删除“MyResourceGroup”资源组。 删除资源组时,DDoS 防护计划及其所有相关资源也会一起删除。

Remove-AzResourceGroup -Name MyResourceGroup

后续步骤

在本快速入门中,你已创建:

  • 资源组
  • 公共 IP 地址

你已使用 Azure PowerShell 启用 DDoS IP 防护。 要了解如何查看和配置 DDoS 防护计划的遥测,请继续阅读教程。