Running instance groups with auto scaling
To create an instance group with auto scaling and network load balancer, follow these steps.
Before you begin
-
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 a folder where you wish to create an 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
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 Create method 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
Result:
id: enpabce123hde4ft1r3t folder_id: b0g12ga82bcv0cdeferg created_at: "2020-11-30T14:57:15Z" name: yc-auto-network
-
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
-
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
Create an instance group with auto scaling and network load balancer
-
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:
CLIMarketplaceyc compute image get-latest-from-family container-optimized-image --folder-id standard-images
Result:
id: <ID of latest COI version> folder_id: standard-images ...
- Go to the page Cloud Marketplace and select the image with the desired configuration:
- Under Product IDs, copy the value of
image_id
.
-
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 }}
-
In the
specification.yaml
file, replace the values in angle brackets for the real values you got in the previous steps. -
Create an instance group named
auto-group
using the specificationspecification.yaml
:CLIAPIRun 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
. -
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
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
-
Create a network 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 Network Load Balancer.
- Click Create load balancer.
- Name it
group-balancer
. - In the Public address field, select the value Auto.
- 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.
- In the window that opens, enter
- Under Target groups, click Add target group.
- 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.
- In the Health check settings window that opens, specify
- 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"
- Create a load balancer using the method Create for the
NetworkLoadBalancer
resource. - Add a listener to the balancer using the method addListener for the
NetworkLoadBalancer
resource. - Attach the target group to the balancer using the method attachTargetGroup for the
NetworkLoadBalancer
resource. - Add the balancer to the instance group using the method addTargets for the
TargetGroup
resource.
-
Make sure that the network load balancer
group-balancer
is created and attached to the instance group:Management consoleCLIAPI- In the management console, select the folder where you created the load balancer.
- Select Network Load Balancer.
- 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
-
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
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 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"
-
Run the script:
CLIsh 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
-
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 network load balancer:
Management consoleCLIAPI- In the management console, select the folder where you created the
group-balancer
load balancer. - Select Network Load Balancer.
- Click
in the
group-balancer
row. - In the resulting menu, click Delete.
- 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. - In the management console, select the folder where you created the
-
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
auto-group
instance group. - In the resulting menu, click Delete.
- 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
. - In the management console, select the folder where you created the
-
Delete the subnets:
Management consoleCLIAPI- In the management console, select the folder where you created the subnets.
- Open the section Virtual Private Cloud.
- Click the name of the subnets' network.
- Click in the line of the subnet to delete.
- In the resulting menu, click Delete.
- In the window that opens, click Delete.
- Repeat the previous three steps to delete the remaining subnets.
-
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
-
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
. -
Delete the network:
Management consoleCLIAPI- In the management console, select the folder where you created the network.
- Open the section Virtual Private Cloud.
- Click in the line of the network to delete.
- In the resulting menu, click Delete.
- 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
. -
Delete the service account:
Management consoleCLIAPI- In the management console, select the folder the service account belongs to.
- Go to the Service accounts tab.
- Click next to the service account and select Delete service account.
- Confirm the deletion.
yc iam service-account delete for-autoscale
Result:
done (2s)
Use the method Delete for the resource
ServiceAccount
.