首页 > 解决方案 > 如何使用 Packer + Terraform 创建小于 30GB 的自定义 Azure 映像?

问题描述

我想在一个辅助项目中创建 4GB 的自定义图像以节省成本。我已经能够使用以下命令在 Terraform 中成功设置 Azure 提供的 Ubuntu 18.04基础映像的大小:

resource "azurerm_managed_disk" "example-disk" {
  ...
  create_option = "FromImage"
  disk_size_gb = "4"
}

resource "azurerm_virtual_machine" "example" {
  ...
  vm_size = "Standard_B1s"

  storage_image_reference {
    publisher = "Canonical"
    offer = "UbuntuServer"
    sku = "18.04-LTS"
    version = "latest"
  }

  storage_os_disk {
    name = azurerm_managed_disk.example-disk.name
    managed_disk_id = azurerm_managed_disk.example-disk.id
    create_option = "Attach"
    caching = "ReadWrite"
  }
  ...
}

因此,我尝试进行以下更改以使用我从这个 Ubuntu 基础映像创建的自定义 Packer 映像(根据 terraform-provider-azurerm 文档,使用托管磁盘 + 自定义映像不是很简单,但这既不是这里也不是那里):

variable "packer_image_id" {}

variable "packer_image_name" {}

data "azurerm_image" "custom" {
  ...
  name = var.packer_image_name
}

resource "azurerm_virtual_machine" "example" {
  ...
  vm_size = "Standard_B1s"

  delete_os_disk_on_termination = true

  storage_image_reference {
    id = data.azurerm_image.custom.id
  }

  storage_os_disk {
    create_option = "FromImage"
    caching = "ReadWrite"
    disk_size_gb = "4"
  }
  ...
}

当我进行更改但出现错误时:

Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="OperationNotAllowed" Message="The specified disk size 4 GB is smaller than the size of the corresponding disk in the VM image: 30 GB. This is not allowed. Please choose equal or greater size or do not specify an explicit size." Target="osDisk.diskSizeGB"

“没什么大不了的”,我想,“我只要把实际图像变成 4GB 就行了”。所以,我尝试将这一行添加"os_disk_size_gb": 4到我的 Packer 模板中:

{
  "variables": [ ... ],
  "builders": [
    {
      "type": "azure-arm",
      "client_id": "{{ user `azure_client_id` }}",
      "client_secret": "{{ user `azure_client_secret` }}",
      "subscription_id": "{{ user `azure_subscription_id` }}",
      "tenant_id": "{{ user `azure_tenant_id` }}",
      "location": "eastus2",
      "vm_size": "Standard_B1s",
      "os_type": "Linux",
      "os_disk_size_gb": 4,
      "image_publisher": "Canonical",
      "image_offer": "UbuntuServer",
      "image_sku": "18.04-LTS",
      "ssh_username": "packer",
      "managed_image_name": "example-{{ isotime \"20060102-150405\" }}",
      "managed_image_resource_group_name": "packer-images",
      "azure_tags": {}
    }
  ],
  "provisioners": [ ... (omitting for space: just a "remote-exec" that creates a new user, downloads Tomcat, and enables service) ]
}

但我得到这个错误:

==> azure-arm: ERROR:   -> OperationNotAllowed : The specified disk size 4 GB is smaller than the size of the corresponding disk in the VM image: 30 GB. This is not allowed. Please choose equal or greater size or do not specify an explicit size.

disk_size_gb = "4"从 Terraform 计划和Packer 模板中删除都可以"os_disk_size_gb": 4成功创建和部署映像,但我正在运行一个 30GB 的 VM 磁盘,这比我需要的要大得多。我在这里有什么遗漏吗?或者使用 Packer + Terraform 在 Azure 中拥有小于 30GB 的自定义图像是不可能的?

标签: azureterraformpackerterraform-provider-azure

解决方案


这不是打包程序限制,而是 Azure 对基本映像的限制。这些映像文件可以小至 1 GB,但默认的 Ubuntu 映像具有 30 GB 的操作系统磁盘。而且您无法创建磁盘小于基本映像的 VM。

https://docs.azure.cn/en-us/articles/azure-marketplace/imageguide#3-

VHD 映像文件的大小必须介于 1GB 和 1TB 之间。

如果你想低于 30GB,你可能需要从头开始创建整个图像。参见例如https://docs.azure.cn/en-us/articles/azure-marketplace/imagecreateonlocal


推荐阅读