使用 Terraform,如何为 Windows 和 Docker 创建 Azure 应用服务

Using Terraform, how to create an Azure App Service for Windows and Docker

我的团队正在尝试使用 Terraform 创建使用 Windows Docker 容器的 Azure 应用服务。

我已经测试 the docker-basic example, from the Terraform GitHub project,成功创建了 Linux Docker 应用服务。

接下来,我使用了 azurerm_app_service and azurerm_app_service_plan, to change the main.tf file to one that will create a Windows Docker App Service, with a Windows IIS Docker image 的 Terraform 文档。这是更新后的 main.tf 文件:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "main" {
  name     = "${var.prefix}-resources"
  location = "${var.location}"
}

resource "azurerm_app_service_plan" "main" {
  name                = "${var.prefix}-asp"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"
  kind                = "Windows"
  reserved            = false

  sku {
    tier = "Standard"
    size = "S1"
  }
}

resource "azurerm_app_service" "main" {
  name                = "${var.prefix}-appservice"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"
  app_service_plan_id = "${azurerm_app_service_plan.main.id}"

  site_config {
    app_command_line = ""
    windows_fx_version = "DOCKER|windows/servercore/iis:windowsservercore-ltsc2019"
  }

  app_settings = {
    "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
    "DOCKER_REGISTRY_SERVER_URL"          = "https://mcr.microsoft.com"
  }
}

有 4 个变化。在 azurerm_app_service_plan 中,kind 字段现在设置为“Windows”,reserved 字段现在设置为 false。在azurerm_app_service中,Docker图像在windows_fx_version字段中指定(而不是linux_fx_version),DOCKER_REGISTRY_SERVER_URL指向微软public docker 注册表 mcr.microsoft.com (而不是 docker.io

Terraform 应用成功,但是创建的应用服务不是 Docker 应用服务。相反,它是在 .NET 堆栈配置中创建的。我们将创建的内容与我们在 Azure 门户中创建的 Windows Docker 应用服务进行了比较,这显然是不正确的。

如果需要更多更改,或者如果我的任何更改有误,我和我的团队无法从官方文档中检测到它们可能是什么。在文档中我们错过了什么吗?文档中是否缺少步骤?这是真正的 Terraform 错误吗?

我在我的实验室试过你给的代码。

在 Portal 和 Terraform 中创建时的主要区别在于,在 Portal 中,它默认使用 “Isxenon:true”kind as “app,container,windows”,即使对于 windows docker 容器也是如此。但是,在使用 Terraform 创建 windows docker 容器时,它需要 “isxenon :false”kind as “app” 这就是为什么它默认提供堆栈设置作为基于代码的应用程序。

其次,Xenonstandard Sku中不支持,应该是premiumv3 Sku[=43] =].

因此,在部署 windows docker 容器时,对代码进行以下更改:

In the App service plan: Kind = “xenon”, Is_xenon = true, Sku tier = premiumv3 Size = p1v3

那么main.tf文件就是这样

provider "azurerm" {
  features {}
}

data "azurerm_resource_group" "main" {
  name     = "example-resources"
}

resource "azurerm_app_service_plan" "main" {
  name                = "testansuman-asp"
  location            = data.azurerm_resource_group.main.location
  resource_group_name = data.azurerm_resource_group.main.name
  kind                = "xenon"
  reserved            = false       
  is_xenon = true
  sku {
    tier = "PremiumV3"
    size = "P1v3"
  }
}

resource "azurerm_app_service" "main" {
  name                = "dockerwindows-appservice"
  location            = data.azurerm_resource_group.main.location
  resource_group_name = data.azurerm_resource_group.main.name
  app_service_plan_id = azurerm_app_service_plan.main.id

  site_config {
    windows_fx_version = "DOCKER|mcr.microsoft.com/azure-app-service/windows/parkingpage:latest"
    use_32_bit_worker_process = true
  }

  app_settings = {
    "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
    "DOCKER_REGISTRY_SERVER_URL"          = "https://mcr.microsoft.com"
  }
}

我也测试过了,现在在配置页面中没有看到任何堆栈设置。

注意: 当您创建基于代码的应用程序时,您可以将类型用作“APP/Windows”,但创建一个 docker 容器应用程序时,您有select 那种“氙气”和你想要使用的 windows docker 图片。