你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
如果你是 IT 管理员,打算改进 Web 应用程序访问,那么你可以优化应用程序网关,根据客户需求动态缩放并跨越多个可用性区域。 本教程可帮助你配置关键的 Azure 应用程序网关 v2 功能(包括自动缩放、区域冗余和静态 VIP),以实现这些改进。 你将使用 Azure PowerShell cmdlet 和 Azure 资源管理器部署模型来解决问题。
本教程中,您将学习如何:
- 创建自签名证书
- 创建自动缩放虚拟网络
- 创建保留的公共 IP
- 设置应用程序网关基础结构
- 指定自动缩放功能
- 创建应用程序网关
- 测试应用程序网关
如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
若要详细了解应用程序网关 v2 中的可用性区域支持,请参阅应用程序网关 v2 的可靠性。
先决条件
注释
建议使用 Azure Az PowerShell 模块与 Azure 交互。 若要开始,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az。
本教程要求在本地运行管理 Azure PowerShell 会话。 必须安装 Azure PowerShell 模块版本 1.0.0 或更高版本。 运行 Get-Module -ListAvailable Az 即可查找版本。 如果需要升级,请参阅安装 Azure PowerShell 模块。 验证 PowerShell 版本以后,请运行 Connect-AzAccount,以便创建与 Azure 的连接。
登录到 Azure
Connect-AzAccount
Select-AzSubscription -Subscription "<sub name>"
创建资源组
在某个可用位置中创建资源组。
$location = "East US 2"
$rg = "AppGW-rg"
#Create a new Resource Group
New-AzResourceGroup -Name $rg -Location $location
创建自签名证书
为供生产使用,应导入由受信任的提供程序签名的有效证书。 对于本教程,请使用 New-SelfSignedCertificate 创建自签名证书。 可以结合返回的指纹使用 Export-PfxCertificate,从证书导出 pfx 文件。
New-SelfSignedCertificate `
-certstorelocation cert:\localmachine\my `
-dnsname www.contoso.com
应会显示如下结果所示的内容:
PSParentPath: Microsoft.PowerShell.Security\Certificate::LocalMachine\my
Thumbprint Subject
---------- -------
E1E81C23B3AD33F9B4D1717B20AB65DBB91AC630 CN=www.contoso.com
使用指纹创建 pfx 文件。 将密码替换为<所选的密码>:
$pwd = ConvertTo-SecureString -String "<password>" -Force -AsPlainText
Export-PfxCertificate `
-cert cert:\localMachine\my\E1E81C23B3AD33F9B4D1717B20AB65DBB91AC630 `
-FilePath c:\appgwcert.pfx `
-Password $pwd
创建虚拟网络
为自动缩放应用程序网关创建具有一个专用子网的虚拟网络。 目前,每个专用子网中只能部署一个自动缩放应用程序网关。
#Create VNet with two subnets
$sub1 = New-AzVirtualNetworkSubnetConfig -Name "AppGwSubnet" -AddressPrefix "10.0.0.0/24"
$sub2 = New-AzVirtualNetworkSubnetConfig -Name "BackendSubnet" -AddressPrefix "10.0.1.0/24"
$vnet = New-AzvirtualNetwork -Name "AutoscaleVNet" -ResourceGroupName $rg `
-Location $location -AddressPrefix "10.0.0.0/16" -Subnet $sub1, $sub2
创建保留的公共 IP
将 PublicIPAddress 的分配方法指定为 Static。 自动缩放应用程序网关 VIP 只能为静态。 不支持动态 IP。 仅支持标准 PublicIpAddress SKU。
#Create static public IP
$pip = New-AzPublicIpAddress -ResourceGroupName $rg -name "AppGwVIP" `
-location $location -AllocationMethod Static -Sku Standard -Zone 1,2,3
检索详细信息
检索本地对象中资源组、子网和 IP 的详细信息,以创建应用程序网关的 IP 配置详细信息。
$publicip = Get-AzPublicIpAddress -ResourceGroupName $rg -name "AppGwVIP"
$vnet = Get-AzvirtualNetwork -Name "AutoscaleVNet" -ResourceGroupName $rg
$gwSubnet = Get-AzVirtualNetworkSubnetConfig -Name "AppGwSubnet" -VirtualNetwork $vnet
创建 Web 应用
为后端池配置两个 Web 应用。 将 <site1-name> 和 <site2-name> 替换为 azurewebsites.net 域中的唯一标识名称。
New-AzAppServicePlan -ResourceGroupName $rg -Name "ASP-01" -Location $location -Tier Basic `
-NumberofWorkers 2 -WorkerSize Small
New-AzWebApp -ResourceGroupName $rg -Name <site1-name> -Location $location -AppServicePlan ASP-01
New-AzWebApp -ResourceGroupName $rg -Name <site2-name> -Location $location -AppServicePlan ASP-01
配置基础结构
以与现有标准应用程序网关相同的格式配置 IP 配置、前端 IP 配置、后端池、HTTP 设置、证书、端口、侦听器和规则。 新 SKU 遵循与标准 SKU 相同的对象模型。
在 $ pool 变量定义中替换两个 Web 应用 FQDN(例如:mywebapp.azurewebsites.net)。
$ipconfig = New-AzApplicationGatewayIPConfiguration -Name "IPConfig" -Subnet $gwSubnet
$fip = New-AzApplicationGatewayFrontendIPConfig -Name "FrontendIPConfig" -PublicIPAddress $publicip
$pool = New-AzApplicationGatewayBackendAddressPool -Name "Pool1" `
-BackendIPAddresses <your first web app FQDN>, <your second web app FQDN>
$fp01 = New-AzApplicationGatewayFrontendPort -Name "SSLPort" -Port 443
$fp02 = New-AzApplicationGatewayFrontendPort -Name "HTTPPort" -Port 80
$securepfxpwd = ConvertTo-SecureString -String "Azure123456!" -AsPlainText -Force
$sslCert01 = New-AzApplicationGatewaySslCertificate -Name "SSLCert" -Password $securepfxpwd `
-CertificateFile "c:\appgwcert.pfx"
$listener01 = New-AzApplicationGatewayHttpListener -Name "SSLListener" `
-Protocol Https -FrontendIPConfiguration $fip -FrontendPort $fp01 -SslCertificate $sslCert01
$listener02 = New-AzApplicationGatewayHttpListener -Name "HTTPListener" `
-Protocol Http -FrontendIPConfiguration $fip -FrontendPort $fp02
$setting = New-AzApplicationGatewayBackendHttpSettings -Name "BackendHttpSetting1" `
-Port 80 -Protocol Http -CookieBasedAffinity Disabled -PickHostNameFromBackendAddress
$rule01 = New-AzApplicationGatewayRequestRoutingRule -Name "Rule1" -RuleType basic `
-BackendHttpSettings $setting -HttpListener $listener01 -BackendAddressPool $pool -Priority 1
$rule02 = New-AzApplicationGatewayRequestRoutingRule -Name "Rule2" -RuleType basic `
-BackendHttpSettings $setting -HttpListener $listener02 -BackendAddressPool $pool -Priority 2
指定自动缩放功能
现在可以为应用程序网关指定自动缩放配置。
$autoscaleConfig = New-AzApplicationGatewayAutoscaleConfiguration -MinCapacity 2
$sku = New-AzApplicationGatewaySku -Name Standard_v2 -Tier Standard_v2
在此模式下,应用程序网关根据应用程序流量模式自动缩放。
创建应用程序网关
创建应用程序网关,并包括冗余区域和自动缩放配置。
$appgw = New-AzApplicationGateway -Name "AutoscalingAppGw" -Zone 1,2,3 `
-ResourceGroupName $rg -Location $location -BackendAddressPools $pool `
-BackendHttpSettingsCollection $setting -GatewayIpConfigurations $ipconfig `
-FrontendIpConfigurations $fip -FrontendPorts $fp01, $fp02 `
-HttpListeners $listener01, $listener02 -RequestRoutingRules $rule01, $rule02 `
-Sku $sku -sslCertificates $sslCert01 -AutoscaleConfiguration $autoscaleConfig
测试应用程序网关
使用 Get-AzPublicIPAddress 获取应用程序网关的公共 IP 地址。 复制公共 IP 地址或 DNS 名称,然后将其粘贴到浏览器的地址栏中。
$pip = Get-AzPublicIPAddress -ResourceGroupName $rg -Name AppGwVIP
$pip.IpAddress
清理资源
首先探索使用应用程序网关创建的资源。 然后,当不再需要资源组、应用程序网关和所有相关资源时,可以使用 Remove-AzResourceGroup 命令将其删除。
Remove-AzResourceGroup -Name $rg