建立 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 區段的值。

註冊提供者

若要使用 Azure Image Builder,您必須註冊該功能。

檢查註冊。

az provider show -n Microsoft.VirtualMachineImages | grep registrationState
az provider show -n Microsoft.KeyVault | grep registrationState
az provider show -n Microsoft.Compute | grep registrationState
az provider show -n Microsoft.Storage | grep registrationState
az provider show -n Microsoft.Network | grep registrationState
az provider show -n Microsoft.ContainerInstance | 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

設定變數和授權

由於我們會重複使用某些資訊,因此我們將建立一些變數來儲存這些資訊。

Image Builder 僅支援在與來源受控映像相同的資源群組中建立自訂映像。 將此範例中的資源群組名稱更新為與來源受控映像相同。

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

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

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

建立資源群組。

az group create -n $sigResourceGroup -l $location

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

Image Builder 會使用所提供的使用者身分識別,將映像插入 Azure Compute Gallery。 在此範例中,您將建立 Azure 角色定義,其中包括將映像發佈到資源庫的細微動作。 然後此將角色定義指派給使用者身分識別。

# create user assigned identity for image builder to access the storage account where the script is located
identityName=aibBuiUserId$(date +'%s')
az identity create -g $sigResourceGroup -n $identityName

# get identity id
imgBuilderCliId=$(az identity show -g $sigResourceGroup -n $identityName --query clientId -o tsv)

# get the user identity URI, needed for the template
imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$sigResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$identityName

# this command will download an Azure role definition template, and update the template with the parameters 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 role definition to the user assigned identity
az role assignment create \
    --assignee $imgBuilderCliId \
    --role "$imageRoleDefName" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup

若要搭配使用 Azure Compute Gallery 和 Image Builder,您需要有現有的資源庫和映像定義。 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 18.04-LTS \
   --os-type Linux

下載並設定 .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 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

根據 Azure 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" \
  --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 :-)         **
*******************************************************

清除資源

如果您現在想要嘗試重新自訂映像版本以建立相同映像的新版本,請略過後續步驟並移至使用 Azure Image Builder 建立另一個映像版本

這會刪除已建立的映像,以及所有其他資源檔。 在刪除資源前,請先確定您已完成此部署。

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

刪除映像建立器範本。

az resource delete \
    --resource-group $sigResourceGroup \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n helloImageTemplateforSIG01

刪除授權指派、角色和身分識別

az role assignment delete \
    --assignee $imgBuilderCliId \
    --role "$imageRoleDefName" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup

az role definition delete --name "$imageRoleDefName"

az identity delete --ids $imgBuilderId

取得映像建立器所建立的映像版本 (此版本一律以 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

刪除映像定義。

az sig image-definition delete \
   -g $sigResourceGroup \
   --gallery-name $sigName \
   --gallery-image-definition $imageDefName \
   --subscription $subscriptionID

刪除資源庫。

az sig delete -r $sigName -g $sigResourceGroup

刪除該資源群組。

az group delete -n $sigResourceGroup -y

下一步

深入了解 Azure Compute Gallery