教程:使用 Ansible 在 Azure 中配置虚拟机规模集

重要

运行本文中的示例剧本需要 Ansible 2.9(或更高版本)。

Azure 虚拟机规模集 是一项 Azure 功能,可用于配置一组相同的负载均衡 VM。 规模集无需额外付费,它们是从虚拟机生成的。 只需为基础计算资源(例如 VM 实例、负载均衡器或托管磁盘存储)付费。 使用规模集时,提供管理和自动化层,以支持应用程序的运行和扩展。 可以改为手动创建和管理单个 VM。 但是,使用规模集有两个关键优势。 它们内置于 Azure 中,可自动缩放虚拟机以满足应用程序需求。

在这篇文章中,你将学会如何:

  • 为 VM 配置资源
  • 配置规模集
  • 通过增加 VM 实例来扩展规模集

先决条件

  • Azure 订阅:如果没有 Azure 订阅,请在开始之前创建 一个免费帐户

配置规模集

本部分中的 playbook 代码定义了以下资源:

  • 您将把所有资源部署到的资源组
  • 10.0.0.0/16 地址空间中的虚拟网络
  • 虚拟网络中的子网
  • 允许你通过 Internet 访问资源的公共 IP 地址
  • 控制规模集进出的网络流量的网络安全组
  • 使用 负载均衡器规则在一组定义的 VM 之间分配流量的负载均衡器
  • 使用所有已创建资源的虚拟机规模集

可以通过两种方法获取示例手册:

  • 下载方案手册 并将文件另存为 vmss-create.yml

  • 创建名为 vmss-create.yml 的新文件。 将以下代码插入新文件中:

- hosts: localhost
  vars:
    resource_group: myResourceGroup
    vmss_name: myvmscalesetname
    vmss_lb_name: myScaleSetLb
    location: eastus
    admin_username: azureuser
    admin_password: "{{ admin_password }}"

  tasks:
    - name: Create a resource group
      azure_rm_resourcegroup:
        name: "{{ resource_group }}"
        location: "{{ location }}"
    - name: Create virtual network
      azure_rm_virtualnetwork:
        resource_group: "{{ resource_group }}"
        name: "{{ vmss_name }}"
        address_prefixes: "10.0.0.0/16"
    - name: Add subnet
      azure_rm_subnet:
        resource_group: "{{ resource_group }}"
        name: "{{ vmss_name }}"
        address_prefix: "10.0.1.0/24"
        virtual_network: "{{ vmss_name }}"
    - name: Create public IP address
      azure_rm_publicipaddress:
        resource_group: "{{ resource_group }}"
        allocation_method: Static
        name: "{{ vmss_name }}"
    - name: Create Network Security Group that allows SSH
      azure_rm_securitygroup:
        resource_group: "{{ resource_group }}"
        name: "{{ vmss_name }}"
        rules:
          - name: SSH
            protocol: Tcp
            destination_port_range: 22
            access: Allow
            priority: 1001
            direction: Inbound

    - name: Create a load balancer
      azure_rm_loadbalancer:
        resource_group: "{{ resource_group }}"
        name: "{{ vmss_name }}lb"
        location: "{{ location }}"
        frontend_ip_configurations:
          - name: "{{ vmss_name }}front-config"
            public_ip_address: "{{ vmss_name }}"
        backend_address_pools:
          - name: "{{ vmss_name }}backend-pool"
        probes:
          - name: "{{ vmss_name }}prob0"
            port: 8080
            interval: 10
            fail_count: 3
        inbound_nat_pools:
          - name: "{{ vmss_name }}nat-pool"
            frontend_ip_configuration_name: "{{ vmss_name }}front-config"
            protocol: Tcp
            frontend_port_range_start: 50000
            frontend_port_range_end: 50040
            backend_port: 22
        load_balancing_rules:
          - name: "{{ vmss_name }}lb-rules"
            frontend_ip_configuration: "{{ vmss_name }}front-config"
            backend_address_pool: "{{ vmss_name }}backend-pool"
            frontend_port: 80
            backend_port: 8080
            load_distribution: Default
            probe: "{{ vmss_name }}prob0"

    - name: Create VMSS
      no_log: true
      azure_rm_virtualmachinescaleset:
        resource_group: "{{ resource_group }}"
        name: "{{ vmss_name }}"
        vm_size: Standard_DS1_v2
        admin_username: "{{ admin_username }}"
        admin_password: "{{ admin_password }}"
        ssh_password_enabled: true
        capacity: 2
        virtual_network_name: "{{ vmss_name }}"
        subnet_name: "{{ vmss_name }}"
        upgrade_policy: Manual
        tier: Standard
        managed_disk_type: Standard_LRS
        os_disk_caching: ReadWrite
        image:
          offer: UbuntuServer
          publisher: Canonical
          sku: 16.04-LTS
          version: latest
        load_balancer: "{{ vmss_name }}lb"
        data_disks:
          - lun: 0
            disk_size_gb: 20
            managed_disk_type: Standard_LRS
            caching: ReadOnly
          - lun: 1
            disk_size_gb: 30
            managed_disk_type: Standard_LRS
            caching: ReadOnly

在运行 playbook 之前,请查看以下注意事项:

  • 在本 vars 部分中,将 {{ admin_password }} 占位符替换为你自己的密码。

运行 playbook,使用 ansible-playbook

ansible-playbook vmss-create.yml

运行 playbook 后,会看到类似于以下结果的输出:

PLAY [localhost] 

TASK [Gathering Facts] 
ok: [localhost]

TASK [Create a resource group] 
changed: [localhost]

TASK [Create virtual network] 
changed: [localhost]

TASK [Add subnet] 
changed: [localhost]

TASK [Create public IP address] 
changed: [localhost]

TASK [Create Network Security Group that allows SSH] 
changed: [localhost]

TASK [Create a load balancer] 
changed: [localhost]

TASK [Create Scale Set] 
changed: [localhost]

PLAY RECAP 
localhost                  : ok=8    changed=7    unreachable=0    failed=0

查看 VM 实例数

配置的规模集当前有两个实例。 以下步骤用于确认该值:

  1. 登录到 Azure 门户

  2. 导航到您配置的虚拟机规模集。

  3. 会看到规模集名称,其中包含括号中的实例数: Standard_DS1_v2 (2 instances)

  4. 还可以通过运行以下命令来验证 Azure Cloud Shell 的实例数:

    az vmss show -n myScaleSet -g myResourceGroup --query '{"capacity":sku.capacity}' 
    

    在 Cloud Shell 中运行 Azure CLI 命令的结果显示存在两个实例:

    {
      "capacity": 2,
    }
    

横向扩展规模集

本节中的 playbook 代码检索有关规模集的信息,并将其容量从 2 增加至 3。

可通过两种方法获取示例剧本:

  • 下载 playbook 并将其保存到 vmss-scale-out.yml

  • 创建名为 vmss-scale-out.yml 的新文件。 将以下代码插入新文件中:

---
- hosts: localhost
  gather_facts: false
  
  vars:
    resource_group: myTestRG
    vmss_name: myTestVMSS
  
  tasks:

    - name: Get scaleset info
      azure_rm_virtualmachine_scaleset_facts:
        resource_group: "{{ resource_group }}"
        name: "{{ vmss_name }}"
        format: curated
      register: output_scaleset

    - name: set image fact
      set_fact:
        vmss_image: "{{ output_scaleset.vmss[0].image }}"

    - name: Create VMSS
      no_log: true
      azure_rm_virtualmachinescaleset:
        resource_group: "{{ resource_group }}"
        name: "{{ vmss_name }}"
        capacity: 3
        image: "{{ vmss_image }}"

运行 playbook 使用 ansible-playbook

ansible-playbook vmss-scale-out.yml

运行 playbook 后,会看到类似于以下结果的输出:

PLAY [localhost] 

TASK [Gathering Facts] 
ok: [localhost]

TASK [Get scaleset info] 
ok: [localhost]

TASK [Set image fact] 
ok: [localhost]

TASK [Change VMSS capacity] 
changed: [localhost]

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

验证结果

通过 Azure 门户验证工作结果:

  1. 登录到 Azure 门户

  2. 导航到配置的规模集。

  3. 会看到规模集名称,其中包含括号中的实例数: Standard_DS1_v2 (3 instances)

  4. 还可以通过运行以下命令来验证 Azure Cloud Shell 的更改:

    az vmss show -n myScaleSet -g myResourceGroup --query '{"capacity":sku.capacity}' 
    

    在 Cloud Shell 中运行 Azure CLI 命令的结果显示,现在存在三个实例:

    {
      "capacity": 3,
    }
    

后续步骤