在 Linux 中使用 Azure VM Image Builder 根據現有的映像建立新的 VM 映像

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

在本文中,您將了解如何更新 Azure Compute Gallery (先前名為共用映像庫) 中的現有映像版本,並將其發佈至資源庫作為新的映像版本。

若要設定映像,您可以使用範例 JSON 範本 helloImageTemplateforSIGfromSIG.json

註冊提供者

若要使用 VM Image Builder,您必須註冊提供者。

  1. 檢查您的提供者註冊。 請確定每一個都有傳回 Registered

    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
    
  2. 如果未傳回 Registered,請執行下列命令來註冊提供者:

    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
    

設定變數和授權

如果您已透過建立映像並將其發佈到 Azure Compute Gallery建立 Azure Compute Gallery,則表示您已建立一些所需的變數。

  1. 如果您尚未建立變數,請執行下列命令:

    # Resource group name 
    sigResourceGroup=ibLinuxGalleryRG
    # Gallery location 
    location=westus2
    # Additional region to replicate the image version to 
    additionalregion=eastus
    # Name of the Azure Compute Gallery 
    sigName=myIbGallery
    # Name of the image definition to use
    imageDefName=myIbImageDef
    # image distribution metadata reference name
    runOutputName=aibSIGLinuxUpdate
    
  2. 為訂閱識別碼建立變數:

    subscriptionID=$(az account show --query id --output tsv)
    
  3. 取得您想要更新的映像版本:

    sigDefImgVersionId=$(az sig image-version list \
      -g $sigResourceGroup \
      --gallery-name $sigName \
      --gallery-image-definition $imageDefName \
      --subscription $subscriptionID --query [].'id' -o tsv)
    

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

您已在先前的範例中設定使用者身分識別,因此現在您需要取得資源識別碼,並將其附加至範本。

#get identity used previously
imgBuilderId=$(az identity list -g $sigResourceGroup --query "[?contains(name, 'aibBuiUserId')].id" -o tsv)

如果您已經有 Azure Compute Gallery,但未依照先前的範例加以設定,則需要將資源群組的權限指派給 VM Image Builder,以便允許存取資源庫。 如需詳細資訊,請參閱建立映像並將其發佈到 Azure Compute Gallery

修改 helloImage 範例

您可以檢閱 helloImageTemplateforSIGfromSIG.json 中即將使用的 JSON 範例。 如需 JSON 檔案的相關資訊,請參閱建立 Azure VM Image Builder 範本

  1. 下載 JSON 範例,步驟可參見使用 Azure CLI 建立 Linux 映像並將其發佈至 Azure Compute Gallery

  2. 使用變數設定 JSON:

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

建立映像

  1. 將映像組態提交至 VM Image Builder 服務:

    az resource create \
        --resource-group $sigResourceGroup \
        --properties @helloImageTemplateforSIGfromSIG.json \
        --is-full-object \
        --resource-type Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateforSIGfromSIG01
    
  2. 啟動映像組建:

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

等待映像建置並複製後,再繼續進行下一個步驟。

建立 VM

  1. 執行下列動作以建立 VM:

    az vm create \
    --resource-group $sigResourceGroup \
    --name aibImgVm001 \
    --admin-username azureuser \
    --location $location \
    --image "/subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup/providers/Microsoft.Compute/galleries/$sigName/images/$imageDefName/versions/latest" \
    --generate-ssh-keys
    
  2. 使用 VM 的公用 IP 位址建立安全殼層 (SSH) 連線。

    ssh azureuser@<pubIp>
    

    建立 SSH 連線後,您應該會接收到「當天的訊息」,顯示該映像是自訂的:

    *******************************************************
    **            This VM was built from the:            **
    **      !! AZURE VM IMAGE BUILDER Custom Image !!    **
    **         You have just been Customized :-)         **
    *******************************************************
    
  3. 輸入 exit 以關閉 SSH 連線。

  4. 若要列出您資源庫中現在可用的映像版本,請執行:

    az sig image-version list -g $sigResourceGroup -r $sigName -i $imageDefName -o table
    

下一步

若要深入了解本文中使用的 JSON 檔案,請參閱建立 Azure VM Image Builder 範本