Moving a VM to a target group in a new availability zone
To move NLB backend VMs to a target group in a different availability zone:
-
Move your VMs to the new availability zone.
-
Replace targets in the target group:
Management consoleCLITerraformAPI- In the management console
, select the folder containing the network load balancer. - In the list of services, select Network Load Balancer.
- In the left-hand panel, select
Target groups. - Select the target group in question.
- Under Targets, remove the old VMs and add new ones.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified when creating the CLI profile is used by default. To change the default folder, use the
yc config set folder-id <folder_ID>
command. You can specify a different folder using the--folder-name
or--folder-id
parameter.-
See the description of the CLI command for removing targets from a target group:
yc load-balancer target-group remove-targets --help
-
Get a list of Network Load Balancer target groups in the default folder.
-
Get a list of targets linked to the target group.
-
Remove old VMs from the target group:
yc load-balancer target-group remove-targets <target_group_ID> \ --target subnet-id=<VM_1_subnet_ID>,address=<VM_1_internal_IP_address> \ --target subnet-id=<VM_2_subnet_ID>,address=<VM_2_internal_IP_address> \ ... --target subnet-id=<VM_n_subnet_ID>,address=<VM_n_internal_IP_address>
Where:
<target_group_ID>
: ID of the target group to remove targets from.--target
: Settings of the target within the target group:subnet-id
: Subnet ID of the target to remove.address
: Internal IP address of the target to remove.
Result:
done (4s) id: enplckhtn77s******** folder_id: b1gt6g8ht345******** created_at: "2023-10-16T10:42:18Z" name: sample-tg region_id: ru-central1 targets:
-
See the description of the CLI command for adding targets to a target group:
yc load-balancer target-group add-targets --help
-
Get a list of subnets in the default folder:
yc vpc subnet list
Result:
+----------------------+------------+----------------------+----------------+---------------+------------------+ | ID | NAME | NETWORK ID | ROUTE TABLE ID | ZONE | RANGE | +----------------------+------------+----------------------+----------------+---------------+------------------+ | b0cnd1srghnm******** | rucentrald | enpv51f8lple******** | | ru-central1-d | [192.168.3.0/24] | | e2li9tcgi7ii******** | rucentralb | enpv51f8lple******** | | ru-central1-b | [192.168.0.0/24] | | e9b4a9ksc88k******** | rucentrala | enpv51f8lple******** | | ru-central1-a | [192.168.1.0/24] | +----------------------+------------+----------------------+----------------+---------------+------------------+
-
Get a list of VMs in the default folder:
yc compute instance list
Result:
+----------------------+------------+-------------------+---------+-----------------+--------------+ | ID | NAME | ZONE ID | STATUS | EXTERNAL IP | INTERNAL IP | +----------------------+------------+-------------------+---------+-----------------+--------------+ | fhmkchjddi40******** | sample-vm1 | ru-central1-a | RUNNING | 130.193.**.*** | 192.168.1.9 | | fhm13ts43oml******** | sample-vm2 | ru-central1-a | RUNNING | 158.160.***.*** | 192.168.1.32 | | epdsj30mndgj******** | sample-vm3 | ru-central1-b | RUNNING | 51.250.***.*** | 192.168.0.7 | +----------------------+------------+-------------------+---------+-----------------+--------------+
-
Add targets to the target group:
yc load-balancer target-group add-targets <target_group_ID> \ --target subnet-id=<VM_1_subnet_ID>,address=<VM_1_internal_IP_address> \ --target subnet-id=<VM_2_subnet_ID>,address=<VM_2_internal_IP_address> ... --target subnet-id=<VM_n_subnet_ID>,address=<VM_n_internal_IP_address>
Where:
<target_group_ID>
: ID of the target group to add targets to.--target
: Settings of the target within the target group:subnet-id
: Subnet ID of the target to add.address
: Internal IP address of the target to add.
Result:
done (4s) id: enplckhtn77s******** folder_id: b1gt6g8ht345******** ... address: 192.168.1.32 - subnet_id: e9b4a9ksc88k******** address: 192.168.1.9
With Terraform
, you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.Terraform is distributed under the Business Source License
. The Yandex Cloud provider for Terraform is distributed under the MPL-2.0 license.For more information about the provider resources, see the documentation on the Terraform
website or mirror website.If you do not have Terraform yet, install it and configure its Yandex Cloud provider.
-
Open the Terraform configuration file, find the section describing the target group, and edit the
subnet_id
andaddress
values undertarget
:Target group description example
resource "yandex_lb_target_group" "foo" { name = "<target_group_name>" target { subnet_id = "<subnet_ID>" address = "<resource_1_internal_IP_address>" } target { subnet_id = "<subnet_ID>" address = "<resource_2_internal_IP_address>" } }
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
You can check updates in the management console
or using this CLI command:yc load-balancer target-group get <target_group_name>
- To remove targets from a target group, use the removeTargets REST API method for the TargetGroup resource or the TargetGroupService/RemoveTargets gRPC API call.
- To add targets to a target group, use the addTargets REST API method for the TargetGroup resource or the TargetGroupService/AddTargets gRPC API call.
- In the management console
-
If you are using an internal network load balancer and need to move it to a subnet in the new availability zone that will host your VMs:
- Delete the current listener of your load balancer.
- Add a new listener for the new availability zone.
This will change the network load balancer IP address.
-
Make sure the target group VMs in the new availability zone have passed health checks and got the
HEALTHY
status.If the new VMs remain
UNHEALTHY
for too long, make sure their security groups are configured to allow health check traffic.