Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Compute Cloud
  • Getting started
    • Overview
    • Creating a Linux VM
    • Creating a Windows VM
    • Creating instance groups
  • Step-by-step instructions
    • All instructions
    • Creating VMs
      • Creating a Linux VM
      • Creating a Windows VM
      • Creating a VM from a set of disks
      • Create a VM with disks restored from snapshots
      • Creating a VM from a custom image
      • Creating a preemptible VM
      • Creating a VM with a GPU
    • DSVM
      • Overview
      • Creating a VM from a public DSVM image
    • Placement groups
      • Creating a placement group
      • Deleting a placement group
      • Creating a VM instance in a placement group
      • Adding a VM to a placement group
      • Removing a VM instance from a placement group
    • Images with pre-installed software
      • Creating a VM from a public image
      • Configuring software
      • Working with a VM based on a public image
      • Getting a list of public images
    • Getting information about a VM
      • Getting information about a VM
      • Viewing serial port output
    • Managing VMs
      • Stopping and starting a VM
      • Attaching a disk to a VM
      • Detaching a disk from a VM
      • Moving a VM to a different availability zone
      • Making a VM's public IP address static
      • Updating a VM
      • Changing VM computing resources
      • Deleting a VM
    • Working on VMs
      • Connecting to a VM via SSH
      • Connecting to a VM via RDP
      • Working with Yandex.Cloud from inside a VM
      • Installing NVIDIA drivers
    • Creating new disks
      • Creating an empty disk
    • Disk management
      • Creating a disk snapshot
      • Updating a disk
      • Deleting a disk
      • Deleting a disk snapshot
    • Creating new images
      • Uploading your image
    • Managing images
      • Deleting a disk image
    • Managing the serial console
      • Getting started
      • Connecting to a serial console via SSH
      • Connecting to a serial console via CLI
      • Start your terminal in the Windows SAC
      • Disabling access to the serial console
    • Creating instance groups
      • Creating a fixed-size instance group
      • Creating a fixed-size instance group with a load balancer
      • Creating an automatically scaled instance group
      • Creating an instance group from Container Optimized Image
    • Getting information about instance groups
      • Getting a list of groups
      • Getting information about a group
      • Getting a list of instances in a group
    • Managing instance groups
      • Update a group
      • Configure application health check on the VM
      • Update a group
        • Incremental update
        • Updating without downtime
      • Stop a group
      • Start a group
      • Delete a group
    • Dedicated hosts
      • Creating a VM in a group of dedicated hosts
      • Creating a VM on a dedicated host
  • Yandex Container Optimized Solutions
  • Scenarios
    • Configuring NTP time synchronization
    • Running instance groups with auto scaling
  • Concepts
    • Relationship between resources
    • Virtual machines
      • Overview
      • Platforms
      • vCPU performance levels
      • Graphics accelerators (GPUs)
      • Preemptible VMs
      • Network on a VM
      • Live migration
      • Placement groups
      • Statuses
      • Metadata
    • Disks
      • Overview
      • Disk snapshots
    • Images
    • Instance groups
      • Overview
      • Access
      • Instance template
      • Variables in an instance template
      • Policies
        • Overview
        • Allocation policy
        • Deployment policy
        • Scaling policy
      • Scaling types
      • Auto-healing
      • Update
        • Overview
        • Allocating instances across zones
        • Deployment algorithm
        • Rules for updating instance groups
      • Statuses
    • Dedicated host
    • Backups
    • Quotas and limits
  • Access management
  • Pricing policy
    • Current pricing policy
    • Archive
      • Before January 1, 2019
      • From January 1 to March 1, 2019
      • From March 1 to May 1, 2019
  • Compute API reference
    • Authentication in the API
    • gRPC
      • Overview
      • DiskPlacementGroupService
      • DiskService
      • DiskTypeService
      • HostGroupService
      • HostTypeService
      • ImageService
      • InstanceService
      • PlacementGroupService
      • SnapshotService
      • ZoneService
      • InstanceGroupService
      • OperationService
    • REST
      • Overview
      • Disk
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • update
      • DiskPlacementGroup
        • Overview
        • create
        • delete
        • get
        • list
        • listDisks
        • listOperations
        • update
      • DiskType
        • Overview
        • get
        • list
      • HostGroup
        • Overview
        • create
        • delete
        • get
        • list
        • listHosts
        • listInstances
        • listOperations
        • update
      • HostType
        • Overview
        • get
        • list
      • Image
        • Overview
        • create
        • delete
        • get
        • getLatestByFamily
        • list
        • listOperations
        • update
      • Instance
        • Overview
        • addOneToOneNat
        • attachDisk
        • create
        • delete
        • detachDisk
        • get
        • getSerialPortOutput
        • list
        • listOperations
        • removeOneToOneNat
        • restart
        • start
        • stop
        • update
        • updateMetadata
        • updateNetworkInterface
      • PlacementGroup
        • Overview
        • create
        • delete
        • get
        • list
        • listInstances
        • listOperations
        • update
      • Snapshot
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • update
      • Zone
        • Overview
        • get
        • list
      • Operation
        • Overview
        • get
      • InstanceGroup
        • Overview
        • createFromYaml
        • update
        • list
        • get
        • delete
        • start
        • stop
        • create
        • listAccessBindings
        • setAccessBindings
        • updateFromYaml
        • listLogRecords
        • listInstances
        • updateAccessBindings
        • listOperations
  • Questions and answers
    • General questions
    • Virtual machines
    • Disks and snapshots
    • Licensing
    • All questions on the same page
  1. Scenarios
  2. Running instance groups with auto scaling

Running instance groups with auto scaling

  • Before you start
  • Prepare the environment
  • Create an instance group with auto scaling and load balancer
  • Add a load balancer with a target group
  • Test your instance group and load balancer
    • Test auto scaling
  • Delete the infrastructure

To create an instance group with auto scaling and network load balancer, do the following.

Before you start

  1. If you don't have the Yandex.Cloud command line interface yet, install it.

  2. For the scripts from the tutorial to run correctly, download and install the jq utility.

  3. To check auto scaling, install the wrk benchmarking tool.

Prepare the environment

  1. Create a service account with the name for-autoscale and assign it the editor role:

    Management console
    CLI
    API
    1. In the management console, select the folder where you want to create a service account.
    2. Go to the Service accounts tab.
    3. Click Create service account.
    4. Enter the name for-autoscale.
    5. To assign the service account a role for the current folder, click Add role and select the role editor.
    6. Click Create.
    1. Create a service account:

      The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

      yc iam service-account create --name for-autoscale
      

      Command execution result:

      id: ajelabcde12f33nol1v5
      folder_id: b0g12ga82bcv0cdeferg
      created_at: "2020-11-30T14:32:18.900092Z"
      name: for-autoscale
      
    2. Assign the role to the service account:

      yc resource-manager folder add-access-binding b1g23ga82bcv0cdeferg \
        --role editor \
        --subject serviceAccount:ajelabcde12f33nol1v5
      

    Use the method Create for the ServiceAccount resource.

  2. Create a network named yc-auto-network and subnets in two availability zones:

    Management console
    CLI
    API
    1. In the management console, select the folder where you want to create a network.
    2. Select Virtual Private Cloud.
    3. Click Create network.
    4. Enter the network name yc-auto-network.
    5. Select the additional option Create subnets.
    6. Click Create network.
    1. Create a network:

      yc vpc network create --name yc-auto-network
      

      Command execution result:

      id: enpabce123hde4ft1r3t
      folder_id: b0g12ga82bcv0cdeferg
      created_at: "2020-11-30T14:57:15Z"
      name: yc-auto-network
      
    2. Create a subnet in the ru-central1-b zone:

      yc vpc subnet create --network-id enpabce123hde4ft1r3t --range 192.168.1.0/24 --zone ru-central1-b
      

      Command execution result:

      id: e1lnabc23r1c9d0efoje
      folder_id: b0g12ga82bcv0cdeferg
      created_at: "2020-11-30T16:23:12Z"
      network_id: enpabce123hde4ft1r3t
      zone_id: ru-central1-b
      v4_cidr_blocks:
      - 192.168.1.0/24
      
    3. Create a subnet in the ru-central1-c zone:

      yc vpc subnet create --network-id enpabce123hde4ft1r3t --range 192.168.2.0/24 --zone ru-central1-c
      

      Command execution result:

      id: b1csa2b3clideftjb121
      folder_id: b0g12ga82bcv0cdeferg
      created_at: "2020-11-30T16:25:02Z"
      network_id: enpabce123hde4ft1r3t
      zone_id: ru-central1-c
      v4_cidr_blocks:
      - 192.168.2.0/24
      
    1. Create a network using the method Create for the resource Networks.

    2. Create subnets in the ru-central1-b and ru-central1-c availability zones using the method Create for the resource Subnets.

Create an instance group with auto scaling and load balancer

  1. Save the specification of the instance group with network load balancer to the file specification.yaml:

    name: auto-group
    service_account_id: ajelabcde12f33nol1v5
    scale_policy:
      auto_scale:
        min_zone_size: 1
        max_size: 6
        measurement_duration: 60s
        warmup_duration: 120s
        cooldown_duration: 60s
        initial_size: 2
        cpu_utilization_rule:
          utilization_target: 40
    deploy_policy:
      max_unavailable: 1
    allocation_policy:
      zones:
        - zone_id: ru-central1-b
        - zone_id: ru-central1-c
    load_balancer_spec:
      target_group_spec:
        name: auto-group-tg
    instance_template:
      service_account_id: ajelabcde12f33nol1v5
      platform_id: standard-v1
      resources_spec:
        memory: 2G
        cores: 2
        core_fraction: 100
      metadata:
        docker-container-declaration: |-
          spec:
            containers:
            - image: cr.yandex/yc/demo/web-app:v1
              securityContext:
                privileged: false
              tty: false
              stdin: false
      boot_disk_spec:
        mode: READ_WRITE
        disk_spec:
          type_id: network-hdd
          size: 10G
          image_id: fd81a49qficqvt0dthu8
      network_interface_specs:
        - network_id: enpabce123hde4ft1r3t
          primary_v4_address_spec: { one_to_one_nat_spec: { ip_version: IPV4 }}
    
  2. Create an instance group named auto-group using the specification specification.yaml:

    CLI
    API

    Run the command:

    yc compute instance-group create --file=specification.yaml
    

    Command execution result:

    done (2m45s)
    id: cl0hmabc1nd2hdefgb7k
    folder_id: b0g12ga82bcv0cdeferg
    ...
        name: auto-group-tg
    service_account_id: ajelabcde12f33nol1v5
    status: ACTIVE
    

    All the instance groups are created from the image Container Optimized Image.Each instance contains a Docker container running a web server that emulates the service utilization.

    Use the method CreateFromYaml for the resource InstanceGroup.

  3. Make sure that the instance group was created:

    Management console
    CLI
    API
    1. In the management console, select the folder where you created the instance group.
    2. Select Compute Cloud.
    3. Go to Instance groups.
    4. Click the auto-group instance group name.
    yc compute instance-group list-instances auto-group
    

    Command execution result:

    +----------------------+---------------------------+----------------+--------------+------------------------+----------------+
    |     INSTANCE ID      |           NAME            |  EXTERNAL IP   | INTERNAL IP  |         STATUS         | STATUS MESSAGE |
    +----------------------+---------------------------+----------------+--------------+------------------------+----------------+
    | epdab0c1ji2ad4ef8l1s | cl0habce1nd2hqstgd7e-fned | 84.201.163.202 | 192.168.1.34 | RUNNING_ACTUAL [4m26s] |                |
    | ef0uabc1s2fbde6f5tlu | cl0habce1nd2hqstga7b-craq | 130.193.56.102 | 192.168.2.19 | RUNNING_ACTUAL [4m14s] |                |
    +----------------------+---------------------------+----------------+--------------+------------------------+----------------+
    

    See the list of the instance groups you created using the method List for the resource InstanceGroup.

Add a load balancer with a target group

  1. Create a load balancer named group-balancer and connect it to the instance group you created:

    Management console
    CLI
    API
    1. In the management console, select the folder where you want to create a load balancer.
    2. Select Load Balancer.
    3. Click Create load balancer.
    4. Enter Name group-balancer.
    5. In the Public address field, select the value Auto.
    6. Click Add listener under Listeners.
      • In the window that opens, enter Listener name http.
      • In the Port field, enter 80: the balancer will use this port to accept the incoming traffic.
      • In the Target port field, enter 80: the balancer will redirect traffic to this port.
      • Click Add.
    7. Under Target groups, click Add target group.
    8. In the Target group field, select the instance group auto-group and click Configure:
      • In the window that opens (Health check settings), enter the Name tcp.
      • Select the Type TCP.
      • In the Port field, enter 80.
      • Click Apply.
    9. Click Create.
    yc load-balancer network-load-balancer create \
      --listener external-ip-version=ipv4,name=http,port=80,protocol=tcp,target-port=80 \
      --name group-balancer \
      --target-group healthcheck-name=tcp,healthcheck-tcp-port=80,target-group-id=cl0hmabc1nd2hdefgb7k
    

    Command execution result:

    done (16s)
    id: b0rbabc1m2edfu2ss579
    folder_id: b0g12ga82bcv0cdeferg
    ...
        healthy_threshold: "2"
        tcp_options:
          port: "80"
    
    1. Create a network load balancer using the method Create for the resource NetworkLoadBalancers.

    2. Add a listener to the balancer using the method AddListener for the resource NetworkLoadBalancers.

    3. Attach the target group to the balancer using the method AttachTargetGroup for the resource NetworkLoadBalancers.

    4. Add the balancer to the instance group using the method AddTargets for the resource TargetGroups.

  2. Make sure that the balancer group-balancer is created and linked to the instance group:

    Management console
    CLI
    API
    1. In the management console, select the folder where you created the load balancer.
    2. Select Load Balancer.
    3. Click the load balancer name group-balancer.
    yc load-balancer network-load-balancer list
    

    Command execution result:

    +----------------------+----------------+-------------+----------+----------------+------------------------+--------+
    |          ID          |      NAME      |  REGION ID  |   TYPE   | LISTENER COUNT | ATTACHED TARGET GROUPS | STATUS |
    +----------------------+----------------+-------------+----------+----------------+------------------------+--------+
    | b0rbabc1m2edfu2ss579 | group-balancer | ru-central1 | EXTERNAL |              1 | b0rdabckribed1fbv2l1   | ACTIVE |
    +----------------------+----------------+-------------+----------+----------------+------------------------+--------+
    

    Use the method List for the resource NetworkLoadBalancer.

Test your instance group and load balancer

  1. Create a load on an instance.

    To do this, save the script named request.sh in the home directory. The script will send a request to one of the instances through the group-balancer load balancer. The request will utilize 100% CPU for 30 seconds.

    EXTERNAL_IP=$(yc load-balancer network-load-balancer get group-balancer --format=json | jq -r .listeners[0].address)
    
    curl "http://$EXTERNAL_IP/burn-cpu?time=30000&load=100"
    
  2. Run the script:

    CLI
    sh request.sh
    

    Command execution result:

    projects/b0g12ga82bcv0cdeferg/zones/ru-central1-b
    
  3. View the instance utilization:

    Management console
    1. In the management console, select the folder where you created the instance group.
    2. Select Compute Cloud.
    3. Go to Instance groups.
    4. Click the auto-group instance group name.
    5. Select the Monitoring tab.
      The load balancer sent the request to an instance in the group. In the availability zone this instance belongs to, the average CPU utilization is higher than in other zones (see the chart Average CPU utilization in zone).

Test auto scaling

To test auto scaling for your instance group, increase the CPU utilization of each instance. In the specification.yaml file, the parameter scale_policy.auto_scale.cpu_utilization_rule.utilization_target has the value 40: it means that the target utilization level is 40% CPU. If you exceed the target utilization, Instance Groups increases the number of instances in the group.

  1. Increase the utilization of the instance group.

    To do this, save the script named load.sh in the home directory. The script will send requests through 12 threads for 10 minutes, utilizing 20% CPU of each instance. The instance group utilization is 240% CPU at any given time. To be sure that requests are evenly distributed across the instances in the group, the script executes multiple parallel requests utilizing 20% CPU rather than one request utilizing 240% CPU.

    EXTERNAL_IP=$(yc load-balancer network-load-balancer get group-balancer --format=json | jq -r .listeners[0].address)
    
    wrk -H "Connection: close" -t12 -c12 -d10m "http://$EXTERNAL_IP/burn-cpu?time=5000&load=20"
    
  2. Run the script:

    CLI
    sh load.sh
    

    Command execution result:

    Running 10m test @ http://130.193.56.111/burn-cpu?time=5000&load=20
      12 threads and 12 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
    ...
    Requests/sec: 15.04
    Transfer/sec: 3.20KB
    
  3. View the utilization levels:

    Management console
    1. In the management console, select the folder where you created the auto-group instance group.
    2. Select Compute Cloud.
    3. Go to Instance groups.
    4. Click the auto-group instance group name.
    5. Select the Monitoring tab.
      The chart Number of instances in zone shows how the number of instances changed in each availability zone. The chart Average CPU utilization in zone shows average CPU utilization in each availability zone.
    6. Click the Logs tab.
      The page displays messages relating to auto scaling of the instance group.

    The total utilization of 240% CPU was evenly distributed between two instances in two availability zones and exceeded the target utilization of 40% CPU. Instance Groups created one instance more in each availability zone to result in four instances in the group. When the script stopped utilizing the CPU, Instance Groups automatically decreased the number of instances in the group to two.

Delete the infrastructure

  1. Delete the load balancer:

    Management console
    CLI
    API
    1. In the management console, select the folder where you created the load balancer group-balancer.
    2. Select Load Balancer.
    3. Click in the row of the load balancer group-balancer.
    4. In the menu that opens, click Delete.
    5. In the Delete load balancer window that opens, click Delete.
    yc load-balancer network-load-balancer delete group-balancer
    

    Command execution result:

    done (15s)
    

    Use the method Delete for the resource NetworkLoadBalancer.

  2. Delete the instance group:

    Management console
    CLI
    API
    1. In the management console, select the folder where you created the auto-group instance group.
    2. Select Compute Cloud.
    3. Go to Instance groups.
    4. Click for the instance group auto-group.
    5. In the menu that opens, click Delete.
    6. In the Delete instance group window that opens, click Delete.
    yc compute instance-group delete auto-group
    

    Command execution result:

    done (1m20s)
    

    Use the method Delete for the resource InstanceGroup.

In this article:
  • Before you start
  • Prepare the environment
  • Create an instance group with auto scaling and load balancer
  • Add a load balancer with a target group
  • Test your instance group and load balancer
  • Test auto scaling
  • Delete the infrastructure
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC