Azure CLI 脚本示例:对多个网站进行负载均衡Azure CLI script example: Load balance multiple websites

本 Azure CLI 脚本示例会使用可用性集中的两个虚拟机 (VM) 创建虚拟网络。This Azure CLI script sample creates a virtual network with two virtual machines (VM) that are members of an availability set. 负载均衡器会将两个单独 IP 地址的流量定向到两台 VM。A load balancer directs traffic for two separate IP addresses to the two VMs. 运行脚本后,可将 Web 服务器软件部署到 VM 上,并可承载多个网站,其中每个网站都有其自身的 IP 地址。After running the script, you could deploy web server software to the VMs and host multiple web sites, each with its own IP address.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

示例脚本Sample script

#!/bin/bash

RgName="MyResourceGroup"
Location="chinaeast"

# Create a resource group.
az group create \
  --name $RgName \
  --location $Location

# Create an availability set for the two VMs that host both websites.
az vm availability-set create \
  --resource-group $RgName \
  --location $Location \
  --name MyAvailabilitySet \
  --platform-fault-domain-count 2 \
  --platform-update-domain-count 2

# Create a virtual network and a subnet.
az network vnet create \
  --resource-group $RgName \
  --name MyVnet \
  --address-prefix 10.0.0.0/16 \
  --location $Location \
  --subnet-name MySubnet \
  --subnet-prefix 10.0.0.0/24

# Create three public IP addresses; one for the load balancer and two for the front-end IP configurations.
az network public-ip create \
  --resource-group $RgName \
  --name MyPublicIp-LoadBalancer \
  --allocation-method Dynamic
az network public-ip create \
  --resource-group $RgName \
  --name MyPublicIp-Contoso \
  --allocation-method Dynamic
az network public-ip create \
  --resource-group $RgName \
  --name MyPublicIp-Fabrikam \
  --allocation-method Dynamic

# Create a load balancer.
az network lb create \
  --resource-group $RgName \
  --location $Location \
  --name MyLoadBalancer \
  --frontend-ip-name FrontEnd \
  --backend-pool-name BackEnd \
  --public-ip-address MyPublicIp-LoadBalancer

# Create two front-end IP configurations for both web sites.
az network lb frontend-ip create \
  --resource-group $RgName \
  --lb-name MyLoadBalancer \
  --public-ip-address MyPublicIp-Contoso \
  --name FeContoso
az network lb frontend-ip create \
  --resource-group $RgName \
  --lb-name MyLoadBalancer \
  --public-ip-address MyPublicIp-Fabrikam \
  --name FeFabrikam

# Create the back-end address pools.
az network lb address-pool create \
  --resource-group $RgName \
  --lb-name MyLoadBalancer \
  --name BeContoso
az network lb address-pool create \
  --resource-group $RgName \
  --lb-name MyLoadBalancer \
  --name BeFabrikam

# Create a probe on port 80.
az network lb probe create \
  --resource-group $RgName \
  --lb-name MyLoadBalancer \
  --name MyProbe \
  --protocol Http \
  --port 80 --path /

# Create the load balancing rules.
az network lb rule create \
  --resource-group $RgName \
  --lb-name MyLoadBalancer \
  --name LBRuleContoso \
  --protocol Tcp \
  --probe-name MyProbe \
  --frontend-port 5000 \
  --backend-port 5000 \
  --frontend-ip-name FeContoso \
  --backend-pool-name BeContoso
az network lb rule create \
  --resource-group $RgName \
  --lb-name MyLoadBalancer \
  --name LBRuleFabrikam \
  --protocol Tcp \
  --probe-name MyProbe \
  --frontend-port 5000 \
  --backend-port 5000 \
  --frontend-ip-name FeFabrikam \
  --backend-pool-name BeFabrikam

# ############## VM1 ###############

# Create an Public IP for the first VM.
az network public-ip create \
  --resource-group $RgName \
  --name MyPublicIp-Vm1 \
  --allocation-method Dynamic

# Create a network interface for VM1.
az network nic create \
  --resource-group $RgName \
  --vnet-name MyVnet \
  --subnet MySubnet \
  --name MyNic-Vm1 \
  --public-ip-address MyPublicIp-Vm1

# Create IP configurations for Contoso and Fabrikam.
az network nic ip-config create \
  --resource-group $RgName \
  --name ipconfig2 \
  --nic-name MyNic-Vm1 \
  --lb-name MyLoadBalancer \
  --lb-address-pools BeContoso
az network nic ip-config create \
  --resource-group $RgName \
  --name ipconfig3 \
  --nic-name MyNic-Vm1 \
  --lb-name MyLoadBalancer \
  --lb-address-pools BeFabrikam

# Create Vm1.
az vm create \
  --resource-group $RgName \
  --name MyVm1 \
  --nics MyNic-Vm1 \
  --image UbuntuLTS \
  --availability-set MyAvailabilitySet \
  --admin-username azureadmin \
  --generate-ssh-keys

############### VM2 ###############

# Create an Public IP for the second VM.
az network public-ip create \
  --resource-group $RgName \
  --name MyPublicIp-Vm2 \
  --allocation-method Dynamic

# Create a network interface for VM2.
az network nic create \
  --resource-group $RgName \
  --vnet-name MyVnet \
  --subnet MySubnet \
  --name MyNic-Vm2 \
  --public-ip-address MyPublicIp-Vm2

# Create IP-Configs for Contoso and Fabrikam.
az network nic ip-config create \
  --resource-group $RgName \
  --name ipconfig2 \
  --nic-name MyNic-Vm2 \
  --lb-name MyLoadBalancer \
  --lb-address-pools BeContoso
az network nic ip-config create \
  --resource-group $RgName \
  --name ipconfig3 \
  --nic-name MyNic-Vm2 \
  --lb-name MyLoadBalancer \
  --lb-address-pools BeFabrikam

# Create Vm2.
az vm create \
  --resource-group $RgName \
  --name MyVm2 \
  --nics MyNic-Vm2 \
  --image UbuntuLTS \
  --availability-set MyAvailabilitySet \
  --admin-username azureadmin \
  --generate-ssh-keys

清理部署Clean up deployment

运行以下命令来删除资源组、VM 和所有相关资源。Run the following command to remove the resource group, VM, and all related resources.

az group delete --name myResourceGroup --yes

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟网络、负载均衡器和所有相关资源。This script uses the following commands to create a resource group, virtual network, load balancer, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

CommandCommand 说明Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az network vnet createaz network vnet create 创建 Azure 虚拟网络和子网。Creates an Azure virtual network and subnet.
az network public-ip createaz network public-ip create 使用静态 IP 地址和关联的 DNS 名称创建公共 IP 地址。Creates a public IP address with a static IP address and an associated DNS name.
az network lb createaz network lb create 创建 Azure 负载均衡器。Creates an Azure Load Balancer.
az network lb probe createaz network lb probe create 创建负载均衡器探测。Creates a load balancer probe. 负载均衡器探测用于监视负载均衡器集中的每个 VM。A load balancer probe is used to monitor each VM in the load balancer set. 如果任何 VM 无法访问,流量将不会路由到该 VM。If any VM becomes inaccessible, traffic is not routed to the VM.
az network lb rule createaz network lb rule create 创建负载均衡器规则。Creates a load balancer rule. 在此示例中,将为端口 80 创建一个规则。In this sample, a rule is created for port 80. 当 HTTP 流量到达负载均衡器时,它会路由到负载均衡器集中某个 VM 的端口 80。As HTTP traffic arrives at the load balancer, it is routed to port 80 one of the VMs in the load balancer set.
az network lb frontend-ip createaz network lb frontend-ip create 为负载均衡器创建前端 IP 地址。Create a frontend IP address for the Load Balancer.
az network lb address-pool createaz network lb address-pool create 创建后端地址池。Creates a backend address pool.
az network nic createaz network nic create 创建虚拟网卡并将其连接到虚拟网络和子网。Creates a virtual network card and attaches it to the virtual network, and subnet.
az vm availability-set createaz vm availability-set create 创建可用性集。Creates an availability set. 可用性集通过将虚拟机分布到各个物理资源上(以便发生故障时,不会影响整个集)来确保应用程序运行时间。Availability sets ensure application uptime by spreading the virtual machines across physical resources such that if failure occurs, the entire set is not effected.
az network nic ip-config createaz network nic ip-config create 创建 IP 配置。Creates an IP configuration. 必须为订阅启用 Microsoft.Network/AllowMultipleIpConfigurationsPerNic 功能。You must have the Microsoft.Network/AllowMultipleIpConfigurationsPerNic feature enabled for your subscription. 对于每个 NIC,仅能使用 --make-primary 标志将一个配置指定为主要 IP 配置。Only one configuration may be designated as the primary IP configuration per NIC, using the --make-primary flag.
az vm createaz vm create 创建虚拟机并将其连接到网卡、虚拟网络、子网和 NSG。Creates the virtual machine and connects it to the network card, virtual network, subnet, and NSG. 此命令还指定要使用的虚拟机映像和管理凭据。This command also specifies the virtual machine image to be used and administrative credentials.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可在 Azure 网络概述文档中找到其他网络 CLI 脚本示例。Additional networking CLI script samples can be found in the Azure Networking Overview documentation.