设置使用 SQL 数据库的 Web 应用

Note

本文涉及的 SDK 和工具是在 Microsoft Corporation 运行的 Azure 服务中使用的,而在中国, Azure 是由 21Vianet 运行的。 请根据需要参阅中国区 Azure 应用程序开发说明

在本主题中,会学习如何创建用于部署 Web 应用和 SQL 数据库的 Azure Resource Manager 模板。你将了解如何定义要部署的资源以及如何定义执行部署时指定的参数。可将此模板用于自己的部署,或自定义此模板以满足要求。

有关创建模板的详细信息,请参阅创作 Azure 资源管理器模板

有关部署应用的详细信息,请参阅通过可预测的方式在 Azure 中部署复杂应用程序

有关完整的模板,请参阅使用 SQL 数据库的 Web 应用的模板

Note

尽管本文中指的是 Web 应用,但内容同样适用于 API 应用和移动应用。

将部署的内容

在此模板中,你将部署:

  • Web 应用
  • SQL 数据库服务器
  • SQL 数据库
  • 自动缩放设置
  • 警报规则

若要自动运行部署,请单击以下按钮:

部署到 Azure

Note

必须修改从 GitHub 存储库“azure-quickstart-templates”部署的模板,以适应 Azure 中国云环境。例如,替换某些终结点 -- 将“blob.core.windows.net”替换为“blob.core.chinacloudapi.cn”,将“cloudapp.azure.com”替换为“chinacloudapp.cn”。Azure 中国目前还不支持 Application Insight,所以需要把资源“Microsoft.Insights/components”删掉。

要指定的参数

使用 Azure 资源管理器,可以定义在部署模板时想要指定的值的参数。该模板具有一个名为 Parameters 的部分,其中包含所有参数值。 你应该为随着要部署的项目或要部署到的环境而变化的值定义参数。不要为永远保持不变的值定义参数。每个参数值可在模板中用来定义所部署的资源。

在定义参数时,请使用 allowedValues 字段来指定用户在部署过程中可以提供哪些值。如果在部署过程中未提供任何值,请使用 defaultValue 字段为该参数赋值。

下面介绍模板中的每个参数。

siteName

要创建的网站的名称。

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

hostingPlanName

用于托管网站的 App Service 计划的名称。

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

sku

托管计划的定价层。

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

模板将定义此参数允许的值,如果未指定任何值,则分配默认值 (S1)。

workerSize

托管计划的实例大小(small、medium 或 large)。

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

模板将定义此参数允许的值(0、1 或 2),如果未指定任何值,则分配默认值 (0)。这些值分别对应于 small、medium 和 large。

administratorLogin

要用于数据库服务器管理员的帐户名称。

"administratorLogin": {
  "type": "string"
}

administratorLoginPassword

要用于数据库服务器管理员的密码。

"administratorLoginPassword": {
  "type": "securestring"
}

databaseName

要创建的新数据库的名称。

"databaseName": {
  "type": "string",
  "defaultValue": "sampledb"
}

collation

要用于管理字符的正确使用的数据库排序规则。

"collation": {
  "type": "string",
  "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
}

edition

要创建的数据库类型。

"edition": {
  "type": "string",
  "defaultValue": "Basic",
  "allowedValues": [
    "Basic",
    "Standard",
    "Premium"
  ],
  "metadata": {
    "description": "The type of database to create."
  }
}

maxSizeBytes

数据库的最大大小(以字节为单位)。

"maxSizeBytes": {
  "type": "string",
  "defaultValue": "1073741824"
}

requestedServiceObjectiveName

版本的性能级别所对应的名称。

"requestedServiceObjectiveName": {
  "type": "string",
  "defaultValue": "Basic",
  "allowedValues": [
    "Basic",
    "S0",
    "S1",
    "S2",
    "P1",
    "P2",
    "P3"
  ],
  "metadata": {
    "description": "Describes the performance level for Edition"
  }
}

名称变量

此模板包含构造模板中使用的名称的变量。变量值使用 uniqueString 函数从资源组 ID 生成名称。

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

要部署的资源

SQL Server 和数据库

创建一个新的 SQL Server 和数据库。在 serverName 参数中指定服务器的名称,在 serverLocation 参数中指定其位置。在创建新服务器时,必须提供数据库服务器管理员的登录名和密码。

{
  "name": "[variables('sqlserverName')]",
  "type": "Microsoft.Sql/servers",
  "location": "[resourceGroup().location]",
  "tags": {
    "displayName": "SqlServer"
  },
  "apiVersion": "2014-04-01-preview",
  "properties": {
    "administratorLogin": "[parameters('administratorLogin')]",
    "administratorLoginPassword": "[parameters('administratorLoginPassword')]"
  },
  "resources": [
    {
      "name": "[parameters('databaseName')]",
      "type": "databases",
      "location": "[resourceGroup().location]",
      "tags": {
        "displayName": "Database"
      },
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [
        "[variables('sqlserverName')]"
      ],
      "properties": {
        "edition": "[parameters('edition')]",
        "collation": "[parameters('collation')]",
        "maxSizeBytes": "[parameters('maxSizeBytes')]",
        "requestedServiceObjectiveName": "[parameters('requestedServiceObjectiveName')]"
      }
    },
    {
      "type": "firewallrules",
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [
        "[variables('sqlserverName')]"
      ],
      "location": "[resourceGroup().location]",
      "name": "AllowAllAzureIps",
      "properties": {
        "endIpAddress": "0.0.0.0",
        "startIpAddress": "0.0.0.0"
      }
    }
  ]
},

App Service 计划

创建用于托管网站的服务计划。通过 hostingPlanName 参数提供计划的名称。计划的位置与用于资源组的位置相同。定价层和辅助角色大小在 skuworkerSize 参数中指定

{
  "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')]"
  }
},

Web 应用

{
  "apiVersion": "2015-08-01",
  "name": "[variables('webSiteName')]",
  "type": "Microsoft.Web/sites",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[variables('hostingPlanName')]"
  ],
  "tags": {
    "[concat('hidden-related:', resourceId('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": "connectionstrings",
      "dependsOn": [
        "[variables('webSiteName')]"
      ],
      "properties": {
        "DefaultConnection": {
          "value": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('databaseName'), ';User Id=', parameters('administratorLogin'), '@', variables('sqlserverName'), ';Password=', parameters('administratorLoginPassword'), ';')]",
          "type": "SQLServer"
        }
      }
    }
  ]
},

自动缩放

{
  "apiVersion": "2014-04-01",
  "name": "[concat(variables('hostingPlanName'), '-', resourceGroup().name)]",
  "type": "Microsoft.Insights/autoscalesettings",
  "location": "[resourceGroup().location]",
  "tags": {
    "[concat('hidden-link:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]": "Resource",
    "displayName": "AutoScaleSettings"
  },
  "dependsOn": [
    "[variables('hostingPlanName')]"
  ],
  "properties": {
    "profiles": [
      {
        "name": "Default",
        "capacity": {
          "minimum": 1,
          "maximum": 2,
          "default": 1
        },
        "rules": [
          {
            "metricTrigger": {
              "metricName": "CpuPercentage",
              "metricResourceUri": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
              "timeGrain": "PT1M",
              "statistic": "Average",
              "timeWindow": "PT10M",
              "timeAggregation": "Average",
              "operator": "GreaterThan",
              "threshold": 80.0
            },
            "scaleAction": {
              "direction": "Increase",
              "type": "ChangeCount",
              "value": 1,
              "cooldown": "PT10M"
            }
          },
          {
            "metricTrigger": {
              "metricName": "CpuPercentage",
              "metricResourceUri": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
              "timeGrain": "PT1M",
              "statistic": "Average",
              "timeWindow": "PT1H",
              "timeAggregation": "Average",
              "operator": "LessThan",
              "threshold": 60.0
            },
            "scaleAction": {
              "direction": "Decrease",
              "type": "ChangeCount",
              "value": 1,
              "cooldown": "PT1H"
            }
          }
        ]
      }
    ],
    "enabled": false,
    "name": "[concat(variables('hostingPlanName'), '-', resourceGroup().name)]",
    "targetResourceUri": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
  }
},

状态代码 403 和 500 的警报规则、高 CPU 及 HTTP 队列长度

{
  "apiVersion": "2014-04-01",
  "name": "[concat('ServerErrors ', variables('webSiteName'))]",
  "type": "Microsoft.Insights/alertrules",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[variables('webSiteName')]"
  ],
  "tags": {
    "[concat('hidden-link:', resourceId('Microsoft.Web/sites', variables('webSiteName')))]": "Resource",
    "displayName": "ServerErrorsAlertRule"
  },
  "properties": {
    "name": "[concat('ServerErrors ', variables('webSiteName'))]",
    "description": "[concat(variables('webSiteName'), ' has some server errors, status code 5xx.')]",
    "isEnabled": false,
    "condition": {
      "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
      "dataSource": {
        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
        "resourceUri": "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]",
        "metricName": "Http5xx"
      },
      "operator": "GreaterThan",
      "threshold": 0.0,
      "windowSize": "PT5M"
    },
    "action": {
      "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
      "sendToServiceOwners": true,
      "customEmails": [ ]
    }
  }
},
{
  "apiVersion": "2014-04-01",
  "name": "[concat('ForbiddenRequests ', variables('webSiteName'))]",
  "type": "Microsoft.Insights/alertrules",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[variables('webSiteName')]"
  ],
  "tags": {
    "[concat('hidden-link:', resourceId('Microsoft.Web/sites', variables('webSiteName')))]": "Resource",
    "displayName": "ForbiddenRequestsAlertRule"
  },
  "properties": {
    "name": "[concat('ForbiddenRequests ', variables('webSiteName'))]",
    "description": "[concat(variables('webSiteName'), ' has some requests that are forbidden, status code 403.')]",
    "isEnabled": false,
    "condition": {
      "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
      "dataSource": {
        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
        "resourceUri": "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]",
        "metricName": "Http403"
      },
      "operator": "GreaterThan",
      "threshold": 0,
      "windowSize": "PT5M"
    },
    "action": {
      "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
      "sendToServiceOwners": true,
      "customEmails": [ ]
    }
  }
},
{
  "apiVersion": "2014-04-01",
  "name": "[concat('CPUHigh ', variables('hostingPlanName'))]",
  "type": "Microsoft.Insights/alertrules",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[variables('hostingPlanName')]"
  ],
  "tags": {
    "[concat('hidden-link:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]": "Resource",
    "displayName": "CPUHighAlertRule"
  },
  "properties": {
    "name": "[concat('CPUHigh ', variables('hostingPlanName'))]",
    "description": "[concat('The average CPU is high across all the instances of ', variables('hostingPlanName'))]",
    "isEnabled": false,
    "condition": {
      "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
      "dataSource": {
        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
        "resourceUri": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "metricName": "CpuPercentage"
      },
      "operator": "GreaterThan",
      "threshold": 90,
      "windowSize": "PT15M"
    },
    "action": {
      "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
      "sendToServiceOwners": true,
      "customEmails": [ ]
    }
  }
},
{
  "apiVersion": "2014-04-01",
  "name": "[concat('LongHttpQueue ', variables('hostingPlanName'))]",
  "type": "Microsoft.Insights/alertrules",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[variables('hostingPlanName')]"
  ],
  "tags": {
    "[concat('hidden-link:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]": "Resource",
    "displayName": "AutoScaleSettings"
  },
  "properties": {
    "name": "[concat('LongHttpQueue ', variables('hostingPlanName'))]",
    "description": "[concat('The HTTP queue for the instances of ', variables('hostingPlanName'), ' has a large number of pending requests.')]",
    "isEnabled": false,
    "condition": {
      "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
      "dataSource": {
        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
        "resourceUri": "[concat(resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', variables('hostingPlanName'))]",
        "metricName": "HttpQueueLength"
      },
      "operator": "GreaterThan",
      "threshold": 100.0,
      "windowSize": "PT5M"
    },
    "action": {
      "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
      "sendToServiceOwners": true,
      "customEmails": [ ]
    }
  }
},

运行部署的命令

若要将资源部署到 Azure,必须登录到 Azure 帐户,并且必须使用 Azure Resource Manager 模块。 若要了解配合使用 Azure Resource Manager 和 Azure PowerShell 或 Azure CLI 的相关信息,请参阅:

以下示例假定你的帐户中已具有一个指定名称的资源组。

PowerShell

New-AzureRmResourceGroupDeployment -TemplateFile path/to/azuredeploy.json

Azure CLI

azure config mode arm
azure group deployment create -g {resource-group-name} --template-file path/to/azuredeploy.json

Azure CLI 2.0(预览版)

Note

在 Azure 中国区使用 Azure CLI 2.0 之前,请先运行 az cloud set -n AzureChinaCloud 来改变云环境。如果想切回国际版 Azure,请再次运行 az cloud set -n AzureCloud

az resource group deployment create -g {resource-group-name} --template-file path/to/azuredeploy.json --parameters '@azuredeploy.parameters.json'
Note

有关参数 JSON 文件的内容,请参阅 azuredeploy.parameters.json