使用範本來建立 Web 應用程式加 Azure Redis 快取
在本文中,您會了解如何建立 Azure Resource Manager 範本,並部署 Azure Web 應用程式與 Azure Cache for Redis。 您會了解下列部署詳細資料:
- 如何定義要部署的資源
- 如何定義執行部署時指定的參數
您可以直接在自己的部署中使用此範本,或自訂此範本以符合您的需求。
如需關於建立範本的詳細資訊,請參閱 編寫 Azure Resource Manager 範本。 若要深入了解快取資源類型的 JSON 語法和屬性,請參閱 Microsoft.Cache 資源類型。
如需完整的範本,請參閱 Web 應用程式與 Azure Redis 快取範本 \(英文\)。
部署內容
在此範本中,您會部署:
- Azure Web 應用程式
- Azure Cache for Redis
若要自動執行部署,請選取下列按鈕:
要指定的參數
透過 Azure 資源管理員,您可以定義在部署範本時想要指定之值的參數。 此範本有一個 Parameters 區段,內含所有參數值。 建議您根據部署專案或部署環境,針對這些值定義不同的參數。 如果是常數值,請勿定義參數。 每個參數值都可在範本中用來定義所部署的資源。
在定義參數時,請使用 [ allowedValues ] 欄位來指定使用者可以在部署期間提供的值。 使用 [ defaultValue ] 欄位來指派部署期間未提供任何值時的參數值。
我們將說明範本中的每個參數。
siteName
想要建立之 Web 應用程式的名稱。
"siteName":{
"type":"string"
}
hostingPlanName
用來主控 Web 應用程式之 App Service 方案的名稱。
"hostingPlanName":{
"type":"string"
}
SKU
主控方案的定價層。
"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
。
workerSize
主控方案的執行個體大小 (小型、中型或大型)。
"workerSize":{
"type":"string",
"allowedValues":[
"0",
"1",
"2"
],
"defaultValue":"0"
}
此範本定義此參數允許的值 (0
、1
或 2
),如果未指定值,範本會指派預設值 0
。 這些值對應小型、中型和大型執行個體。
cacheSKUName
新的 Azure Cache for Redis 的定價層。
"cacheSKUName": {
"type": "string",
"allowedValues": [
"Basic",
"Standard",
"Premium"
],
"defaultValue": "Basic",
"metadata": {
"description": "The pricing tier of the new Azure Cache for Redis."
}
},
此範本定義此參數允許的值 (基本、標準或進階),如果未指定值,則範本會指派預設值 (基本)。 「基本」提供單一節點,有多種大小可用,最大為 53 GB。 「標準」提供雙節點「主要/複本」,有多種大小可用,最大為 53 GB,還有高達 99.9% 的 SLA。
cacheSKUFamily
Sku 系列。
"cacheSKUFamily": {
"type": "string",
"allowedValue/s": [
"C",
"P"
],
"defaultValue": "C",
"metadata": {
"description": "The family for the sku."
}
},
cacheSKUCapacity
新 Azure Cache for Redis 執行個體的大小。
如果是基本和標準系列:
"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. "
}
}
進階值快取容量的定義相同,但允許的值為 1 至 5,而不是 0 至 6。
此範本定義此參數允許的整數值 (基本和標準系列為 0 至 6,進階系列為 1 至 5)。 如果未指定值,則範本會針對基本和標準指派預設值 0,針對進階指派預設值 1。
這些值對應下列快取大小:
值 | 基本與標準 快取大小 |
進階 快取大小 |
---|---|---|
0 | 250 MB (預設) | n/a |
1 | 1 GB | 6 GB (預設) |
2 | 2.5 GB | 13GB |
3 | 6 GB | 26GB |
4 | 13GB | 53 GB |
5 | 26GB | 120 GB |
6 | 53 GB | n/a |
名稱的變數
這個範本會使用變數來建構資源的名稱。 它會使用 uniqueString 函式,根據資源群組識別碼來建構值。
"variables": {
"hostingPlanName": "[concat('hostingplan', uniqueString(resourceGroup().id))]",
"webSiteName": "[concat('webSite', uniqueString(resourceGroup().id))]",
"cacheName": "[concat('cache', uniqueString(resourceGroup().id))]"
},
要部署的資源
App Service 方案
建立主控 Web 應用程式的服務方案。 您可以透過 hostingPlanName 參數提供方案名稱。 方案的位置與用於資源群組的位置相同。 定價層和背景工作角色大小以 sku 和 workerSize 參數指定。
{
"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')]"
}
},
Azure Cache for Redis
建立與 Web 應用程式搭配使用的「Azure Redis 快取」。 快取的名稱指定於 cacheName 變數中。
範本會在資源群組的相同位置建立快取。
{
"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 應用程式 (Azure Cache for Redis)
使用 webSiteName 變數中所指定的名稱來建立 Web 應用程式。
請注意,Web 應用程式上會設定可讓它與「Azure Redis 快取」搭配運作的應用程式設定屬性。 這些應用程式設定是根據部署期間提供的值動態建立。
{
"apiVersion": "2015-08-01",
"name": "[variables('webSiteName')]",
"type": "Microsoft.Web/sites",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Web/serverFarms/', variables('hostingPlanName'))]"
],
"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('cacheHostName'),'.redis.cache.windows.net,abortConnect=false,ssl=true,password=', listKeys(resourceId('Microsoft.Cache/Redis', variables('cacheName')), '2015-08-01').primaryKey)]"
}
}
]
}
Web 應用程式 (RedisEnterprise)
如果是 RedisEnterprise,因為資源類型稍有不同,所以執行 listKeys 的方式也不同:
{
"apiVersion": "2015-08-01",
"name": "[variables('webSiteName')]",
"type": "Microsoft.Web/sites",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Web/serverFarms/', variables('hostingPlanName'))]"
],
"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/RedisEnterprise/databases/', variables('cacheName'), "/default")]",
],
"properties": {
"CacheConnection": "[concat(variables('cacheHostName'),abortConnect=false,ssl=true,password=', listKeys(resourceId('Microsoft.Cache/RedisEnterprise', variables('cacheName'), 'default'), '2020-03-01').primaryKey)]"
}
}
]
}
執行部署的命令
若要將資源部署至 Azure,您必須登入 Azure 帳戶,而且必須使用 Azure Resource Manager 模組。 若要了解如何搭配使用 Azure 資源管理員與 Azure PowerShell 或 Azure CLI,請參閱:
下例假設您的帳戶中已經有已指定名稱的資源群組。
PowerShell
New-AzResourceGroupDeployment -TemplateUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.web/web-app-with-redis-cache/azuredeploy.json -ResourceGroupName ExampleDeployGroup
Azure CLI
azure group deployment create --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.web/web-app-with-redis-cache/azuredeploy.json -g ExampleDeployGroup