繁体   English   中英

使用 ARM 模板为容器部署 Web 应用程序

[英]Deploying Web App for containers using ARM templates

我一直在尝试将我的资源自动部署到 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 应用程序并使用 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 应用程序,我将不胜感激。

对我来说,这些其他答案都不起作用。 在 Azure 支持和这些其他答案的帮助下,我想出了以下模板,该模板成功地创建了一个应用服务计划,其中包含运行 Azure 容器存储库中的自定义 Docker 映像的容器的 Linux 应用服务:

{
  "$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的应用程序服务计划。

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

以下 ARM 模板对我有用。

  • 它允许指定私有 Azure 容器注册表的身份验证详细信息。
  • 还要确保_MY_REGISTRY_USERNAME_-on.azurecr.io/_MY_NAMESPACE_/_MY_DOCKER_IMAGE_NAME_:latest镜像名称遵循以下模式: _MY_REGISTRY_USERNAME_-on.azurecr.io/_MY_NAMESPACE_/_MY_DOCKER_IMAGE_NAME_:latest : _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'))]"
      }
    }
  ]
}

关于Azure Web App for Container,其实模板中与Azure Web App只有一点不同。 重点是那种类型。

Azure Web 应用程序:

"kind": "app"

适用于容器的 Azure Web 应用:

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

因此,您可以使用模板创建用于容器的 Azure Web 应用程序,只需使用app,linux,container设置种类即可。

更新

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

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

对于我所观察到的, linuxFxVersion问题取决于托管 Web 应用程序的应用程序服务计划。 帮助我的是将应用服务计划资源的属性对象上的保留属性设置为true

Microsoft doumentation关于保留设置的说明:

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

它没有说明它的默认值,但从我观察到的它的false

这就是我的应用服务计划资源 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
  }
}

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

[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 个步骤部署过程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM