教學課程:使用 Azure 入口網站針對網際網路至 VM 的流量進行負載平衡Tutorial: Load balance internet traffic to VMs using the Azure portal

負載平衡會將傳入要求分散於多部虛擬機器,藉此提供高可用性和範圍。Load balancing provides a higher level of availability and scale by spreading incoming requests across multiple virtual machines. 在本教學課程中,您會了解 Azure Standard Load Balancer 有哪些不同的元件可分散網際網路至 VM 的流量並提供高可用性。In this tutorial, you learn about the different components of the Azure Standard Load Balancer that distribute internet traffic to VMs and provide high availability. 您會了解如何:You learn how to:

  • 建立 Azure Load BalancerCreate an Azure Load Balancer
  • 建立負載平衡器資源Create Load Balancer resources
  • 建立虛擬機器並安裝 IIS 伺服器Create virtual machines and install IIS server
  • 檢視作用中的 Load BalancerView Load Balancer in action
  • 從 Load Balancer 中新增和移除 VMAdd and remove VMs from a Load Balancer

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

登入 Azure 入口網站Sign in to the Azure portal

登入 Azure 入口網站:https://portal.azure.comSign in to the Azure portal at https://portal.azure.com.

建立標準負載平衡器Create a Standard Load Balancer

在本節中,您會建立標準負載平衡器,以協助平衡虛擬機器的負載。In this section, you create a Standard Load Balancer that helps load balance virtual machines. 標準負載平衡器只支援標準公用 IP 位址。Standard Load Balancer only supports a Standard Public IP address. 當您建立標準負載平衡器時,您也必須建立新的標準公用 IP 位址,而該 IP 位址會設定為標準負載平衡器的前端 (預設的名稱為 LoadBalancerFrontend )。When you create a Standard Load Balancer, you must also create a new Standard Public IP address that is configured as the frontend (named as LoadBalancerFrontend by default) for the Standard Load Balancer.

  1. 在畫面的左上方,按一下 [建立資源] > [網路] > [負載平衡器] 。On the top left-hand side of the screen, click Create a resource > Networking > Load Balancer.

  2. 在 [建立負載平衡器] 頁面的 [基本資料] 中,輸入或選取下列資訊、接受其餘設定的預設值,然後選取 [檢閱 + 建立] :In the Basics tab of the Create load balancer page, enter or select the following information, accept the defaults for the remaining settings, and then select Review + create:

    設定Setting Value
    訂用帳戶Subscription 選取您的訂用帳戶。Select your subscription.
    資源群組Resource group 選取 [新建] ,並在文字方塊中輸入 myResourceGroupSLBSelect Create new and type myResourceGroupSLB in the text box.
    名稱Name myLoadBalancermyLoadBalancer
    區域Region 選取 [西歐] 。Select West Europe.
    類型Type 選取 [公用] 。Select Public.
    SKUSKU 選取 [標準] 。Select Standard.
    公用 IP 位址Public IP address 選取 [建立新的] 。Select Create new.
    公用 IP 位址名稱Public IP address name 在文字方塊中輸入 myPublicIPType myPublicIP in the text box.
    可用性區域Availability zone 選取 [區域備援] 。Select Zone redundant.
  3. 在 [檢閱 + 建立] 索引標籤中,按一下 [建立] 。In the Review + create tab, click Create.

    建立標準負載平衡器

建立負載平衡器資源Create Load Balancer resources

在本節中,您會設定後端位址集區的負載平衡器設定、健康狀態探查,並指定平衡器規則。In this section, you configure Load Balancer settings for a backend address pool, a health probe, and specify a balancer rule.

建立後端位址集區Create a backend address pool

若要將流量分散至 VM,後端位址集區包含已連線至負載平衡器之虛擬 (NIC) 的 IP 位址。To distribute traffic to the VMs, a backend address pool contains the IP addresses of the virtual (NICs) connected to the Load Balancer. 建立後端位址集區 myBackendPool,以包含用於平衡網際網路流量負載的虛擬機器。Create the backend address pool myBackendPool to include virtual machines for load-balancing internet traffic.

  1. 選取左側功能表中的 [所有服務] 、選取 [所有資源] ,然後從資源清單中按一下 [myLoadBalancer] 。Select All services in the left-hand menu, select All resources, and then click myLoadBalancer from the resources list.
  2. 在 [設定] 之下,依序按一下 [後端集區] 和 [新增] 。Under Settings, click Backend pools, then click Add.
  3. 在 [新增後端集區] 頁面上,針對名稱輸入 myBackEndPool 作為後端集區的名稱,然後選取 [新增] 。On the Add a backend pool page, for name, type myBackendPool, as the name for your backend pool, and then select Add.

建立健康狀態探查Create a health probe

若要讓負載平衡器監視應用程式的狀態,請使用健康狀態探查。To allow the Load Balancer to monitor the status of your app, you use a health probe. 健康狀態探查會根據 VM 對健康情況檢查的回應,以動態方式從負載平衡器輪替中新增或移除 VM。The health probe dynamically adds or removes VMs from the Load Balancer rotation based on their response to health checks. 建立健康狀態探查 myHealthProbe 以監視 VM 的健康狀態。Create a health probe myHealthProbe to monitor the health of the VMs.

  1. 選取左側功能表中的 [所有服務] 、選取 [所有資源] ,然後從資源清單中按一下 [myLoadBalancer] 。Select All services in the left-hand menu, select All resources, and then click myLoadBalancer from the resources list.

  2. 在 [設定] 之下,依序按一下 [健康狀態探查] 和 [新增] 。Under Settings, click Health probes, then click Add.

  3. 使用下列值來建立健康狀態探查:Use these values to create the health probe:

    設定Setting Value
    名稱Name 輸入 myHealthProbeEnter myHealthProbe.
    通訊協定Protocol 選取 [HTTP] 。Select HTTP.
    連接埠Port 輸入 80Enter 80.
    間隔Interval 輸入 15 作為探查嘗試之間的 [間隔] 秒數。Enter 15 for number of Interval in seconds between probe attempts.
    狀況不良臨界值Unhealthy threshold 選取 [2] 作為 [狀況不良閾值] 的數值,或將 VM 視為狀況不良之前,必須達到的連續探查失敗次數。Select 2 for number of Unhealthy threshold or consecutive probe failures that must occur before a VM is considered unhealthy.
  4. 選取 [確定] 。Select OK.

建立負載平衡器規則Create a Load Balancer rule

負載平衡器規則用來定義如何將流量分散至 VM。A Load Balancer rule is used to define how traffic is distributed to the VMs. 您可定義連入流量的前端 IP 組態及後端 IP 集區來接收流量,以及所需的來源和目的地連接埠。You define the frontend IP configuration for the incoming traffic and the backend IP pool to receive the traffic, along with the required source and destination port. 建立負載平衡器規則 myLoadBalancerRuleWeb,用來接聽前端 FrontendLoadBalancer 中的連接埠 80,以及用來將負載平衡的網路流量傳送到後端位址集區 myBackEndPool (也是使用連接埠 80)。Create a Load Balancer rule myLoadBalancerRuleWeb for listening to port 80 in the frontend FrontendLoadBalancer and sending load-balanced network traffic to the backend address pool myBackEndPool also using port 80.

  1. 選取左側功能表中的 [所有服務] 、選取 [所有資源] ,然後從資源清單中按一下 [myLoadBalancer] 。Select All services in the left-hand menu, select All resources, and then click myLoadBalancer from the resources list.

  2. 在 [設定] 之下,按一下 [負載平衡規則] ,然後按一下 [新增] 。Under Settings, click Load balancing rules, then click Add.

  3. 使用下列值來設定負載平衡規則:Use these values to configure the load-balancing rule:

    設定Setting Value
    名稱Name 輸入 myHTTPRuleEnter myHTTPRule.
    通訊協定Protocol 選取 [TCP] 。Select TCP.
    連接埠Port 輸入 80Enter 80.
    後端連接埠Backend port 輸入 80Enter 80.
    後端集區Backend pool 選取 [myBackendPool] 。Select myBackendPool.
    健全狀況探查Health probe 選取 [myHealthProbe] 。Select myHealthProbe.
  4. 保留其餘的預設值,然後選取 [確定] 。Leave the rest of the defaults and select OK.

建立後端伺服器Create backend servers

在本節中,您會建立一個虛擬網路、針對 Load Balancer 的後端集區建立三部虛擬機器,然後在虛擬機器上安裝 IIS 以協助測試 Load Balancer。In this section, you create a virtual network, create three virtual machines for the backend pool of the Load Balancer, and then install IIS on the virtual machines to help test the Load Balancer.

虛擬網路和參數Virtual network and parameters

在本節中,您需要使用下列資訊來取代步驟中的下列參數:In this section you'll need to replace the following parameters in the steps with the information below:

參數Parameter Value
<resource-group-name> myResourceGroupSLB (選取現有的資源群組)myResourceGroupSLB (Select existing resource group)
<virtual-network-name> myVNetmyVNet
<region-name> 西歐West Europe
<IPv4-address-space> 10.1.0.0/1610.1.0.0/16
<subnet-name> mySubnetmySubnet
<subnet-address-range> 10.1.0.0/2410.1.0.0/24

建立虛擬網路Create the virtual network

在本節中,您會建立虛擬網路和子網路。In this section, you'll create a virtual network and subnet.

  1. 在畫面的左上方,選取 [建立資源] > [網路] > [虛擬網路] ,或在搜尋方塊中搜尋 [虛擬網路] 。On the upper-left side of the screen, select Create a resource > Networking > Virtual network or search for Virtual network in the search box.

  2. 在 [建立虛擬網路] 中,在 [基本] 索引標籤中輸入或選取這項資訊:In Create virtual network, enter or select this information in the Basics tab:

    設定Setting ReplTest1Value
    專案詳細資料Project Details
    訂用帳戶Subscription 選取您的 Azure 訂用帳戶Select your Azure subscription
    資源群組Resource Group 選取 [新建],輸入 <resource-group-name> ,然後選取 [確定],或根據參數選取現有的 <resource-group-name>Select Create new, enter <resource-group-name>, then select OK, or select an existing <resource-group-name> based on parameters.
    執行個體詳細資料Instance details
    名稱Name 輸入 <virtual-network-name>Enter <virtual-network-name>
    區域Region 選取 <region-name>Select <region-name>
  3. 選取 [IP 位址] 索引標籤,或選取頁面底部的 [下一步: IP 位置] 按鈕。Select the IP Addresses tab or select the Next: IP Addresses button at the bottom of the page.

  4. 在 [IP 位址] 索引標籤中,輸入這項資訊:In the IP Addresses tab, enter this information:

    設定Setting Value
    IPv4 位址空間IPv4 address space 輸入 <IPv4-address-space>Enter <IPv4-address-space>
  5. 在 [子網路名稱] 下,選取 [預設] 字組。Under Subnet name, select the word default.

  6. 在 [編輯子網路] 中,輸入這項資訊:In Edit subnet, enter this information:

    設定Setting Value
    子網路名稱Subnet name 輸入 <subnet-name>Enter <subnet-name>
    子網路位址範圍Subnet address range 輸入 <subnet-address-range>Enter <subnet-address-range>
  7. 選取 [儲存] 。Select Save.

  8. 選取 [檢閱 + 建立] 索引標籤,或選取 [檢閱 + 建立] 按鈕。Select the Review + create tab or select the Review + create button.

  9. 選取 [建立] 。Select Create.

建立虛擬機器Create virtual machines

標準負載平衡器只支援在後端集區中具有標準 IP 位址的 VM。Standard Load Balancer only supports VMs with Standard IP addresses in the backend pool. 在本節中,您將使用三個不同區域 (區域 1**、區域 2** 及區域 3**) 中的標準公用 IP 位址來建立三部 VM (myVM1、*myVM2* 及 myVM3),這些區域已新增至稍早建立的 Standard Load Balancer 後端集區。In this section, you will create three VMs (myVM1, myVM2, and myVM3) with a Standard public IP address in three different zones (Zone 1, Zone 2, and Zone 3) that are added to the backend pool of the Standard Load Balancer that was created earlier.

  1. 在入口網站的左上方,選取 [建立資源] > [計算] > [Windows Server 2016 Datacenter]。On the upper-left side of the portal, select Create a resource > Compute > Windows Server 2016 Datacenter.

  2. 在 [建立虛擬機器] 中,輸入或選取 [基本資訊] 索引標籤中的下列值:In Create a virtual machine, type or select the following values in the Basics tab:

    • [訂用帳戶]**** > [資源群組]****:選取 [myResourceGroupSLB]****。Subscription > Resource Group: Select myResourceGroupSLB.
    • 執行個體詳細資料 > 虛擬機器名稱:輸入 myVM1Instance Details > Virtual machine name: Type myVM1.
    • [執行個體詳細資料]**** > [區域]**** > 選取 [歐洲西部]****。Instance Details > Region > select West Europe.
    • [執行個體詳細資料]**** > [可用性選項]**** > 選取 [可用性區域]****。Instance Details > Availability Options > Select Availability zones.
    • [執行個體詳細資料]**** > [可用性區域]**** > 選取 [1]****。Instance Details > Availability zone > Select 1.
  3. 選取 [網路] 索引標籤,或選取 [下一步:磁碟] ,然後選取 [下一步:網路]Select the Networking tab, or select Next: Disks, then Next: Networking.

    • 請確定已選取下列項目:Make sure the following are selected:

      • 虛擬網路MyVNetVirtual network: myVnet
      • 子網路MyBackendSubnetSubnet: myBackendSubnet
      • [公用 IP]**** > 選取 [新建]****,然後在 [建立公用 IP 位址]**** 視窗中,針對 [SKU]**** 選取 [標準]****,並針對 [可用性區域]**** 選取 [區域備援]****Public IP > select Create new, and in the Create public IP address window, for SKU, select Standard, and for Availability zone, select Zone-redundant
    • 若要在 [網路安全性群組]**** 之下建立新的網路安全性群組 (NSG,一種防火牆類型),請選取 [進階]****。To create a new network security group (NSG), a type of firewall, under Network Security Group, select Advanced.

      1. 在 [設定網路安全性群組]**** 欄位中,選取 [新建]****。In the Configure network security group field, select Create new.
      2. 輸入 myNetworkSecurityGroup,然後選取 [確定]****。Type myNetworkSecurityGroup, and select OK.
    • 若要使 VM 成為負載平衡器後端集區的一部分,請完成下列步驟:To make the VM a part of the Load Balancer's backend pool, complete the following steps:

      • 在 [負載平衡]**** 中,針對 [要將此虛擬機器放在現有負載平衡解決方案後面嗎?]**** 選取 [是]****。In Load Balancing, for Place this virtual machine behind an existing load balancing solution?, select Yes.
      • 在 [負載平衡設定]**** 中,針對 [負載平衡選項]**** 選取 [Azure Load Balancer]****。In Load balancing settings, for Load balancing options, select Azure load balancer.
      • 針對 [選取負載平衡器]**** 選取 [myLoadBalancer]**。For Select a load balancer, myLoadBalancer.
  4. 選取 [管理] 索引標籤,或選取 [下一步] > [管理]。Select the Management tab, or select Next > Management. 在 [監視] 下,將 [開機診斷] 設定為 [關閉]。Under Monitoring, set Boot diagnostics to Off.

  5. 選取 [檢閱 + 建立]。Select Review + create.

  6. 檢閱設定,然後選取 [建立]。Review the settings, and then select Create.

  7. 依照下列步驟,分別使用可用性區域 23 中的標準 SKU 公用 IP 位址來建立兩部額外的 VM (myVM2myVM3),而所有其他設定則與 myVM1 相同。Follow the steps to create two additional VMs - myVM2 and myVM3, with a Standard SKU public IP address in Availability zone 2 and 3 respectively, and all the other settings the same as myVM1.

建立網路安全性群組規則Create network security group rule

在本節中,您會建立網路安全性群組規則,以允許使用 HTTP 的輸入連線。In this section, you create a network security group rule to allow inbound connections using HTTP.

  1. 選取左側功能表中的 [所有服務]****、選取 [所有資源]****,然後從資源清單中按一下 [myNetworkSecurityGroup]****,其位於 myResourceGroupSLB 資源群組中。Select All services in the left-hand menu, select All resources, and then from the resources list click myNetworkSecurityGroup that is located in the myResourceGroupSLB resource group.
  2. 在 [設定] 底下,按一下 [輸入安全性規則],然後按一下 [新增]。Under Settings, click Inbound security rules, and then click Add.
  3. 輸入輸入安全性規則 (名為 myHTTPRule) 的下列值,以允許使用連接埠 80 的輸入 HTTP 連線:Enter these values for the inbound security rule named myHTTPRule to allow for an inbound HTTP connections using port 80:
    • 服務標記 - 作為 [來源]。Service Tag - for Source.
    • 網際網路 - 作為 [來源服務標記]Internet - for Source service tag
    • 80 - 作為 [目的地連接埠範圍]80 - for Destination port ranges
    • TCP - 作為 [通訊協定]TCP - for Protocol
    • 允許 - 作為 [動作]Allow - for Action
    • 100 作為 [優先順序]100 for Priority
    • myHTTPRule 作為名稱myHTTPRule for name
    • 允許 HTTP - 作為描述Allow HTTP - for description
  4. 選取 [新增] 。Select Add.

在 VM 上安裝 IISInstall IIS on VMs

  1. 選取左側功能表中的 [所有服務]****、選取 [所有資源]****,然後從資源清單中按一下 [myVM1]****,其位於 myResourceGroupSLB 資源群組中。Select All services in the left-hand menu, select All resources, and then from the resources list click myVM1 that is located in the myResourceGroupSLB resource group.

  2. 在 [概觀] 頁面上,按一下 [連線] 以透過 RDP 連入 VM。On the Overview page, click Connect to RDP into the VM.

  3. 在 [連線至虛擬機器]**** 快顯視窗中選取 [下載 RDP 檔案]****,然後開啟下載的 RDP 檔案。In the Connect to virtual machine pop-up window, select Download RDP File, and then Open the downloaded RDP file.

  4. 在 [遠端桌面連線]**** 視窗中,按一下 [連線]****。In the Remote Desktop Connection window, click Connect.

  5. 使用您在此 VM 建立期間提供的認證登入 VM。Log into the VM with the credentials that you provided during the creation of this VM. 這會啟動虛擬機器的遠端桌面工作階段 - myVM1This launches a remote desktop session with virtual machine - myVM1.

  6. 在伺服器桌面上,瀏覽至 [Windows 系統管理工具]****>[Windows PowerShell]****。On the server desktop, navigate to Windows Administrative Tools>Windows PowerShell.

  7. 在 PowerShell 視窗中,執行下列命令以安裝 IIS 伺服器、移除預設 iisstart.htm 檔案,然後新增會顯示 VM 名稱的 iisstart.htm 檔案:In the PowerShell Window, run the following commands to install the IIS server, remove the default iisstart.htm file, and then add a new iisstart.htm file that displays the name of the VM:

    
     # install IIS server role
     Install-WindowsFeature -name Web-Server -IncludeManagementTools
    
     # remove default htm file
      remove-item  C:\inetpub\wwwroot\iisstart.htm
    
     # Add a new htm file that displays server name
      Add-Content -Path "C:\inetpub\wwwroot\iisstart.htm" -Value $("Hello World from " + $env:computername)
    
  8. 使用 myVM1 關閉 RDP 工作階段。Close the RDP session with myVM1.

  9. 重複步驟 1 到 6,在 myVM2** 和 myVM3** 上安裝 IIS 和更新的 iisstart.htm 檔案。Repeat steps 1 to 6 to install IIS and the updated iisstart.htm file on myVM2 and myVM3.

測試負載平衡器Test the Load Balancer

  1. 在 [概觀]**** 畫面上尋找負載平衡器的公用 IP 位址。Find the public IP address for the Load Balancer on the Overview screen. 選取左側功能表中的 [所有服務]****、選取 [所有資源]****,然後按一下 [myPublicIP]****。Select All services in the left-hand menu, select All resources, and then click myPublicIP.

  2. 將公用 IP 位址複製並貼到您瀏覽器的網址列。Copy the public IP address, and then paste it into the address bar of your browser. IIS Web 伺服器的預設頁面會顯示在瀏覽器上。The default page of IIS Web server is displayed on the browser.

    IIS Web 伺服器

若要查看 Load Balancer 如何將流量分散於這三部執行您應用程式的 VM,您可以強制重新整理網頁瀏覽器。To see the Load Balancer distribute traffic across the three VMs running your app, you can force-refresh your web browser.

從後端集區移除或新增 VMRemove or add VMs from the backend pool

您可能需要在執行您應用程式的 VM 上執行維護,例如安裝 OS 更新。You may need to perform maintenance on the VMs running your app, such as installing OS updates. 若要處理您應用程式增加的流量,您可能需要新增額外的 VM。To deal with increased traffic to your app, you may need to add additional VMs. 本節示範如何從 Load Balancer 中移除或新增 VM (myVM1)。This section shows you how to remove or add a VM (myVM1) from the Load Balancer.

從後端集區移除 VMRemove VM from a backend pool

若要從後端集區移除 myVM1,請完成下列步驟:To remove myVM1 from the backend pool, complete the following steps:

  1. 選取左側功能表中的 [所有服務]****、選取 [所有資源]****,然後從資源清單中按一下 [myLoadBalancer]****。Select All services in the left-hand menu, select All resources, and then click myLoadBalancer from the resources list.
  2. 在 [設定]**** 下按一下 [後端集區]****,然後在後端集區的清單中,按一下 [myBackendPool]****。Under Settings, click Backend pools, then within the backend pool's list, click myBackendPool.
  3. 在 [myBackendPool]**** 頁面上,若要移除 VM1,請選取顯示 myVM1 之資料列結尾的 [刪除] 圖示,然後按一下 [儲存]****。On the myBackendPool page, to remove VM1 select the delete icon at the end of the row that displays myVM1, and then click Save.

當 myVM1** 已不在後端位址集區中之後,您即可對 myVM1** 執行任何維護工作,例如安裝軟體更新。With myVM1 no longer in the backend address pool, you can perform any maintenance tasks on myVM1, such as installing software updates. 在沒有 VM1 的情況下,負載現在會在 myVM2myVM3 之間取得平衡。In the absence of VM1, the load is now balanced across myVM2 and myVM3.

將 VM 新增至後端集區Add VM to a backend pool

若要將 myVM1 新增回後端集區,請完成下列步驟:To add myVM1 back to the backend pool, complete the following steps:

  1. 選取左側功能表中的 [所有服務]****、選取 [所有資源]****,然後從資源清單中選取 [myVM1]****。Select All services in the left-hand menu, select All resources, and then select myVM1 from the resources list.
  2. 在 [VM1]**** 頁面的 [設定]**** 下方,選取 [網路]****。In the VM1 page, under Settings, select Networking.
  3. 在 [網路]**** 頁面上,選取 [負載平衡]**** 索引標籤,然後選取 [新增負載平衡]****。In the Networking page, select the Load balancing tab, and then select Add load balancing.
  4. 在 [新增負載平衡]**** 頁面中,執行下列動作:In the Add load balancing page, do the following:
    1. 針對 [負載平衡選項]**** 選取 [Azure Load Balancer]****。For Load balancing options, select Azure load balancer.
    2. 針對 [選取負載平衡器]**** 選取 [myLoadBalancer]**。For Select a load balancer, select myLoadBalancer.
    3. 針對 [選取後端集區]**** 選取 [MyBackendPool]**。For Select a backend pool, select myBackendPool.

清除資源Clean up resources

不再需要時,請刪除資源群組、Load Balancer 和所有相關的資源。When they are no longer needed, delete the resource group, Load Balancer, and all related resources. 若要這樣做,請選取包含 Load Balancer 的 myResouceGroupSLB 資源群組,然後選取 [刪除]****。To do so, select the myResouceGroupSLB resource group that contains the Load Balancer, and then select Delete.

後續步驟Next steps

在本教學課程中,您已建立 Standard Load Balancer、將 VM 與其連結、設定 Load Balancer 流量規則、健康狀態探查,接著測試了 Load Balancer。In this tutorial, you created a Standard Load Balancer, attached VMs to it, configured the Load Balancer traffic rule, health probe, and then tested the Load Balancer. 您也已從負載平衡集中移除 VM,並重新將該 VM 新增至後端位址集區。You also removed a VM from the load-balanced set, and added the VM back to the backend address pool. 若要深入了解 Azure Load Balancer,請繼續 Azure Load Balancer 的教學課程。To learn more about Azure Load Balancer, continue to the tutorials for Azure Load Balancer.