你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
可以通过 Azure CLI 使用 TLS 终端的证书创建应用程序网关。 对于后端服务器,可以使用虚拟机规模集。 在此示例中,规模集包含两个添加到应用程序网关的默认后端池的虚拟机实例。
在本文中,学习如何:
- 创建自签名证书
- 设置网络
- 使用证书创建应用程序网关
- 使用默认后端池创建虚拟机规模集
如果需要,可以使用 Azure PowerShell 完成此过程。
如果没有 Azure 帐户,请在开始前创建一个免费帐户。
先决条件
在 Azure Cloud Shell 中使用 Bash 环境。 有关详细信息,请参阅 Azure Cloud Shell 入门。
如果要在本地运行 CLI 引用命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI。
如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 要完成身份验证过程,请执行终端中显示的步骤。 有关其他登录选项,请参阅 使用 Azure CLI 向 Azure 进行身份验证。
按照提示,请在首次使用时安装 Azure CLI 扩展。 有关扩展的详细信息,请参阅 使用和管理 Azure CLI 中的扩展。
运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade。
- 本教程需要 Azure CLI 版本 2.0.4 或更高版本。 如果使用 Azure Cloud Shell,则最新版本已安装。
创建自签名证书
为供生产使用,应导入由受信任的提供程序签名的有效证书。 对于本文中的情况,请使用 openssl 命令创建自签名证书和 pfx 文件。
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out appgwcert.crt
输入对证书有意义的值。 可接受默认值。
openssl pkcs12 -export -out appgwcert.pfx -inkey privateKey.key -in appgwcert.crt
输入证书的密码。 在此示例中,使用了 Azure123456!。
创建资源组
资源组是在其中部署和管理 Azure 资源的逻辑容器。 使用 az group create 创建资源组。
以下示例在 eastus 位置创建一个名为 myResourceGroupAG 的资源组。
az group create --name myResourceGroupAG --___location eastus
创建网络资源
使用 az network vnet create 创建名为 myVNet 的虚拟网络和名为 myAGSubnet 的子网。 然后,可以使用 az network vnet subnet create 添加后端服务器所需的名为 myBackendSubnet 的子网。 使用 az network public-ip create 创建名为 myAGPublicIPAddress 的公共 IP 地址。
az network vnet create \
--name myVNet \
--resource-group myResourceGroupAG \
--___location eastus \
--address-prefix 10.0.0.0/16 \
--subnet-name myAGSubnet \
--subnet-prefix 10.0.1.0/24
az network vnet subnet create \
--name myBackendSubnet \
--resource-group myResourceGroupAG \
--vnet-name myVNet \
--address-prefix 10.0.2.0/24
az network public-ip create \
--resource-group myResourceGroupAG \
--name myAGPublicIPAddress \
--allocation-method Static \
--sku Standard \
--___location eastus
创建应用程序网关
可以使用 az network application-gateway create 创建应用程序网关。 使用 Azure CLI 创建应用程序网关时,请指定配置信息,例如容量、sku 和 HTTP 设置。
将应用程序网关分配给之前创建的 myAGSubnet 和 myAGPublicIPAddress。 在此示例中,在创建应用程序网关时将关联所创建的证书及其密码。
az network application-gateway create \
--name myAppGateway \
--___location eastus \
--resource-group myResourceGroupAG \
--vnet-name myVNet \
--subnet myAGsubnet \
--capacity 2 \
--sku Standard_v2 \
--http-settings-cookie-based-affinity Disabled \
--frontend-port 443 \
--http-settings-port 80 \
--http-settings-protocol Http \
--priority "1" \
--public-ip-address myAGPublicIPAddress \
--cert-file appgwcert.pfx \
--cert-password "Azure123456!"
创建应用程序网关可能需要几分钟时间。 创建应用程序网关后,可以看到它的这些新功能:
- appGatewayBackendPool - 应用程序网关必须至少具有一个后端地址池。
- appGatewayBackendHttpSettings - 指定将端口 80 和 HTTP 协议用于通信。
- appGatewayHttpListener - 与 appGatewayBackendPool 关联的默认侦听器。
- appGatewayFrontendIP - 将 myAGPublicIPAddress 分配给 appGatewayHttpListener。
- rule1 - 与 appGatewayHttpListener 关联的默认路由规则。
创建虚拟机规模集
在此示例中,将创建虚拟机规模集,以便为应用程序网关的默认后端池提供服务器。 规模集中的虚拟机与 myBackendSubnet 和 appGatewayBackendPool 相关联。 若要创建规模集,可以使用 az vmss create。
az vmss create \
--name myvmss \
--resource-group myResourceGroupAG \
--image Ubuntu2204 \
--admin-username azureuser \
--admin-password Azure123456! \
--instance-count 2 \
--vnet-name myVNet \
--subnet myBackendSubnet \
--vm-sku Standard_DS2 \
--upgrade-policy-mode Automatic \
--app-gateway myAppGateway \
--backend-pool-name appGatewayBackendPool
安装 NGINX
az vmss extension set \
--publisher Microsoft.Azure.Extensions \
--version 2.0 \
--name CustomScript \
--resource-group myResourceGroupAG \
--vmss-name myvmss \
--settings '{ "fileUris": ["https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/install_nginx.sh"],
"commandToExecute": "./install_nginx.sh" }'
测试应用程序网关
若要获取应用程序网关的公共 IP 地址,可以使用 az network public-ip show。
az network public-ip show \
--resource-group myResourceGroupAG \
--name myAGPublicIPAddress \
--query [ipAddress] \
--output tsv
复制该公共 IP 地址,并将其粘贴到浏览器的地址栏。 对于此示例,URL 为 https://52.170.203.149。
若要接受有关使用自签名证书的安全警告,请依次选择“详细信息”和“继续转到网页”。 随即显示受保护的 NGINX 站点,如下例所示:
清理资源
当不再需要资源组、应用程序网关以及所有相关资源时,请将其删除。
az group delete --name myResourceGroupAG --___location eastus