是什么
Gallery 是用户版本image 管理工具,管理自定义的image。(比如用户需要一个安装了java8 的 centos7 image)
对应的一个概念是 Azure Marketplace images ,这是微软云提供的image,供用户使用。
Shared Image Gallery 是微软2019年推出的服务。在此之前,用户可以创建custom image,但这个image 不能跨 region,也不能跨 subscription,这项服务就满足了这项需求。
相关概念
Graphic showing how you can have multiple versions of an image in your gallery根据上面这张图,创建资源的顺序是 image Gallery -> Image definition -> image Version.
在最后一步 image version 中选择合适的 image。
Create a share image gallery
az sig create --gallery-name myGallery --resource-group myGalleryRG
Create an image definition
az sig image-definition create \
--resource-group myGalleryRG \
--gallery-name myGallery \
--gallery-image-definition myImageDefinition \
--publisher myPublisher \
--offer myOffer \
--sku mySKU \
--os-type Linux \
--os-state specialized
Create the image version
az sig image-version create \
--resource-group myGalleryRG \
--gallery-name myGallery \
--gallery-image-definition myImageDefinition \
--gallery-image-version 1.0.0 \
--target-regions "westcentralus" "southcentralus=1" "eastus=1=standard_zrs" \
--managed-image "/subscriptions/<Subscription ID>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM"
调用
查询
可以从网页上找到Resource ID 的值,然后在创建VM的时候直接使用它
也可以用命令行查找 image definition
resourceGroup=myRG
gallery=my_gallery
az sig image-definition list --resource-group $resourceGroup --gallery-name $gallery --query "[].[name, id]" --output tsv
...
# 获取
/subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/galleries/my_gallery/images/base
这个image definition 可以用来直接创建 VM。
还可以更具体地查询image version。
resourceGroup=myRG
gallery=my_gallery
imageDefinition=base
az sig image-version list --resource-group $resourceGroup --gallery-name $gallery --gallery-image-definition $imageDefinition --query "[].[name, id]" --output tsv
...
# 获取
0.1.1 /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/galleries/my_gallery/images/base/versions/0.1.1
0.1.2 /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/galleries/my_gallery/images/base/versions/0.1.2
创建VM
Azure CLI 方式
可以用 image definition
az vm create -g roy-gallery -n roy-vm --generate-ssh-keys --image /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/galleries/my_gallery/images/base
也可以用 image version(注意对比,就是在结尾部分多了版本信息)
az vm create -g roy-gallery -n roy-version --generate-ssh-keys --image /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/galleries/my_gallery/images/base/versions/0.1.2
Terraform 方式
通过data 来获取属性,在module 中调用
data "azurerm_shared_image_version" "base" {
name = "0.1.2"
image_name = "myIdf"
gallery_name = "my_gallery"
resource_group_name = "MyRG"
provider = azurerm.images
}
module "compute" {
source = "Azure/compute/azurerm"
version = "3.2.0"
vm_os_id = data.azurerm_shared_image_version.base.id
...
}
参考:
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/shared-images-portal
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/shared-image-galleries
https://docs.microsoft.com/en-us/azure/virtual-machines/linux/shared-images-portal
https://docs.microsoft.com/en-us/cli/azure/sig?view=azure-cli-latest#az-sig-create
https://docs.microsoft.com/en-us/azure/virtual-machines/image-version-vm-cli
https://docs.microsoft.com/en-us/azure/virtual-machines/vm-generalized-image-version-cli
网友评论