|
| 1 | +--- |
| 2 | +title: Create an application gateway with a virtual machine scale set - Azure PowerShell | Microsoft Docs |
| 3 | +description: Learn how to create an application gateway with a virtual machine scale set using Azure PowerShell. |
| 4 | +services: application-gateway |
| 5 | +author: davidmu1 |
| 6 | +manager: timlt |
| 7 | +editor: tysonn |
| 8 | + |
| 9 | +ms.service: application-gateway |
| 10 | +ms.topic: article |
| 11 | +ms.workload: infrastructure-services |
| 12 | +ms.date: 11/14/2017 |
| 13 | +ms.author: davidmu |
| 14 | + |
| 15 | +--- |
| 16 | +# Create an application gateway and virtual machine scale set using Azure PowerShell |
| 17 | + |
| 18 | +You can use Azure PowerShell to create an [application gateway](application-gateway-introduction.md) that uses a [virtual machine scale set](../virtual-machine-scale-sets/virtual-machine-scale-sets-overview.md) for backend servers. In this example, the scale set contains two virtual machine instances that are added to the default backend pool of the application gateway. |
| 19 | + |
| 20 | +In this article, you learn how to |
| 21 | + |
| 22 | +> [!div class="checklist"] |
| 23 | +> * Create a virtual machine scale set |
| 24 | +> * Create an application gateway |
| 25 | +> * Add servers to the default backend pool |
| 26 | +
|
| 27 | +If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 28 | + |
| 29 | +[!INCLUDE [cloud-shell-powershell.md](../../includes/cloud-shell-powershell.md)] |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +## Create a resource group |
| 34 | + |
| 35 | +Create an Azure resource group using [New-AzureRmResourceGroup](/powershell/module/azurerm.resources/new-azurermresourcegroup). A resource group is a logical container into which Azure resources are deployed and managed. |
| 36 | + |
| 37 | +```azurepowershell-interactive |
| 38 | +New-AzureRmResourceGroup -Name myResourceGroupAG -Location eastus |
| 39 | +``` |
| 40 | + |
| 41 | +## Create a virtual network, subnets, and public IP address |
| 42 | + |
| 43 | +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. |
| 44 | + |
| 45 | +```azurepowershell-interactive |
| 46 | +$backendSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig ` |
| 47 | + -Name myBackendSubnet ` |
| 48 | + -AddressPrefix 10.0.1.0/24 |
| 49 | +$agSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig ` |
| 50 | + -Name myAGSubnet ` |
| 51 | + -AddressPrefix 10.0.2.0/24 |
| 52 | +$vnet = New-AzureRmVirtualNetwork ` |
| 53 | + -ResourceGroupName myResourceGroupAG ` |
| 54 | + -Location eastus ` |
| 55 | + -Name myVNet ` |
| 56 | + -AddressPrefix 10.0.0.0/16 ` |
| 57 | + -Subnet $backendSubnetConfig, $agSubnetConfig |
| 58 | +$pip = New-AzureRmPublicIpAddress ` |
| 59 | + -ResourceGroupName myResourceGroupAG ` |
| 60 | + -Location eastus ` |
| 61 | + -Name myAGPublicIPAddress ` |
| 62 | + -AllocationMethod Dynamic |
| 63 | +``` |
| 64 | + |
| 65 | +## Create a virtual machine scale set |
| 66 | + |
| 67 | +In this example, you create a virtual machine scale set to provide servers for the backend pool in the application gateway. You can use [New-AzureRmVmssIpConfig](/powershell/module/azurerm.compute/new-azurermvmssipconfig) and [New-AzureRmVmssConfig](/powershell/module/azurerm.compute/new-azurermvmssconfig) to configure the scale set to use the *myBackendSubnet* subnet and to specify that the scale set should contain two virtual machine instances. |
| 68 | + |
| 69 | +You use [Set-AzureRmVmssStorageProfile](/powershell/module/azurerm.compute/set-azurermvmssstorageprofile) and [Set-AzureRmVmssOsProfile](/powershell/module/azurerm.compute/set-azurermvmssosprofile) to specify and configure the operating system on the virtual machines. To configure the network interface for the virtual machines, you can use [Add-AzureRmVmssNetworkInterfaceConfiguration](/powershell/module/azurerm.compute/add-azurermvmssnetworkinterfaceconfiguration). And finally, you can use [New-AzureRmVmss](/powershell/module/azurerm.compute/new-azurermvmss) to create the scale set. |
| 70 | + |
| 71 | +```azurepowershell-interactive |
| 72 | +$ipConfig = New-AzureRmVmssIpConfig ` |
| 73 | + -Name myVmssIPConfig ` |
| 74 | + -SubnetId $vnet.Subnets[0].Id |
| 75 | +$vmssConfig = New-AzureRmVmssConfig ` |
| 76 | + -Location eastus ` |
| 77 | + -SkuCapacity 2 ` |
| 78 | + -SkuName Standard_DS2 ` |
| 79 | + -UpgradePolicyMode Automatic |
| 80 | +Set-AzureRmVmssStorageProfile $vmssConfig ` |
| 81 | + -ImageReferencePublisher MicrosoftWindowsServer ` |
| 82 | + -ImageReferenceOffer WindowsServer ` |
| 83 | + -ImageReferenceSku 2016-Datacenter ` |
| 84 | + -ImageReferenceVersion latest |
| 85 | +Set-AzureRmVmssOsProfile $vmssConfig ` |
| 86 | + -AdminUsername azureuser ` |
| 87 | + -AdminPassword "Azure123456!" ` |
| 88 | + -ComputerNamePrefix myvmss |
| 89 | +Add-AzureRmVmssNetworkInterfaceConfiguration ` |
| 90 | + -VirtualMachineScaleSet $vmssConfig ` |
| 91 | + -Name myVmssNetConfig ` |
| 92 | + -Primary $true ` |
| 93 | + -IPConfiguration $ipConfig |
| 94 | +New-AzureRmVmss ` |
| 95 | + -ResourceGroupName myResourceGroupAG ` |
| 96 | + -Name myvmss ` |
| 97 | + -VirtualMachineScaleSet $vmssConfig |
| 98 | +``` |
| 99 | + |
| 100 | +### Install IIS |
| 101 | + |
| 102 | +```azurepowershell-interactive |
| 103 | +$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/davidmu1/samplescripts/master/appgatewayurl.ps1"); |
| 104 | + "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" } |
| 105 | +
|
| 106 | +$vmss = Get-AzureRmVmss -ResourceGroupName myResourceGroupAG -VMScaleSetName myvmss |
| 107 | +Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmss ` |
| 108 | + -Name "customScript" ` |
| 109 | + -Publisher "Microsoft.Compute" ` |
| 110 | + -Type "CustomScriptExtension" ` |
| 111 | + -TypeHandlerVersion 1.8 ` |
| 112 | + -Setting $publicSettings |
| 113 | +
|
| 114 | +Update-AzureRmVmss ` |
| 115 | + -ResourceGroupName myResourceGroupAG ` |
| 116 | + -Name myvmss ` |
| 117 | + -VirtualMachineScaleSet $vmss |
| 118 | +``` |
| 119 | + |
| 120 | +## Create an application gateway |
| 121 | + |
| 122 | +### Create the IP configurations |
| 123 | + |
| 124 | +Use [New-AzureRmApplicationGatewayIPConfiguration](/powershell/module/azurerm.network/new-azurermapplicationgatewayipconfiguration) to create the configuration that associates the subnet that you previously created with the application gateway. Use [New-AzureRmApplicationGatewayFrontendIPConfig](/powershell/module/azurerm.network/new-azurermapplicationgatewayfrontendipconfig) to create the configuration that assigns the public IP address that you also previously created to the application gateway. |
| 125 | + |
| 126 | +```azurepowershell-interactive |
| 127 | +$subnet=$vnet.Subnets[1] |
| 128 | +$gipconfig = New-AzureRmApplicationGatewayIPConfiguration ` |
| 129 | + -Name myAGIPConfig ` |
| 130 | + -Subnet $subnet |
| 131 | +$fipconfig = New-AzureRmApplicationGatewayFrontendIPConfig ` |
| 132 | + -Name myAGFrontendIPConfig ` |
| 133 | + -PublicIPAddress $pip |
| 134 | +``` |
| 135 | + |
| 136 | +### Create the frontend port |
| 137 | + |
| 138 | +Use [New-AzureRmApplicationGatewayFrontendPort](/powershell/module/azurerm.network/new-azurermapplicationgatewayfrontendport) to assign port 80 to be used to access the application gateway. |
| 139 | + |
| 140 | +```azurepowershell-interactive |
| 141 | +$frontendport = New-AzureRmApplicationGatewayFrontendPort ` |
| 142 | + -Name myFrontendPort ` |
| 143 | + -Port 80 |
| 144 | +``` |
| 145 | + |
| 146 | +### Create the default backend pool |
| 147 | + |
| 148 | +1. Use [Get-AzureRmNetworkInterface](/powershell/module/azurerm.network/get-azurermnetworkinterface) to get the private IP address of each virtual machine in the scale set. After running the command, record the private IP addresses for each virtual machine. |
| 149 | + |
| 150 | + ```azurepowershell-interactive |
| 151 | + Get-AzureRmNetworkInterface -ResourceGroupName myResourceGroupAG -VirtualMachineScaleSetName myvmss | ForEach-Object {$_.ipConfigurations[0].PrivateIPAddress} |
| 152 | + ``` |
| 153 | +
|
| 154 | +2. Use [New-AzureRmApplicationGatewayBackendAddressPool](/powershell/module/azurerm.network/new-azurermapplicationgatewaybackendaddresspool) to create the backend address pool for the application gateway. Configure the settings for the backend address pools using [New-AzureRmApplicationGatewayBackendHttpSettings](/powershell/module/azurerm.network/new-azurermapplicationgatewaybackendhttpsettings). Change the server IP addresses to the two that you previously recorded. |
| 155 | +
|
| 156 | + ```azurepowershell-interactive |
| 157 | + $defaultPool = New-AzureRmApplicationGatewayBackendAddressPool ` |
| 158 | + -Name appGatewayBackendPool ` |
| 159 | + -BackendIPAddresses 10.0.0.4, 10.0.0.5 |
| 160 | + $poolSettings = New-AzureRmApplicationGatewayBackendHttpSettings ` |
| 161 | + -Name myPoolSettings ` |
| 162 | + -Port 80 ` |
| 163 | + -Protocol Http ` |
| 164 | + -CookieBasedAffinity Enabled ` |
| 165 | + -RequestTimeout 120 |
| 166 | + ``` |
| 167 | +
|
| 168 | +### Create the default listener and rule |
| 169 | +
|
| 170 | +Listeners are required to enable the application gateway to route traffic appropriately to the backend address pools. In this example, you create a basic listener that listens for traffic at the root URL. |
| 171 | +
|
| 172 | +Create a 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. Use [New-AzureRmApplicationGatewayRequestRoutingRule](/powershell/module/azurerm.network/new-azurermapplicationgatewayrequestroutingrule) to create a basic rule named *rule1*. |
| 173 | +
|
| 174 | +```azurepowershell-interactive |
| 175 | +$defaultlistener = New-AzureRmApplicationGatewayHttpListener ` |
| 176 | + -Name mydefaultListener ` |
| 177 | + -Protocol Http ` |
| 178 | + -FrontendIPConfiguration $fipconfig ` |
| 179 | + -FrontendPort $frontendport |
| 180 | +$frontendRule = New-AzureRmApplicationGatewayRequestRoutingRule ` |
| 181 | + -Name rule1 ` |
| 182 | + -RuleType Basic ` |
| 183 | + -HttpListener $defaultlistener ` |
| 184 | + -BackendAddressPool $defaultPool ` |
| 185 | + -BackendHttpSettings $poolSettings |
| 186 | +``` |
| 187 | + |
| 188 | +### Create the application gateway |
| 189 | + |
| 190 | +Now that you created the necessary supporting resources, use [New-AzureRmApplicationGatewaySku](/powershell/module/azurerm.network/new-azurermapplicationgatewaysku) to specify parameters for the application gateway, and then use [New-AzureRmApplicationGateway](/powershell/module/azurerm.network/new-azurermapplicationgateway) to create it. |
| 191 | + |
| 192 | +```azurepowershell-interactive |
| 193 | +$sku = New-AzureRmApplicationGatewaySku ` |
| 194 | + -Name Standard_Medium ` |
| 195 | + -Tier Standard ` |
| 196 | + -Capacity 2 |
| 197 | +$appgw = New-AzureRmApplicationGateway ` |
| 198 | + -Name myAppGateway ` |
| 199 | + -ResourceGroupName myResourceGroupAG ` |
| 200 | + -Location eastus ` |
| 201 | + -BackendAddressPools $defaultPool ` |
| 202 | + -BackendHttpSettingsCollection $poolSettings ` |
| 203 | + -FrontendIpConfigurations $fipconfig ` |
| 204 | + -GatewayIpConfigurations $gipconfig ` |
| 205 | + -FrontendPorts $frontendport ` |
| 206 | + -HttpListeners $defaultlistener ` |
| 207 | + -RequestRoutingRules $frontendRule ` |
| 208 | + -Sku $sku |
| 209 | +``` |
| 210 | + |
| 211 | +## Test the application gateway |
| 212 | + |
| 213 | +1. Use [Get-AzureRmPublicIPAddress](/powershell/module/azurerm.network/get-azurermpublicipaddress) to get the public IP address of the application gateway. |
| 214 | + |
| 215 | + ```azurepowershell-interactive |
| 216 | + Get-AzureRmPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress |
| 217 | + ``` |
| 218 | +
|
| 219 | +2. Copy the public IP address, and then paste it into the address bar of your browser. |
| 220 | +
|
| 221 | +  |
| 222 | +
|
| 223 | +## Next steps |
| 224 | +
|
| 225 | +In this tutorial, you learned how to: |
| 226 | +
|
| 227 | +> [!div class="checklist"] |
| 228 | +> * Create a virtual machine scale set |
| 229 | +> * Create an application gateway |
| 230 | +> * Add servers to the default backend pool |
| 231 | +
|
| 232 | +
|
0 commit comments