Try   HackMD

使用 Azure CLI 创建 Azure VM 的自定义映像

tags: AZURE

1.您需要先有一台自己的VM

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2.啟用Azure Cloud Shell(會產生些微的儲存體費用)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

3.使用CTL創建在美國東部的映像檔倉庫(亞洲無法建立)
要進行鏡像VM的資源組需和要建立的鏡像倉庫同資源組和地區。

az group create --name alvinGalleryRG --location eastus
az sig create --resource-group alvinGalleryRG --gallery-name alvinGallery

輸入指令後,輸出如下圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

4.查看可以建立映像檔的VM列表

az vm list --output table

輸入指令後,輸出如下圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

5.獲取您的VM名稱和所在資源組後,獲取VM-ID

az vm get-instance-view -g alvinGalleryRG -n myvm --query id

輸入指令後,輸出如下圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

複製 VM 的 ID 供稍後使用。
6.創建映像檔分組(在此示例中,映像定義名為 myImageDefinition,適用於 Linux OS 映像)。

az sig image-definition create \
   --resource-group alvinGalleryRG \
   --gallery-name alvinGallery \
   --gallery-image-definition myImageDefinition\
   --publisher OpenLogic \
   --offer CentOS \
   --sku 7.9 \
   --os-type Linux \
   --os-state Specialized

輸入指令後,輸出如下圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

複製輸出中的映像定義 ID 供稍後使用。
7.創建映像檔版本
在此示例中,映像版本為 1.0.0。我們將使用區域冗餘存儲在“美國中西部”區域創建 2 個副本,在“美國中南部”區域創建 1 個副本,在“美國東部 2”區域創建 1 個副本 。複製區域必須包含源 VM 所在的區域。(建立的映像檔副本必須包含要使用的區域)

請將此示例中的 managed-image 值替換為上一步的 VM ID。

az sig image-version create \
   --resource-group alvinGalleryRG \
   --gallery-name alvinGallery \
   --gallery-image-definition myImageDefinition \
   --gallery-image-version 1.0.0 \
   --target-regions "westcentralus" "southcentralus=1" "eastus=1=standard_zrs" \
   --replica-count 2 \
   --managed-image VM ID

輸入指令後,輸出如下圖,代表您的映像檔建立完成

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

8.測試從剛建立的印象檔創建VM
在此示例中,我们將從 myImageDefinition 映像的最新版本創建 VM 在 southcentralus。

az group create --name alvin2GalleryRG --location southcentralus
az vm create --resource-group alvin2GalleryRG \
    --name myVM2 \
    --image 映像定義 ID
    --specialized \
    --generate-ssh-keys

輸入指令後,輸出如下圖(如果您新建的VM要建立在相同資源組可以忽略此步驟)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

輸入建立VM指令後,輸出如下圖,代表建立完成(如果使用金鑰登入機器,需注意金鑰問題)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

9.驗證VM是否建立成功 (成功建立就可以看到新的VM在不同的地區運行)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

您也可參考官方文件進行操作
https://docs.microsoft.com/zh-cn/azure/virtual-machines/linux/tutorial-custom-images#code-try-1