Linux で Azure VM Image Builder を使用して既存のイメージから新しい VM イメージを作成する

適用対象: ✔️ Linux VM ✔️ フレキシブルなスケール セット

この記事では、既存のイメージ バージョンを Azure Compute Gallery (旧称 Shared Image 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. サブスクリプション ID の変数を作成します。

    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)
    

ユーザー割り当て ID を作成し、リソース グループにアクセス許可を設定する

前の例でユーザー ID を設定したので、今度はリソース ID を取得する必要があります。この ID はテンプレートに追加されます。

#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 の例の変更

使用しようとしている JSON の例を helloImageTemplateforSIGfromSIG.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 アドレスを使用して VM への Secure Shell (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 テンプレートを作成する」を参照してください。