مشاركة عبر


استخدم Azure VM Image Builder لـ Linux VMs للوصول إلى Azure Virtual Network الحالية

ينطبق على: ✔️ أجهزة Linux الظاهرية ✔️ مجموعات مقياس مرنة

توضح لك هذه المقالة كيفية استخدام Azure VM Image Builder لإنشاء صورة Linux أساسية ومخصصة يمكنها الوصول إلى الموارد الموجودة على شبكة ظاهرية. يتم توزيع الجهاز الظاهري للبناء (VM) الذي تقوم بإنشائه على شبكة ظاهرية جديدة أو موجودة تحددها في اشتراكك. عند استخدام Azure Virtual Network الحالية، لا يتطلب VM Image Builder اتصالاً بالشبكة العامة.

المتطلبات الأساسية

تعيين المتغيرات والأذونات

لهذه المهمة، تستخدم بعض المعلومات بشكل متكرر. قم بإنشاء بعض المتغيرات لتخزين تلك المعلومات.

# 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 إلى الجهاز الظاهري للوكيل. المنفذ 60001 لنظام Linux، والمنفذ 60000 لنظام Windows. يتصل الخادم الوكيل VM بالبناء الظاهري باستخدام المنفذ 22 لنظام التشغيل Linux أو المنفذ 5986 لنظام التشغيل Windows.

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 \
  --private-link-service-network-policies Disabled 

لمزيد من المعلومات، راجع خيارات شبكة 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 هوية المستخدم المتوفرة لإدخال الصورة في Azure Compute Gallery. في هذا المثال، تقوم بإنشاء تعريف دور Azure يمكنه توزيع الصورة في المعرض. ثم يتم تعيين تعريف الدور لهوية المستخدم.

# 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 دقة أقل وامتيازاً متزايداً، يمكنك إنشاء دورين. يمنح أحد الأدوار للمُنشئ أذونات لإنشاء صورة، ويسمح له الآخر بتوصيل جهاز 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 VM Image Builder باستخدام Azure CLI أو تكوين أذونات Azure VM Image Builder باستخدام PowerShell.

إنشاء الصورة

قم بإرسال تكوين الصورة إلى 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 Image Builder.

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

استخدم Secure Shell (SSH) للوصول إلى الجهاز الظاهري.

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

حذف تعيينات الأذونات والأدوار والهوية:

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