Moving a VM to a target group in a new availability zone
To move VMs behind a network load balancer 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 you need.
- Under Targets, remove the old VMs and add new ones.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
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.-
View a description of the CLI command for removing resources 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 the 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
: Resource parameters in 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:
-
View a 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 your 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
: Resource parameters in the target group:subnet-id
: ID of the subnet of the target to be added.address
: Internal IP address of the target to be added.
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 don't have Terraform, install it and configure the Yandex Cloud provider.
-
Open the target group description fragment in the Terraform configuration file and edit the
subnet_id
andaddress
field values in thetarget
section:Sample target group description
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 the update using the management console
or 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 have an internal network load balancer and need to move it to a subnet in the new availability zone for your VMs:
- Remove your load balancer's existing traffic listener.
- Add a new traffic listener for the new availability zone.
This will change the network load balancer's IP address.
-
Make sure that resources of the target group in the new availability zone have passed a health check and their current status is
HEALTHY
.If the new VMs remain in the
UNHEALTHY
status for a long time, check if they have security groups linked that allow health check traffic.