|
| 1 | +--- |
| 2 | +title: Tutorial - Route web traffic based on the URL - Azure CLI |
| 3 | +description: Learn how to route web traffic based on the URL to specific scalable pools of servers using the Azure CLI. |
| 4 | +services: application-gateway |
| 5 | +author: vhorne |
| 6 | +manager: jpconnock |
| 7 | + |
| 8 | +ms.service: application-gateway |
| 9 | +ms.topic: tutorial |
| 10 | +ms.workload: infrastructure-services |
| 11 | +ms.date: 3/20/2018 |
| 12 | +ms.author: victorh |
| 13 | +ms.custom: mvc |
| 14 | +#Customer intent: As an IT administrator, I want to use Azure CLI to set up routing of web traffic to specific pools of servers based on the URL that the customer uses, so I can ensure my customers have the most efficient route to the information they need. |
| 15 | +--- |
| 16 | +# Tutorial: Route web traffic based on the URL using the Azure CLI |
| 17 | + |
| 18 | +You can use Azure CLI to configure web traffic routing to specific scalable server pools based on the URL that is used to access your application. In this tutorial, you create an [Azure Application Gateway](application-gateway-introduction.md) with three backend pools using [Virtual Machine Scale Sets](../virtual-machine-scale-sets/virtual-machine-scale-sets-overview.md). Each of the backend pools serves a specific purpose such as, common data, images, and video. Routing traffic to separate pools ensures that your customers get the information that they need when they need it. |
| 19 | + |
| 20 | +To enable traffic routing, you create [routing rules](application-gateway-url-route-overview.md) assigned to listeners that listen on specific ports to ensure web traffic arrives at the appropriate servers in the pools. |
| 21 | + |
| 22 | +In this tutorial, you learn how to: |
| 23 | + |
| 24 | +> [!div class="checklist"] |
| 25 | +> * Set up the network |
| 26 | +> * Create listeners, URL path map, and rules |
| 27 | +> * Create scalable backend pools |
| 28 | +
|
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 33 | + |
| 34 | +[!INCLUDE [cloud-shell-try-it.md](../../includes/cloud-shell-try-it.md)] |
| 35 | + |
| 36 | +If you choose to install and use the CLI locally, this tutorial requires you to run the Azure CLI version 2.0.4 or later. To find the version, run `az --version`. If you need to install or upgrade, see [Install Azure CLI 2.0](/cli/azure/install-azure-cli). |
| 37 | + |
| 38 | +## Create a resource group |
| 39 | + |
| 40 | +A resource group is a logical container where Azure resources are deployed and managed. Create a resource group using [az group create](/cli/azure/group#create). |
| 41 | + |
| 42 | +The following example creates a resource group named *myResourceGroupAG* in the *eastus* location. |
| 43 | + |
| 44 | +```azurecli-interactive |
| 45 | +az group create --name myResourceGroupAG --location eastus |
| 46 | +``` |
| 47 | + |
| 48 | +## Create network resources |
| 49 | + |
| 50 | +Create the virtual network named *myVNet* and the subnet named *myAGSubnet* using [az network vnet create](/cli/azure/network/vnet#az_net). Then add a subnet named *myBackendSubnet* that's needed by the backend servers using [az network vnet subnet create](/cli/azure/network/vnet/subnet#az_network_vnet_subnet_create). Create the public IP address named *myAGPublicIPAddress* using [az network public-ip create](/cli/azure/public-ip#az_network_public_ip_create). |
| 51 | + |
| 52 | +```azurecli-interactive |
| 53 | +az network vnet create \ |
| 54 | + --name myVNet \ |
| 55 | + --resource-group myResourceGroupAG \ |
| 56 | + --location eastus \ |
| 57 | + --address-prefix 10.0.0.0/16 \ |
| 58 | + --subnet-name myAGSubnet \ |
| 59 | + --subnet-prefix 10.0.1.0/24 |
| 60 | +
|
| 61 | +az network vnet subnet create \ |
| 62 | + --name myBackendSubnet \ |
| 63 | + --resource-group myResourceGroupAG \ |
| 64 | + --vnet-name myVNet \ |
| 65 | + --address-prefix 10.0.2.0/24 |
| 66 | +
|
| 67 | +az network public-ip create \ |
| 68 | + --resource-group myResourceGroupAG \ |
| 69 | + --name myAGPublicIPAddress |
| 70 | +``` |
| 71 | + |
| 72 | +## Create the application gateway with URL map |
| 73 | + |
| 74 | +Use [az network application-gateway create](/cli/azure/application-gateway#create) to create an application gateway named *myAppGateway*. When you create an application gateway using the Azure CLI, you specify configuration information, such as capacity, sku, and HTTP settings. The application gateway is assigned to *myAGSubnet* and *myAGPublicIPAddress* that you previously created. |
| 75 | + |
| 76 | +```azurecli-interactive |
| 77 | +az network application-gateway create \ |
| 78 | + --name myAppGateway \ |
| 79 | + --location eastus \ |
| 80 | + --resource-group myResourceGroupAG \ |
| 81 | + --vnet-name myVNet \ |
| 82 | + --subnet myAGsubnet \ |
| 83 | + --capacity 2 \ |
| 84 | + --sku Standard_Medium \ |
| 85 | + --http-settings-cookie-based-affinity Disabled \ |
| 86 | + --frontend-port 80 \ |
| 87 | + --http-settings-port 80 \ |
| 88 | + --http-settings-protocol Http \ |
| 89 | + --public-ip-address myAGPublicIPAddress |
| 90 | +``` |
| 91 | + |
| 92 | + It may take several minutes to create the application gateway. After the application gateway is created, you can see these new features: |
| 93 | + |
| 94 | +- *appGatewayBackendPool* - An application gateway must have at least one backend address pool. |
| 95 | +- *appGatewayBackendHttpSettings* - Specifies that port 80 and an HTTP protocol is used for communication. |
| 96 | +- *appGatewayHttpListener* - The default listener associated with *appGatewayBackendPool*. |
| 97 | +- *appGatewayFrontendIP* - Assigns *myAGPublicIPAddress* to *appGatewayHttpListener*. |
| 98 | +- *rule1* - The default routing rule that is associated with *appGatewayHttpListener*. |
| 99 | + |
| 100 | + |
| 101 | +### Add image and video backend pools and port |
| 102 | + |
| 103 | +Add backend pools named *imagesBackendPool* and *videoBackendPool* to your application gateway by using [az network application-gateway address-pool create](/cli/azure/application-gateway#az_network_application_gateway_address-pool_create). You add the frontend port for the pools using [az network application-gateway frontend-port create](/cli/azure/application-gateway#az_network_application_gateway_frontend_port_create). |
| 104 | + |
| 105 | +```azurecli-interactive |
| 106 | +az network application-gateway address-pool create \ |
| 107 | + --gateway-name myAppGateway \ |
| 108 | + --resource-group myResourceGroupAG \ |
| 109 | + --name imagesBackendPool |
| 110 | +
|
| 111 | +az network application-gateway address-pool create \ |
| 112 | + --gateway-name myAppGateway \ |
| 113 | + --resource-group myResourceGroupAG \ |
| 114 | + --name videoBackendPool |
| 115 | +
|
| 116 | +az network application-gateway frontend-port create \ |
| 117 | + --port 8080 \ |
| 118 | + --gateway-name myAppGateway \ |
| 119 | + --resource-group myResourceGroupAG \ |
| 120 | + --name port8080 |
| 121 | +``` |
| 122 | + |
| 123 | +### Add backend listener |
| 124 | + |
| 125 | +Add the backend listener named *backendListener* that's needed to route traffic using [az network application-gateway http-listener create](/cli/azure/application-gateway#az_network_application_gateway_http_listener_create). |
| 126 | + |
| 127 | + |
| 128 | +```azurecli-interactive |
| 129 | +az network application-gateway http-listener create \ |
| 130 | + --name backendListener \ |
| 131 | + --frontend-ip appGatewayFrontendIP \ |
| 132 | + --frontend-port port8080 \ |
| 133 | + --resource-group myResourceGroupAG \ |
| 134 | + --gateway-name myAppGateway |
| 135 | +``` |
| 136 | + |
| 137 | +### Add URL path map |
| 138 | + |
| 139 | +URL path maps ensure that specific URLs are routed to specific backend pools. Create URL path maps named *imagePathRule* and *videoPathRule* using [az network application-gateway url-path-map create](/cli/azure/application-gateway#az_network_application_gateway_url_path_map_create) and [az network application-gateway url-path-map rule create](/cli/azure/application-gateway#az_network_application_gateway_url_path_map_rule_create) |
| 140 | + |
| 141 | +```azurecli-interactive |
| 142 | +az network application-gateway url-path-map create \ |
| 143 | + --gateway-name myAppGateway \ |
| 144 | + --name myPathMap \ |
| 145 | + --paths /images/* \ |
| 146 | + --resource-group myResourceGroupAG \ |
| 147 | + --address-pool imagesBackendPool \ |
| 148 | + --default-address-pool appGatewayBackendPool \ |
| 149 | + --default-http-settings appGatewayBackendHttpSettings \ |
| 150 | + --http-settings appGatewayBackendHttpSettings \ |
| 151 | + --rule-name imagePathRule |
| 152 | +
|
| 153 | +az network application-gateway url-path-map rule create \ |
| 154 | + --gateway-name myAppGateway \ |
| 155 | + --name videoPathRule \ |
| 156 | + --resource-group myResourceGroupAG \ |
| 157 | + --path-map-name myPathMap \ |
| 158 | + --paths /video/* \ |
| 159 | + --address-pool videoBackendPool |
| 160 | +``` |
| 161 | + |
| 162 | +### Add routing rule |
| 163 | + |
| 164 | +The routing rule associates the URL maps with the listener that you created. Add a rule named *rule2* using [az network application-gateway rule create](/cli/azure/application-gateway#az_network_application_gateway_rule_create). |
| 165 | + |
| 166 | +```azurecli-interactive |
| 167 | +az network application-gateway rule create \ |
| 168 | + --gateway-name myAppGateway \ |
| 169 | + --name rule2 \ |
| 170 | + --resource-group myResourceGroupAG \ |
| 171 | + --http-listener backendListener \ |
| 172 | + --rule-type PathBasedRouting \ |
| 173 | + --url-path-map myPathMap \ |
| 174 | + --address-pool appGatewayBackendPool |
| 175 | +``` |
| 176 | + |
| 177 | +## Create virtual machine scale sets |
| 178 | + |
| 179 | +In this tutorial, you create three virtual machine scale sets that support the three backend pools you created. You create scale sets named *myvmss1*, *myvmss2*, and *myvmss3*. Each scale set contains two virtual machine instances where you install NGINX. |
| 180 | + |
| 181 | +```azurecli-interactive |
| 182 | +for i in `seq 1 3`; do |
| 183 | +
|
| 184 | + if [ $i -eq 1 ] |
| 185 | + then |
| 186 | + poolName="appGatewayBackendPool" |
| 187 | + fi |
| 188 | +
|
| 189 | + if [ $i -eq 2 ] |
| 190 | + then |
| 191 | + poolName="imagesBackendPool" |
| 192 | + fi |
| 193 | +
|
| 194 | + if [ $i -eq 3 ] |
| 195 | + then |
| 196 | + poolName="videoBackendPool" |
| 197 | + fi |
| 198 | +
|
| 199 | + az vmss create \ |
| 200 | + --name myvmss$i \ |
| 201 | + --resource-group myResourceGroupAG \ |
| 202 | + --image UbuntuLTS \ |
| 203 | + --admin-username azureuser \ |
| 204 | + --admin-password Azure123456! \ |
| 205 | + --instance-count 2 \ |
| 206 | + --vnet-name myVNet \ |
| 207 | + --subnet myBackendSubnet \ |
| 208 | + --vm-sku Standard_DS2 \ |
| 209 | + --upgrade-policy-mode Automatic \ |
| 210 | + --app-gateway myAppGateway \ |
| 211 | + --backend-pool-name $poolName |
| 212 | +done |
| 213 | +``` |
| 214 | + |
| 215 | +### Install NGINX |
| 216 | + |
| 217 | +```azurecli-interactive |
| 218 | +for i in `seq 1 3`; do |
| 219 | + az vmss extension set \ |
| 220 | + --publisher Microsoft.Azure.Extensions \ |
| 221 | + --version 2.0 \ |
| 222 | + --name CustomScript \ |
| 223 | + --resource-group myResourceGroupAG \ |
| 224 | + --vmss-name myvmss$i \ |
| 225 | + --settings '{ "fileUris": ["https://raw.githubusercontent.com/davidmu1/samplescripts/master/install_nginx.sh"], "commandToExecute": "./install_nginx.sh" }' |
| 226 | +done |
| 227 | +``` |
| 228 | + |
| 229 | +## Test the application gateway |
| 230 | + |
| 231 | +To get the public IP address of the application gateway, use [az network public-ip show](/cli/azure/network/public-ip#az_network_public_ip_show). Copy the public IP address, and then paste it into the address bar of your browser. Such as, *http://40.121.222.19*, *http://40.121.222.19:8080/images/test.htm*, or *http://40.121.222.19:8080/video/test.htm*. |
| 232 | + |
| 233 | +```azurecli-interactive |
| 234 | +az network public-ip show \ |
| 235 | + --resource-group myResourceGroupAG \ |
| 236 | + --name myAGPublicIPAddress \ |
| 237 | + --query [ipAddress] \ |
| 238 | + --output tsv |
| 239 | +``` |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | +Change the URL to http://<ip-address>:8080/video/test.html, substituting your IP address for <ip-address>, and you should see something like the following example: |
| 244 | + |
| 245 | + |
| 246 | + |
| 247 | +Change the URL to http://<ip-address>:8080/video/test.html, substituting your IP address for <ip-address>, and you should see something like the following example. |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | +## Clean up resources |
| 252 | + |
| 253 | +When no longer needed, remove the resource group, application gateway, and all related resources. |
| 254 | + |
| 255 | +```azurecli-interactive |
| 256 | +az group delete --name myResourceGroupAG --location eastus |
| 257 | +``` |
| 258 | + |
| 259 | +## Next steps |
| 260 | + |
| 261 | +In this tutorial, you learned how to: |
| 262 | + |
| 263 | +> [!div class="checklist"] |
| 264 | +> * Set up the network |
| 265 | +> * Create listeners, URL path map, and rules |
| 266 | +> * Create scalable backend pools |
| 267 | +
|
| 268 | +> [!div class="nextstepaction"] |
| 269 | +> [Create an application gateway with URL path-based redirection](./tutorial-url-redirect-cli.md) |
0 commit comments