Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
© 2022 Yandex.Cloud LLC
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
      • Creating a VM with disks from snapshots
      • Creating a VM from a custom image
      • Creating a VM with a GPU
      • Making a VM preemptible
    • DSVM
      • Overview
      • Creating a VM from a public DSVM image
    • Placement groups
      • Creating a placement group
      • Deleting a placement group
      • Creating a VM in a placement group
      • Adding a VM to a placement group
      • Removing a VM from a placement group
    • Images with pre-installed software
      • Creating a VM from a public image
      • Configuring software
      • Running a VM based on a public image
      • Getting a list of public images
    • Getting information about a VM
      • Getting information about a VM
      • Getting serial port's output
    • Managing VMs
      • Stopping and starting a VM
      • Resetting a VM user password
      • Attaching a disk to a VM
      • Detaching a disk from a VM
      • Moving a VM to a different availability zone
      • Moving a VM to another folder
      • Attaching a public IP address to a VM
      • Detaching a public IP address from a VM
      • 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
      • Connecting to a VM via PowerShell
      • Working with Yandex Cloud from inside a VM
      • Installing NVIDIA drivers
      • Restoring access to a VM
    • Managing the password reset agent
      • Verifying agent operation
      • Installing the agent
      • Deleting the agent
    • Creating new disks
      • Creating an empty disk
      • Creating an empty disk with a large block
      • Creating a non-replicated disk
    • Disk management
      • Creating a disk snapshot
      • Updating a disk
      • Moving a disk to another folder
      • Deleting a disk
      • Deleting a disk snapshot
    • Disk placement groups
      • Creating a disk placement group
      • Removing a disk from a placement group
    • Creating new images
      • Preparing a disk image
      • Uploading your image
      • Creating an image from a disk
      • Creating an image from a snapshot
      • Creating an image from other custom image
    • Managing images
      • Getting a list of images
      • Deleting a disk image
    • File storage
      • Creating file storage
      • Attaching file storage to a VM
      • Detaching file storage from a VM
      • Updating file storage
      • Deleting file storage
    • Managing the serial console
      • Getting started
      • Connecting to a serial console via SSH
      • Connecting to a serial console via CLI
      • Starting your terminal in the Windows serial console (SAC)
      • Disabling access to the serial console
    • Creating instance groups
      • Creating a fixed-size instance group
      • Creating a fixed-size instance group with a network load balancer
      • Creating a fixed-size instance group with an L7 load balancer
      • Creating an automatically scaled instance group
      • Creating an instance group from a Container Optimized Image
      • Creating an instance group based on the YAML specification
    • Getting information about instance groups
      • Getting a list of instance groups
      • Getting information about an instance group
      • Getting a list of instances in a group
    • Managing instance groups
      • Editing an instance group
      • Edit an instance group based on the YAML specification
      • Configuring application health check on the VM
      • Updating a group
        • Incremental update
        • Uninterrupted updates
      • Pausing an instance group
      • Resuming an instance group
      • Stopping an instance group
      • Starting an instance group
      • Deleting an instance group
    • Dedicated hosts
      • Creating a VM in a group of dedicated hosts
      • Creating a VM on a dedicated host
  • Yandex Container Solution
  • Practical guidelines
    • Configuring NTP time synchronization
    • Running instance groups with auto scaling
    • Automatically scaling an instance group for handling messages from a queue
    • Updating an instance group under load
    • Deploying Remote Desktop Gateway
    • Transferring logs from a VM instance to Yandex Cloud Logging
    • Creating a VM backup with Hystax Acura Backup
  • Concepts
    • Relationship between resources
    • Virtual machines
      • Overview
      • Platforms
      • vCPU performance levels
      • Preemptible VMs
      • Network on a VM
      • Software-accelerated network
      • Live migration
      • Placement groups
      • Statuses
      • Metadata
      • Resetting a Windows VM password
    • Graphics accelerators
    • Disks and file storage
      • Overview
      • Disks
      • Disk snapshots
      • Non-replicated disk placement groups
      • File storage
      • Read and write operations
    • Images
    • Instance groups
      • Overview
      • Access
      • YAML specification
      • Instance template
      • Variables in an instance template
      • Policies
        • Overview
        • Allocation policy
        • Deployment policy
        • Scaling policy
      • Scaling types
      • Auto-healing
      • Updating
        • Overview
        • Allocating instances across zones
        • Deployment algorithm
        • Rules for updating virtual machines
        • Changing secondary disks in an instance template
      • Stopping and pausing an instance group
      • 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
  • API reference
    • Authentication in the API
    • gRPC
      • Overview
      • DiskPlacementGroupService
      • DiskService
      • DiskTypeService
      • FilesystemService
      • HostGroupService
      • HostTypeService
      • ImageService
      • InstanceService
      • PlacementGroupService
      • SnapshotScheduleService
      • SnapshotService
      • ZoneService
      • InstanceGroupService
      • OperationService
    • REST
      • Overview
      • DiskPlacementGroup
        • Overview
        • create
        • delete
        • get
        • list
        • listDisks
        • listOperations
        • update
      • Disk
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • move
        • update
      • DiskType
        • Overview
        • get
        • list
      • Filesystem
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • update
      • 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
        • attachFilesystem
        • create
        • delete
        • detachDisk
        • detachFilesystem
        • get
        • getSerialPortOutput
        • list
        • listOperations
        • move
        • removeOneToOneNat
        • restart
        • start
        • stop
        • update
        • updateMetadata
        • updateNetworkInterface
      • PlacementGroup
        • Overview
        • create
        • delete
        • get
        • list
        • listInstances
        • listOperations
        • update
      • SnapshotSchedule
        • Overview
        • create
        • delete
        • disable
        • enable
        • get
        • list
        • listDisks
        • listOperations
        • listSnapshots
        • update
        • updateDisks
      • Snapshot
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • update
      • Zone
        • Overview
        • get
        • list
      • Operation
        • Overview
        • get
      • InstanceGroup
        • Overview
        • list
        • get
        • listLogRecords
        • updateFromYaml
        • updateAccessBindings
        • pauseProcesses
        • stop
        • start
        • delete
        • listInstances
        • createFromYaml
        • update
        • setAccessBindings
        • listOperations
        • create
        • listAccessBindings
        • resumeProcesses
  • Questions and answers
    • General questions
    • Virtual machines
    • Connection
    • Disks, snapshots, and images
    • Disaster recovery
    • Monitoring
    • Licensing
    • All questions on the same page
  1. Practical guidelines
  2. Running instance groups with auto scaling

Running instance groups with auto scaling

Written by
Yandex Cloud
  • Before you begin
  • Prepare the environment
  • Create an instance group with auto scaling and network load balancer
  • Add a network load balancer with a target group
  • Test your instance group and network load balancer
    • Test auto scaling
  • Delete the infrastructure

To create an instance group with auto scaling and network load balancer, follow these steps.

Before you begin

  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 a folder where you wish to create an 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
      

      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 Create method 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
      

      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-a zone:

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

      Result:

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

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

      Result:

      id: b1csa2b3clideftjb121
      folder_id: b0g12ga82bcv0cdeferg
      created_at: "2020-11-30T16:25:02Z"
      network_id: enpabce123hde4ft1r3t
      zone_id: ru-central1-b
      v4_cidr_blocks:
      - 192.168.2.0/24
      
    1. Create a network using the method Create for the Network resource.
    2. Create subnets in the ru-central1-a and ru-central1-b availability zones using the method Create method for the Subnets resource.

Create an instance group with auto scaling and network load balancer

  1. 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.

    Find out the ID of the latest version of the public Container Optimized Image.

    The Container Optimized Image in the Yandex Container Registry can be updated and changed according to releases. This will not automatically update the VM image to the latest version. To create an instance group with the latest Container Optimized Image version, you need to check whether it's available yourself:

    CLI
    Marketplace
    yc compute image get-latest-from-family container-optimized-image --folder-id standard-images
    

    Result:

    id: <ID of latest COI version>
    folder_id: standard-images
    ...
    
    1. Go to the page Cloud Marketplace and select the image with the desired configuration:
      • Container Optimized Image.
      • Container Optimized Image GPU.
    2. Under Product IDs, copy the value of image_id.
  2. Save the specification of the instance group with network load balancer to the file specification.yaml:

    name: auto-group
    service_account_id: <service account ID>
    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-a
        - zone_id: ru-central1-b
    load_balancer_spec:
      target_group_spec:
        name: auto-group-tg
    instance_template:
      service_account_id: <service account ID>
      platform_id: standard-v3
      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: fd8iv792kirahcnqnt0q # ID of the public Container Optimized Image.
      network_interface_specs:
        - network_id: <Cloud network ID>
          primary_v4_address_spec: { one_to_one_nat_spec: { ip_version: IPV4 }}
    
  3. In the specification.yaml file, replace the values in angle brackets for the real values you got in the previous steps.

  4. 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
    

    Result:

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

    Use the method CreateFromYaml for the resource InstanceGroup.

  5. 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
    

    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 network load balancer with a target group

  1. Create a network 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 Network Load Balancer.
    3. Click Create load balancer.
    4. Name it group-balancer.
    5. In the Public address field, select the value Auto.
    6. Click Add listener under Listeners.
      • In the window that opens, enter http as the Listener name.
      • 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 Health check settings window that opens, specify tcp in the Name field.
      • In the Type field, select 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
    

    Result:

    done (16s)
    id: b0rbabc1m2edfu2ss579
    folder_id: b0g12ga82bcv0cdeferg
    ...
        healthy_threshold: "2"
        tcp_options:
          port: "80"
    
    1. Create a load balancer using the method Create for the NetworkLoadBalancer resource.
    2. Add a listener to the balancer using the method addListener for the NetworkLoadBalancer resource.
    3. Attach the target group to the balancer using the method attachTargetGroup for the NetworkLoadBalancer resource.
    4. Add the balancer to the instance group using the method addTargets for the TargetGroup resource.
  2. Make sure that the network load balancer group-balancer is created and attached to the instance group:

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

    Result:

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

    Use the list method for the NetworkLoadBalancer resource.

Test your instance group and network 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
    

    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 sends requests to the instance group through 12 threads for 10 minutes. Each VM instance utilizes 20% CPU on each core that processes the request. 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
    

    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 network load balancer:

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

    Result:

    done (15s)
    

    Use the Delete method for the NetworkLoadBalancer resource.

  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 auto-group instance group.
    5. In the resulting menu, click Delete.
    6. In the Delete instance group window that opens, click Delete.
    yc compute instance-group delete auto-group
    

    Result:

    done (1m20s)
    

    Use the method Delete for the resource InstanceGroup.

  3. Delete the subnets:

    Management console
    CLI
    API
    1. In the management console, select the folder where you created the subnets.
    2. Open the section Virtual Private Cloud.
    3. Click the name of the subnets' network.
    4. Click in the line of the subnet to delete.
    5. In the resulting menu, click Delete.
    6. In the window that opens, click Delete.
    7. Repeat the previous three steps to delete the remaining subnets.
    1. Delete the subnet in the ru-central1-a zone:

      yc vpc subnet delete e1lnabc23r1c9d0efoje
      

      Result:

      done (1s)
      id: e1lnabc23r1c9d0efoje
      folder_id: b0g12ga82bcv0cdeferg
      created_at: "2020-11-30T16:23:12Z"
      network_id: enpabce123hde4ft1r3t
      zone_id: ru-central1-a
      v4_cidr_blocks:
      - 192.168.1.0/24         
      
    2. Delete the subnet in the ru-central1-b zone:

      yc vpc subnet delete b1csa2b3clideftjb121
      

      Result:

      done (1s)
      id: b1csa2b3clideftjb121
      folder_id: b0g12ga82bcv0cdeferg
      created_at: "2020-11-30T16:25:02Z"
      network_id: enpabce123hde4ft1r3t
      zone_id: ru-central1-b
      v4_cidr_blocks:
      - 192.168.2.0/24
      

    Use the method Delete for the resource Subnet.

  4. Delete the network:

    Management console
    CLI
    API
    1. In the management console, select the folder where you created the network.
    2. Open the section Virtual Private Cloud.
    3. Click in the line of the network to delete.
    4. In the resulting menu, click Delete.
    5. In the window that opens, click Delete.
    yc vpc network delete yc-auto-network
    

    Result:

    id: enpabce123hde4ft1r3t
    folder_id: b0g12ga82bcv0cdeferg
    created_at: "2020-11-30T14:57:15Z"
    name: yc-auto-network
    

    Use the method Delete for the resource Network.

  5. Delete the service account:

    Management console
    CLI
    API
    1. In the management console, select the folder the service account belongs to.
    2. Go to the Service accounts tab.
    3. Click next to the service account and select Delete service account.
    4. Confirm the deletion.
    yc iam service-account delete for-autoscale
    

    Result:

    done (2s)
    

    Use the method Delete for the resource ServiceAccount.

Was the article helpful?

Language / Region
© 2022 Yandex.Cloud LLC
In this article:
  • Before you begin
  • Prepare the environment
  • Create an instance group with auto scaling and network load balancer
  • Add a network load balancer with a target group
  • Test your instance group and network load balancer
  • Test auto scaling
  • Delete the infrastructure