教學課程:使用 Azure CLI 建立及部署高可用性的虛擬機器Tutorial: Create and deploy highly available virtual machines with the Azure CLI

在本教學課程中,您會學到如何使用稱為「可用性設定組」的功能,增加 Azure 虛擬機器解決方案的可用性和可靠性。In this tutorial, you learn how to increase the availability and reliability of your Virtual Machine solutions on Azure using a capability called Availability Sets. 可用性設定組可確保您在 Azure 上部署的 VM 會分散到多個各自獨立的硬體叢集中。Availability sets ensure that the VMs you deploy on Azure are distributed across multiple isolated hardware clusters. 這麼做可以確保當 Azure 發生硬體或軟體故障時,受到影響的只會是一部分的 VM 子集,您整體的解決方案則會維持可用且正常運作。Doing this ensures that if a hardware or software failure within Azure happens, only a subset of your VMs is impacted and that your overall solution remains available and operational.

在本教學課程中,您了解如何:In this tutorial, you learn how to:

  • 建立可用性設定組Create an availability set
  • 在可用性設定組中建立 VMCreate a VM in an availability set
  • 檢查可用的 VM 大小Check available VM sizes

開啟 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,並且會透過瀏覽器來使用。Azure Cloud Shell is an interactive shell environment hosted in Azure and used through your browse. Azure Cloud Shell 可讓您使用 bashPowerShell 殼層來執行各種可與 Azure 服務搭配運作的工具。Azure Cloud Shell allows you to use either bash or PowerShell shells to run a variety of tools to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文的內容,而不必在本機環境上安裝任何工具。Azure Cloud Shell comes pre-installed with the commands to allow you to run the content of this article without having to install anything on your local environment.

若要在 Azure Cloud Shell 上執行本文所包含的任何程式碼,請開啟 Cloud Shell 工作階段、使用某個程式碼區塊上的 [複製] 按鈕來複製程式碼,然後使用 Ctrl+Shift+V (在 Windows 和 Linux 上) 或 Cmd+Shift+V (在 macOS 上) 將程式碼貼到 Cloud Shell 工作階段中。To run any code contained in this article on Azure Cloud Shell, open a Cloud Shell session, use the Copy button on a code block to copy the code, and paste it into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS. 貼上的文字不會自動執行,因此請按 Enter 來執行程式碼。Pasted text is not automatically executed, so press Enter to run code.

您可以使用下列方式來啟動 Azure Cloud Shell:You can launch Azure Cloud Shell with:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 這__不會__自動將文字複製到 Cloud Shell。This doesn't automatically copy text to Cloud Shell. Azure Cloud Shell 的試試看範例
在瀏覽器中開啟 Azure Cloud ShellOpen Azure Cloud Shell in your browser. <a href="https://shell.azure.com" title="啟動 Azure Cloud Shell
選取 Azure 入口網站右上角功能表上的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

如果您選擇在本機安裝和使用 CLI,本教學課程會要求您執行 Azure CLI 2.0.30 版或更新版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.30 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

可用性設定組概觀Availability set overview

可用性設定組是一種可在 Azure 中使用的邏輯群組功能,用以確保其中所放置的 VM 資源在部署到 Azure 資料中心時會彼此隔離。An Availability Set is a logical grouping capability that you can use in Azure to ensure that the VM resources you place within it are isolated from each other when they are deployed within an Azure datacenter. Azure 可確保您在可用性設定組中所放置的 VM,會橫跨多部實體伺服器、計算機架、儲存體單位和網路交換器來執行。Azure ensures that the VMs you place within an Availability Set run across multiple physical servers, compute racks, storage units, and network switches. 如果硬體或 Azure 軟體發生故障時,只有一部分的 VM 子集會受到影響,整體的應用程式則會保持運作狀態,可供客戶繼續使用。If a hardware or Azure software failure occurs, only a subset of your VMs are impacted, and your overall application stays up and continues to be available to your customers. 如果您想要建置可靠的雲端解決方案,就一定要使用可用性設定組功能。Availability Sets are an essential capability when you want to build reliable cloud solutions.

請想想典型的 VM 架構解決方案,在此解決方案中,您可能有 4 個前端 Web 伺服器,並使用 2 個後端 VM 來裝載資料庫。Let’s consider a typical VM-based solution where you might have four front-end web servers and use two back-end VMs that host a database. 在使用 Azure 時,建議您先定義兩個可用性設定組,再部署 VM︰一個可用性設定組用來放置「Web」層,另一個可用性設定組用來放置「資料庫」層。With Azure, you’d want to define two availability sets before you deploy your VMs: one availability set for the “web” tier and one availability set for the “database” tier. 當您建立新的 VM 時,您便可以將可用性設定組指定為 az vm create 命令的參數,Azure 會自動確保您在可用性設定組內所建立的 VM,會跨多個實體硬體資源來隔離。When you create a new VM you can then specify the availability set as a parameter to the az vm create command, and Azure automatically ensures that the VMs you create within the available set are isolated across multiple physical hardware resources. 如果其中一個用來執行 Web 伺服器或資料庫伺服器 VM 的實體硬體發生問題,Web 伺服器和資料庫 VM 的其他執行個體會繼續執行,因為它們位於不同硬體上。If the physical hardware that one of your Web Server or Database Server VMs is running on has a problem, you know that the other instances of your Web Server and Database VMs remain running because they are on different hardware.

如果您想要在 Azure 內部署可靠的 VM 架構解決方案,請使用可用性設定組。Use Availability Sets when you want to deploy reliable VM-based solutions within Azure.

建立可用性設定組Create an availability set

您可以使用 az vm availability-set create 來建立可用性設定組。You can create an availability set using az vm availability-set create. 在此範例中,我們會針對 myResourceGroupAvailability 資源群組中名為 myAvailabilitySet 的可用性設定組,將更新和容錯網域數目設定為 2。In this example, the number of update and fault domains is set to 2 for the availability set named myAvailabilitySet in the myResourceGroupAvailability resource group.

首先,使用 az group create 建立資源群組,然後建立可用性設定組:First, create a resource group with az group create, then create the availability set:

az group create --name myResourceGroupAvailability --location eastus

az vm availability-set create \
    --resource-group myResourceGroupAvailability \
    --name myAvailabilitySet \
    --platform-fault-domain-count 2 \
    --platform-update-domain-count 2

可用性設定組可讓您跨容錯網域和更新網域來隔離資源。Availability Sets allow you to isolate resources across fault domains and update domains. 容錯網域代表以隔離方式集合在一起的伺服器、網路和儲存體資源。A fault domain represents an isolated collection of server + network + storage resources. 在前面的範例中,在部署 VM 時,可用性設定組會分散到至少兩個容錯網域。In the preceding example, the availability set is distributed across at least two fault domains when the VMs are deployed. 可用性設定組也會分散到兩個更新網域The availability set is also distributed across two update domains. 兩個更新網域可確保當 Azure 執行軟體更新時,VM 資源是隔離的,以免在 VM 上執行的所有軟體同時更新。Two update domains ensure that when Azure performs software updates, the VM resources are isolated, preventing all the software that runs on the VM from being updated at the same time.

建立位於可用性設定組內的 VMCreate VMs inside an availability set

您必須將 VM 建立於可用性設定組內,才能確保 VM 會在硬體中正確地分散。VMs must be created within the availability set to make sure they are correctly distributed across the hardware. 建立可用性設定組之後,就無法在其中新增現有的 VM。An existing VM cannot be added to an availability set after it is created.

透過 az vm create 建立 VM 時,使用 --availability-set 參數來指定可用性設定組的名稱。When a VM is created with az vm create, use the --availability-set parameter to specify the name of the availability set.

for i in `seq 1 2`; do
   az vm create \
     --resource-group myResourceGroupAvailability \
     --name myVM$i \
     --availability-set myAvailabilitySet \
     --size Standard_DS1_v2  \
     --vnet-name myVnet \
     --subnet mySubnet \
     --image UbuntuLTS \
     --admin-username azureuser \
     --generate-ssh-keys
done

可用性設定組內有兩部虛擬機器。There are now two virtual machines within the availability set. 由於它們位於相同的可用性設定組內,Azure 會確保 VM 和其所有資源 (包括資料磁碟) 會分散到隔離開來的實體硬體中。Because they are in the same availability set, Azure ensures that the VMs and all their resources (including data disks) are distributed across isolated physical hardware. 這樣的分佈方式有助於確保 VM 解決方案整體有更高的可用性。This distribution helps ensure much higher availability of the overall VM solution.

移至 [資源群組] > [myResourceGroupAvailability] > [myAvailabilitySet],即可在入口網站中檢視可用性設定組分佈方式。The availability set distribution can be viewed in the portal by going to Resource Groups > myResourceGroupAvailability > myAvailabilitySet. VM 會分散到兩個容錯和更新網域,如下列範例所示:The VMs are distributed across the two fault and update domains, as shown in the following example:

入口網站中的可用性設定組

檢查可用的 VM 大小Check for available VM sizes

稍後可以將其他 VM 新增至可用性設定組,其中的 VM 大小可用於硬體上。Additional VMs can be added to the availability set later, where VM sizes are available on the hardware. 針對可用性設定組,使用 az vm availability-set list-sizes 列出硬體叢集上所有可用的大小。Use az vm availability-set list-sizes to list all the available sizes on the hardware cluster for the availability set:

az vm availability-set list-sizes \
     --resource-group myResourceGroupAvailability \
     --name myAvailabilitySet \
     --output table

後續步驟Next steps

在本教學課程中,您已了解如何:In this tutorial, you learned how to:

  • 建立可用性設定組Create an availability set
  • 在可用性設定組中建立 VMCreate a VM in an availability set
  • 檢查可用的 VM 大小Check available VM sizes

請前進到下一個教學課程,以了解虛擬機器擴展集。Advance to the next tutorial to learn about virtual machine scale sets.