إنشاء مسبار مخصص لبوابة تطبيق Azure باستخدام PowerShell لإدارة موارد Azure

في هذه المقالة، يمكنك إضافة اختبار مخصص إلى بوابة تطبيق حالية مع PowerShell. تعد المسبارات المخصصة مفيدة للتطبيقات التي تحتوي على صفحة معينة لفحص السلامة وكذلك للتطبيقات التي لا توفر استجابة ناجحة تجاه تطبيق الويب الافتراضي.

إشعار

نوصي باستخدام الوحدة النمطية Azure Az PowerShell للتفاعل مع Azure. راجع تثبيت Azure PowerShell للبدء. لمعرفة كيفية الترحيل إلى الوحدة النمطية Az PowerShell، راجع ترحيل Azure PowerShell من AzureRM إلى Az.

المتطلبات الأساسية: تثبيت الوحدة النمطية Azure PowerShell

لتنفيذ الخطوات الواردة في هذه المقالة، تحتاج إلى تثبيت وتكوين الوحدة النمطية Azure PowerShell. تأكد من إكمال جميع التعليمات. بعد الانتهاء من التثبيت، سجل الدخول إلى Azure وحدد اشتراكك.

إشعار

تحتاج إلى حساب Azure لإكمال هذه الخطوات. إذا لم يكن لديك حساب Azure، يمكنك التسجيل للحصول على نسخة تجريبية مجانية.

إنشاء بوابة تطبيق باستخدام مسبار مخصص

تسجيل الدخول وإنشاء مجموعة موارد

  1. استخدم Connect-AzAccount للمصادقة

    Connect-AzAccount
    
  2. احصل على الاشتراكات للحساب.

    Get-AzSubscription
    
  3. اختر أي اشتراك من اشتراكات Azure الخاصة بك لاستخدامه.

    Select-AzSubscription -Subscriptionid '{subscriptionGuid}'
    
  4. قم بإنشاء مجموعة موارد. يمكنك تخطي هذه الخطوة إذا كان لديك مجموعة موارد حالية.

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

تتطلب Azure Resource Manager بأن تقوم كافة مجموعات الموارد بتحديد الموقع. يستخدم هذا الموقع كموقع افتراضي للموارد في مجموعة الموارد المذكورة. تأكد من أن كافة الأوامر لإنشاء بوابة تطبيق تستخدم نفس مجموعة الموارد.

في المثال السابق، أنشأنا مجموعة موارد تسمى appgw-RG في موقع غرب الولايات المتحدة.

إنشاء شبكة افتراضية وشبكة فرعية

ينشئ المثال التالي شبكة افتراضية وشبكة فرعية لبوابة التطبيق. تتطلب بوابة التطبيق شبكة فرعية خاصة بها لتستخدمها. لهذا السبب، الشبكة الفرعية التي تم إنشاؤها لبوابة التطبيق يجب أن تكون أصغر من مساحة العنوان التي تخص VNET للسماح بإنشاء شبكات فرعية أخرى واستخدامها.

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

إنشاء عنوان IP عام لتكوين الواجهة الأمامية

إنشاء IP01 عام لمورد IP عام في مجموعة الموارد appgw-rg لمنطقة غرب الولايات المتحدة. يستخدم هذا المثال عنوان IP عام لعنوان IP الأمامي لبوابة التطبيق. تتطلب بوابة التطبيق من عنوان IP العام بأن يكون له اسم DNS تم إنشاؤه ديناميكيًا ولذلك -DomainNameLabel لا يمكن تحديده أثناء إنشاء عنوان IP العام.

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

إنشاء بوابة تطبيق

إعداد كافة عناصر الواجهة قبل إنشاء بوابة التطبيق. إن المثال التالي يقوم بإعداد عناصر الواجهة التي تحتاجها موارد بوابة التطبيق.

المكون الوصف
تكوين IP للبوابة واجهة IP لبوابة التطبيق.
مجموعة خلفية تجمع عناوين IP أو FQDN أو NIC المخصصة لخوادم التطبيق التي تستضيف تطبيق الويب
فحص سلامة مسبار مخصص يستخدم لمراقبة صحة أعضاء تجمع الواجهة الخلفية
إعدادات HTTP مجموعة من الإعدادات بما في ذلك المنفذ والبروتوكول والتقارب المستند إلى ملفات تعريف الارتباط والمسبار والمستقطع الزمني. تحدد هذه الإعدادات كيفية توجيه حركة المرور إلى أعضاء المجموعة الخلفية
منفذ أمامي المنفذ الذي تصغي له بوابة التطبيق لحركة المرور مفتوحٌ
وحدة الاستماع مزيج من بروتوكول وواجهة IP الأمامية ومنفذ أمامي. هذه الوحدة تستمع للطلبات الواردة.
القاعده توجيه حركة المرور إلى الخلفية المناسبة استنادًا إلى إعدادات 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

إضافة مسبار إلى بوابة تطبيق حالية

إن قصاصة الكود التالية تضيف مسبارًا إلى بوابة تطبيق حالية.

# 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

إزالة مسبار من بوابة تطبيق حالية

إن قصاصة الكود التالية تزيل المسبار من بوابة التطبيق الحالية.

# 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

الحصول على اسم DNS لبوابة التطبيق

بمجرد إنشاء البوابة، فإن الخطوة التالية هي تكوين الواجهة الأمامية للاتصال. عند استخدام عنوان IP عام، تتطلب بوابة التطبيق اسم DNS معين ديناميكيا، وهو غير مألوف. للتأكد من أن المستخدمين النهائيين يمكنهم الوصول إلى بوابة التطبيق عندئذ يمكن استخدام سجل CNAME للإشارة إلى نقطة النهاية العامة لبوابة التطبيق. تكوين اسم مجال مخصص في Azure. للقيام بذلك، يتم استرجاع التفاصيل عن بوابة التطبيق واسم IP/DNS المقترن به باستخدام عنصر PublicIPAddress المرفق مع بوابة التطبيق. يجب استخدام اسم DNS الخاص ببوابة التطبيق لإنشاء سجل CNAME، والذي يحدد تطبيقي الويب من أجل اسم DNS المذكور. لا ينصح باستخدام السجلات A لأن VIP قد يتغير عند إعادة تشغيل بوابة التطبيق.

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"
                            }

الخطوات التالية

تعلم كيفية تكوين TLS للتفريغ عن طريق زيارة: تكوين TLS للتفريغ (Configure TLS Offload)