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

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

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

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

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

Button to deploy the Resource Manager template to 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.22.6.54827",
      "templateHash": "17110451938184928271"
    }
  },
  "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, 5)"
      }
    },
    "builtInAccessPolicyName": {
      "type": "string",
      "defaultValue": "Data Reader",
      "allowedValues": [
        "Data Owner",
        "Data Contributor",
        "Data Reader"
      ],
      "metadata": {
        "description": "Specify name of Built-In access policy to use as assignment."
      }
    },
    "builtInAccessPolicyAssignmentName": {
      "type": "string",
      "defaultValue": "[format('builtInAccessPolicyAssignment-{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Specify name of custom access policy to create."
      }
    },
    "builtInAccessPolicyAssignmentObjectId": {
      "type": "string",
      "defaultValue": "[newGuid()]",
      "metadata": {
        "description": "Specify the valid objectId(usually it is a GUID) of the Microsoft Entra Service Principal or Managed Identity or User Principal to which the built-in access policy would be assigned."
      }
    },
    "builtInAccessPolicyAssignmentObjectAlias": {
      "type": "string",
      "defaultValue": "[format('builtInAccessPolicyApplication-{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Specify human readable name of principal Id of the Microsoft Entra Application name or Managed Identity name used for built-in policy assignment."
      }
    },
    "customAccessPolicyName": {
      "type": "string",
      "defaultValue": "[format('customAccessPolicy-{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Specify name of custom access policy to create."
      }
    },
    "customAccessPolicyPermissions": {
      "type": "string",
      "defaultValue": "+@connection +get +hget allkeys",
      "metadata": {
        "description": "Specify the valid permissions for the customer access policy to create. For details refer to https://aka.ms/redis/ConfigureAccessPolicyPermissions"
      }
    },
    "customAccessPolicyAssignmentName": {
      "type": "string",
      "defaultValue": "[format('customAccessPolicyAssignment-{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Specify name of custom access policy to create."
      }
    },
    "customAccessPolicyAssignmentObjectId": {
      "type": "string",
      "defaultValue": "[newGuid()]",
      "metadata": {
        "description": "Specify the valid objectId(usually it is a GUID) of the Microsoft Entra Service Principal or Managed Identity or User Principal to which the custom access policy would be assigned."
      }
    },
    "customAccessPolicyAssignmentObjectAlias": {
      "type": "string",
      "defaultValue": "[format('customAccessPolicyApplication-{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Specify human readable name of principal Id of the Microsoft Entra Application name or Managed Identity name used for custom policy assignment."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Cache/redis",
      "apiVersion": "2023-08-01",
      "name": "[parameters('redisCacheName')]",
      "location": "[parameters('location')]",
      "properties": {
        "enableNonSslPort": false,
        "minimumTlsVersion": "1.2",
        "sku": {
          "capacity": "[parameters('redisCacheCapacity')]",
          "family": "[parameters('redisCacheFamily')]",
          "name": "[parameters('redisCacheSKU')]"
        },
        "redisConfiguration": {
          "aad-enabled": "true"
        }
      }
    },
    {
      "type": "Microsoft.Cache/redis/accessPolicyAssignments",
      "apiVersion": "2023-08-01",
      "name": "[format('{0}/{1}', parameters('redisCacheName'), parameters('builtInAccessPolicyAssignmentName'))]",
      "properties": {
        "accessPolicyName": "[parameters('builtInAccessPolicyName')]",
        "objectId": "[parameters('builtInAccessPolicyAssignmentObjectId')]",
        "objectIdAlias": "[parameters('builtInAccessPolicyAssignmentObjectAlias')]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Cache/redis', parameters('redisCacheName'))]"
      ]
    },
    {
      "type": "Microsoft.Cache/redis/accessPolicies",
      "apiVersion": "2023-08-01",
      "name": "[format('{0}/{1}', parameters('redisCacheName'), parameters('customAccessPolicyName'))]",
      "properties": {
        "permissions": "[parameters('customAccessPolicyPermissions')]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Cache/redis', parameters('redisCacheName'))]",
        "[resourceId('Microsoft.Cache/redis/accessPolicyAssignments', parameters('redisCacheName'), parameters('builtInAccessPolicyAssignmentName'))]"
      ]
    },
    {
      "type": "Microsoft.Cache/redis/accessPolicyAssignments",
      "apiVersion": "2023-08-01",
      "name": "[format('{0}/{1}', parameters('redisCacheName'), parameters('customAccessPolicyAssignmentName'))]",
      "properties": {
        "accessPolicyName": "[parameters('customAccessPolicyName')]",
        "objectId": "[parameters('customAccessPolicyAssignmentObjectId')]",
        "objectIdAlias": "[parameters('customAccessPolicyAssignmentObjectAlias')]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Cache/redis', parameters('redisCacheName'))]",
        "[resourceId('Microsoft.Cache/redis/accessPolicies', parameters('redisCacheName'), parameters('customAccessPolicyName'))]"
      ]
    }
  ]
}

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

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

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

部署模板

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

    Button to deploy the Resource Manager template to 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