Aracılığıyla paylaş


Azure PowerShell ile web trafiğini yönetme

Bu betik, arka uç sunucuları için bir sanal makine ölçek kümesi kullanan bir uygulama ağ geçidi oluşturur. Ardından uygulama ağ geçidi web trafiğini yönetmek üzere yapılandırılabilir. Betiği çalıştırdıktan sonra, genel IP adresini kullanarak uygulama ağ geçidini test edebilirsiniz.

Bu örnek için Azure PowerShell gerekir. Sürümü bulmak için Get-Module -ListAvailable Az komutunu çalıştırın. Yüklemeniz veya yükseltmeniz gerekirse, bkz. Azure PowerShell Modülü yükleme.

Azure'a bağlanmak için Bağlan-AzAccount cmdlet'ini çalıştırın.

Azure aboneliğiniz yoksa başlamadan önce birücretsiz Azure hesabı oluşturun.

Örnek betik

# Create a resource group
New-AzResourceGroup -Name myResourceGroupAG -Location eastus

# Create network resources
$backendSubnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name myBackendSubnet `
  -AddressPrefix 10.0.1.0/24
$agSubnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name myAGSubnet `
  -AddressPrefix 10.0.2.0/24
$vnet = New-AzVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -Name myVNet `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $backendSubnetConfig, $agSubnetConfig
$pip = New-AzPublicIpAddress `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -Name myAGPublicIPAddress `
  -AllocationMethod Dynamic

# Create IP configurations and frontend port
$vnet = Get-AzVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Name myVNet
$subnet=$vnet.Subnets[0]
$gipconfig = New-AzApplicationGatewayIPConfiguration `
  -Name myAGIPConfig `
  -Subnet $subnet
$fipconfig = New-AzApplicationGatewayFrontendIPConfig `
  -Name myAGFrontendIPConfig `
  -PublicIPAddress $pip
$frontendport = New-AzApplicationGatewayFrontendPort `
  -Name myFrontendPort `
  -Port 80

# Create the backend pool and settings
$defaultPool = New-AzApplicationGatewayBackendAddressPool `
  -Name appGatewayBackendPool 
$poolSettings = New-AzApplicationGatewayBackendHttpSettings `
  -Name myPoolSettings `
  -Port 80 `
  -Protocol Http `
  -CookieBasedAffinity Enabled `
  -RequestTimeout 120

# Create the default listener and rule
$defaultlistener = New-AzApplicationGatewayHttpListener `
  -Name mydefaultListener `
  -Protocol Http `
  -FrontendIPConfiguration $fipconfig `
  -FrontendPort $frontendport
$frontendRule = New-AzApplicationGatewayRequestRoutingRule `
  -Name rule1 `
  -RuleType Basic `
  -HttpListener $defaultlistener `
  -BackendAddressPool $defaultPool `
  -BackendHttpSettings $poolSettings

# Create the application gateway
$sku = New-AzApplicationGatewaySku `
  -Name WAF_Medium `
  -Tier WAF `
  -Capacity 2
$appgw = New-AzApplicationGateway `
  -Name myAppGateway `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -BackendAddressPools $defaultPool `
  -BackendHttpSettingsCollection $poolSettings `
  -FrontendIpConfigurations $fipconfig `
  -GatewayIpConfigurations $gipconfig `
  -FrontendPorts $frontendport `
  -HttpListeners $defaultlistener `
  -RequestRoutingRules $frontendRule `
  -Sku $sku 

# Create a virtual machine scale set
$vnet = Get-AzVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Name myVNet
$appgw = Get-AzApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway
$backendPool = Get-AzApplicationGatewayBackendAddressPool `
  -Name appGatewayBackendPool `
  -ApplicationGateway $appgw
$ipConfig = New-AzVmssIpConfig `
  -Name myVmssIPConfig `
  -SubnetId $vnet.Subnets[1].Id `
  -ApplicationGatewayBackendAddressPoolsId $backendPool.Id
$vmssConfig = New-AzVmssConfig `
  -Location eastus `
  -SkuCapacity 2 `
  -SkuName Standard_DS2 `
  -UpgradePolicyMode Automatic
Set-AzVmssStorageProfile $vmssConfig `
  -ImageReferencePublisher MicrosoftWindowsServer `
  -ImageReferenceOffer WindowsServer `
  -ImageReferenceSku 2016-Datacenter `
  -ImageReferenceVersion latest
  -OsDiskCreateOption FromImage
Set-AzVmssOsProfile $vmssConfig `
  -AdminUsername azureuser `
  -AdminPassword "Azure123456!" `
  -ComputerNamePrefix myvmss
Add-AzVmssNetworkInterfaceConfiguration `
  -VirtualMachineScaleSet $vmssConfig `
  -Name myVmssNetConfig `
  -Primary $true `
  -IPConfiguration $ipConfig
New-AzVmss `
  -ResourceGroupName myResourceGroupAG `
  -Name myvmss `
  -VirtualMachineScaleSet $vmssConfig

# Install IIS
$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/davidmu1/samplescripts/master/appgatewayurl.ps1"); 
  "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" }
$vmss = Get-AzVmss -ResourceGroupName myResourceGroupAG -VMScaleSetName myvmss
Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
  -Name "customScript" `
  -Publisher "Microsoft.Compute" `
  -Type "CustomScriptExtension" `
  -TypeHandlerVersion 1.8 `
  -Setting $publicSettings
Update-AzVmss `
  -ResourceGroupName myResourceGroupAG `
  -Name myvmss `
  -VirtualMachineScaleSet $vmss

# Get the IP address
Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress

Dağıtımı temizleme

Kaynak grubunu, uygulama ağ geçidini ve ilgili tüm kaynakları kaldırmak için aşağıdaki komutu çalıştırın.

Remove-AzResourceGroup -Name myResourceGroupAG

Betik açıklaması

Bu betik, dağıtımı oluşturmak için aşağıdaki komutları kullanır. Tablodaki her öğe, komuta özgü belgelere yönlendirir.

Command Notlar
New-AzResourceGroup Tüm kaynakların depolandığı bir kaynak grubu oluşturur.
New-AzVirtualNetworkSubnetConfig Alt ağ yapılandırmasını oluşturur.
New-AzVirtualNetwork Alt ağ yapılandırmalarıyla kullanarak sanal ağı oluşturur.
New-AzPublicIpAddress Uygulama ağ geçidi için genel IP adresini oluşturur.
New-AzApplicationGatewayIPConfiguration Bir alt ağı uygulama ağ geçidiyle ilişkilendiren yapılandırmayı oluşturur.
New-AzApplicationGatewayFrontendIPConfig Uygulama ağ geçidine genel bir IP adresi atayan yapılandırmayı oluşturur.
New-AzApplicationGatewayFrontendPort Uygulama ağ geçidine erişmek için kullanılacak bir bağlantı noktası atar.
New-AzApplicationGatewayBackendAddressPool Uygulama ağ geçidi için bir arka uç havuzu oluşturur.
New-AzApplicationGatewayBackendHttp Ayarlar Arka uç havuzu için ayarları yapılandırır.
New-AzApplicationGatewayHttpListener Dinleyici oluşturur.
New-AzApplicationGatewayRequestRoutingRule Yönlendirme kuralı oluşturur.
New-AzApplicationGatewaySku Uygulama ağ geçidi için katman ve kapasiteyi belirtin.
New-AzApplicationGateway Uygulama ağ geçidi oluşturur.
Set-AzVmss Depolama Profile Ölçek kümesi için bir depolama profili oluşturun.
Set-AzVmssOsProfile Ölçek kümesi için işletim sistemini tanımlayın.
Add-AzVmssNetworkInterfaceConfiguration Ölçek kümesi için ağ arabirimini tanımlayın.
New-AzVmss Sanal makine ölçek kümesi oluşturma
Get-AzPublicIPAddress Uygulama ağ geçidinin genel IP adresini alır.
Remove-AzResourceGroup Kaynak grubunu ve grubun içerdiği tüm kaynakları kaldırır.

Sonraki adımlar

Azure PowerShell modülü hakkında daha fazla bilgi için bkz. Azure PowerShell belgeleri.

Ek uygulama ağ geçidi PowerShell betiği örnekleri, Azure Application Gateway belgeleri içinde bulunabilir.