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

教程:使用 Ansible 在 Azure 中自动缩放虚拟机规模集Tutorial: Autoscale virtual machine scale sets in Azure using Ansible

重要

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

Azure 虚拟机规模集是一项 Azure 功能,可让你配置一组相同的、负载均衡的 VM。Azure virtual machine scale sets is an Azure feature that lets you configure a group of identical, load balanced VMs. 规模集不需要额外的成本,它们是从虚拟机构建的。There's no additional cost to scale sets and they're built from virtual machines. 只需为基础的计算资源(例如 VM 实例、负载均衡器或托管磁盘存储)付费。You pay only for the underlying compute resources such as the VM instances, load balancers, or Managed Disk storage. 使用规模集时,会提供管理层和自动层来运行和缩放应用程序。With scale sets, the management and automation layers are provided to run and scale your applications. 你可以改为手动创建和管理各个 VM。You could instead manually create and manage individual VMs. 但是,使用规模集有两个主要优点。However, there are two key benefits to using scale sets. 它们内置于 Azure 中,并自动缩放虚拟机以满足应用程序需求。They're built into Azure and they automatically scale your virtual machines to meet application needs.

自动调整 VM 实例数量的功能被称为自动缩放The feature of automatically adjusting the number of VM instances is called autoscale. 自动缩放的优势在于减少监视和优化应用程序性能所产生的管理开销。The benefit of autoscale is that it reduces the management overhead to monitor and optimize the performance of your application. 自动缩放可根据需求或规定计划进行配置。Autoscale can be configured in response to demand or on a defined schedule. 通过 Ansible 可指定自动缩放规则,用于定义提供积极客户体验而可接受的性能。Using Ansible, you can specify the autoscale rules that define the acceptable performance for a positive customer experience.

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

  • 定义自动缩放配置文件Define an autoscale profile
  • 按定期计划自动缩放Autoscale based on a recurring schedule
  • 按应用性能自动缩放Autoscale based on app performance
  • 检索自动缩放设置信息Retrieve autoscale settings information
  • 禁用自动缩放设置Disable an autoscale setting

先决条件Prerequisites

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

按计划自动缩放Autoscale based on a schedule

若要在规模集上启用自动缩放,首先要定义自动缩放配置文件。To enable autoscale on a scale set, you first define an autoscale profile. 此配置文件定义默认、最小和最大规模集容量。This profile defines the default, minimum, and maximum scale set capacity. 这些限制可让你通过不继续创建 VM 实例来控制成本,并可使用缩小事件中保留的最小数量的实例均衡可接受的性能。These limits let you control cost by not continually creating VM instances, and balance acceptable performance with a minimum number of instances that remain in a scale-in event.

通过 Ansible 可按特定日期或定期计划缩放规模集。Ansible allows you to scale your scale sets on a specific date or recurring schedule.

本部分的 playbook 代码在每周一上午 10 点将 VM 实例数增加到 3 个。The playbook code in this section increases the number of VM instances to three at 10:00 every Monday.

将以下 playbook 保存为 vmss-auto-scale.ymlSave the following playbook as vmss-auto-scale.yml:

---
- hosts: localhost
  vars:
    resource_group: myResourceGroup
    vmss_name: myScaleSet
    name: autoscalesetting
  tasks: 
    - name: Create autoscaling
      azure_rm_autoscale:
         resource_group: "{{ resource_group }}"
         name: "{{ name }}"
         target:
           namespace: "Microsoft.Compute"
           types: "virtualMachineScaleSets"
           name: "{{ vmss_name }}"
         enabled: true
         profiles:
         - count: '3'
           min_count: '3'
           max_count: '3'
           name: Auto created scale condition
           recurrence_timezone: Pacific Standard Time
           recurrence_frequency: Week
           recurrence_days:
              - Monday
           recurrence_mins:
              - '0'
           recurrence_hours:
              - '10'

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

ansible-playbook vmss-auto-scale.yml

按性能数据自动缩放Autoscale based on performance data

如果应用程序需求提高,规模集中 VM 实例上的负载将会增大。If your application demand increases, the load on the VM instances in your scale sets increases. 如果这种负载增大持续稳定,而不只是短暂的需求,那么可以配置自动缩放规则来增加规模集中的 VM 实例数。If this increased load is consistent, rather than just a brief demand, you can configure autoscale rules to increase the number of VM instances in the scale set. 创建这些 VM 实例并部署应用程序后,规模集会开始通过负载均衡器将流量分配到这些实例和应用程序。When these VM instances are created and your applications are deployed, the scale set starts to distribute traffic to them through the load balancer. 通过 Ansible 可控制 CPU 使用情况、磁盘使用情况和应用加载时间等要监视的指标。Ansible allows you to control what metrics to monitor, such as CPU usage, disk usage, and app-load time. 可根据性能指标阈值,按定期计划或特定日期缩放和扩展规模集。You can scale in and scale out in scale sets based on performance metric thresholds, by a recurring schedule, or by a particular date.

本部分的 playbook 代码在每周一下午 6 点检查过去 10 分钟的 CPU 工作负载。The playbook code in this section checks the CPU workload for the previous 10 minutes at 18:00 every Monday.

playbook 根据 CPU 百分比指标执行以下操作之一:Based on the CPU percentage metrics, the playbook does one of the following actions:

  • 将 VM 实例数扩展为 4 个Scales out the number of VM instances to four
  • 将 VM 实例数缩减为 1 个Scales in the number of VM instances to one

将以下 playbook 保存为 vmss-auto-scale-metrics.ymlSave the following playbook as vmss-auto-scale-metrics.yml:

---
- hosts: localhost
  vars:
    resource_group: myResourceGroup
    vmss_name: myScaleSet
    name: autoscalesetting
  tasks:
  - name: Get facts of the resource group
    azure_rm_resourcegroup_facts:
      name: "{{ resource_group }}"
    register: rg

  - name: Get scale set resource uri
    set_fact:
      vmss_id: "{{ rg.ansible_facts.azure_resourcegroups[0].id }}/providers/Microsoft.Compute/virtualMachineScaleSets/{{ vmss_name }}"
    
  - name: Create autoscaling
    azure_rm_autoscale:
      resource_group: "{{ resource_group }}"
      name: "{{ name }}"
      target: "{{ vmss_id }}"
      enabled: true
      profiles:
      - count: '1'
        max_count: '1'
        min_count: '1'
        name: 'This scale condition is executed when none of the other scale condition(s) match'
        recurrence_days:
          - Monday
        recurrence_frequency: Week
        recurrence_hours:
          - 18
        recurrence_mins:
          - 0
        recurrence_timezone: Pacific Standard Time
      - count: '1'
        min_count: '1'
        max_count: '4'
        name: Auto created scale condition
        recurrence_days:
          - Monday
        recurrence_frequency: Week
        recurrence_hours:
          - 18
        recurrence_mins:
          - 0
        recurrence_timezone: Pacific Standard Time
        rules:
          - cooldown: 5
            direction: Increase
            metric_name: Percentage CPU
            metric_resource_uri: "{{ vmss_id }}"
            operator: GreaterThan
            statistic: Average
            threshold: 70
            time_aggregation: Average
            time_grain: 1
            time_window: 10
            type: ChangeCount
            value: '1'
          - cooldown: 5
            direction: Decrease
            metric_name: Percentage CPU
            metric_resource_uri: "{{ vmss_id }}"
            operator: LessThan
            statistic: Average
            threshold: 30
            time_aggregation: Average
            time_grain: 1
            time_window: 10
            type: ChangeCount
            value: '1'

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

ansible-playbook vmss-auto-scale-metrics.yml

获取自动缩放设置信息Get autoscale settings information

本部分的 playbook 代码使用 azure_rm_autoscale_facts 模块来检索自动缩放设置的详细信息。The playbook code in this section uses the azure_rm_autoscale_facts module to retrieve the details of autoscale setting.

将以下 playbook 保存为 vmss-auto-scale-get-settings.ymlSave the following playbook as vmss-auto-scale-get-settings.yml:

- hosts: localhost
  vars:
    resource_group: myResourceGroup
    name: autoscalesetting
  tasks: 
    - name: Retrieve autoscale settings information
      azure_rm_autoscale_facts:
        resource_group: "{{ resource_group }}"
        name: "{{ name }}"
      register: autoscale_query
    
    - debug:
        var: autoscale_query.autoscales[0]

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

ansible-playbook vmss-auto-scale-get-settings.yml

禁用自动缩放设置Disable autoscale settings

有两种方法可禁用自动缩放设置。There are two ways to disable autoscale settings. 一种方法是将 enabled 密钥从 true 更改为 falseOne way is to change the enabled key from true to false. 另一种方法是删除该设置。The second way is to delete the setting.

本部分的 playbook 代码将删除自动缩放设置。The playbook code in this section deletes the autoscale setting.

将以下 playbook 保存为 vmss-auto-scale-delete-setting.ymlSave the following playbook as vmss-auto-scale-delete-setting.yml:

- hosts: localhost
  vars:
    resource_group: myResourceGroup
    name: autoscalesetting
  tasks: 
    - name: Delete autoscaling
      azure_rm_autoscale:
         resource_group: "{{ resource_group }}"
         name: "{{ name }}"
         state: absent

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

vmss-auto-scale-delete-setting.yml

后续步骤Next steps