使用 Azure CLI 建立 Linux 映像並將其發佈至 Azure Compute Gallery

適用於:✔️ Linux VM ✔️ 彈性擴展集

在本文中,您會了解如何使用 Azure Image Builder 及 Azure CLI 在 Azure Compute Gallery (先前稱作共用映像庫) 中建立映像版本,然後以全域方式發佈該映像。 您也可以使用 Azure PowerShell 建立映像版本。

本文會使用範例 JSON 範本來設定映像。 JSON 檔案位於 helloImageTemplateforSIG.json

為了將映像散發到 Azure Compute Gallery,範本會使用 sharedImage 作為範本 distribute 區段的值。

註冊提供者

若要使用 VM Image Builder,您必須註冊提供者。 執行下列命令來檢查您的註冊:

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 Image Builder 僅支援在與來源受控映像相同的資源群組中建立自訂映像。 在下列範例中,將資源群組名稱更新為與來源受控映像相同。

# 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

為訂用帳戶識別碼建立變數:

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

建立資源群組:

az group create -n $sigResourceGroup -l $location

建立使用者指派的身分識別,並在資源群組上設定權限

VM Image Builder 會使用所提供的使用者身分識別,將映像插入 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

若要搭配使用 Azure Compute Gallery 和 VM Image Builder,您需要有現有的資源庫和映像定義。 VM Image Builder 不會為您建立資源庫和映像定義。

如果您尚未有可用的映像庫和映像定義,請先建立。

首先,建立資源庫:

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 Image Builder 服務:

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 Image Builder 所建立的映像版本建立 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 Image Builder 建立另一個映像版本

如果您不再需要您依照本文中的程序所建立的資源,您可以執行下列動作來刪除這些資源。

此程序會刪除您建立的映像和其他所有資源檔。 刪除資源之前,請確定您已完成此部署。

刪除資源庫的資源時,您必須先刪除所有映像版本,才能刪除用來建立它們的映像定義。 若要刪除資源庫,您必須先刪除資源庫中的所有映像定義。

  1. 刪除 VM Image Builder 範本。

    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 Image Builder 所建立的映像版本 (一律以 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