Scheduled scaling of instance groups using the management console, CLI, and API
To set up scheduled scaling of instance groups, follow these steps:
- Prepare your cloud.
- Create an instance group.
- Create Cloud Functions resources.
- Test instance group scaling.
If you no longer need the resources you created, delete them.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
We will use a folder named example-folder
as an example.
Required paid resources
The infrastructure support costs include:
- Fee for disks and continuously running VMs (see Compute Cloud pricing).
- Function calls, computing resources allocated to executing the function, and outgoing traffic (see Cloud Functions pricing).
Create auxiliary resources
Create a service account
The service account will be linked to the Compute Cloud instance group and Cloud Functions function.
- In the management console
, select theexample-folder
folder. - At the top of the screen, go to the Service accounts tab.
- At the top right, click Create service account.
- In the Name field, specify
vm-scale-scheduled-sa
. - Click
Add role and select the following roles:compute.admin
: To manage the instance group.iam.serviceAccounts.user
: To link the service account to instances in the group.functions.functionInvoker
: To call the Cloud Functions function.
- Click Create.
-
Create a service account:
yc iam service-account create --name vm-scale-scheduled-sa \ --folder-name example-folder
Result:
id: ajeab0cnib1p******** folder_id: b0g12ga82bcv******** created_at: "2022-03-15T09:44:35.989446Z" name: vm-scale-scheduled-sa
For more information about the
yc iam service-account create
command, see the CLI reference. -
Assign the following roles to the service account:
-
compute.admin
: To manage the instance group:yc resource-manager folder add-access-binding example-folder \ --service-account-name vm-scale-scheduled-sa \ --role compute.admin \ --folder-name example-folder
-
iam.serviceAccounts.user
: To link the service account to instances in the group:yc resource-manager folder add-access-binding example-folder \ --service-account-name vm-scale-scheduled-sa \ --role iam.serviceAccounts.user \ --folder-name example-folder
-
functions.functionInvoker
: To call the Cloud Functions function:yc resource-manager folder add-access-binding example-folder \ --service-account-name vm-scale-scheduled-sa \ --role functions.functionInvoker \ --folder-name example-folder
For more information about the
yc resource-manager folder add-access-binding
command, see the CLI reference. -
-
Create a service account named
vm-scale-scheduled-sa
. To do this, use the create REST API method for the ServiceAccount resource or the ServiceAccountService/Create gRPC API call. -
Assign the service account the following roles for the current folder:
compute.admin
: To manage the instance group.iam.serviceAccounts.user
: To link the service account to instances in the group.functions.functionInvoker
: To call the Cloud Functions function.
To do this, use the setAccessBindings REST API method for the Folder resource or the FolderService/SetAccessBindings gRPC API call.
Create a cloud network
The instance group will be hosted in the Yandex Virtual Private Cloud](../../vpc/concepts/network.md#network) [cloud network and its subnets.
- In the management console
, select theexample-folder
folder. - In the list of services, select Virtual Private Cloud.
- At the top right, click Create network.
- In the Name field, specify
vm-scale-scheduled-network
. - Select Create subnets.
- Click Create network.
-
Create a network named
vm-scale-scheduled-network
:yc vpc network create --name vm-scale-scheduled-network \ --folder-name example-folder
Result:
id: enpabce123hd******** folder_id: b0g12ga82bcv******** created_at: "2022-03-15T09:55:50.004472Z" name: vm-scale-scheduled-network
For more information about the
yc vpc network create
command, see the CLI reference. -
Create a subnet named
vm-scale-scheduled-subnet-a
in theru-central1-a
availability zone:yc vpc subnet create --name vm-scale-scheduled-subnet-a \ --folder-name example-folder \ --network-name vm-scale-scheduled-network --range 192.168.1.0/24 \ --zone ru-central1-a
Result:
id: e1lnabc23r1c******** folder_id: b0g12ga82bcv******** created_at: "2022-03-15T09:56:51.859345Z" name: vm-scale-scheduled-subnet-a network_id: enpabce123hd******** zone_id: ru-central1-a v4_cidr_blocks: - 192.168.1.0/24
For more information about the
yc vpc subnet create
command, see the CLI reference. -
Create a subnet named
vm-scale-scheduled-network-b
in theru-central1-b
availability zone:yc vpc subnet create --name vm-scale-scheduled-subnet-b \ --folder-name example-folder \ --network-name vm-scale-scheduled-network --range 192.168.2.0/24 \ --zone ru-central1-b
Result:
id: b1csa2b3clid******** folder_id: b0g12ga82bcv******** created_at: "2022-03-15T09:57:48.934429Z" name: vm-scale-scheduled-subnet-b network_id: enpabce123hd******** zone_id: ru-central1-b v4_cidr_blocks: - 192.168.2.0/24
Create an instance group
An instance group will be created with manual scaling so that a Cloud Functions function can manage the group size.
- In the management console
, selectexample-folder
. - In the list of services, select Compute Cloud.
- In the left-hand panel, select
Instance groups. - At the top right, click Create group of virtual machines.
- Under Basic parameters:
- In the Name field, specify
vm-scale-scheduled-ig
. - Select Service account
vm-scale-scheduled-sa
.
- In the Name field, specify
- Under Allocation, select
ru-central1-a
andru-central1-b
in the Availability zone field. - Under Instance template, click Define and do the following in the window that opens:
- Under Boot disk image, go to the Boot disk image tab and select Ubuntu 20.04.
- Under Computing resources, specify the following configuration:
- Platform:
Intel Ice Lake
. - vCPU:
2
. - Guaranteed vCPU performance:
20%
. - RAM:
2 GB
.
- Platform:
- Under Network settings:
- In the Network field, select
vm-scale-scheduled-network
. - In the Public address field, select
No address
.
- In the Network field, select
- Under Access:
- In the Service account field, select
vm-scale-scheduled-sa
. - In the Login field, enter the name of the user to be created on the VM. You can come up with any username you like.
- In the SSH key field, paste the contents of the public SSH key. You can create a key pair by following this guide.
- In the Service account field, select
- Click Save.
- Under Allow when creating and updating, specify the following configuration:
- Add above target value:
2
. - Reduce below target value:
2
. - Create simultaneously:
2
. - Stop simultaneously:
2
.
- Add above target value:
- Under Scaling:
- In the Type field, select
Fixed
. - Specify Size
2
.
- In the Type field, select
- Click Create.
-
Get the IDs of the resources you created at the previous steps:
-
vm-scale-scheduled-sa
service account ID:yc iam service-account get vm-scale-scheduled-sa \ --folder-name example-folder | grep "^id"
Result:
id: ajeab0cnib1p********
For more information about the
yc iam service-account get
command, see the CLI reference. -
vm-scale-scheduled-network
network ID:yc vpc network get vm-scale-scheduled-network \ --folder-name example-folder | grep "^id"
Result:
id: enpabce123hd********
For more information about the
yc vpc network get
command, see the CLI reference. -
vm-scale-scheduled-subnet-a
subnet ID:yc vpc subnet get vm-scale-scheduled-subnet-a \ --folder-name example-folder | grep "^id"
Result:
id: e1lnabc23r1c********
For more information about the
yc vpc subnet get
command, see the CLI reference. -
vm-scale-scheduled-subnet-b
subnet ID:yc vpc subnet get vm-scale-scheduled-subnet-b \ --folder-name example-folder | grep "^id"
Result:
id: b1csa2b3clid********
For more information about the
yc vpc subnet get
command, see the CLI reference.
-
-
Get the ID of the latest version of the Ubuntu 20.04 LTS public image:
yc compute image get-latest-from-family ubuntu-2004-lts \ --folder-id standard-images | grep "^id"
Result:
id: fd83n3uou8m0********
For more information about the
yc compute image get-latest-from-family
command, see the CLI reference. -
Insert the obtained IDs in the
specification.yaml
file with the VM group specification:name: vm-scale-scheduled-ig service_account_id: <service_ account_ID> allocation_policy: zones: - zone_id: ru-central1-a - zone_id: ru-central1-b instance_template: platform_id: standard-v3 resources_spec: memory: 2G cores: 2 core_fraction: 20 boot_disk_spec: mode: READ_WRITE disk_spec: type_id: network-hdd size: 15G image_id: <ID_of_Ubuntu_20.04_LTS_latest_version> network_interface_specs: - network_id: <network_ID> subnet_ids: - <subnet_ID_in_ru-central1-a_zone> - <subnet_ID_in_ru-central1-b_zone> primary_v4_address_spec: {} scale_policy: fixed_scale: size: 2 deploy_policy: max_unavailable: 2 max_creating: 2 max_expansion: 2 max_deleting: 2
-
Create an instance group named
vm-scale-scheduled-ig
based onspecification.yaml
:yc compute instance-group create --file specification.yaml \ --folder-name example-folder
Result:
done (2m18s) id: cl0kabcquk1g******** folder_id: b0g12ga82bcv******** ... service_account_id: ajeab0cnib1p******** status: ACTIVE
-
Get the IDs of the resources you created at the previous steps:
vm-scale-scheduled-sa
service account ID: Using the get REST API method for the ServiceAccount resource or the ServiceAccountService/Get gRPC API call.vm-scale-scheduled-network
network ID: Using the get REST API method for the Network resource or the NetworkService/Get gRPC API call.vm-scale-scheduled-subnet-a
andvm-scale-scheduled-subnet-b
subnet IDs: Using the get REST API method for the Subnet resource or the SubnetService/Get gRPC API call.
-
Get the ID of the latest version of the public Ubuntu 20.04 image in the
standard-images
family using the getLatestByFamily REST API method for the Image resource or the ImageService/GetLatestByFamily gRPC API call. -
Insert the obtained IDs in the
specification.yaml
file with the VM group specification:name: vm-scale-scheduled-ig service_account_id: <service_ account_ID> allocation_policy: zones: - zone_id: ru-central1-a - zone_id: ru-central1-b instance_template: platform_id: standard-v3 resources_spec: memory: 2G cores: 2 core_fraction: 20 boot_disk_spec: mode: READ_WRITE disk_spec: type_id: network-hdd size: 15G image_id: <ID_of_Ubuntu_20.04_LTS_latest_version> network_interface_specs: - network_id: <network_ID> subnet_ids: - <subnet_ID_in_ru-central1-a_zone> - <subnet_ID_in_ru-central1-b_zone> primary_v4_address_spec: {} scale_policy: fixed_scale: size: 2 deploy_policy: max_unavailable: 2 max_creating: 2 max_expansion: 2 max_deleting: 2
-
Create an instance group named
vm-scale-scheduled-ig
based onspecification.yaml
. To do this, use the createFromYaml REST API method for the InstanceGroup resource or the InstanceGroupService/CreateFromYaml gRPC API call.
Create Cloud Functions resources
Create a function
The function will contain the code with the Yandex Cloud CLI commands that update the instance group.
-
In the management console
, selectexample-folder
. -
Select Cloud Functions.
-
At the top right, click Create function.
-
In the Name field, specify
vm-scale-scheduled-function
. -
Click Create.
-
Select the Bash runtime environment.
-
Enable Add files with code examples.
-
Click Continue.
-
Under Editor, select the
handler.sh
file. -
Replace the file contents with the code below:
# Get ID and current size of the instance group IG_SPEC=$(yc compute instance-group get --name $IG_NAME --folder-id $FOLDER_ID --format json) IG_ID=$(jq -r ".id" <<< $IG_SPEC) IG_SIZE=$(jq -r ".scale_policy.fixed_scale.size" <<< $IG_SPEC) # Calculate new size for the instance group if [ $IG_SIZE = $IG_BASE_SIZE ]; then IG_SIZE="$(($IG_BASE_SIZE + 1))" else IG_SIZE=$IG_BASE_SIZE fi # Update the instance group yc compute instance-group update --id $IG_ID --scale-policy-fixed-scale-size $IG_SIZE
-
Under Parameters:
- Set the Timeout, sec field value to
60
. - In the Service account field, select
vm-scale-scheduled-sa
. - Under Environment variables, add the following variables:
IG_NAME
=vm-scale-scheduled-ig
.IG_BASE_SIZE
=2
.FOLDER_ID
= Folder ID. You can get the ID by following this guide.
- Set the Timeout, sec field value to
-
In the top-right corner, click Save changes.
-
Create a file named
config.pkr.hcl
with the following code:# Get ID and current size of the instance group IG_SPEC=$(yc compute instance-group get --name $IG_NAME --folder-id $FOLDER_ID --format json) IG_ID=$(jq -r ".id" <<< $IG_SPEC) IG_SIZE=$(jq -r ".scale_policy.fixed_scale.size" <<< $IG_SPEC) # Calculate new size for the instance group if [ $IG_SIZE = $IG_BASE_SIZE ]; then IG_SIZE="$(($IG_BASE_SIZE + 1))" else IG_SIZE=$IG_BASE_SIZE fi # Update the instance group yc compute instance-group update --id $IG_ID --scale-policy-fixed-scale-size $IG_SIZE
-
Create a function:
yc serverless function create vm-scale-scheduled-function \ --folder-name example-folder
Result:
done (1s) id: d4e7d67ikvmq******** folder_id: b1g9hv2loamq******** ... log_group_id: ckgij6l0845h******** http_invoke_url: https://functions.yandexcloud.net/d4e7d67ikvmq******** status: ACTIVE
-
Create a function version:
yc serverless function version create --folder-name example-folder \ --function-name vm-scale-scheduled-function --runtime bash \ --source-path ./handler.sh --entrypoint "handler.sh" \ --execution-timeout 60s \ --service-account-id <service_account_ID> \ --environment \ IG_NAME=vm-scale-scheduled-ig,IG_BASE_SIZE=2,FOLDER_ID=<folder_ID>
Where:
--service-account-id
:vm-scale-schedule-sa
service account ID. To get the ID, follow the Create a VM group step on the CLI tab.FOLDER_ID
in--environment
: Folder ID. You can get the ID by following this guide.
Result:
done (1s) id: d4efmv9n3qf1******** function_id: d4e7d67ikvmq******** ... FOLDER_ID: b1g9hv2loamq******** IG_BASE_SIZE: "2" IG_NAME: vm-scale-scheduled-ig
-
Create a function named
vm-scale-scheduled-function
using the create REST API method for the Function resource or the FunctionService/Create gRPC API call. You can find the ID of the created function in the output. -
Create a function version using the createVersion REST API method for the Function resource or the FunctionService/CreateVersion gRPC API call. The function version should have the following code:
# Get ID and current size of the instance group IG_SPEC=$(yc compute instance-group get --name $IG_NAME --folder-id $FOLDER_ID --format json) IG_ID=$(jq -r ".id" <<< $IG_SPEC) IG_SIZE=$(jq -r ".scale_policy.fixed_scale.size" <<< $IG_SPEC) # Calculate new size for the instance group if [ $IG_SIZE = $IG_BASE_SIZE ]; then IG_SIZE="$(($IG_BASE_SIZE + 1))" else IG_SIZE=$IG_BASE_SIZE fi # Update the instance group yc compute instance-group update --id $IG_ID --scale-policy-fixed-scale-size $IG_SIZE
Create a trigger
A trigger sets conditions for running a function. For scheduled function calls, use a timer trigger.
- In the management console
, selectexample-folder
. - Select Cloud Functions.
- In the left-hand panel, select
Triggers. - At the top right, click Create trigger.
- Under Basic settings:
- In the Name field, specify
vm-scale-scheduled-trigger
. - In the Type field, select
Timer
. - In the Launched resource field, select
Function
.
- In the Name field, specify
- Under Timer settings, in the Cron expression field, specify
*/2 * * * ? *
. The trigger will fire every two minutes, e.g., at 09:58, 10:00, 10:02, 10:04, etc. - Under Function settings:
- In the Function field, specify
vm-scale-scheduled-function
. - In the Function version tag field, select
$latest
. - Select Service account
vm-scale-scheduled-sa
.
- In the Function field, specify
- Under Dead Letter Queue settings, reset the Service account (
Not selected
) field. - Click Create trigger.
Run the following command:
yc serverless trigger create timer --folder-name example-folder \
--name vm-scale-scheduled-trigger \
--cron-expression "*/2 * * * ? *" \
--invoke-function-name vm-scale-scheduled-function \
--invoke-function-tag '$latest' \
--invoke-function-service-account-name vm-scale-scheduled-sa
Result:
id: a1s22pb0v5i4********
folder_id: b1g9hv2loamq********
created_at: "2022-04-06T14:56:54.253709098Z"
...
function_tag: $latest
service_account_id: aje9s4pud13h********
status: ACTIVE
Use the create REST API method for the Trigger resource or the TriggerService/Create gRPC API call to create a trigger of the timer
type with the */2 * * * ? *
cron expression. This trigger will be linked with the vm-scale-scheduled-function
function of the $latest
version and the vm-scale-scheduled-sa
service account.
Test instance group scaling
- In the management console
, selectexample-folder
. - In the list of services, select Compute Cloud.
- In the left-hand panel, select
Instance groups. - Select the
vm-scale-scheduled-ig
group. - Under VM states, make sure the number of instances changes every two minutes: increases from 2 to 3, then decreases from 3 to 2, etc. You can also check if the instance group has been updated by opening the
Operations tab.
Run the following command several times:
yc compute instance-group get vm-scale-scheduled-ig \
--folder-name example-folder
Result:
id: cl1l0ljqbmkp********
folder_id: b1g9hv2loamq********
created_at: "2022-03-28T13:24:20.693Z"
...
managed_instances_state:
target_size: "2"
running_actual_count: "2"
...
The value of the target_size
field for the group should change from 2
to 3
and back.
Get information about the vm-scale-scheduled-ig
instance group multiple times using the get REST API method for the InstanceGroup resource or the InstanceGroupService/Get gRPC API call. The value of the target_size
field for the group should change from 2
to 3
and back.
How to delete the resources you created
To stop paying for the resources you created:
- Delete the trigger.
- Delete the function.
- Delete the instance group.