Running instance groups with auto scaling
To create an instance group with auto scaling and network load balancer, do the following.
Before you start
-
If you don't have the Yandex.Cloud command line interface yet, install it.
-
For the scripts from the tutorial to run correctly, download and install the jq utility.
-
To check auto scaling, install the wrk benchmarking tool.
Prepare the environment
-
Create a service account with the name
for-autoscale
and assign it theeditor
role:Management consoleCLIAPI- In the management console, select the folder where you want to create a service account.
- Go to the Service accounts tab.
- Click Create service account.
- Enter the name
for-autoscale
. - To assign the service account a role for the current folder, click Add role and select the role
editor
. - Click Create.
-
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
-
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. -
Create a network named
yc-auto-network
and subnets in two availability zones:Management consoleCLIAPI- In the management console, select the folder where you want to create a network.
- Select Virtual Private Cloud.
- Click Create network.
- Enter the network name
yc-auto-network
. - Select the additional option Create subnets.
- Click Create network.
-
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
-
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
-
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
Create an instance group with auto scaling and load balancer
-
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 }}
-
Create an instance group named
auto-group
using the specificationspecification.yaml
:CLIAPIRun 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
. -
Make sure that the instance group was created:
Management consoleCLIAPI- In the management console, select the folder where you created the instance group.
- Select Compute Cloud.
- Go to Instance groups.
- 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
-
Create a load balancer named
group-balancer
and connect it to the instance group you created:Management consoleCLIAPI- In the management console, select the folder where you want to create a load balancer.
- Select Load Balancer.
- Click Create load balancer.
- Enter Name
group-balancer
. - In the Public address field, select the value Auto.
- 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.
- In the window that opens, enter Listener name
- Under Target groups, click Add target group.
- 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.
- In the window that opens (Health check settings), enter the Name
- 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"
-
Create a network load balancer using the method Create for the resource
NetworkLoadBalancers
. -
Add a listener to the balancer using the method AddListener for the resource
NetworkLoadBalancers
. -
Attach the target group to the balancer using the method AttachTargetGroup for the resource
NetworkLoadBalancers
. -
Add the balancer to the instance group using the method AddTargets for the resource
TargetGroups
.
-
Make sure that the balancer
group-balancer
is created and linked to the instance group:Management consoleCLIAPI- In the management console, select the folder where you created the load balancer.
- Select Load Balancer.
- 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
-
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 thegroup-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"
-
Run the script:
CLIsh request.sh
Command execution result:
projects/b0g12ga82bcv0cdeferg/zones/ru-central1-b
-
View the instance utilization:
Management console- In the management console, select the folder where you created the instance group.
- Select Compute Cloud.
- Go to Instance groups.
- Click the
auto-group
instance group name. - 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.
-
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"
-
Run the script:
CLIsh 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
-
View the utilization levels:
Management console- In the management console, select the folder where you created the
auto-group
instance group. - Select Compute Cloud.
- Go to Instance groups.
- Click the
auto-group
instance group name. - 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. - 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.
- In the management console, select the folder where you created the
Delete the infrastructure
-
Delete the load balancer:
Management consoleCLIAPI- In the management console, select the folder where you created the load balancer
group-balancer
. - Select Load Balancer.
- Click
in the row of the load balancer
group-balancer
. - In the menu that opens, click Delete.
- 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
. - In the management console, select the folder where you created the load balancer
-
Delete the instance group:
Management consoleCLIAPI- In the management console, select the folder where you created the
auto-group
instance group. - Select Compute Cloud.
- Go to Instance groups.
- Click
for the instance group
auto-group
. - In the menu that opens, click Delete.
- 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 the management console, select the folder where you created the