如何使用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映像(根据地形提供商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.

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

这不是打包器限制,而是Azure对基本映像的限制。这些映像文件可以小到1GB,但默认的Ubuntu映像有一个30GB的操作系统磁盘。而且,您不能创建一个磁盘小于基本映像的虚拟机。

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

最新更新