如何使用Packer + Terraform创建小于30GB的自定义Azure映像? [英] How do I create custom Azure images smaller than 30GB with Packer + Terraform?
问题描述
我想在副项目上创建4GB的自定义映像,以节省成本.我已经能够使用以下命令成功在Terraform中设置Azure提供的Ubuntu 18.04 基本映像的大小:
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映像具有30GB的操作系统磁盘.而且,您无法使用小于基本映像的磁盘来创建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/zh-cn/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屋!