使用模板创建 Web 应用和 Azure Redis 缓存Create a Web App plus Azure Cache for Redis using a template

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本主题介绍如何创建 Azure 资源管理器模板来部署包含 Azure Redis 缓存的 Azure Web 应用。In this topic, you will learn how to create an Azure Resource Manager template that deploys an Azure Web App with Azure Cache for Redis. 了解如何定义要部署的资源以及如何定义执行部署时指定的参数。You will learn how to define which resources are deployed and how to define parameters that are specified when the deployment is executed. 可将此模板用于自己的部署,或自定义此模板以满足要求。You can use this template for your own deployments, or customize it to meet your requirements.

有关创建模板的详细信息,请参阅创作 Azure Resource Manager 模板For more information about creating templates, see Authoring Azure Resource Manager Templates. 若要了解缓存资源类型的 JSON 语法和属性,请参阅 Microsoft.Cache 资源类型To learn about the JSON syntax and properties for cache resource types, see Microsoft.Cache resource types.

有关完整的模板,请参阅包含 Azure Redis 缓存的 Web 应用模板For the complete template, see Web App with Azure Cache for Redis template.

要部署的内容What you will deploy

在此模板中,你将部署:In this template, you will deploy:

  • Azure Web 应用Azure Web App
  • 用于 Redis 的 Azure 缓存Azure Cache for Redis

若要自动运行部署,请单击以下按钮:To run the deployment automatically, click the following button:

“部署到 Azure”Deploy to Azure

要指定的参数Parameters to specify

使用 Azure Resource Manager,可以定义在部署模板时想要指定的值的参数。With Azure Resource Manager, you define parameters for values you want to specify when the template is deployed. 该模板具有一个名为 Parameters 的部分,其中包含所有参数值。The template includes a section called Parameters that contains all of the parameter values. 应该为随着要部署的项目或要部署到的环境而变化的值定义参数。You should define a parameter for those values that will vary based on the project you are deploying or based on the environment you are deploying to. 不要为永远保持不变的值定义参数。Do not define parameters for values that will always stay the same. 每个参数值可在模板中用来定义所部署的资源。Each parameter value is used in the template to define the resources that are deployed.

定义参数时,使用 allowedValues 字段指定用户在部署过程中可以提供哪些值 。When defining parameters, use the allowedValues field to specify which values a user can provide during deployment. 如果在部署过程中未提供任何值,请使用 defaultValue 字段为该参数赋值 。Use the defaultValue field to assign a value to the parameter, if no value is provided during deployment.

下面介绍模板中的每个参数。We will describe each parameter in the template.

siteNamesiteName

要创建的 Web 应用的名称。The name of the web app that you wish to create.

"siteName":{
  "type":"string"
}

hostingPlanNamehostingPlanName

用于托管 Web 应用的应用服务计划的名称。The name of the App Service plan to use for hosting the web app.

"hostingPlanName":{
  "type":"string"
}

skusku

托管计划的定价层。The pricing tier for the hosting plan.

"sku": {
  "type": "string",
  "allowedValues": [
    "F1",
    "D1",
    "B1",
    "B2",
    "B3",
    "S1",
    "S2",
    "S3",
    "P1",
    "P2",
    "P3",
    "P4"
  ],
  "defaultValue": "S1",
  "metadata": {
    "description": "The pricing tier for the hosting plan."
  }
}

模板将定义此参数允许的值,如果未指定任何值,则分配默认值 (S1)。The template defines the values that are permitted for this parameter, and assigns a default value (S1) if no value is specified.

workerSizeworkerSize

托管计划的实例大小(small、medium 或 large)。The instance size of the hosting plan (small, medium, or large).

"workerSize":{
  "type":"string",
  "allowedValues":[
    "0",
    "1",
    "2"
  ],
  "defaultValue":"0"
}

模板将定义此参数允许的值(0、1 或 2),如果未指定任何值,则分配默认值 (0)。The template defines the values that are permitted for this parameter (0, 1, or 2), and assigns a default value (0) if no value is specified. 这些值分别对应于 small、medium 和 large。The values correspond to small, medium and large.

cacheSKUNamecacheSKUName

新 Azure Redis 缓存的定价层。The pricing tier of the new Azure Cache for Redis.

"cacheSKUName": {
  "type": "string",
  "allowedValues": [
    "Basic",
    "Standard"
  ],
  "defaultValue": "Basic",
  "metadata": {
    "description": "The pricing tier of the new Azure Cache for Redis."
  }
},

模板将定义此参数允许的值(Basic 或 Standard),如果未指定任何值,则分配默认值 (Basic)。The template defines the values that are permitted for this parameter (Basic or Standard), and assigns a default value (Basic) if no value is specified. Basic 提供单个节点,该节点具有多种大小,最大大小为 53 GB。Basic provides a single node with multiple sizes available up to 53 GB. Standard 提供“主/副本”两个节点,这些节点具有多种大小(最大 53 GB)并提供 99.9% SLA。Standard provides two-node Primary/Replica with multiple sizes available up to 53 GB and 99.9% SLA.

cacheSKUFamilycacheSKUFamily

SKU 的系列。The family for the sku.

"cacheSKUFamily": {
  "type": "string",
  "allowedValues": [
    "C"
  ],
  "defaultValue": "C",
  "metadata": {
    "description": "The family for the sku."
  }
},

cacheSKUCapacitycacheSKUCapacity

新 Azure Redis 缓存实例的大小。The size of the new Azure Cache for Redis instance.

"cacheSKUCapacity": {
  "type": "int",
  "allowedValues": [
    0,
    1,
    2,
    3,
    4,
    5,
    6
  ],
  "defaultValue": 0,
  "metadata": {
    "description": "The size of the new Azure Cache for Redis instance. "
  }
}

模板将定义此参数允许的值(0、1、2、3、4、5 或 6),如果未指定任何值,则分配默认值 (0)。The template defines the values that are permitted for this parameter (0, 1, 2, 3, 4, 5 or 6), and assigns a default value (0) if no value is specified. 这些数字对应于以下缓存大小:0 = 250 MB,1 = 1 GB,2 = 2.5 GB,3 = 6 GB,4 = 13 GB,5 = 26 GB,6 = 53 GBThose numbers correspond to following cache sizes: 0 = 250 MB, 1 = 1 GB, 2 = 2.5 GB, 3 = 6 GB, 4 = 13 GB, 5 = 26 GB, 6 = 53 GB

名称变量Variables for names

此模板使用变量来构造资源的名称。This template uses variables to construct names for the resources. 它使用 uniqueString 函数来构造基于资源组 ID 的值。It uses the uniqueString function to construct a value based on the resource group id.

"variables": {
  "hostingPlanName": "[concat('hostingplan', uniqueString(resourceGroup().id))]",
  "webSiteName": "[concat('webSite', uniqueString(resourceGroup().id))]",
  "cacheName": "[concat('cache', uniqueString(resourceGroup().id))]"
},

要部署的资源Resources to deploy

应用服务计划App Service plan

创建用于托管 Web 应用的服务计划。Creates the service plan for hosting the web app. 通过 hostingPlanName 参数提供计划的名称 。You provide the name of the plan through the hostingPlanName parameter. 计划的位置与用于资源组的位置相同。The location of the plan is the same location used for the resource group. 定价层和辅助角色大小在 sku 和 workerSize 参数中指定 The pricing tier and worker size are specified in the sku and workerSize parameters

{
  "apiVersion": "2015-08-01",
  "name": "[parameters('hostingPlanName')]",
  "type": "Microsoft.Web/serverfarms",
  "location": "[resourceGroup().location]",
  "sku": {
    "name": "[parameters('sku')]",
    "capacity": "[parameters('workerSize')]"
  },
  "properties": {
    "name": "[parameters('hostingPlanName')]"
  }
},

用于 Redis 的 Azure 缓存Azure Cache for Redis

创建用于 Web 应用的 Azure Redis 缓存。Creates the Azure Cache for Redis that is used with the web app. 缓存的名称在 cacheName 变量中指定。The name of the cache is specified in the cacheName variable.

该模板在资源组所在的同一位置中创建缓存。The template creates the cache in the same location as the resource group.

{
  "name": "[variables('cacheName')]",
  "type": "Microsoft.Cache/Redis",
  "location": "[resourceGroup().location]",
  "apiVersion": "2015-08-01",
  "dependsOn": [ ],
  "tags": {
    "displayName": "cache"
  },
  "properties": {
    "sku": {
      "name": "[parameters('cacheSKUName')]",
      "family": "[parameters('cacheSKUFamily')]",
      "capacity": "[parameters('cacheSKUCapacity')]"
    }
  }
}

Web 应用Web app

使用 webSiteName 变量中指定的名称创建 Web 应用。Creates the web app with name specified in the webSiteName variable.

请注意,在 Web 应用中配置的应用设置属性使其可与 Azure Redis 缓存配合工作。Notice that the web app is configured with app setting properties that enable it to work with the Azure Cache for Redis. 这些应用设置是根据部署期间提供的值动态创建的。These app settings are dynamically created based on values provided during deployment.

{
  "apiVersion": "2015-08-01",
  "name": "[variables('webSiteName')]",
  "type": "Microsoft.Web/sites",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[concat('Microsoft.Web/serverFarms/', variables('hostingPlanName'))]",
    "[concat('Microsoft.Cache/Redis/', variables('cacheName'))]"
  ],
  "tags": {
    "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', variables('hostingPlanName'))]": "empty",
    "displayName": "Website"
  },
  "properties": {
    "name": "[variables('webSiteName')]",
    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
  },
  "resources": [
    {
      "apiVersion": "2015-08-01",
      "type": "config",
      "name": "appsettings",
      "dependsOn": [
        "[concat('Microsoft.Web/Sites/', variables('webSiteName'))]",
        "[concat('Microsoft.Cache/Redis/', variables('cacheName'))]"
      ],
      "properties": {
        "CacheConnection": "[concat(variables('cacheName'),'.redis.cache.chinacloudapi.cn,abortConnect=false,ssl=true,password=', listKeys(resourceId('Microsoft.Cache/Redis', variables('cacheName')), '2015-08-01').primaryKey)]"
      }
    }
  ]
}

运行部署的命令Commands to run deployment

要将资源部署到 Azure,必须登录到 Azure 帐户,并且必须使用 Azure 资源管理器模块。To deploy the resources to Azure, you must be signed in to your Azure account and you must use the Azure Resource Manager module. 若要了解配合使用 Azure Resource Manager 和 Azure PowerShell 或 Azure CLI 的相关信息,请参阅:To learn about using Azure Resource Manager with either Azure PowerShell or Azure CLI, see:

以下示例假定帐户中已具有一个指定名称的资源组。The following examples assume you already have a resource group in your account with the specified name.

PowerShellPowerShell

New-AzResourceGroupDeployment -TemplateUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/201-web-app-with-redis-cache/azuredeploy.json -ResourceGroupName ExampleDeployGroup

Azure CLIAzure CLI

azure group deployment create --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/201-web-app-with-redis-cache/azuredeploy.json -g ExampleDeployGroup