使用ARM模板为容器部署Web App

时间:2018-09-04 19:25:21

标签: json azure arm-template

我一直在尝试自动将资源部署到Azure上的资源组。现在,我正在使用ARM模板,到目前为止,我已经能够使用模板创建App Insights和App Service Plan。看起来像这样:

{
   "apiVersion": "2015-05-01",
   "name": "[variables('servicePlan')]",
   "kind": "linux",
   "type": "Microsoft.Web/serverfarms",
   "location": "[resourceGroup().location]",
   "tags": {
           "displayName": "BTC Push Notification Settings HostingPlan"
    },
    "sku": {
           "name": "[variables('skuNamePlan')]",
           "capacity": "[variables('skuSizePlan')]"
    },
    "properties": {
            "name": "[variables('servicePlan')]"
    }
},
{
    "apiVersion": "2015-05-01",
    "name": "[variables('appInsights')]",
    "type": "Microsoft.Insights/components",
    "location": "southcentralus",
    "tags": {
            "[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', variables('appInsights'))]": "Resource",
            "displayName": "BTC Push Notification Settings App Insights"
     },
     "properties": {
            "applicationId": "[variables('appInsights')]"
        }
 }

我很难为容器创建Web App,并使用ARM模板将其指向我的docker映像。我已经手动完成了它,并且奏效了,同样,我也像这样azure-cli通过az webapp create --resource-group ExampleGroupAlpina --plan myAppServicePlan --name DockerContainer --deployment-container-image-name this-is-my-image/sample-docker做到了,而且也奏效了。如果有人建议使用ARM模板为容器创建此Web App,我将不胜感激。

5 个答案:

答案 0 :(得分:5)

对我来说,其他所有答案都无效。在Azure支持和其他答案的帮助下,我想到了以下模板,该模板成功地为运行Linux容器的Linux应用服务创建了一个应用服务计划,该容器运行来自Azure容器存储库的自定义Docker映像:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "environment":{
      "type": "string"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location (region) for all resources."
      }
    },   
    "appServiceSku": {
      "type": "string",
      "defaultValue": "B1",
      "metadata": {
        "description": "The SKU of App Service Plan "
      }
    },
    "dockerImageName": {
      "type": "string",
      "defaultValue": "_MY_REGISTRY_USERNAME_.azurecr.io/_MY_NAMESPACE_/_MY_DOCKER_IMAGE_NAME_:_TAG_"
    },
    "dockerRegistryUrl": {
      "type": "string",
      "defaultValue": "https://_MY_REGISTRY_USERNAME_.azurecr.io"
    },
    "dockerRegistryUsername": {
      "type": "string",
      "defaultValue": "_MY_REGISTRY_USERNAME_"
    },
    "dockerRegistryPassword": {
      "type": "string",
      "defaultValue": "_MY_REGISTRY_PSW_"
    },
    "_artifactsLocation": {
      "type": "string"
    },
    "_artifactsLocationSasToken": {
      "type": "securestring"
    }
  },
  "variables": {
    "name": "projectname-",
    "webAppPortalName": "[concat(variables('name'), parameters('environment'), '-webapp')]",
    "appServicePlanName": "[concat(variables('name'), parameters('environment'),'-hosting')]",
  "resources": [       
    {
      "apiVersion": "2017-08-01",
      "type": "Microsoft.Web/serverfarms",
      "kind": "linux",
      "name": "[variables('appServicePlanName')]",
      "location": "[parameters('location')]",
      "comments": "This app service plan is used for the web app and slots.",
      "properties": {
        "reserved": true
      },
      "dependsOn": [],
      "sku": {
        "name": "[parameters('appServiceSku')]"
      }
    },
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2016-08-01",
      "name": "[variables('webAppPortalName')]",
      "kind": "app,linux,container",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]"
      ],
      "properties": {
        "name": "[variables('webAppPortalName')]",
        "siteConfig": {
          "linuxFxVersion": "[concat('DOCKER|', parameters('dockerImageName'))]",
          "alwaysOn": true,
          "appSettings": [
            {
              "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
              "value": "false"
            },
            {
              "name": "DOCKER_REGISTRY_SERVER_URL",
              "value": "[parameters('dockerRegistryUrl')]"
            },
            {
              "name": "DOCKER_REGISTRY_SERVER_USERNAME",
              "value": "[parameters('dockerRegistryUsername')]"
            },
            {
              "name": "DOCKER_REGISTRY_SERVER_PASSWORD",
              "value": "[parameters('dockerRegistryPassword')]"
            }
          ]
        },
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]"
      }
    }
  ]
}

_artifactsLocation_artifactsLocationSasToken不需要值,但是需要以某种方式将它们包括在内。与其他答案的主要区别是应用服务计划的reserved中包含了properties属性。

希望这可以为我省去一些头痛!

答案 1 :(得分:2)

以下ARM模板对我有用。

  • 它允许指定私有Azure容器注册表的身份验证详细信息。
  • 还要确保docker映像名称遵循以下模式:_MY_REGISTRY_USERNAME_-on.azurecr.io/_MY_NAMESPACE_/_MY_DOCKER_IMAGE_NAME_:latest

我这样运行az命令:

az group deployment create \
  --name "deployAzureApp" \
  --resource-group <MY_RESOURCE_GROUP_NAME> \
  --template-file <MY_ARM_JSON_TEMPLATE>.json  --verbose --debug

这是Azure资源管理器(ARM)JSON模板:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "appName": {
      "type": "String",
      "defaultValue": "_MY_APP_NAME_"
    },
    "dockerImageName": {
      "type": "String",
      "defaultValue": "_MY_REGISTRY_USERNAME_-on.azurecr.io/_MY_NAMESPACE_/_MY_DOCKER_IMAGE_NAME_:latest"
    },
    "dockerRegistryUrl": {
      "type": "String",
      "defaultValue": "https://_MY_REGISTRY_USERNAME_-on.azurecr.io"
    },
    "dockerRegistryUsername": {
      "type": "String",
      "defaultValue": "_MY_REGISTRY_USERNAME_"
    },
    "dockerRegistryPassword": {
      "type": "String",
      "defaultValue": "_MY_REGISTRY_PSW_"
    },
    "servicePlanName": {
      "type": "String",
      "defaultValue": "_MY_SERVICE_PLAN_NAME_"
    },
    "appLocation": {
      "type": "String",
      "defaultValue": "_MY_REGION_"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2016-08-01",
      "name": "[parameters('appName')]",
      "kind": "app,linux,container",
      "location": "[parameters('appLocation')]",
      "properties": {
        "name": "[parameters('appName')]",
        "siteConfig": {
          "linuxFxVersion": "[concat('DOCKER|', parameters('dockerImageName'))]",
          "alwaysOn": true,
          "appSettings": [
            {
              "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
              "value": "false"
            },
            {
              "name": "DOCKER_REGISTRY_SERVER_URL",
              "value": "[parameters('dockerRegistryUrl')]"
            },
            {
              "name": "DOCKER_REGISTRY_SERVER_USERNAME",
              "value": "[parameters('dockerRegistryUsername')]"
            },
            {
              "name": "DOCKER_REGISTRY_SERVER_PASSWORD",
              "value": "[parameters('dockerRegistryPassword')]"
            }
          ]
        },
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('servicePlanName'))]"
      }
    }
  ]
}

答案 2 :(得分:1)

关于容器的Azure Web App,实际上,模板中的Azure Web App仅有一点不同。关键是种类。

Azure Web应用程序:

"kind": "app"

Azure容器Web应用程序:

"kind": "app,linux,container",

因此,只需使用app,linux,container设置种类,就可以使用模板创建Azure Web App容器。

更新

我进行了测试,发现网站类型不是最重要的。关键是网站的属性:

"siteConfig": {
                    "linuxFxVersion": "DOCKER|nginx"
                },

模板将如下所示,并且做得很好。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "webAppName": {
            "type": "string",
            "metadata": {
                "description": "Base name of the resource such as web app name and app service plan "
            },
            "minLength": 2
        },
        "sku": {
            "type": "string",
            "defaultValue": "S1",
            "metadata": {
                "description": "The SKU of App Service Plan "
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Location for all resources."
            }
        }
    },
    "variables": {
        "webAppPortalName": "[concat(parameters('webAppName'), '-webapp')]",
        "appServicePlanName": "[concat('AppServicePlan-', parameters('webAppName'))]"
    },
    "resources": [
        {
            "apiVersion": "2017-08-01",
            "type": "Microsoft.Web/serverfarms",
            "kind": "linux",
            "name": "[variables('appServicePlanName')]",
            "location": "[parameters('location')]",
            "comments": "This app service plan is used for the web app and slots.",
            "properties": {},
            "dependsOn": [],
            "sku": {
                "name": "[parameters('sku')]"
            }
        },
        {
            "apiVersion": "2016-08-01",
            "type": "Microsoft.Web/sites",
            "name": "[variables('webAppPortalName')]",
            "location": "[parameters('location')]",
            "comments": "This is the web app, also the default 'nameless' slot.",
            "properties": {
                "name": "[parameters('webAppName')]",
                "siteConfig": {
                    "appCommandLine": "",
                    "linuxFxVersion": "DOCKER|nginx"
                },
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]"
            ]
        }
    ]
}

答案 3 :(得分:1)

根据我的观察, linuxFxVersion 问题取决于托管Web App的App Service计划。帮助我的是将App Service Plan资源的属性对象上的保留属性设置为 true

Microsoft doumentation保留设置的评价:

如果Linux应用服务计划为true,否则为false。

关于它的默认值,它什么也没有说,但是从我观察到它的 false 来看。

这是我的App Service Plan资源ARM模板的外观。

{
  "type": "Microsoft.Web/serverfarms",
  "apiVersion": "2019-08-01",
  "name": "[variables('appServicePlanName')]",
  "location": "[parameters('location')]",
  "sku": {
    "name": "[parameters('appServicePlanSkuName')]",
    "tier": "[parameters('appServicePlanSkuTier')]",
    "size": "[parameters('appServicePlanSkuSize')]",
    "family": "[parameters('appServicePlanSkuFamily')]",
    "capacity": "[parameters('appServicePlanSkuCapacity')]"
  },
  "kind": "linux",
  "properties": {
    "name": "[variables('appServicePlanName')]",
    "reserved": true
  }
}

答案 4 :(得分:0)

上面的模板现在不起作用,将显示以下日志-

[error]BadRequest: {
  "Code": "BadRequest",
  "Message": "The parameter LinuxFxVersion has an invalid value.",
  "Target": null,
  "Details": [
    {
      "Message": "The parameter LinuxFxVersion has an invalid value."
    },
    {
      "Code": "BadRequest"
    },
    {
      "ErrorEntity": {
        "ExtendedCode": "01007",
        "MessageTemplate": "The parameter {0} has an invalid value.",
        "Parameters": [
          "LinuxFxVersion"
        ],
        "Code": "BadRequest",
        "Message": "The parameter LinuxFxVersion has an invalid value."
      }
    }
  ],
  "Innererror": null
}

要解决此问题,我们可以按照以下博客中所述将其部署为2个步骤 Updated Solution

相关问题