Share via


Azure VM Image Builder を使用して、Linux VM から既存の Azure 仮想ネットワークにアクセスする

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

この記事では、Azure VM Image Builder を使用して、仮想ネットワーク上の既存のリソースにアクセスできる基本的なカスタマイズ Linux イメージを作成する方法について説明します。 作成したビルド仮想マシン (VM) は、サブスクリプションに指定した新規または既存の仮想ネットワークにデプロイされます。 既存の Azure 仮想ネットワークを使用する場合、VM Image Builder ではパブリック ネットワーク接続は必要ありません。

前提条件

  • Azure Cloud Shell で Bash 環境を使用します。 詳細については、「Azure Cloud Shell の Bash のクイックスタート」を参照してください。

  • CLI リファレンス コマンドをローカルで実行する場合、Azure CLI をインストールします。 Windows または macOS で実行している場合は、Docker コンテナーで Azure CLI を実行することを検討してください。 詳細については、「Docker コンテナーで Azure CLI を実行する方法」を参照してください。

    • ローカル インストールを使用する場合は、az login コマンドを使用して Azure CLI にサインインします。 認証プロセスを完了するには、ターミナルに表示される手順に従います。 その他のサインイン オプションについては、Azure CLI でのサインインに関するページを参照してください。

    • 初回使用時にインストールを求められたら、Azure CLI 拡張機能をインストールします。 拡張機能の詳細については、Azure CLI で拡張機能を使用する方法に関するページを参照してください。

    • az version を実行し、インストールされているバージョンおよび依存ライブラリを検索します。 最新バージョンにアップグレードするには、az upgrade を実行します。

変数とアクセス許可の設定

このタスクでは、いくつかの情報を繰り返し使用します。 そのような情報を格納する変数をいくつか作成します。

# set your environment variables here!!!!

# destination image resource group
imageResourceGroup=aibImageRG01

# location (see possible locations in main docs)
location=WestUS2

# your subscription
# get the current subID : 'az account show | grep id'
subscriptionID=$(az account show --query id --output tsv)

# name of the image to be created
imageName=aibCustomLinuxImg01

# image distribution metadata reference name
runOutputName=aibCustLinManImg01ro


# VNET properties (update to match your existing VNET, or leave as-is for demo)
# VNET name
vnetName=myexistingvnet01
# subnet name
subnetName=subnet01
# VNET resource group name
# NOTE! The VNET must always be in the same region as the Azure Image Builder service region.
vnetRgName=existingVnetRG
# Existing Subnet NSG Name or the demo will create it
nsgName=aibdemoNsg

リソース グループを作成します。

az group create -n $imageResourceGroup -l $location

ネットワークを構成する

既存の仮想ネットワーク、サブネット、ネットワーク セキュリティ グループ (NSG) を持たない場合は、次のスクリプトを使用して作成します。


# Create a resource group

az group create -n $vnetRgName -l $location

# Create VNET

az network vnet create \
    --resource-group $vnetRgName \
    --name $vnetName --address-prefix 10.0.0.0/16 \
    --subnet-name $subnetName --subnet-prefix 10.0.0.0/24

# Create base NSG to simulate an existing NSG

az network nsg create -g $vnetRgName -n $nsgName

az network vnet subnet update \
    --resource-group $vnetRgName \
    --vnet-name $vnetName \
    --name $subnetName \
    --network-security-group $nsgName
    
#  NOTE! The virtual network must always be in the same region as the Azure Image Builder service region.

NSG 規則を追加する

この規則によって、VM Image Builder のロード バランサーからプロキシ VM への接続が可能になります。 ポート 60001 は Linux 用、ポート 60000 は Windows 用です。 プロキシ VM は、ポート 22 (Linux の場合) またはポート 5986 (Windows の場合) を使用してビルド VM に接続します。

az network nsg rule create \
    --resource-group $vnetRgName \
    --nsg-name $nsgName \
    -n AzureImageBuilderNsgRule \
    --priority 400 \
    --source-address-prefixes AzureLoadBalancer \
    --destination-address-prefixes VirtualNetwork \
    --destination-port-ranges 60000-60001 --direction inbound \
    --access Allow --protocol Tcp \
    --description "Allow Image Builder Private Link Access to Proxy VM"

サブネットでプライベート サービス ポリシーを無効にする

その方法は次のとおりです。

az network vnet subnet update \
  --name $subnetName \
  --resource-group $vnetRgName \
  --vnet-name $vnetName \
  --disable-private-link-service-network-policies true 

詳細については、「Azure VM Image Builder のネットワーク オプション」をご覧ください。

サンプル テンプレートを変更し、ロールを作成する

ネットワークを構成したら、サンプル テンプレートを変更してロールを作成できます。 その方法は次のとおりです。

# download the example and configure it with your vars

curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/quickquickstarts/1a_Creating_a_Custom_Linux_Image_on_Existing_VNET/existingVNETLinux.json -o existingVNETLinux.json
curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleNetworking.json -o aibRoleNetworking.json
curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json

sed -i -e "s/<subscriptionID>/$subscriptionID/g" existingVNETLinux.json
sed -i -e "s/<rgName>/$imageResourceGroup/g" existingVNETLinux.json
sed -i -e "s/<region>/$location/g" existingVNETLinux.json
sed -i -e "s/<imageName>/$imageName/g" existingVNETLinux.json
sed -i -e "s/<runOutputName>/$runOutputName/g" existingVNETLinux.json

sed -i -e "s/<vnetName>/$vnetName/g" existingVNETLinux.json
sed -i -e "s/<subnetName>/$subnetName/g" existingVNETLinux.json
sed -i -e "s/<vnetRgName>/$vnetRgName/g" existingVNETLinux.json

sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleImageCreation.json
sed -i -e "s/<rgName>/$imageResourceGroup/g" aibRoleImageCreation.json

sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleNetworking.json
sed -i -e "s/<vnetRgName>/$vnetRgName/g" aibRoleNetworking.json

リソース グループのアクセス許可を設定する

VM Image Builder は、指定されたユーザー ID を使用して、Azure Compute Gallery にイメージを挿入します。 この例では、ギャラリーにイメージを配布できる Azure ロールの定義を作成します。 このロール定義はその後、ユーザー ID に割り当てられます。

# create user assigned identity for image builder
identityName=aibBuiUserId$(date +'%s')
az identity create -g $imageResourceGroup -n $identityName

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

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

# update the template
sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" existingVNETLinux.json

# make role name unique, to avoid clashes in the same Azure Active Directory domain
imageRoleDefName="Azure Image Builder Image Def"$(date +'%s')
netRoleDefName="Azure Image Builder Network Def"$(date +'%s')

# update the definitions
sed -i -e "s/Azure Image Builder Service Image Creation Role/$imageRoleDefName/g" aibRoleImageCreation.json
sed -i -e "s/Azure Image Builder Service Networking Role/$netRoleDefName/g" aibRoleNetworking.json

VM Image Builder の粒度を下げたり特権を上げたりするのではなく、2 つのロールを作成することができます。 1 つのロールはイメージを作成するアクセス許可をビルダーに付与するもので、もう 1 つはビルド VM とロードバランサーを仮想ネットワークに接続できるようにするものです。

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

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

az role assignment create \
    --assignee $imgBuilderCliId \
    --role "$netRoleDefName" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$vnetRgName

詳細については、「Azure CLI を使用して Azure VM Image Builder のアクセス許可を構成する」または「PowerShell を使用して Azure VM Image Builder のアクセス許可を構成する」をご覧ください。

イメージの作成

VM Image Builder にイメージ構成を送信します。

az resource create \
    --resource-group $imageResourceGroup \
    --properties @existingVNETLinux.json \
    --is-full-object \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n existingVNETLinuxTemplate01

# Wait approximately 1-3 mins (validation, permissions etc.)

イメージのビルドを開始します。

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

# Wait approximately 15 mins

イメージを作成し、両方のリージョンにレプリケートするまでに時間がかかる場合があります。 この部分が終了するまで待ってから、VM の作成に進みます。

VM の作成

VM Image Builder で作成されたイメージ バージョンから VM を作成します。

az vm create \
  --resource-group $imageResourceGroup \
  --name aibImgVm0001 \
  --admin-username aibuser \
  --image $imageName \
  --location $location \
  --generate-ssh-keys

Secure Shell (SSH) を使用して VM にアクセスします。

ssh aibuser@<publicIpAddress>

SSH 接続が確立されるとすぐに、イメージが当日のメッセージでカスタマイズされたことがわかります。

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

リソースをクリーンアップする

ここで同じイメージの新しいバージョンを作成するように、イメージ バージョンを再カスタマイズしたい場合は、次の手順をスキップして、Azure VM Image Builder を使用した別のイメージ バージョンの作成に関するページに進みます。

次では、作成されたイメージが他のすべてのリソース ファイルと共に削除されます。 リソースを削除する前に、このデプロイを終了していることを確認します。

ギャラリー リソースを削除する場合、それらの作成に使用したイメージ定義を削除する前に、すべてのイメージ バージョンを削除する必要があります。 ギャラリーを削除するには、最初にギャラリー内のすべてのイメージ定義を削除していることが必要です。

VM Image Builder テンプレートを削除します。

az resource delete \
    --resource-group $imageResourceGroup \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n existingVNETLinuxTemplate01

アクセス許可の割り当て、ロール、および ID を削除します。

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

az role assignment delete \
    --assignee $imgBuilderCliId \
    --role $netRoleDefName \
    --scope /subscriptions/$subscriptionID/resourceGroups/$vnetRgName


az role definition delete --name "$imageRoleDefName"
az role definition delete --name "$netRoleDefName"

az identity delete --ids $imgBuilderId

リソース グループを削除します。

az group delete -n $imageResourceGroup

このクイック スタート用に仮想ネットワークを作成した場合は、もう使用しなくなった仮想ネットワークを削除できます。

次の手順

Azure Compute Gallery