如何使用 Packer + Terraform 创建小于 30GB 的自定义 Azure 映像? [英] How do I create custom Azure images smaller than 30GB with Packer + Terraform?
问题描述
我想在一个业余项目中创建 4GB 的自定义图像以节省成本.我已经能够使用以下方法在 Terraform 中成功设置 Azure 提供的 Ubuntu 18.04 base 映像的大小:
I want to create custom images that are 4GB for cost-saving purposes on a side project. I've been able set the size for the Azure-provided Ubuntu 18.04 base image in Terraform successfully using the following:
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 文档,使用托管磁盘 + 自定义映像不是很简单,但这既不是这里也不是那里):
So I tried making the following changes to use a custom Packer image that I had created from this Ubuntu base image (per the terraform-provider-azurerm docs using a managed disk + custom image not very straightforward, but that's neither here not there):
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"
}
...
}
当我进行更改但出现错误时:
When I make that change but I get the error:
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 模板中:
"No big deal", I thought, "I'll just make the actual image 4GB". So, I tried adding the line "os_disk_size_gb": 4
to my Packer template:
{
"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 的 VM 磁盘,这比我需要的要大得多.我在这里有什么遗漏吗?或者使用 Packer + Terraform 在 Azure 中拥有小于 30GB 的自定义图像是不可能的?
Removing both disk_size_gb = "4"
from the Terraform plan and "os_disk_size_gb": 4
from the Packer template results in successful image creation and deploy, but I'm running a 30GB VM disk which is way larger than what I need. Is there anything I'm missing here? Or it is just not possible to have have custom images in Azure less than 30GB using Packer + Terraform?
推荐答案
这不是打包程序限制,而是 Azure 对基础映像的限制.这些映像文件可以小至 1 GB,但默认的 Ubuntu 映像具有 30 GB 的操作系统磁盘.而且您无法创建磁盘小于基本映像的 VM.
This is not a packer restriction but rather an restriction of Azure with regards to the base image. Those image files can be as small as 1 GB, but the default Ubuntu image has a 30GB OS disk. And you can't create a VM with a disk smaller than the base image.
https://docs.azure.cn/zh-cn/articles/azure-marketplace/imageguide#3-
VHD 图像文件的大小必须介于 1GB 和 1TB 之间.
VHD image files must be between 1GB and 1TB in size.
如果您想要低于 30GB,您可能需要从头开始创建整个映像.参见例如https://docs.azure.cn/en-us/articles/azure-marketplace/imagecreateonlocal
You probably need to create the whole image from scratch if you want to go below 30GB. See e.g. https://docs.azure.cn/en-us/articles/azure-marketplace/imagecreateonlocal
这篇关于如何使用 Packer + Terraform 创建小于 30GB 的自定义 Azure 映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!