重要
网络安全组(NSG)流日志将于 2027 年 9 月 30 日停用。 由于该功能即将退役,它不再支持新的 NSG 流日志创建。 迁移 到 虚拟网络流日志,解决NSG流日志的局限性。 退役后,Azure 将不再支持启用流量分析的 NSG 流日志,并且会删除你订阅中现有的 NSG 流日志资源。 然而,Azure 并不会从Azure 存储中删除现有的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'))]"
]
}
]
}
该模板中定义了以下资源:
前面的示例中突出显示的代码显示了 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门户中禁用或删除流日志:
在门户顶部的搜索框中,输入Network Watcher。 从搜索结果中选择 网络观察程序。
在“日志”下,选择“流日志”。
在 网络观察程序 |流日志,选中要删除的流日志复选框。
相关内容
本快速入门介绍了如何使用 ARM 模板来启用 NSG 流日志。 接下来,了解如何使用流量分析直观显示 NSG 流数据: