使用 Azure CLI 创建托管多个网站的应用程序网关Create an application gateway that hosts multiple web sites using the Azure CLI

创建应用程序网关时,可以使用 Azure CLI 配置多个网站的托管You can use the Azure CLI to configure the hosting of multiple web sites when you create an application gateway. 本文使用虚拟机规模集定义后端地址池。In this article, you define backend address pools using virtual machines scale sets. 然后,基于所拥有的域配置侦听器和规则,以确保 Web 流量可到达池中的相应服务器。You then configure listeners and rules based on domains that you own to make sure web traffic arrives at the appropriate servers in the pools. 本文假定你拥有多个域,并使用示例 www.contoso.com 和 www.fabrikam.com 。This article assumes that you own multiple domains and uses examples of www.contoso.com and www.fabrikam.com.

在本文中,学习如何:In this article, you learn how to:

  • 设置网络Set up the network
  • 创建应用程序网关Create an application gateway
  • 创建后端侦听器Create backend listeners
  • 创建路由规则Create routing rules
  • 使用后端池创建虚拟机规模集Create virtual machine scale sets with the backend pools
  • 在域中创建 CNAME 记录Create a CNAME record in your domain

多站点路由示例

如果需要,可以使用 Azure PowerShell 完成此过程。If you prefer, you can complete this procedure using Azure PowerShell.

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

如果选择在本地安装并使用 CLI,此快速入门教程要求运行 Azure CLI 2.0.4 版或更高版本。If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.4 or later. 若要查找版本,请运行 az --versionTo find the version, run az --version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed. 使用 az group create 创建资源组。Create a resource group using az group create.

以下示例在“chinanorth” 位置创建名为“myResourceGroupAG” 的资源组。The following example creates a resource group named myResourceGroupAG in the chinanorth location.

az group create --name myResourceGroupAG --location chinanorth

创建网络资源Create network resources

使用 az network vnet create 创建虚拟网络和名为 myAGSubnet 的子网。Create the virtual network and the subnet named myAGSubnet using az network vnet create. 然后,可以使用 az network vnet subnet create 添加后端服务器所需的子网。You can then add the subnet that's needed by the backend servers using az network vnet subnet create. 使用 az network public-ip create 创建名为 myAGPublicIPAddress 的公共 IP 地址。Create the public IP address named myAGPublicIPAddress using az network public-ip create.

az network vnet create `
  --name myVNet `
  --resource-group myResourceGroupAG `
  --location chinanorth `
  --address-prefix 10.0.0.0/16 `
  --subnet-name myAGSubnet `
  --subnet-prefix 10.0.1.0/24

az network vnet subnet create `
  --name myBackendSubnet `
  --resource-group myResourceGroupAG `
  --vnet-name myVNet `
  --address-prefix 10.0.2.0/24

az network public-ip create `
  --resource-group myResourceGroupAG `
  --name myAGPublicIPAddress

创建应用程序网关Create the application gateway

可以使用 az network application-gateway create 创建应用程序网关。You can use az network application-gateway create to create the application gateway. 使用 Azure CLI 创建应用程序网关时,请指定配置信息,例如容量、sku 和 HTTP 设置。When you create an application gateway using the Azure CLI, you specify configuration information, such as capacity, sku, and HTTP settings. 将应用程序网关分配给之前创建的 myAGSubnetmyAGPublicIPAddressThe application gateway is assigned to myAGSubnet and myAGPublicIPAddress that you previously created.

az network application-gateway create `
  --name myAppGateway `
  --location chinanorth `
  --resource-group myResourceGroupAG `
  --vnet-name myVNet `
  --subnet myAGsubnet `
  --capacity 2 `
  --sku Standard_Medium `
  --http-settings-cookie-based-affinity Disabled `
  --frontend-port 80 `
  --http-settings-port 80 `
  --http-settings-protocol Http `
  --public-ip-address myAGPublicIPAddress

创建应用程序网关可能需要几分钟时间。It may take several minutes for the application gateway to be created. 创建应用程序网关后,可以看到它的这些新功能:After the application gateway is created, you can see these new features of it:

  • appGatewayBackendPool - 应用程序网关必须至少具有一个后端地址池。appGatewayBackendPool - An application gateway must have at least one backend address pool.
  • appGatewayBackendHttpSettings - 指定将端口 80 和 HTTP 协议用于通信。appGatewayBackendHttpSettings - Specifies that port 80 and an HTTP protocol is used for communication.
  • appGatewayHttpListener - 与 appGatewayBackendPool 关联的默认侦听器。appGatewayHttpListener - The default listener associated with appGatewayBackendPool.
  • appGatewayFrontendIP - 将 myAGPublicIPAddress 分配给 appGatewayHttpListenerappGatewayFrontendIP - Assigns myAGPublicIPAddress to appGatewayHttpListener.
  • rule1 - 与 appGatewayHttpListener 关联的默认路由规则。rule1 - The default routing rule that is associated with appGatewayHttpListener.

添加后端池Add the backend pools

使用 az network application-gateway address-pool create 添加包含后端服务器所需的后端池Add the backend pools that are needed to contain the backend servers using az network application-gateway address-pool create

az network application-gateway address-pool create `
  --gateway-name myAppGateway `
  --resource-group myResourceGroupAG `
  --name contosoPool

az network application-gateway address-pool create `
  --gateway-name myAppGateway `
  --resource-group myResourceGroupAG `
  --name fabrikamPool

添加后端侦听器Add backend listeners

使用 az network application-gateway http-listener create 添加路由流量所需的后端侦听器。Add the backend listeners that are needed to route traffic using az network application-gateway http-listener create.

az network application-gateway http-listener create `
  --name contosoListener `
  --frontend-ip appGatewayFrontendIP `
  --frontend-port appGatewayFrontendPort `
  --resource-group myResourceGroupAG `
  --gateway-name myAppGateway `
  --host-name www.contoso.com

az network application-gateway http-listener create `
  --name fabrikamListener `
  --frontend-ip appGatewayFrontendIP `
  --frontend-port appGatewayFrontendPort `
  --resource-group myResourceGroupAG `
  --gateway-name myAppGateway `
  --host-name www.fabrikam.com   

添加路由规则Add routing rules

规则按其列出的顺序进行处理,并且流量使用匹配的第一个规则进行定向,而无论特殊性如何。Rules are processed in the order they are listed, and traffic is directed using the first rule that matches regardless of specificity. 例如,如果在同一端口上同时有使用基本侦听器的规则和使用多站点侦听器的规则,则使用多站点侦听器的规则必须在使用基本侦听器的规则之前列出,多站点规则才能正常运行。For example, if you have a rule using a basic listener and a rule using a multi-site listener both on the same port, the rule with the multi-site listener must be listed before the rule with the basic listener in order for the multi-site rule to function as expected.

在此示例中,将创建两个新规则并删除在创建应用程序网关时创建的默认规则。In this example, you create two new rules and delete the default rule that was created when you created the application gateway. 可以使用 az network application-gateway rule create 添加规则。You can add the rule using az network application-gateway rule create.

az network application-gateway rule create `
  --gateway-name myAppGateway `
  --name contosoRule `
  --resource-group myResourceGroupAG `
  --http-listener contosoListener `
  --rule-type Basic `
  --address-pool contosoPool

az network application-gateway rule create `
  --gateway-name myAppGateway `
  --name fabrikamRule `
  --resource-group myResourceGroupAG `
  --http-listener fabrikamListener `
  --rule-type Basic `
  --address-pool fabrikamPool

az network application-gateway rule delete `
  --gateway-name myAppGateway `
  --name rule1 `
  --resource-group myResourceGroupAG

创建虚拟机规模集Create virtual machine scale sets

在此示例中,将创建三个虚拟机规模集以支持应用程序网关中的三个后端池。In this example, you create three virtual machine scale sets that support the three backend pools in the application gateway. 创建的规模集分别名为 myvmss1myvmss2myvmss3The scale sets that you create are named myvmss1, myvmss2, and myvmss3. 每个规模集包含两个在其上安装了 IIS 的虚拟机实例。Each scale set contains two virtual machine instances on which you install IIS.

for i in `seq 1 2`; do

  if [ $i -eq 1 ]
  then
    poolName="contosoPool"
  fi

  if [ $i -eq 2 ]
  then
    poolName="fabrikamPool"
  fi

  az vmss create `
    --name myvmss$i `
    --resource-group myResourceGroupAG `
    --image UbuntuLTS `
    --admin-username azureuser `
    --admin-password Azure123456! `
    --instance-count 2 `
    --vnet-name myVNet `
    --subnet myBackendSubnet `
    --vm-sku Standard_DS2 `
    --upgrade-policy-mode Automatic `
    --app-gateway myAppGateway `
    --backend-pool-name $poolName
done

安装 NGINXInstall NGINX

for i in `seq 1 2`; do

  az vmss extension set `
    --publisher Microsoft.Azure.Extensions `
    --version 2.0 `
    --name CustomScript `
    --resource-group myResourceGroupAG `
    --vmss-name myvmss$i `
    --settings '{ "fileUris": ["https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/install_nginx.sh"],
  "commandToExecute": "./install_nginx.sh" }'

done

在域中创建 CNAME 记录Create a CNAME record in your domain

使用其公共 IP 地址创建应用程序网关后,可以获取 DNS 地址并使用它在域中创建 CNAME 记录。After the application gateway is created with its public IP address, you can get the DNS address and use it to create a CNAME record in your domain. 可以使用 az network public-ip show 获取应用程序网关的 DNS 地址。You can use az network public-ip show to get the DNS address of the application gateway. 复制 DNSSettings 的 fqdn 值并使用它作为所创建的 CNAME 记录的值。Copy the fqdn value of the DNSSettings and use it as the value of the CNAME record that you create.

az network public-ip show `
  --resource-group myResourceGroupAG `
  --name myAGPublicIPAddress `
  --query [dnsSettings.fqdn] `
  --output tsv

不建议使用 A 记录,因为重新启动应用程序网关后 VIP 可能会变化。The use of A-records is not recommended because the VIP may change when the application gateway is restarted.

测试应用程序网关Test the application gateway

在浏览器的地址栏中输入域名。Enter your domain name into the address bar of your browser. 例如, http://www.contoso.comSuch as, http://www.contoso.com.

在应用程序网关中测试 contoso 站点

将地址更改为其他域,应看到类似下例所示的内容:Change the address to your other domain and you should see something like the following example:

在应用程序网关中测试 fabrikam 站点

清理资源Clean up resources

当不再需要资源组、应用程序网关以及所有相关资源时,请将其删除。When no longer needed, remove the resource group, application gateway, and all related resources.

az group delete --name myResourceGroupAG --location chinanorth

后续步骤Next steps