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

快速入门:使用 Azure 资源管理器 (ARM) 模板配置 Azure 网络观察程序 NSG 流日志

本快速入门介绍如何使用 Azure 资源管理器模板 (ARM) 模板和 Azure PowerShell 启用 NSG 流日志。 有关详细信息,请参阅 Azure 资源管理器是什么?NSG 流日志概述

Azure 资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。

首先,我们将概述 NSG 流日志对象的属性。 我们将提供示例模板。 然后,我们将使用本地 Azure PowerShell 实例部署模板。

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。 模板将在 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 门户中禁用或删除流日志:

  1. 在门户顶部的搜索框中,输入“网络观察程序”。 在搜索结果中选择“网络观察程序”。

  2. 在“日志”下,选择“流日志”。

  3. 在“网络观察程序|流日志”中,选择要删除的流日志的复选框。

  4. 选择“禁用”或“删除”。 有关详细信息,请参阅禁用流日志删除流日志

本快速入门介绍了如何使用 ARM 模板来启用 NSG 流日志。 接下来,了解如何使用流量分析直观显示 NSG 流数据: