Azure ARM 模板和 REST API

Azure ARM Templates and REST API

我正在尝试学习 Azure 资源模板,并试图了解何时使用它们以及何时使用 REST 背后的工作流 API。

我的感觉是,在 Azure 中创建虚拟网络和子网是一种相当罕见的情况,一旦您根据需要进行设置,就不会过于频繁地修改它,您可以将东西部署到该结构中。

关于 ARM 模板,假设我有一个包含 VNET 和子网资源的模板。以 https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-template-walkthrough#virtual-network-and-subnet 为例,我可能有:

{
"apiVersion": "2015-06-15",
"type": "Microsoft.Network/virtualNetworks",
"name": "[parameters('vnetName')]",
"location": "[resourceGroup().location]",
"properties": {
 "addressSpace": {
   "addressPrefixes": [
     "10.0.0.0/16"
   ]
 },
 "subnets": [
   {
     "name": "[variables('subnetName')]",
     "properties": {
       "addressPrefix": "10.0.0.0/24"
     }
   }
 ]
}
}

我将其部署到资源组。假设我随后添加了一个负载均衡器并重新部署了模板。在这种情况下,用户随后会被要求再次提供 vnetName 参数的值,当然不能提供相同的值,因此我们最终会得到另一个 VNET,这不是我们想要的。

您一次性定义 ARM 模板(VNET、LB、子网、NIC 等)然后部署的工作流程是什么?然后,当您想要部署 VM、规模集等时,您使用 REST API 部署到资源组/VNET 子网?或者有没有一种方法可以增量构建 ARM 模板并多次部署它,如果 VNET 已经存在(例如),则不会提示用户提供另一个模板的详细信息?

我已经阅读并看到了增量模式(默认模式,除非指定了完整模式)但不确定这是否相关以及如何使用它。

非常感谢您的帮助!

更新

好的,我现在可以使用 azure group deployment create -f azuredeploy.json -g ARM-Template-Tests -m Incremental 并修改了模板中来自

的 VNET 资源
{
  "apiVersion": "2016-09-01",
  "type": "Microsoft.Network/virtualNetworks",
  "name": "[variables('virtualNetworkName')]",
  "location": "[resourceGroup().location]",
  "properties": {
    "addressSpace": {
      "addressPrefixes": [
        "[variables('addressPrefix')]"
      ]
    },
    "subnets": [
      {
        "name": "[variables('subnetName')]",
        "properties": {
          "addressPrefix": "[variables('subnetPrefix')]"
        }
      }
    ]
  }
},

{
  "apiVersion": "2015-05-01-preview",
  "type": "Microsoft.Network/virtualNetworks",
  "name": "[parameters('virtualNetworkName')]",
  "location": "[resourceGroup().location]",
  "properties": {
    "addressSpace": {
      "addressPrefixes": [
        "[parameters('addressPrefix')]"
      ]
    },
    "subnets": [
      {
        "name": "[parameters('subnet1Name')]",
        "properties": {
          "addressPrefix": "[parameters('subnet1Prefix')]"
        }
      },
      {
        "name": "[parameters('gatewaySubnet')]",
        "properties": {
          "addressPrefix": "[parameters('gatewaySubnetPrefix')]"
        }
      }
    ]
  }
},

但子网没有改变。他们应该使用 azure group deployment create -f azuredeploy.json -g ARM-Template-Tests -m Incremental

可以以增量模式部署新模板,这会将新资源添加到现有资源组。只定义模板中的新资源,不会改变现有资源。

从 powershell 使用以下 cmdlet

New-AzureRmResourceGroupDeployment -ResourceGroupName "YourResourceGroupName" -TemplateFile "path\to\template.json" -Mode Incremental -Force

我要依靠这个 Azure documentation。参考以下相应部分:

Incremental and complete deployments

When deploying your resources, you specify that the deployment is either an incremental update or a complete update. By default, Resource Manager handles deployments as incremental updates to the resource group.

With incremental deployment, Resource Manager

  1. leaves unchanged resources that exist in the resource group but are not specified in the template
  2. adds resources that are specified in the template but do not exist in the resource group
  3. does not reprovision resources that exist in the resource group in the same condition defined in the template
  4. reprovisions existing resources that have updated settings in the template

With complete deployment, Resource Manager:

  1. deletes resources that exist in the resource group but are not specified in the template
  2. adds resources that are specified in the template but do not exist in the resource group
  3. does not reprovision resources that exist in the resource group in the same condition defined in the template
  4. reprovisions existing resources that have updated settings in the template

选择增量更新还是完全更新取决于您是否有正在使用的资源。如果 devops 要求始终使资源与 json 模板中定义的内容同步,则应使用 Complete Update 模式。使用模板和源代码部署资源最大的好处是防止配置漂移,使用Complete Update模式是有好处的。

至于指定参数,如果在参数文件中指定,则无需再次指定。

我的经验是,对于我想要拆除的东西,或者对于我想要跨订阅复制的东西,我使用 ARM 模板。

例如,我们想要测试环境中的东西,我只是将它武装起来,根据开发人员的要求构建脚本("Hey I need a cache","Oh by the way I need to start using a Service Bus"),使用增量模式我们可以直接推送它到 Dev,然后当我们迁移到不同的环境时,您只需部署到 Azure 中的不同订阅,它应该一切就绪。

此外,我们已经开始在 VMSS 中配置我们自己的云负载测试代理,这是一个简单的 ARM 模板,由构建调用以扩展到 x 台机器,然后完成后,我们就将资源组丢弃。它是可重复且可靠的,确保您可以编写脚本,但由于 TFS 有部署这些东西的任务(也有时间表)...

我遇到的一件美妙的事情是 Key Vault,当你武装它并从你的服务总线、存储中获取所有值时 accounts/whatevers,你可以简单地获得连接 strings/keys/whatevers 并将它们直接放入 Key Vault 中,因此您永远不必担心,如果您想重新生成任何内容(比如开发人员想要更改缓存的名称或其他任何内容,或者不小心将密钥发布到 GitHub),只需重新部署(通常我会把整个资源组都丢弃),它会为您更新保险库。