重要
2027 年 9 月 30 日,网络安全组(NSG)流日志将停用。 作为此停用的一部分,从 2025 年 6 月 30 日起,您将无法再创建新的 NSG 流日志。 建议迁移到虚拟网络流日志,从而克服 NSG 流日志的限制。 停用日期后,将不再支持使用 NSG 流日志启用的流量分析,并且订阅中的现有 NSG 流日志资源将被删除。 但是,不会删除 NSG 流日志记录,并且将继续遵循其各自的保留策略。 有关详细信息,请查看官方公告。
本快速入门介绍如何使用 Azure 资源管理器模板 (ARM) 模板和 Azure PowerShell 启用 NSG 流日志。 有关详细信息,请参阅 Azure 资源管理器是什么?和 NSG 流日志概述。
Azure 资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。
首先,我们将概述 NSG 流日志对象的属性。 我们将提供示例模板。 然后,我们将使用本地 Azure PowerShell 实例部署模板。
如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。 模板将在 Azure 门户中打开。
具有活动订阅的 Azure 帐户。 如果您还没有,请在开始之前创建一个试用订阅。
本快速入门中使用的模板来自 Azure 快速入门模板。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.9.1.41621",
"templateHash": "14580725600461536175"
}
},
"parameters": {
"networkWatcherName": {
"type": "string",
"defaultValue": "[format('NetworkWatcher_{0}', parameters('location'))]",
"metadata": {
"description": "Name of the Network Watcher attached to your subscription. Format: NetworkWatcher_<region_name>"
}
},
"flowLogName": {
"type": "string",
"defaultValue": "FlowLog1",
"metadata": {
"description": "Name of your Flow log resource"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Region where you resources are located"
}
},
"existingNSG": {
"type": "string",
"metadata": {
"description": "Resource ID of the target NSG"
}
},
"retentionDays": {
"type": "int",
"defaultValue": 0,
"maxValue": 365,
"minValue": 0,
"metadata": {
"description": "Retention period in days. Default is zero which stands for permanent retention. Can be any Integer from 0 to 365"
}
},
"flowLogsVersion": {
"type": "int",
"defaultValue": 2,
"allowedValues": [
1,
2
],
"metadata": {
"description": "FlowLogs Version. Correct values are 1 or 2 (default)"
}
},
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_ZRS"
],
"metadata": {
"description": "Storage Account type"
}
}
},
"variables": {
"storageAccountName": "[format('flowlogs{0}', uniqueString(resourceGroup().id))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-09-01",
"name": "[variables('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"kind": "StorageV2",
"properties": {}
},
{
"type": "Microsoft.Network/networkWatchers",
"apiVersion": "2022-01-01",
"name": "[parameters('networkWatcherName')]",
"location": "[parameters('location')]",
"properties": {}
},
{
"type": "Microsoft.Network/networkWatchers/flowLogs",
"apiVersion": "2022-01-01",
"name": "[format('{0}/{1}', parameters('networkWatcherName'), parameters('flowLogName'))]",
"location": "[parameters('location')]",
"properties": {
"targetResourceId": "[parameters('existingNSG')]",
"storageId": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
"enabled": true,
"retentionPolicy": {
"days": "[parameters('retentionDays')]",
"enabled": true
},
"format": {
"type": "JSON",
"version": "[parameters('flowLogsVersion')]"
}
},
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
]
}
]
}
该模板中定义了以下资源:
- Microsoft.Storage/storageAccounts
- Microsoft.Network 网络观察器
- Microsoft.Network networkWatchers/flowLogs
前面的示例中突出显示的代码显示了 NSG 流日志资源定义。
本教程假定你拥有现有资源组和可以启用流日志记录的 NSG。
你可以在本地将本文所示的任意示例模板另存为 azuredeploy.json。 更新属性值,使其指向订阅中的有效资源。
若要部署模板,请在 Azure PowerShell 中运行以下命令:
$context = Get-AzSubscription -SubscriptionId <subscription Id>
Set-AzContext $context
New-AzResourceGroupDeployment -Name EnableFlowLog -ResourceGroupName NetworkWatcherRG `
-TemplateFile "C:\MyTemplates\azuredeploy.json"
备注
这些命令会将资源部署到 NetworkWatcherRG 资源组,而不是包含网络安全组的资源组。
可通过两个选项来查看部署是否成功:
- PowerShell 控制台将
ProvisioningState
显示为Succeeded
。 - 转到 NSG 流日志门户页确认所做的更改。
如果部署出现问题,请参阅排查使用 Azure 资源管理器时的常见 Azure 部署错误。
可以使用完整部署模式删除 Azure 资源。 要删除流日志资源,请在完整模式下指定部署,但不包含要删除的资源。 详细了解完整部署模式。
还可以在 Azure 门户中禁用或删除流日志:
在门户顶部的搜索框中,输入“网络观察程序”。 在搜索结果中,选择“网络监视器”。
在“日志”下,选择“流日志”。
在“网络观察程序|流日志”中,选择要删除的流日志的复选框。
本快速入门介绍了如何使用 ARM 模板来启用 NSG 流日志。 接下来,了解如何使用流量分析直观显示 NSG 流数据: