Share via


教學課程:使用 Azure CLI 建立和管理 Linux VM

警告

本文參考 CentOS,這是接近結束生命週期 (EOL) 狀態的 Linux 發行版本。 請據以考慮您的使用和規劃。 如需詳細資訊,請參閱 CentOS 生命週期結束指引

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

Azure 虛擬機器提供完全可設定且彈性的計算環境。 本教學課程涵蓋基本的「Azure 虛擬機器」部署項目,例如選取 VM 大小、選取 VM 映像、部署 VM。 您將學習如何:

  • 建立及連線到 VM
  • 選取及使用 VM 映像
  • 檢視及使用特定 VM 大小
  • 調整 VM 大小
  • 檢視及了解 VM 狀態

本教學課程會使用 Azure Cloud Shell 內的 CLI,這會不斷更新至最新版本。

如果您選擇在本機安裝和使用 CLI,本教學課程會要求您執行 Azure CLI 2.0.30 版或更新版本。 執行 az --version 以尋找版本。 如果您需要安裝或升級,請參閱安裝 Azure CLI

建立資源群組

使用 az group create 命令來建立資源群組。

Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。 必須先建立資源群組,再建立虛擬機器。 在此範例中,會在 eastus2 區域中建立名為 myResourceGroupVM 的資源群組。

az group create --name myResourceGroupVM --location eastus2

在建立或修改 VM 時,會指定資源群組,在本教學課程的整個過程中可以看到此操作。

建立虛擬機器

使用 az vm create 命令建立虛擬機器。

建立虛擬機器時,有數個可用的選項,例如作業系統映像、磁碟大小及系統管理認證。 下列範例會建立名為 myVM 的 VM,以執行 SUSE Linux Enterprise Server (SLES)。 VM 上會建立名為 azureuser 的使用者帳戶,而如果預設金鑰位置 (~/.ssh) 沒有 SSH 金鑰,則會加以產生:

az vm create \
    --resource-group myResourceGroupVM \
    --name myVM \
    --image SuseSles15SP3 \
    --public-ip-sku Standard \
    --admin-username azureuser \
    --generate-ssh-keys

系統可能需要幾分鐘的時間來建立 VM。 建立 VM 之後,Azure CLI 就會輸出 VM 的相關資訊。 請記下 publicIpAddress,此位址可用來存取虛擬機器。

{
  "fqdns": "",
  "id": "/subscriptions/d5b9d4b7-6fc1-0000-0000-000000000000/resourceGroups/myResourceGroupVM/providers/Microsoft.Compute/virtualMachines/myVM",
  "location": "eastus2",
  "macAddress": "00-0D-3A-23-9A-49",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "52.174.34.95",
  "resourceGroup": "myResourceGroupVM"
}

連接到 VM

您現在可以在 Azure Cloud Shell 中使用 SSH 連線到 VM,也可以從本機電腦與 VM 連線。 請使用先前步驟中記下的 publicIpAddress 來取代範例 IP 位址。

ssh azureuser@52.174.34.95

在登入 VM 後,您就可以安裝和設定應用程式。 完成時,請像平常一樣地關閉 SSH 工作階段:

exit

了解 VM 映像

Azure Marketplace 包含許多可用來建立 VM 的映像。 在先前的步驟中,是使用 Ubuntu 映像來建立虛擬機器。 在此步驟中,則是使用 Azure CLI 來搜尋 Marketplace 中的 CentOS 映像,然後使用此映像來部署第二部虛擬機器。

若要查看最常用的映像清單,請使用 az vm image list 命令。

az vm image list --output table

此命令輸出會傳回 Azure 上最常用的 VM 映像。

Architecture    Offer                         Publisher               Sku                                 Urn                                                                             UrnAlias                 Version
--------------  ----------------------------  ----------------------  ----------------------------------  ------------------------------------------------------------------------------  -----------------------  ---------
x64             CentOS                        OpenLogic               7.5                                 OpenLogic:CentOS:7.5:latest                                                     CentOS                   latest
x64             debian-10                     Debian                  10                                  Debian:debian-10:10:latest                                                      Debian                   latest
x64             flatcar-container-linux-free  kinvolk                 stable                              kinvolk:flatcar-container-linux-free:stable:latest                              Flatcar                  latest
x64             opensuse-leap-15-3            SUSE                    gen2                                SUSE:opensuse-leap-15-3:gen2:latest                                             openSUSE-Leap            latest
x64             RHEL                          RedHat                  7-LVM                               RedHat:RHEL:7-LVM:latest                                                        RHEL                     latest
x64             sles-15-sp3                   SUSE                    gen2                                SUSE:sles-15-sp3:gen2:latest                                                    SLES                     latest
x64             UbuntuServer                  Canonical               18.04-LTS                           Canonical:UbuntuServer:18.04-LTS:latest                                         UbuntuLTS                latest
x64             WindowsServer                 MicrosoftWindowsServer  2022-Datacenter                     MicrosoftWindowsServer:WindowsServer:2022-Datacenter:latest                     Win2022Datacenter        latest
x64             WindowsServer                 MicrosoftWindowsServer  2022-datacenter-azure-edition-core  MicrosoftWindowsServer:WindowsServer:2022-datacenter-azure-edition-core:latest  Win2022AzureEditionCore  latest
x64             WindowsServer                 MicrosoftWindowsServer  2019-Datacenter                     MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest                     Win2019Datacenter        latest
x64             WindowsServer                 MicrosoftWindowsServer  2016-Datacenter                     MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest                     Win2016Datacenter        latest
x64             WindowsServer                 MicrosoftWindowsServer  2012-R2-Datacenter                  MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest                  Win2012R2Datacenter      latest
x64             WindowsServer                 MicrosoftWindowsServer  2012-Datacenter                     MicrosoftWindowsServer:WindowsServer:2012-Datacenter:latest                     Win2012Datacenter        latest
x64             WindowsServer                 MicrosoftWindowsServer  2008-R2-SP1                         MicrosoftWindowsServer:WindowsServer:2008-R2-SP1:latest                         Win2008R2SP1             latest

新增 --all 參數即可查看完整的清單。 您也可以依 --publisher–-offer 來篩選此映像清單。 在此範例中,清單會針對 OpenLogic 發行的所有映像進行篩選,並提供符合 CentOS 的供應項目。

az vm image list --offer CentOS --publisher OpenLogic --all --output table

部分輸出範例:

Architecture    Offer                      Publisher    Sku              Urn                                                       Version
--------------  -------------------------  -----------  ---------------  --------------------------------------------------------  ---------------
x64             CentOS                     OpenLogic    8_2              OpenLogic:CentOS:8_2:8.2.2020111800                       8.2.2020111800
x64             CentOS                     OpenLogic    8_2-gen2         OpenLogic:CentOS:8_2-gen2:8.2.2020062401                  8.2.2020062401
x64             CentOS                     OpenLogic    8_2-gen2         OpenLogic:CentOS:8_2-gen2:8.2.2020100601                  8.2.2020100601
x64             CentOS                     OpenLogic    8_2-gen2         OpenLogic:CentOS:8_2-gen2:8.2.2020111801                  8.2.2020111801
x64             CentOS                     OpenLogic    8_3              OpenLogic:CentOS:8_3:8.3.2020120900                       8.3.2020120900
x64             CentOS                     OpenLogic    8_3              OpenLogic:CentOS:8_3:8.3.2021020400                       8.3.2021020400
x64             CentOS                     OpenLogic    8_3-gen2         OpenLogic:CentOS:8_3-gen2:8.3.2020120901                  8.3.2020120901
x64             CentOS                     OpenLogic    8_3-gen2         OpenLogic:CentOS:8_3-gen2:8.3.2021020401                  8.3.2021020401
x64             CentOS                     OpenLogic    8_4              OpenLogic:CentOS:8_4:8.4.2021071900                       8.4.2021071900
x64             CentOS                     OpenLogic    8_4-gen2         OpenLogic:CentOS:8_4-gen2:8.4.2021071901                  8.4.2021071901
x64             CentOS                     OpenLogic    8_5              OpenLogic:CentOS:8_5:8.5.2022012100                       8.5.2022012100
x64             CentOS                     OpenLogic    8_5              OpenLogic:CentOS:8_5:8.5.2022101800                       8.5.2022101800
x64             CentOS                     OpenLogic    8_5-gen2         OpenLogic:CentOS:8_5-gen2:8.5.2022012101                  8.5.2022012101

注意

Canonical 已變更它們用於最新版本的供應項目名稱。 在 Ubuntu 20.04之前,供應項目名稱為 UbuntuServer。 針對 Ubuntu 20.04,供應項目名稱是 0001-com-ubuntu-server-focal,而 Ubuntu22.04 則為 0001-com-ubuntu-server-jammy

若要使用特定映像部署 VM,請記下 Urn 資料行中的值,其中包含可用來識別映像的發行者、供應項目、SKU 和版本號碼 (選擇性)。 指定映像時,可以使用 latest 來取代映像版本號碼,這會選取最新的散發版本。 在此範例中,--image 參數用於指定最新版的 CentOS 8.5。

az vm create --resource-group myResourceGroupVM --name myVM2 --image OpenLogic:CentOS:8_5:latest --generate-ssh-keys

了解 VM 大小

虛擬機器大小會決定可供虛擬機器使用的計算資源 (例如 CPU、GPU 和記憶體) 數量。 您必須針對預期的工作負載,來適當設定虛擬機器的大小。 如果工作負載增加,可以調整現有虛擬機器的大小。

VM 大小

下表會將大小分類成各種使用案例。

類型 描述
一般用途 平衡的 CPU 對記憶體。 適用於開發/測試及小型到中型應用程式和資料解決方案。
計算最佳化 CPU 對記憶體的比例高。 適用於中流量應用程式、網路設備,以及批次處理。
記憶體最佳化 記憶體對核心的比例高。 很適合用於關聯式資料庫、中型至大型快取,以及記憶體內部分析。
儲存體最佳化 高磁碟輸送量及 IO。 非常適合巨量資料、SQL 與 NoSQL 資料庫。
GPU 以大量圖形轉譯和影片編輯為目標的特製化 VM。
高效能 我們的最強大 CPU VM,可搭配選用的高輸送量網路介面 (RDMA)。

尋找可用的 VM 大小

若要查看特定區域中可用的 VM 大小清單,請使用 az vm list-sizes 命令。

az vm list-sizes --location eastus2 --output table

部分輸出範例:

  MaxDataDiskCount    MemoryInMb  Name                      NumberOfCores    OsDiskSizeInMb    ResourceDiskSizeInMb
------------------  ------------  ----------------------  ---------------  ----------------  ----------------------
4                   8192          Standard_D2ds_v4           2                1047552           76800
8                   16384         Standard_D4ds_v4           4                1047552           153600
16                  32768         Standard_D8ds_v4           8                1047552           307200
32                  65536         Standard_D16ds_v4          16               1047552           614400
32                  131072        Standard_D32ds_v4          32               1047552           1228800
32                  196608        Standard_D48ds_v4          48               1047552           1843200
32                  262144        Standard_D64ds_v4          64               1047552           2457600
4                   8192          Standard_D2ds_v5           2                1047552           76800
8                   16384         Standard_D4ds_v5           4                1047552           153600
16                  32768         Standard_D8ds_v5           8                1047552           307200
32                  65536         Standard_D16ds_v5          16               1047552           614400
32                  131072        Standard_D32ds_v5          32               1047552           1228800
32                  196608        Standard_D48ds_v5          48               1047552           1843200
32                  262144        Standard_D64ds_v5          64               1047552           2457600
32                  393216        Standard_D96ds_v5          96               1047552           3686400

建立特定大小的 VM

在先前的 VM 建立範例中,並未提供大小,因此是採用預設大小。 您可以在建立 VM 時,使用 az vm create--size 參數來選取 VM 大小。

az vm create \
    --resource-group myResourceGroupVM \
    --name myVM3 \
    --image SuseSles15SP3 \
    --size Standard_D2ds_v4  \
    --generate-ssh-keys

調整 VM 大小

在部署 VM 之後,可以調整其大小以增加或減少資源配置。 您可以使用 az vm show 檢視 VM 目前的大小:

az vm show --resource-group myResourceGroupVM --name myVM --query hardwareProfile.vmSize

在調整 VM 大小之前,請先檢查目前的 Azure 叢集上是否有所需的大小可用。 az vm list-vm-resize-options 命令會傳回大小清單。

az vm list-vm-resize-options --resource-group myResourceGroupVM --name myVM --query [].name

如果有所需的大小可用,即可從已開機狀態調整 VM 的大小,但是會在作業期間重新開機。 請使用 az vm resize 命令來執行調整大小作業。

az vm resize --resource-group myResourceGroupVM --name myVM --size Standard_D4s_v3

如果目前的叢集上沒有所需的大小,則必須先將 VM 解除配置,才能進行調整大小作業。 請使用 az vm deallocate 命令將 VM 停止並解除配置。 請注意,重新開啟 VM 電源之後,可能會移除暫存磁碟上的任何資料。 公用 IP 位址也會變更,除非使用的是靜態 IP 位址。

az vm deallocate --resource-group myResourceGroupVM --name myVM

一旦解除配置,便可以調整大小。

az vm resize --resource-group myResourceGroupVM --name myVM --size Standard_GS1

調整大小之後,即可重新啟動 VM。

az vm start --resource-group myResourceGroupVM --name myVM

VM 電源狀態

Azure VM 的電源狀態可以是許多電源狀態的其中一種。 這個狀態代表從 Hypervisor 的觀點來看,VM 的目前狀態。

電源狀態

電源狀態 描述
啟動中 表示虛擬機器正在啟動。
執行中 表示虛擬機器正在執行。
正在停止 表示虛擬機器正在停止。
已停止 表示虛擬機器已停止。 處於已停止狀態的虛擬機器仍然會產生運算費用。
正在解除配置 表示虛擬機器正在解除配置。
已解除配置 表示虛擬機器已從 Hypervisor 中移除,但仍可在控制平面中使用。 處於「已解除配置」狀態的虛擬機器不會產生計算費用。
- 表示虛擬機器的電源狀態不明。

尋找電源狀態

若要擷取特定 VM 的狀態,請使用 az vm get-instance-view 命令。 請務必為虛擬機器和資源群組指定有效的名稱。

az vm get-instance-view \
    --name myVM \
    --resource-group myResourceGroupVM \
    --query instanceView.statuses[1] --output table

輸出:

Code                Level    DisplayStatus
------------------  -------  ---------------
PowerState/running  Info     VM running

若要取得訂用帳戶中所有 VM 的電源狀態,請使用虛擬機器 - 列出所有 API,並將參數 statusOnly 設定為 True

管理工作

在虛擬機器的生命週期內,您可以執行一些管理工作,例如啟動、停止或刪除虛擬機器。 此外,您也可以建立指令碼來自動執行重複性或複雜的工作。 使用 Azure CLI 即可從命令列或在指令碼中執行許多常見的管理工作。

取得 IP 位址

此命令會傳回虛擬機器的私人和公用 IP 位址。

az vm list-ip-addresses --resource-group myResourceGroupVM --name myVM --output table

停止虛擬機器

az vm stop --resource-group myResourceGroupVM --name myVM

啟動虛擬機器

az vm start --resource-group myResourceGroupVM --name myVM

刪除 VM 資源

可能只刪除 VM 資源,而不是網路和磁碟資源,視您刪除 VM 的方式而定。 刪除 VM 時,可以變更預設行為以刪除其他資源。 如需詳細資訊,請參閱刪除 VM 和已連結的資源

刪除資源群組也會刪除資源群組中的所有資源,例如 VM、虛擬網路和磁碟。 --no-wait 參數不會等待作業完成,就會將控制項傳回給提示字元。 --yes 參數會確認您想要刪除資源,而不另外對您提示將要進行此作業。

az group delete --name myResourceGroupVM --no-wait --yes

下一步

在本教學課程中,您已了解基本的 VM 建立和管理,像是如何:

  • 建立及連線到 VM
  • 選取及使用 VM 映像
  • 檢視及使用特定 VM 大小
  • 調整 VM 大小
  • 檢視及了解 VM 狀態

請前進到下一個教學課程,以了解 VM 磁碟。