Membuat pemeriksaan kustom untuk Azure Application Gateway dengan menggunakan PowerShell di Resource Manager

Dalam artikel ini, Anda menambahkan pemeriksaan kustom ke gateway aplikasi yang sudah tersedia menggunakan PowerShell. Pemeriksaan kustom berguna untuk aplikasi yang memiliki halaman pemeriksaan kesehatan tertentu atau aplikasi yang gagal memberikan respons pada aplikasi web default.

Catatan

Sebaiknya Anda menggunakan modul Azure Az PowerShell untuk berinteraksi dengan Azure. Lihat Menginstal Azure PowerShell untuk memulai. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

Prasyarat: Menginstal modul Azure PowerShell

Untuk melakukan langkah-langkah dalam artikel ini, Anda perlu menginstal dan mengonfigurasi modul Azure PowerShell. Pastikan untuk menyelesaikan semua petunjuk. Setelah penginstalan selesai, masuk ke Azure dan pilih langganan Anda.

Catatan

Anda memerlukan akun Azure untuk menyelesaikan langkah-langkah ini. Jika Anda tidak memiliki akun Azure, Anda dapat mendaftar untuk coba gratis.

Membuat gateway aplikasi dengan pemeriksaan kustom

Masuk dan buat grup sumber daya

  1. Gunakan Connect-AzAccount untuk mengautentikasi.

    Connect-AzAccount
    
  2. Dapatkan langganan untuk akun tersebut.

    Get-AzSubscription
    
  3. Pilih langganan Azure mana yang akan digunakan.

    Select-AzSubscription -Subscriptionid '{subscriptionGuid}'
    
  4. Buat grup sumber daya. Lewati langkah ini jika grup sumber daya sudah ada.

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

Azure Resource Manager mengharuskan semua grup sumber daya menentukan lokasi. Lokasi ini digunakan sebagai lokasi default untuk sumber daya di grup sumber daya tersebut. Pastikan semua perintah untuk membuat gateway aplikasi menggunakan grup sumber daya yang sama.

Di contoh sebelumnya, kita membuat grup sumber daya yang disebut appgw-RG di lokasi US Barat.

Membuat jaringan virtual dan subnet

Contoh berikut membuat jaringan virtual dan subnet untuk gateway aplikasi. Gateway aplikasi memerlukan subnet sendiri untuk digunakan. Karena itu, subnet yang dibuat untuk gateway aplikasi harus lebih kecil dari ruang alamat VNET agar memungkinkan pembuatan dan penggunaan subnet lain.

# 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]

Membuat alamat IP publik untuk konfigurasi frontend

Buat sumber daya IP publik publicIP01 di grup sumber daya appgw-rg untuk wilayah AS Barat. Contoh ini menggunakan alamat IP publik untuk alamat IP frontend gateway aplikasi. Gateway aplikasi mengharuskan alamat IP publik memiliki nama DNS yang dibuat secara dinamis sehingga -DomainNameLabel tidak dapat ditentukan selama pembuatan alamat IP publik.

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

Membuat gateway aplikasi

Semua item konfigurasi disiapkan sebelum membuat gateway aplikasi. Langkah berikut membuat item konfigurasi yang diperlukan sebagai sumber daya gateway aplikasi.

Komponen Keterangan
Konfigurasi IP Gateway Konfigurasi IP gateway aplikasi.
Kumpulan backend Kumpulan alamat IP, FQDN, atau NIC untuk server aplikasi yang menghosting aplikasi web
Pemeriksaan kesehatan Pemeriksaan kustom yang digunakan untuk memantau kondisi anggota kumpulan ujung belakang
Pengaturan HTTP Kumpulan pengaturan termasuk, port, protokol, afinitas berbasis cookie, pemeriksaan, dan waktu habis. Pengaturan ini menentukan bagaimana lalu lintas dirutekan ke anggota kumpulan ujung belakang
Port ujung depan Port tempat gateway aplikasi mendengarkan lalu lintas
Listener Kombinasi protokol, konfigurasi IP ujung depan, dan port ujung depan. Kombinasi ini yang mendengarkan permintaan masuk.
Aturan Merutekan lalu lintas ke ujung belakang yang sesuai berdasarkan setelan HTTP.
# 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 120 -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 previous defined http settings and backend address pool.  It also associates the listener to the rule
$rule = New-AzApplicationGatewayRequestRoutingRule -Name rule01 -RuleType Basic -BackendHttpSettings $poolSetting -HttpListener $listener -BackendAddressPool $pool

# Sets the SKU of the application gateway, in this example we create a small standard application gateway with 2 instances.
$sku = New-AzApplicationGatewaySku -Name Standard_Small -Tier Standard -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

Menambahkan pemeriksaan ke gateway aplikasi yang sudah ada

Cuplikan kode berikut menambahkan pemeriksaan ke gateway aplikasi yang sudah ada.

# 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 120 -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

Menghapus pemeriksaan dari gateway aplikasi yang sudah ada

Cuplikan kode berikut menghapus pemeriksaan dari gateway aplikasi yang sudah ada.

# 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

Dapatkan nama DNS gateway aplikasi

Setelah gateway dibuat, langkah berikutnya adalah mengonfigurasi ujung depan untuk komunikasi. Saat Anda menggunakan alamat IP publik, gateway aplikasi memerlukan nama DNS yang ditetapkan secara dinamis, yang tidak ramah. Untuk memastikan pengguna akhir dapat mencapai gateway aplikasi, data CNAME dapat digunakan untuk mengarahkan ke titik akhir publik gateway aplikasi. Mengonfigurasi nama domain kustom untuk Azure. Caranya, ambil detail gateway aplikasi dan nama IP/DNS terkait menggunakan elemen PublicIPAddress yang dilampirkan ke gateway aplikasi. Nama DNS gateway aplikasi harus digunakan untuk membuat data CNAME, yang menunjuk dua aplikasi web ke nama DNS ini. Penggunaan catatan A tidak disarankan karena VIP dapat berubah saat pengaktifan ulang gateway aplikasi.

Get-AzPublicIpAddress -ResourceGroupName appgw-RG -Name publicIP01
Name                     : publicIP01
ResourceGroupName        : appgw-RG
Location                 : westus
Id                       : /subscriptions/<subscription_id>/resourceGroups/appgw-RG/providers/Microsoft.Network/publicIPAddresses/publicIP01
Etag                     : W/"00000d5b-54ed-4907-bae8-99bd5766d0e5"
ResourceGuid             : 00000000-0000-0000-0000-000000000000
ProvisioningState        : Succeeded
Tags                     : 
PublicIpAllocationMethod : Dynamic
IpAddress                : xx.xx.xxx.xx
PublicIpAddressVersion   : IPv4
IdleTimeoutInMinutes     : 4
IpConfiguration          : {
                                "Id": "/subscriptions/<subscription_id>/resourceGroups/appgw-RG/providers/Microsoft.Network/applicationGateways/appgwtest/frontendIP
                            Configurations/frontend1"
                            }
DnsSettings              : {
                                "Fqdn": "00000000-0000-xxxx-xxxx-xxxxxxxxxxxx.cloudapp.net"
                            }

Langkah berikutnya

Belajar mengonfigurasi offload TLS dengan mengunjungi: Mengonfigurasi Offload TLS