快速入門:使用 ARM 範本建立 Azure 管理的 Redis 快取

了解如何建立用來部署 Azure Cache for Redis 的 Azure Resource Manager 範本 (ARM 範本)。 快取可以搭配現有的儲存體帳戶以保留診斷資料。 您將學習如何定義要部署哪些資源,以及如何定義執行部署時所指定的參數。 您可以直接在自己的部署中使用此範本,或自訂此範本以符合您的需求。 目前對於訂用帳戶,同一區域中所有快取的診斷設定是共用的。 更新區域中的一個快取將會影響區域中的所有其他快取。

Azure Resource Manager 模板 是一個 JavaScript 物件符號(JSON)檔案,用來定義你專案的基礎架構與設定。 範本使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。

如果你的環境符合前提條件,且熟悉使用 ARM 範本,請選擇 部署到 Azure 按鈕。 範本會在 Azure 入口網站中開啟。

按鈕用來部署 Resource Manager 範本到 Azure。

Prerequisites

Azure Cache for Redis

檢閱範本

本快速入門中使用的範本是來自 Azure 快速入門範本。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.27.1.19265",
      "templateHash": "10339364272812551477"
    }
  },
  "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'))]"
      ]
    }
  ]
}

樣本中定義了下列資源:

也可以使用新進階層的 Resource Manager 範本。

若要查看最新的範本,請參閱 Azure 快速入門範本並搜尋 Azure Cache for Redis。

部署範本

  1. 選取以下影像來登入 Azure 並開啟範本。

    按鈕用來部署 Resource Manager 範本到 Azure。

  2. 選取或輸入下列值:

    • 訂閱:選擇一個用於建立資料共享及其他資源的 Azure 訂閱。
    • 資源群組:選擇 「建立新」 以建立新的資源群組,或選擇現有的資源群組。
    • 地點:為資源群組選擇一個地點。 儲存體帳戶和 Redis 快取必須位於相同的區域中。 根據預設,Redis 快取會使用與資源群組相同的位置。 因此,請指定與儲存體帳戶相同的位置。
    • Redis 快取名稱:輸入 Redis 快取的名稱。
    • 現有診斷儲存帳戶:輸入儲存帳戶的資源 ID。 語法是 /subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.Storage/storageAccounts/<STORAGE ACCOUNT NAME>。

    其餘設定請使用預設值。

  3. 選取 [我同意上方所述的條款及條件],然後選取 [購買]。

Azure Managed Redis

檢閱範本

修改 cachename 和 region 參數。 將它複製到檔案 azuredeploy.json。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "cachename": {
            "defaultValue": "myAMRcache",
            "type": "String"
        },
        "region": {
            "defaultValue": "centraluseuap",
            "type": "String"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Cache/redisEnterprise",
            "apiVersion": "2024-05-01-preview",
            "name": "[parameters('cachename')]",
            "location": "[parameters('region')]",
            "sku": {
                "name": "Balanced_B5"
            },
            "identity": {
                "type": "None"
            },
            "properties": {
                "minimumTlsVersion": "1.2"
            }
        },
        {
            "type": "Microsoft.Cache/redisEnterprise/databases",
            "apiVersion": "2024-05-01-preview",
            "name": "[concat(parameters('cachename'), '/default')]",
            "dependsOn": [
                "[resourceId('Microsoft.Cache/redisEnterprise', parameters('cachename'))]"
            ],
            "properties": {
                "clientProtocol": "Encrypted",
                "port": 10000,
                "clusteringPolicy": "OSSCluster",
                "evictionPolicy": "NoEviction",
                "persistence": {
                    "aofEnabled": false,
                    "rdbEnabled": false
                }
            }
        }
    ]
}

部署範本

  1. 將 Azure Resource Manager 範本儲存為 azuredeploy.json 至您的本機電腦。

  2. 使用 Azure CLI 或 Azure PowerShell 部署範本。

    az deployment group create --resource-group exampleRG --template-file main.bicep
    

檢閱已部署的資源

  1. 登入 Azure 入口網站。
  2. 開啟您建立的 Redis 快取。

清理資源

如果不再需要,請刪除資源群組,這會刪除資源群組中的資源。

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