你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure CLI 创建 Linux 映像并将其分发到 Azure Compute Gallery

适用于:✔️ Linux VM ✔️ 灵活规模集

在本文中,你将了解如何使用 Azure VM 映像生成器和 Azure CLI 在 Azure Compute Gallery(以前称为 共享映像库)中创建映像版本,然后全局分发此映像。 还可以使用 Azure PowerShell 创建映像版本。

本文使用示例 JSON 模板来配置映像。 JSON 文件位于 helloImageTemplateforSIG.json

此模板使用 sharedImage 作为模板的 distribute 部分的值,以便将映像分发到 Azure Compute Gallery。

注册提供程序

若要使用 VM 映像生成器,则需要注提供程序。 运行以下命令来检查注册:

az provider show -n Microsoft.VirtualMachineImages -o json | grep registrationState
az provider show -n Microsoft.KeyVault -o json | grep registrationState
az provider show -n Microsoft.Compute -o json | grep registrationState
az provider show -n Microsoft.Storage -o json | grep registrationState
az provider show -n Microsoft.Network -o json | grep registrationState
az provider show -n Microsoft.ContainerInstance -o json | grep registrationState

如果输出未显示“已注册”,请运行以下命令:

az provider register -n Microsoft.VirtualMachineImages
az provider register -n Microsoft.Compute
az provider register -n Microsoft.KeyVault
az provider register -n Microsoft.Storage
az provider register -n Microsoft.Network
az provider register -n Microsoft.ContainerInstance

设置变量和访问权限

因为你将重复使用某些信息,所以要创建一些变量来存储这些信息。

VM 映像生成器仅支持在与源托管映像相同的资源组中创建自定义映像。 在以下示例中,将资源组名称更新为与源托管映像相同的资源组。

# Resource group name - ibLinuxGalleryRG in this example
sigResourceGroup=ibLinuxGalleryRG
# Datacenter location - West US 2 in this example
location=westus2
# Additional region to replicate the image to - East US in this example
additionalregion=eastus
# Name of the Azure Compute Gallery - myGallery in this example
sigName=myIbGallery
# Name of the image definition to be created - myImageDef in this example
imageDefName=myIbImageDef
# Reference name in the image distribution metadata
runOutputName=aibLinuxSIG

为你的订阅 ID 创建变量:

subscriptionID=$(az account show --query id --output tsv)

创建资源组:

az group create -n $sigResourceGroup -l $location

创建用户分配的标识,并在资源组上设置权限

VM 映像生成器使用提供的用户标识将映像注入到 Azure Compute Gallery 中。 在此示例中将创建一个 Azure 角色定义,该定义具有分发映像的特定操作。 然后将角色定义分配给用户标识。

# Create user-assigned identity for VM Image Builder to access the storage account where the script is stored
identityName=aibBuiUserId$(date +'%s')
az identity create -g $sigResourceGroup -n $identityName

# Get the identity ID
imgBuilderCliId=$(az identity show -g $sigResourceGroup -n $identityName --query clientId -o tsv)

# Get the user identity URI that's needed for the template
imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$sigResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$identityName

# Download an Azure role-definition template, and update the template with the parameters that were specified earlier
curl https://raw.githubusercontent.com/Azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json

imageRoleDefName="Azure Image Builder Image Def"$(date +'%s')

# Update the definition
sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleImageCreation.json
sed -i -e "s/<rgName>/$sigResourceGroup/g" aibRoleImageCreation.json
sed -i -e "s/Azure Image Builder Service Image Creation Role/$imageRoleDefName/g" aibRoleImageCreation.json

# Create role definitions
az role definition create --role-definition ./aibRoleImageCreation.json

# Grant a role definition to the user-assigned identity
az role assignment create \
    --assignee $imgBuilderCliId \
    --role "$imageRoleDefName" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup

若要将 VM 映像生成器与 Azure Compute Gallery 配合使用,需要现有的库和映像定义。 VM 映像生成器不会创建库和映像定义。

如果还没有可使用的库和映像定义,请先创建它们。

首先,创建库:

az sig create \
    -g $sigResourceGroup \
    --gallery-name $sigName

然后,创建映像定义:

az sig image-definition create \
   -g $sigResourceGroup \
   --gallery-name $sigName \
   --gallery-image-definition $imageDefName \
   --publisher myIbPublisher \
   --offer myOffer \
   --sku 20_04-lts-gen2 \
   --os-type Linux \
   --hyper-v-generation V2 \
   --features SecurityType=TrustedLaunchSupported

下载并配置 JSON 文件

下载 JSON 模板,并使用你的变量对它进行配置:

curl https://raw.githubusercontent.com/Azure/azvmimagebuilder/master/quickquickstarts/1_Creating_a_Custom_Linux_Shared_Image_Gallery_Image/helloImageTemplateforSIG.json -o helloImageTemplateforSIG.json
sed -i -e "s/<subscriptionID>/$subscriptionID/g" helloImageTemplateforSIG.json
sed -i -e "s/<rgName>/$sigResourceGroup/g" helloImageTemplateforSIG.json
sed -i -e "s/<imageDefName>/$imageDefName/g" helloImageTemplateforSIG.json
sed -i -e "s/<sharedImageGalName>/$sigName/g" helloImageTemplateforSIG.json
sed -i -e "s/<region1>/$location/g" helloImageTemplateforSIG.json
sed -i -e "s/<region2>/$additionalregion/g" helloImageTemplateforSIG.json
sed -i -e "s/<runOutputName>/$runOutputName/g" helloImageTemplateforSIG.json
sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" helloImageTemplateforSIG.json

创建映像版本

在本部分中,将在库中创建映像版本。

将映像配置提交到 Azure VM 映像生成器服务:

az resource create \
    --resource-group $sigResourceGroup \
    --properties @helloImageTemplateforSIG.json \
    --is-full-object \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n helloImageTemplateforSIG01

启动映像生成:

az resource invoke-action \
     --resource-group $sigResourceGroup \
     --resource-type  Microsoft.VirtualMachineImages/imageTemplates \
     -n helloImageTemplateforSIG01 \
     --action Run

创建映像并复制到这两个区域可能需要一些时间。 等待至此部分完成,然后再继续创建 VM。

创建 VM

根据 VM 映像生成器创建的映像版本创建 VM。

az vm create \
  --resource-group $sigResourceGroup \
  --name myAibGalleryVM \
  --admin-username aibuser \
  --location $location \
  --image "/subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup/providers/Microsoft.Compute/galleries/$sigName/images/$imageDefName/versions/latest" \
  --security-type TrustedLaunch \
  --generate-ssh-keys

通过安全外壳 (SSH) 连接到 VM:

ssh aibuser@<publicIpAddress>

建立 SSH 连接后,应会立即看到映像已使用当天的一个消息进行了自定义:

*******************************************************
**            This VM was built from the:            **
**      !! AZURE VM IMAGE BUILDER Custom Image !!    **
**         You have just been Customized :-)         **
*******************************************************

清理资源

注意

如果你现在就想要尝试重新自定义映像版本以创建同一映像的新版本,请跳过此处概述的步骤并转到使用 VM 映像生成器创建另一个映像版本

如果你按照本文中的过程创建的资源已不再需要,则可以通过执行以下操来删除资源。

此过程会删除你已创建的映像以及所有其他资源文件。 在删除资源之前,请确保已完成此部署。

删除库资源时,需要先删除所有版本的映像,然后才能删除用于创建它们的映像定义。 若要删除库,首先需要删除库中的所有映像定义。

  1. 删除 VM 映像生成器模板。

    az resource delete \
        --resource-group $sigResourceGroup \
        --resource-type Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateforSIG01
    
  2. 删除权限分配、角色和标识。

    az role assignment delete \
        --assignee $imgBuilderCliId \
        --role "$imageRoleDefName" \
        --scope /subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup
    
    az role definition delete --name "$imageRoleDefName"
    
    az identity delete --ids $imgBuilderId
    
  3. 获取由 VM 映像生成器创建的映像版本(始终以 0. 开头),然后将其删除。

    sigDefImgVersion=$(az sig image-version list \
    -g $sigResourceGroup \
    --gallery-name $sigName \
    --gallery-image-definition $imageDefName \
    --subscription $subscriptionID --query [].'name' -o json | grep 0. | tr -d '"')
    az sig image-version delete \
    -g $sigResourceGroup \
    --gallery-image-version $sigDefImgVersion \
    --gallery-name $sigName \
    --gallery-image-definition $imageDefName \
    --subscription $subscriptionID
    
  4. 删除映像定义。

    az sig image-definition delete \
    -g $sigResourceGroup \
    --gallery-name $sigName \
    --gallery-image-definition $imageDefName \
    --subscription $subscriptionID
    
  5. 删除库。

    az sig delete -r $sigName -g $sigResourceGroup
    
  6. 删除该资源组。

    az group delete -n $sigResourceGroup -y
    

后续步骤

详细了解 Azure Compute Gallery