您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Ansible 在 Azure 应用服务中缩放应用Tutorial: Scale apps in Azure App Service using Ansible

重要

运行本文中的示例 playbook 需要 Ansible 2.7(或更高版本)。Ansible 2.7 (or later) is required to run the sample playbooks in this article.

Azure 应用服务使你可以生成和托管代码。Azure App Service enables you to build and host code. 此代码可以采用 Web 应用、移动后端和 RESTful API 的形式。This code can be in the form of web apps, mobile backends, and RESTful APIs. 借助应用服务,可以使用选择的编程语言开发代码,而无需管理基础结构。Using App Service, you can develop you code using the programming language of your choice without managing infrastructure. 应用服务支持 Windows 和 Linux。App Service supports both Windows and Linux. 支持从任何 Git 存储库进行自动部署,包括 GitHub 和 Azure DevOps。Automated deployments from any Git repo are supported, including GitHub and Azure DevOps.

在本教程中,Ansible 用于:In this tutorial, Ansible is used to:

  • 获取现有的应用服务计划的事实Get facts of an existing App Service plan
  • 将应用服务计划纵向扩展为具有三个辅助角色的 S2Scale up the App Service plan to S2 with three workers

先决条件Prerequisites

  • Azure 订阅:如果还没有 Azure 订阅,可以在开始前创建一个 免费帐户Azure subscription: If you don't have an Azure subscription, create a free account before you begin.

纵向扩展应用Scale up an app

有两个工作流可用于缩放、纵向扩展和横向扩展。There are two workflows for scaling: scale up and scale out.

纵向扩展: 纵向扩展意味着要获得更多资源。Scale up: To scale up means to acquire more resources. 这些资源包括 CPU、内存、磁盘空间、VM 等。These resources include CPU, memory, disk space, VMs, and more. 可通过更改应用所属的应用服务计划的定价层来纵向扩展应用。You scale up an app by changing the pricing tier of the App Service plan to which the app belongs. 横向扩展: 横向扩展意味着要增加运行应用的 VM 实例数量。Scale out: To scale out means to increase the number of VM instances that run your app. 根据应用服务计划定价层,可横向扩展到多达 20 个实例。Depending on your App Service plan pricing tier, you can scale out to as many as 20 instances. 借助自动缩放,可以根据预定义的规则和计划自动横向扩展实例计数。Autoscaling allows you to scale instance count automatically based on predefined rules and schedules.

本部分中的 playbook 代码定义以下操作:The playbook code in this section defines following operation:

  • 获取现有的应用服务计划的事实Get facts of an existing App Service plan
  • 将应用服务计划更新为具有三个辅助角色的 S2Update the App service plan to S2 with three workers

将以下 playbook 保存为 webapp_scaleup.ymlSave the following playbook as webapp_scaleup.yml:

- hosts: localhost
  connection: local
  vars:
    resource_group: myResourceGroup
    plan_name: myAppServicePlan
    location: eastus

  tasks:
  - name: Get facts of existing App service plan
    azure_rm_appserviceplan_facts:
      resource_group: "{{ resource_group }}"
      name: "{{ plan_name }}"
    register: facts

  - debug: 
      var: facts.appserviceplans[0].sku

  - name: Scale up the App service plan
    azure_rm_appserviceplan:
      resource_group: "{{ resource_group }}"
      name: "{{ plan_name }}"
      is_linux: true
      sku: S2
      number_of_workers: 3
      
  - name: Get facts
    azure_rm_appserviceplan_facts:
      resource_group: "{{ resource_group }}"
      name: "{{ plan_name }}"
    register: facts

  - debug: 
      var: facts.appserviceplans[0].sku

使用 ansible-playbook 命令运行 playbook:Run the playbook using the ansible-playbook command:

ansible-playbook webapp_scaleup.yml

运行 playbook 后,可看到类似于以下结果的输出:After running the playbook, you see output similar to the following results:

PLAY [localhost] 

TASK [Gathering Facts] 
ok: [localhost]

TASK [Get facts of existing App service plan] 
 [WARNING]: Azure API profile latest does not define an entry for WebSiteManagementClient

ok: [localhost]

TASK [debug] 
ok: [localhost] => {
    "facts.appserviceplans[0].sku": {
        "capacity": 1,
        "family": "S",
        "name": "S1",
        "size": "S1",
        "tier": "Standard"
    }
}

TASK [Scale up the App service plan] 
changed: [localhost]

TASK [Get facts] 
ok: [localhost]

TASK [debug] 
ok: [localhost] => {
    "facts.appserviceplans[0].sku": {
        "capacity": 3,
        "family": "S",
        "name": "S2",
        "size": "S2",
        "tier": "Standard"
    }
}

PLAY RECAP 
localhost                  : ok=6    changed=1    unreachable=0    failed=0 

后续步骤Next steps