|
| 1 | +--- |
| 2 | +title: Create an application gateway that hosts multiple web sites - Azure PowerShell |
| 3 | +description: Learn how to create an application gateway that hosts multiple web sites using Azure Powershell. |
| 4 | +services: application-gateway |
| 5 | +author: vhorne |
| 6 | + |
| 7 | +ms.service: application-gateway |
| 8 | +ms.topic: tutorial |
| 9 | +ms.workload: infrastructure-services |
| 10 | +ms.date: 3/22/2018 |
| 11 | +ms.author: victorh |
| 12 | +ms.custom: mvc |
| 13 | +#Customer intent: As an IT administrator, I want to use Azure PowerShell to configure Application Gateway to host multiple web sites , so I can ensure my customers can acess the web information they need. |
| 14 | +--- |
| 15 | +# Tutorial: Create an application gateway that hosts multiple web sites using Azure PowerShell |
| 16 | + |
| 17 | +You can use Azure Powershell to [configure the hosting of multiple web sites](multiple-site-overview.md) when you create an [application gateway](overview.md). In this tutorial, you define backend address pools using virtual machines scale sets. 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. This tutorial assumes that you own multiple domains and uses examples of *www.contoso.com* and *www.fabrikam.com*. |
| 18 | + |
| 19 | +In this tutorial, you learn how to: |
| 20 | + |
| 21 | +> [!div class="checklist"] |
| 22 | +> * Set up the network |
| 23 | +> * Create an application gateway |
| 24 | +> * Create backend listeners |
| 25 | +> * Create routing rules |
| 26 | +> * Create virtual machine scale sets with the backend pools |
| 27 | +> * Create a CNAME record in your domain |
| 28 | +
|
| 29 | + |
| 30 | + |
| 31 | +If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 32 | + |
| 33 | +[!INCLUDE [cloud-shell-powershell.md](../../includes/cloud-shell-powershell.md)] |
| 34 | + |
| 35 | +If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 3.6 or later. To find the version, run ` Get-Module -ListAvailable AzureRM` . If you need to upgrade, see [Install Azure PowerShell module](/powershell/azure/install-azurerm-ps). If you are running PowerShell locally, you also need to run `Login-AzureRmAccount` to create a connection with Azure. |
| 36 | + |
| 37 | +## Create a resource group |
| 38 | + |
| 39 | +A resource group is a logical container into which Azure resources are deployed and managed. Create an Azure resource group using [New-AzureRmResourceGroup](/powershell/module/azurerm.resources/new-azurermresourcegroup). |
| 40 | + |
| 41 | +```azurepowershell-interactive |
| 42 | +New-AzureRmResourceGroup -Name myResourceGroupAG -Location eastus |
| 43 | +``` |
| 44 | + |
| 45 | +## Create network resources |
| 46 | + |
| 47 | +Create the subnet configurations using [New-AzureRmVirtualNetworkSubnetConfig](/powershell/module/azurerm.network/new-azurermvirtualnetworksubnetconfig). Create the virtual network using [New-AzureRmVirtualNetwork](/powershell/module/azurerm.network/new-azurermvirtualnetwork) with the subnet configurations. And finally, create the public IP address using [New-AzureRmPublicIpAddress](/powershell/module/azurerm.network/new-azurermpublicipaddress). These resources are used to provide network connectivity to the application gateway and its associated resources. |
| 48 | + |
| 49 | +```azurepowershell-interactive |
| 50 | +$backendSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig ` |
| 51 | + -Name myBackendSubnet ` |
| 52 | + -AddressPrefix 10.0.1.0/24 |
| 53 | +
|
| 54 | +$agSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig ` |
| 55 | + -Name myAGSubnet ` |
| 56 | + -AddressPrefix 10.0.2.0/24 |
| 57 | +
|
| 58 | +$vnet = New-AzureRmVirtualNetwork ` |
| 59 | + -ResourceGroupName myResourceGroupAG ` |
| 60 | + -Location eastus ` |
| 61 | + -Name myVNet ` |
| 62 | + -AddressPrefix 10.0.0.0/16 ` |
| 63 | + -Subnet $backendSubnetConfig, $agSubnetConfig |
| 64 | +
|
| 65 | +$pip = New-AzureRmPublicIpAddress ` |
| 66 | + -ResourceGroupName myResourceGroupAG ` |
| 67 | + -Location eastus ` |
| 68 | + -Name myAGPublicIPAddress ` |
| 69 | + -AllocationMethod Dynamic |
| 70 | +``` |
| 71 | + |
| 72 | +## Create an application gateway |
| 73 | + |
| 74 | +### Create the IP configurations and frontend port |
| 75 | + |
| 76 | +Associate the subnet that you previously created to the application gateway using [New-AzureRmApplicationGatewayIPConfiguration](/powershell/module/azurerm.network/new-azurermapplicationgatewayipconfiguration). Assign the public IP address to the application gateway using [New-AzureRmApplicationGatewayFrontendIPConfig](/powershell/module/azurerm.network/new-azurermapplicationgatewayfrontendipconfig). |
| 77 | + |
| 78 | +```azurepowershell-interactive |
| 79 | +$vnet = Get-AzureRmVirtualNetwork ` |
| 80 | + -ResourceGroupName myResourceGroupAG ` |
| 81 | + -Name myVNet |
| 82 | +
|
| 83 | +$subnet=$vnet.Subnets[0] |
| 84 | +
|
| 85 | +$gipconfig = New-AzureRmApplicationGatewayIPConfiguration ` |
| 86 | + -Name myAGIPConfig ` |
| 87 | + -Subnet $subnet |
| 88 | +
|
| 89 | +$fipconfig = New-AzureRmApplicationGatewayFrontendIPConfig ` |
| 90 | + -Name myAGFrontendIPConfig ` |
| 91 | + -PublicIPAddress $pip |
| 92 | +
|
| 93 | +$frontendport = New-AzureRmApplicationGatewayFrontendPort ` |
| 94 | + -Name myFrontendPort ` |
| 95 | + -Port 80 |
| 96 | +``` |
| 97 | + |
| 98 | +### Create the backend pools and settings |
| 99 | + |
| 100 | +Create the first backend address pool for the application gateway using [New-AzureRmApplicationGatewayBackendAddressPool](/powershell/module/azurerm.network/new-azurermapplicationgatewaybackendaddresspool). Configure the settings for the pool using [New-AzureRmApplicationGatewayBackendHttpSettings](/powershell/module/azurerm.network/new-azurermapplicationgatewaybackendhttpsettings). |
| 101 | + |
| 102 | +```azurepowershell-interactive |
| 103 | +$contosoPool = New-AzureRmApplicationGatewayBackendAddressPool ` |
| 104 | + -Name contosoPool |
| 105 | +
|
| 106 | +$fabrikamPool = New-AzureRmApplicationGatewayBackendAddressPool ` |
| 107 | + -Name fabrikamPool |
| 108 | +
|
| 109 | +$poolSettings = New-AzureRmApplicationGatewayBackendHttpSettings ` |
| 110 | + -Name myPoolSettings ` |
| 111 | + -Port 80 ` |
| 112 | + -Protocol Http ` |
| 113 | + -CookieBasedAffinity Enabled ` |
| 114 | + -RequestTimeout 120 |
| 115 | +``` |
| 116 | + |
| 117 | +### Create the listeners and rules |
| 118 | + |
| 119 | +Listeners are required to enable the application gateway to route traffic appropriately to the backend address pools. In this tutorial, you create two listeners for your two domains. In this example, listeners are created for the domains of *www.contoso.com* and *www.fabrikam.com*. |
| 120 | + |
| 121 | +Create the first listener using [New-AzureRmApplicationGatewayHttpListener](/powershell/module/azurerm.network/new-azurermapplicationgatewayhttplistener) with the frontend configuration and frontend port that you previously created. A rule is required for the listener to know which backend pool to use for incoming traffic. Create a basic rule named *contosoRule* using [New-AzureRmApplicationGatewayRequestRoutingRule](/powershell/module/azurerm.network/new-azurermapplicationgatewayrequestroutingrule). |
| 122 | + |
| 123 | +```azurepowershell-interactive |
| 124 | +$contosolistener = New-AzureRmApplicationGatewayHttpListener ` |
| 125 | + -Name contosoListener ` |
| 126 | + -Protocol Http ` |
| 127 | + -FrontendIPConfiguration $fipconfig ` |
| 128 | + -FrontendPort $frontendport ` |
| 129 | + -HostName "www.contoso.com" |
| 130 | +
|
| 131 | +$fabrikamlistener = New-AzureRmApplicationGatewayHttpListener ` |
| 132 | + -Name fabrikamListener ` |
| 133 | + -Protocol Http ` |
| 134 | + -FrontendIPConfiguration $fipconfig ` |
| 135 | + -FrontendPort $frontendport ` |
| 136 | + -HostName "www.fabrikam.com" |
| 137 | +
|
| 138 | +$contosoRule = New-AzureRmApplicationGatewayRequestRoutingRule ` |
| 139 | + -Name contosoRule ` |
| 140 | + -RuleType Basic ` |
| 141 | + -HttpListener $contosoListener ` |
| 142 | + -BackendAddressPool $contosoPool ` |
| 143 | + -BackendHttpSettings $poolSettings |
| 144 | +
|
| 145 | +$fabrikamRule = New-AzureRmApplicationGatewayRequestRoutingRule ` |
| 146 | + -Name fabrikamRule ` |
| 147 | + -RuleType Basic ` |
| 148 | + -HttpListener $fabrikamListener ` |
| 149 | + -BackendAddressPool $fabrikamPool ` |
| 150 | + -BackendHttpSettings $poolSettings |
| 151 | +``` |
| 152 | + |
| 153 | +### Create the application gateway |
| 154 | + |
| 155 | +Now that you created the necessary supporting resources, specify parameters for the application gateway using [New-AzureRmApplicationGatewaySku](/powershell/module/azurerm.network/new-azurermapplicationgatewaysku), and then create it using [New-AzureRmApplicationGateway](/powershell/module/azurerm.network/new-azurermapplicationgateway). |
| 156 | + |
| 157 | +```azurepowershell-interactive |
| 158 | +$sku = New-AzureRmApplicationGatewaySku ` |
| 159 | + -Name Standard_Medium ` |
| 160 | + -Tier Standard ` |
| 161 | + -Capacity 2 |
| 162 | +
|
| 163 | +$appgw = New-AzureRmApplicationGateway ` |
| 164 | + -Name myAppGateway ` |
| 165 | + -ResourceGroupName myResourceGroupAG ` |
| 166 | + -Location eastus ` |
| 167 | + -BackendAddressPools $contosoPool, $fabrikamPool ` |
| 168 | + -BackendHttpSettingsCollection $poolSettings ` |
| 169 | + -FrontendIpConfigurations $fipconfig ` |
| 170 | + -GatewayIpConfigurations $gipconfig ` |
| 171 | + -FrontendPorts $frontendport ` |
| 172 | + -HttpListeners $contosoListener, $fabrikamListener ` |
| 173 | + -RequestRoutingRules $contosoRule, $fabrikamRule ` |
| 174 | + -Sku $sku |
| 175 | +``` |
| 176 | + |
| 177 | +## Create virtual machine scale sets |
| 178 | + |
| 179 | +In this example, you create two virtual machine scale sets that support the two backend pools that you created. The scale sets that you create are named *myvmss1* and *myvmss2*. Each scale set contains two virtual machine instances on which you install IIS. You assign the scale set to the backend pool when you configure the IP settings. |
| 180 | + |
| 181 | +```azurepowershell-interactive |
| 182 | +$vnet = Get-AzureRmVirtualNetwork ` |
| 183 | + -ResourceGroupName myResourceGroupAG ` |
| 184 | + -Name myVNet |
| 185 | +
|
| 186 | +$appgw = Get-AzureRmApplicationGateway ` |
| 187 | + -ResourceGroupName myResourceGroupAG ` |
| 188 | + -Name myAppGateway |
| 189 | +
|
| 190 | +$contosoPool = Get-AzureRmApplicationGatewayBackendAddressPool ` |
| 191 | + -Name contosoPool ` |
| 192 | + -ApplicationGateway $appgw |
| 193 | +
|
| 194 | +$fabrikamPool = Get-AzureRmApplicationGatewayBackendAddressPool ` |
| 195 | + -Name fabrikamPool ` |
| 196 | + -ApplicationGateway $appgw |
| 197 | +
|
| 198 | +for ($i=1; $i -le 2; $i++) |
| 199 | +{ |
| 200 | + if ($i -eq 1) |
| 201 | + { |
| 202 | + $poolId = $contosoPool.Id |
| 203 | + } |
| 204 | + if ($i -eq 2) |
| 205 | + { |
| 206 | + $poolId = $fabrikamPool.Id |
| 207 | + } |
| 208 | +
|
| 209 | + $ipConfig = New-AzureRmVmssIpConfig ` |
| 210 | + -Name myVmssIPConfig$i ` |
| 211 | + -SubnetId $vnet.Subnets[1].Id ` |
| 212 | + -ApplicationGatewayBackendAddressPoolsId $poolId |
| 213 | +
|
| 214 | + $vmssConfig = New-AzureRmVmssConfig ` |
| 215 | + -Location eastus ` |
| 216 | + -SkuCapacity 2 ` |
| 217 | + -SkuName Standard_DS2 ` |
| 218 | + -UpgradePolicyMode Automatic |
| 219 | +
|
| 220 | + Set-AzureRmVmssStorageProfile $vmssConfig ` |
| 221 | + -ImageReferencePublisher MicrosoftWindowsServer ` |
| 222 | + -ImageReferenceOffer WindowsServer ` |
| 223 | + -ImageReferenceSku 2016-Datacenter ` |
| 224 | + -ImageReferenceVersion latest |
| 225 | +
|
| 226 | + Set-AzureRmVmssOsProfile $vmssConfig ` |
| 227 | + -AdminUsername azureuser ` |
| 228 | + -AdminPassword "Azure123456!" ` |
| 229 | + -ComputerNamePrefix myvmss$i |
| 230 | +
|
| 231 | + Add-AzureRmVmssNetworkInterfaceConfiguration ` |
| 232 | + -VirtualMachineScaleSet $vmssConfig ` |
| 233 | + -Name myVmssNetConfig$i ` |
| 234 | + -Primary $true ` |
| 235 | + -IPConfiguration $ipConfig |
| 236 | +
|
| 237 | + New-AzureRmVmss ` |
| 238 | + -ResourceGroupName myResourceGroupAG ` |
| 239 | + -Name myvmss$i ` |
| 240 | + -VirtualMachineScaleSet $vmssConfig |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +### Install IIS |
| 245 | + |
| 246 | +```azurepowershell-interactive |
| 247 | +$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/davidmu1/samplescripts/master/appgatewayurl.ps1"); |
| 248 | + "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" } |
| 249 | +
|
| 250 | +for ($i=1; $i -le 2; $i++) |
| 251 | +{ |
| 252 | + $vmss = Get-AzureRmVmss ` |
| 253 | + -ResourceGroupName myResourceGroupAG ` |
| 254 | + -VMScaleSetName myvmss$i |
| 255 | +
|
| 256 | + Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmss ` |
| 257 | + -Name "customScript" ` |
| 258 | + -Publisher "Microsoft.Compute" ` |
| 259 | + -Type "CustomScriptExtension" ` |
| 260 | + -TypeHandlerVersion 1.8 ` |
| 261 | + -Setting $publicSettings |
| 262 | +
|
| 263 | + Update-AzureRmVmss ` |
| 264 | + -ResourceGroupName myResourceGroupAG ` |
| 265 | + -Name myvmss$i ` |
| 266 | + -VirtualMachineScaleSet $vmss |
| 267 | +} |
| 268 | +``` |
| 269 | + |
| 270 | +## Create CNAME record in your domain |
| 271 | + |
| 272 | +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. You can use [Get-AzureRmPublicIPAddress](/powershell/module/azurerm.network/get-azurermpublicipaddress) to get the DNS address of the application gateway. Copy the *fqdn* value of the DNSSettings and use it as the value of the CNAME record that you create. The use of A-records is not recommended because the VIP may change when the application gateway is restarted. |
| 273 | + |
| 274 | +```azurepowershell-interactive |
| 275 | +Get-AzureRmPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress |
| 276 | +``` |
| 277 | + |
| 278 | +## Test the application gateway |
| 279 | + |
| 280 | +Enter your domain name into the address bar of your browser. Such as, http://www.contoso.com. |
| 281 | + |
| 282 | + |
| 283 | + |
| 284 | +Change the address to your other domain and you should see something like the following example: |
| 285 | + |
| 286 | + |
| 287 | + |
| 288 | +## Clean up resources |
| 289 | + |
| 290 | +When no longer needed, remove the resource group, application gateway, and all related resources using [Remove-AzureRmResourceGroup](/powershell/module/azurerm.resources/remove-azurermresourcegroup). |
| 291 | + |
| 292 | +```azurepowershell-interactive |
| 293 | +Remove-AzureRmResourceGroup -Name myResourceGroupAG |
| 294 | +``` |
| 295 | + |
| 296 | +## Next steps |
| 297 | + |
| 298 | +In this tutorial, you learned how to: |
| 299 | + |
| 300 | +> [!div class="checklist"] |
| 301 | +> * Set up the network |
| 302 | +> * Create an application gateway |
| 303 | +> * Create backend listeners |
| 304 | +> * Create routing rules |
| 305 | +> * Create virtual machine scale sets with the backend pools |
| 306 | +> * Create a CNAME record in your domain |
| 307 | +
|
| 308 | +> [!div class="nextstepaction"] |
| 309 | +> [Create an application gateway with URL path-based routing rules](./tutorial-url-route-powershell.md) |
0 commit comments