快速入门:使用 ARM 模板创建 Azure Redis 缓存

了解如何创建 Azure 资源管理器模板(ARM 模板),以便部署 Azure Cache for Redis。 该缓存可以用于现有存储帐户以保存诊断数据。 还将了解如何定义要部署的资源以及如何定义执行部署时指定的参数。 可将此模板用于自己的部署,或自定义此模板以满足要求。 目前,对订阅的同一区域中的所有缓存共享诊断设置。 更新区域中的一个缓存会影响该区域中的所有其他缓存。

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

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。 Azure 门户中会打开模板。

用于将资源管理器模板部署到 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.6.1.6515",
      "templateHash": "6977754622100752630"
    }
  },
  "parameters": {
    "redisCacheName": {
      "type": "string",
      "defaultValue": "[format('redisCache-{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Specify the name of the Azure Redis Cache to create."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location of all resources"
      }
    },
    "redisCacheSKU": {
      "type": "string",
      "defaultValue": "Standard",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ],
      "metadata": {
        "description": "Specify the pricing tier of the new Azure Redis Cache."
      }
    },
    "redisCacheFamily": {
      "type": "string",
      "defaultValue": "C",
      "allowedValues": [
        "C",
        "P"
      ],
      "metadata": {
        "description": "Specify the family for the sku. C = Basic/Standard, P = Premium."
      }
    },
    "redisCacheCapacity": {
      "type": "int",
      "defaultValue": 1,
      "allowedValues": [
        0,
        1,
        2,
        3,
        4,
        5,
        6
      ],
      "metadata": {
        "description": "Specify the size of the new Azure Redis Cache instance. Valid values: for C (Basic/Standard) family (0, 1, 2, 3, 4, 5, 6), for P (Premium) family (1, 2, 3, 4)"
      }
    },
    "enableNonSslPort": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specify a boolean value that indicates whether to allow access via non-SSL ports."
      }
    },
    "diagnosticsEnabled": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specify a boolean value that indicates whether diagnostics should be saved to the specified storage account."
      }
    },
    "existingDiagnosticsStorageAccountName": {
      "type": "string",
      "metadata": {
        "description": "Specify the name of an existing storage account for diagnostics."
      }
    },
    "existingDiagnosticsStorageAccountResourceGroup": {
      "type": "string",
      "metadata": {
        "description": "Specify the resource group name of an existing storage account for diagnostics."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Cache/redis",
      "apiVersion": "2020-06-01",
      "name": "[parameters('redisCacheName')]",
      "location": "[parameters('location')]",
      "properties": {
        "enableNonSslPort": "[parameters('enableNonSslPort')]",
        "minimumTlsVersion": "1.2",
        "sku": {
          "capacity": "[parameters('redisCacheCapacity')]",
          "family": "[parameters('redisCacheFamily')]",
          "name": "[parameters('redisCacheSKU')]"
        }
      }
    },
    {
      "type": "Microsoft.Insights/diagnosticSettings",
      "apiVersion": "2021-05-01-preview",
      "scope": "[format('Microsoft.Cache/redis/{0}', parameters('redisCacheName'))]",
      "name": "[parameters('redisCacheName')]",
      "properties": {
        "storageAccountId": "[extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, parameters('existingDiagnosticsStorageAccountResourceGroup')), 'Microsoft.Storage/storageAccounts', parameters('existingDiagnosticsStorageAccountName'))]",
        "metrics": [
          {
            "timeGrain": "AllMetrics",
            "enabled": "[parameters('diagnosticsEnabled')]",
            "retentionPolicy": {
              "days": 90,
              "enabled": "[parameters('diagnosticsEnabled')]"
            }
          }
        ]
      },
      "dependsOn": [
        "[resourceId('Microsoft.Cache/redis', parameters('redisCacheName'))]"
      ]
    }
  ]
}

该模板中定义了以下资源:

适用于新高级层的资源管理器模板也已推出。

若要检查最新模板,请参阅 Azure 快速入门模板并搜索 Azure Cache for Redis。

部署模板

  1. 选择下图登录到 Azure 并打开模板。

    用于将资源管理器模板部署到 Azure 的按钮。

  2. 选择或输入以下值:

    • 订阅:选择用于创建数据共享和其他资源的 Azure 订阅。
    • 资源组:选择“新建”以创建新的资源组,或选择现有资源组。
    • 位置:选择资源组的位置。 存储帐户和 Redis 缓存必须位于同一区域。 默认情况下,Redis 缓存使用的位置与资源组位置相同。 因此指定存储帐户所在的同一位置。
    • Redis 缓存名称:输入 Redis 缓存的名称。
    • 现有的诊断存储帐户:输入存储帐户的资源 ID。 语法为 /subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.Storage/storageAccounts/<STORAGE ACCOUNT NAME>

    其余设置均采用默认值。

  3. 选择“我同意上述条款和条件”,然后选择“购买” 。

查看已部署的资源

  1. 登录 Azure 门户
  2. 打开创建的 Redis 缓存。

清理资源

如果不再需要资源组,可以将其删除,这将删除资源组中的资源。

$resourceGroupName = Read-Host -Prompt "Enter the resource group name"
Remove-AzResourceGroup -Name $resourceGroupName
Write-Host "Press [ENTER] to continue..."

后续步骤

本教程介绍了如何创建 Azure 资源管理器模板,以便部署 Azure Cache for Redis。 若要了解如何创建 Azure 资源管理器模板来部署包含 Azure Cache for Redis 的 Azure Web 应用,请参阅使用模板创建 Web 应用和 Azure Cache for Redis