使用 Azure CLI 管理 Web 流量Manage web traffic using the Azure CLI

此脚本创建使用虚拟机规模集作为后端服务器的应用程序网关。This script creates an application gateway that uses a virtual machine scale set for backend servers. 然后,可以对应用程序网关进行配置来管理 Web 流量。The application gateway can then be configured to manage web traffic. 在运行脚本后,可以使用应用程序网关的公用 IP 地址测试该网关。After running the script, you can test the application gateway using its public IP address.

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

此示例在 Bash shell 中正常工作。This sample works in a Bash shell. 有关在 Windows 客户端上运行 Azure CLI 脚本的选项,请参阅在 Windows 上安装 Azure CLIFor options on running Azure CLI scripts on Windows client, see Install the Azure CLI on Windows.

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

示例脚本Sample script

# Create a resource group
az group create --name myResourceGroupAG --location chinanorth

# Create network resources
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 `
  --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

# Create a virtual machine scale set
az vmss create `
  --name myvmss `
  --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 appGatewayBackendPool

# Install NGINX
az vmss extension set `
  --publisher Microsoft.Azure.Extensions `
  --version 2.0 `
  --name CustomScript `
  --resource-group myResourceGroupAG `
  --vmss-name myvmss `
  --settings '{ "fileUris": ["https://raw.githubusercontent.com/davidmu1/samplescripts/master/install_nginx.sh"], "commandToExecute": "./install_nginx.sh" }'

# Get the IP address
az network public-ip show `
  --resource-group myResourceGroupAG `
  --name myAGPublicIPAddress `
  --query [ipAddress] `
  --output tsv

清理部署Clean up deployment

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

az group delete --name myResourceGroupAG --yes

脚本说明Script explanation

此脚本使用以下命令创建部署。This script uses the following commands to create the deployment. 表中的每一项均链接到特定于命令的文档。Each item in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az network vnet createaz network vnet create 创建虚拟网络。Creates a virtual network.
az network vnet subnet createaz network vnet subnet create 在虚拟网络中创建子网。Creates a subnet in a virtual network.
az network public-ip createaz network public-ip create 创建应用程序网关的公用 IP 地址。Creates the public IP address for the application gateway.
az network application-gateway createaz network application-gateway create 创建应用程序网关。Create an application gateway.
az vmss createaz vmss create 创建虚拟机规模集。Creates a virtual machine scale set.
az network public-ip showaz network public-ip show 获取应用程序网关的公共 IP 地址。Gets the public IP address of the application gateway.

后续步骤Next steps

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

可以在 Azure Windows VM 文档中找到其他应用程序网关 CLI 脚本示例。Additional application gateway CLI script samples can be found in the Azure Windows VM documentation.