Skapa en anpassad avsökning för Azure Application Gateway med hjälp av PowerShell för Azure Resource Manager

I den här artikeln lägger du till en anpassad prob till en befintlig applikationsgateway med PowerShell. Anpassade sonder är användbara för program som har en specifik hälsokontrollsida eller för program som inte ger ett lyckat svar i standardwebbapplikationen.

Anteckning

Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Se Installera Azure PowerShell för att komma igång. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till Az.

Förutsättning: Installera Azure PowerShell-modulen

Om du vill utföra stegen i den här artikeln, måste du installera och konfigurera Azure PowerShell-modulen. Glöm inte att slutföra alla instruktionerna. När installationen är klar, loggar du in på Azure och väljer din prenumeration.

Anteckning

Du behöver ett Azure-konto för att kunna slutföra de här stegen. Om du inte har ett Azure-konto kan du skapa ett kostnadsfritt.

Skapa en programgateway med en anpassad sond

Logga in och skapa resursgrupp

  1. Använd Connect-AzAccount för att autentisera.

    Connect-AzAccount
    
  2. Hämta prenumerationerna för kontot.

    Get-AzSubscription
    
  3. Välj vilka av dina Azure-prenumerationer som du vill använda.

    Select-AzSubscription -Subscriptionid '{subscriptionGuid}'
    
  4. Skapa en resursgrupp. Du kan hoppa över det här steget om du har en befintlig resursgrupp.

    New-AzResourceGroup -Name appgw-rg -Location 'West US'
    

Azure Resource Manager kräver att alla resursgrupper definierar en plats. Denna plats avgör var Azure lagrar metadata för resursgruppen. Kontrollera att alla kommandon för att skapa en programgateway använder samma resursgrupp.

I föregående exempel skapade vi en resursgrupp med namnet appgw-RG på plats USA, västra.

Skapa ett virtuellt nätverk och ett undernät

I följande exempel skapas ett virtuellt nätverk och ett undernät för programgatewayen. Application Gateway kräver ett eget undernät för användning. Därför bör undernätet som skapats för programgatewayen vara mindre än adressutrymmet för det virtuella nätverket så att andra undernät kan skapas och användas.

# Assign the address range 10.0.0.0/24 to a subnet variable to be used to create a virtual network.
$subnet = New-AzVirtualNetworkSubnetConfig -Name subnet01 -AddressPrefix 10.0.0.0/24

# Create a virtual network named appgwvnet in resource group appgw-rg for the West US region using the prefix 10.0.0.0/16 with subnet 10.0.0.0/24.
$vnet = New-AzVirtualNetwork -Name appgwvnet -ResourceGroupName appgw-rg -Location 'West US' -AddressPrefix 10.0.0.0/16 -Subnet $subnet

# Assign a subnet variable for the next steps, which create an application gateway.
$subnet = $vnet.Subnets[0]

Skapa en offentlig IP-adress för klientdelskonfigurationen

Skapa en standard SKU statisk offentlig IP-resurs med namnet publicIP01 i resursgruppen appgw-rg för västra USA. Application Gateway v2 stöder endast Standard SKU statiska publika IP-adresser.

$publicip = New-AzPublicIpAddress -ResourceGroupName appgw-rg -Name publicIP01 -Location 'West US' -AllocationMethod Static -Sku Standard

Skapa en applikationsgateway

Du konfigurerar alla konfigurationsobjekt innan du skapar programgatewayen. I följande exempel skapas de konfigurationsobjekt som behövs för en application gateway-resurs.

Komponent Beskrivning
Gateway IP-konfiguration En IP-konfiguration för en programgateway.
Backend-pool En pool med IP-adresser, FQDN:er eller nätverkskort som är till de programservrar som är värdar för webbprogrammet
Hälsoavsökning En anpassad sondering som används för att övervaka hälsotillståndet för medlemmarna i bakdelsgruppen
HTTP-inställningar En samling inställningar som inkluderar port, protokoll, cookiebaserad tillhörighet, avsökning och timeout. De här inställningarna avgör hur trafik dirigeras till medlemmarna i den bakomliggande poolen
Frontend-port Porten som programgatewayen använder för att lyssna efter trafik
Lyssnare En kombination av ett protokoll, klientdels-IP-konfiguration och klientdelsport. Det är detta som lyssnar efter inkommande begäranden.
Regel Dirigerar trafiken till rätt serverdel baserat på HTTP-inställningar.
# Creates an application gateway Frontend IP configuration named gatewayIP01
$gipconfig = New-AzApplicationGatewayIPConfiguration -Name gatewayIP01 -Subnet $subnet

#Creates a backend IP address pool named pool01 with IP addresses 134.170.185.46, 134.170.188.221, 134.170.185.50.
$pool = New-AzApplicationGatewayBackendAddressPool -Name pool01 -BackendIPAddresses 134.170.185.46, 134.170.188.221, 134.170.185.50

# Creates a probe that will check health at http://contoso.com/path/path.htm
$probe = New-AzApplicationGatewayProbeConfig -Name probe01 -Protocol Http -HostName 'contoso.com' -Path '/path/path.htm' -Interval 30 -Timeout 30 -UnhealthyThreshold 8

# Creates the backend http settings to be used. This component references the $probe created in the previous command.
$poolSetting = New-AzApplicationGatewayBackendHttpSettings -Name poolsetting01 -Port 80 -Protocol Http -CookieBasedAffinity Disabled -Probe $probe -RequestTimeout 80

# Creates a frontend port for the application gateway to listen on port 80 that will be used by the listener.
$fp = New-AzApplicationGatewayFrontendPort -Name frontendport01 -Port 80

# Creates a frontend IP configuration. This associates the $publicip variable defined previously with the frontend IP that will be used by the listener.
$fipconfig = New-AzApplicationGatewayFrontendIPConfig -Name fipconfig01 -PublicIPAddress $publicip

# Creates the listener. The listener is a combination of protocol and the frontend IP configuration $fipconfig and frontend port $fp created in previous steps.
$listener = New-AzApplicationGatewayHttpListener -Name listener01  -Protocol Http -FrontendIPConfiguration $fipconfig -FrontendPort $fp

# Creates the rule that routes traffic to the backend pools. In this example, we create a basic rule that uses the previously defined HTTP settings and backend address pool. It also associates the listener with the rule.
$rule = New-AzApplicationGatewayRequestRoutingRule -Name rule01 -RuleType Basic -BackendHttpSettings $poolSetting -HttpListener $listener -BackendAddressPool $pool -Priority 1

# Sets the SKU of the application gateway. In this example, we create a Standard v2 application gateway with two instances.
$sku = New-AzApplicationGatewaySku -Name Standard_v2 -Tier Standard_v2 -Capacity 2

# The final step creates the application gateway with all the previously defined components.
$appgw = New-AzApplicationGateway -Name appgwtest -ResourceGroupName appgw-rg -Location 'West US' -BackendAddressPools $pool -Probes $probe -BackendHttpSettingsCollection $poolSetting -FrontendIpConfigurations $fipconfig  -GatewayIpConfigurations $gipconfig -FrontendPorts $fp -HttpListeners $listener -RequestRoutingRules $rule -Sku $sku

Lägg till en sond till en befintlig programgateway

Följande kodfragment lägger till en avkänningspunkt till en befintlig programgateway.

# Load the application gateway resource into a PowerShell variable by using Get-AzApplicationGateway.
$getgw =  Get-AzApplicationGateway -Name appgwtest -ResourceGroupName appgw-rg

# Create the probe object that will check health at http://contoso.com/path/path.htm
$probe = Add-AzApplicationGatewayProbeConfig -ApplicationGateway $getgw -Name probe01 -Protocol Http -HostName 'contoso.com' -Path '/path/custompath.htm' -Interval 30 -Timeout 30 -UnhealthyThreshold 8

# Set the backend HTTP settings to use the new probe
$getgw = Set-AzApplicationGatewayBackendHttpSettings -ApplicationGateway $getgw -Name $getgw.BackendHttpSettingsCollection.name -Port 80 -Protocol Http -CookieBasedAffinity Disabled -Probe $probe -RequestTimeout 120

# Save the application gateway with the configuration changes
Set-AzApplicationGateway -ApplicationGateway $getgw

Ta bort en prob från en befintlig applikationsgateway

Följande kodfragment tar bort en probe från en befintlig applikationsgateway.

# Load the application gateway resource into a PowerShell variable by using Get-AzApplicationGateway.
$getgw =  Get-AzApplicationGateway -Name appgwtest -ResourceGroupName appgw-rg

# Remove the probe from the application gateway configuration object
$getgw = Remove-AzApplicationGatewayProbeConfig -ApplicationGateway $getgw -Name $getgw.Probes.name

# Set the backend HTTP settings to remove the reference to the probe. The backend http settings now use the default probe
$getgw = Set-AzApplicationGatewayBackendHttpSettings -ApplicationGateway $getgw -Name $getgw.BackendHttpSettingsCollection.name -Port 80 -Protocol http -CookieBasedAffinity Disabled

# Save the application gateway with the configuration changes
Set-AzApplicationGateway -ApplicationGateway $getgw

Få applikationsgatewayens publika IP-adress

Efter att du skapat gatewayen, hämta dess statiska publika IP-adress. För att använda en anpassad domän, skapa en A-post som mappar domänen till denna adress. För mer information, se Mappa en anpassad domän till en Azure-resurs.

$publicip = Get-AzPublicIpAddress -ResourceGroupName appgw-rg -Name publicIP01
$publicip.IpAddress

Nästa steg

Lär dig hur du konfigurerar TLS-avlastning genom att besöka: Konfigurera TLS-avlastning