Moving a VM to a different availability zone
When creating a VM, you can select a Yandex Cloud availability zone to place it in.
You can move an existing VM to a different availability zone using a special command in the management console
Warning
To monitor the migration progress and avoid potential issues, use disk snapshots or backups from Cloud Backup. This way, you can decide when to shut down your VM in the source availability zone and when to make it appear in the target availability zone. You can keep the VM in the source availability zone running until you make sure the VM you created from the snapshot in the new zone works correctly. You can read more about the relevant best practices in Migrating resources to a different availability zone.
Currently, you can use the relocate
command to migrate VMs and disks only to the ru-central1-d
zone from any other zone.
Warning
The ru-central1-d
zone does not support VMs based on the Intel Broadwell platform. To move such VMs to the ru-central1-d
zone, do one of the following:
- Take a disk snapshot and use it to create a new VM in the
ru-central1-d
zone on a different platform. - Stop the VM, change the platform, and move the VM by running
relocate
.
Moving a VM using disk snapshots
To move a VM to a different availability zone using snapshots, create its copy in the target availability zone and delete the source VM.
Create a snapshot of each of the VM disks
Prepare the disks
A disk snapshot only contains the data that was written to the disk at the time its snapshot was created. If the disk is attached to a running VM, the OS and app cache will not be included in the snapshot.
To ensure the snapshot data integrity:
-
Stop all disk write operations in applications.
-
Write the OS cache to the disk:
sync
-
Freeze the file system:
sudo fsfreeze --freeze <mount_point>
Where
--freeze
is the parameter to freeze the file system. Replace<mount_point>
with the folder where the file system is mounted, e.g.,/mnt/vdc2
. -
Create a snapshot by following the steps below.
-
Unfreeze the file system:
sudo fsfreeze --unfreeze <mount_point>
Where
--unfreeze
is the parameter to unfreeze the file system. Replace<mount_point>
with the folder where the file system is mounted, e.g.,/mnt/vdc2
.
- Stop the VM (see Stopping).
- Wait until the VM status changes to
STOPPED
.
Create snapshots
To create a disk snapshot:
-
In the management console
, select the folder containing the disk. -
Select Compute Cloud.
-
In the left-hand panel, select
Disks. -
In the line with the disk, click
and select Create snapshot. -
Enter a name for the snapshot. The naming requirements are as follows:
- It must be 2 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
-
Provide a description for the snapshot, if required.
-
Click Create.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI commands for creating snapshots:
yc compute snapshot create --help
-
Select the disk to create a snapshot of. To get a list of disks in the default folder, run this command:
yc compute disk list
Result:
+----------------------+--------------+-------------+-------------------+--------+----------------------+-----------------+-------------+ | ID | NAME | SIZE | ZONE | STATUS | INSTANCE IDS | PLACEMENT GROUP | DESCRIPTION | +----------------------+--------------+-------------+-------------------+--------+----------------------+-----------------+-------------+ | a7lqgbt0bb9s******** | first-disk | 20401094656 | ru-central1-a | READY | a7lcvu28njbh******** | | | | a7lv5j5hm1p1******** | second-disk | 21474836480 | ru-central1-a | READY | | | | +----------------------+--------------+-------------+-------------------+--------+----------------------+-----------------+-------------+
-
Create a snapshot in the default folder:
yc compute snapshot create \ --name first-snapshot \ --description "my first snapshot via CLI" \ --disk-id fhm4aq4hvq5g********
This will create a disk snapshot named
first-snapshot
and described asmy first snapshot via CLI
.The snapshot naming requirements are as follows:
- It must be 2 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
If you don't have Terraform, install it and configure the Yandex Cloud provider.
-
Define the parameters of the
yandex_compute_snapshot
resource in the configuration file.Here is an example of the configuration file structure:
resource "yandex_compute_snapshot" "snapshot-1" { name = "disk-snapshot" source_disk_id = "<disk_ID>" }
For more information about the resources you can create with Terraform, see the relevant provider documentation
. -
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is correct, the terminal will display a list of resources to create and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy the cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating the resources.
All the resources you need will then be created in the specified folder. You can check the new resources and their settings using the management console
. -
- Get a list of disks using the list REST API method for the Disk resource or the DiskService/List gRPC API call.
- Create a snapshot using the create REST API method for the Snapshot resource or the SnapshotService/Create gRPC API call.
Repeat the steps above to create snapshots of all the disks.
Create a VM in a different availability zone with the disks from the snapshots
To create a VM in a different availability zone with the disks from the snapshots:
-
In the management console
, select the folder where you want to create your VM. -
In the list of services, select Compute Cloud.
-
In the left-hand panel, select
Virtual machines. -
Click Create virtual machine.
-
Under Boot disk image:
- Go to the Custom tab.
- Click
, and, in the window that opens, select Create new. - In the Contents field, select
Snapshot
and then select the disk snapshot you need from the list below. Use the filter as needed. - Enter a name for the new boot disk.
- Select the disk type.
- Specify the required disk and block size.
- Optionally, enable Additional in the Delete along with the virtual machine field if you need this disk automatically deleted when deleting the VM.
- Click Add disk.
-
Under Location, select an availability zone for your VM.
-
Add a secondary disk:
- Under Disks and file storages, click Add.
- In the window that opens, select Disk →
Create new
. - In the Contents field, select
Snapshot
. - Enter a name for the new disk.
- Select the disk type.
- Specify the required disk and block size.
- Optionally, enable Additional in the Delete along with the virtual machine field if you need this disk automatically deleted when deleting the VM.
- Click Add disk.
-
Optionally, to encrypt a boot disk or a secondary disk, under Disks and file storages, click
to the right of the disk name and configure encryption parameters for the disk:- Select Encrypted disk.
- In the KMS key field, select the key you want to use to encrypt the disk. To create a new key, click Create new key.
Warning
You can specify encryption settings only when creating a disk. You cannot disable or change disk encryption.
If you deactivate the key used to encrypt a disk, image, or snapshot, access to the data will be suspended until you reactivate the key.
Alert
If you destroy the key or its version used to encrypt a disk, image, or snapshot, access to the data will be irrevocably lost. For details, see Destroying key versions.
-
Optionally, connect a file storage:
-
Under Disks and file storages, click Add.
-
In the window that opens, select File storage and choose the storage you want to connect from the list.
-
Click Add file storage.
-
-
-
Under Computing resources, select one of the preset configurations or create a custom one. To create a custom configuration:
- Go to the Custom tab.
- Select a platform.
- Specify the guaranteed performance and required number of vCPUs, as well as RAM size.
- Optionally, enable a software-accelerated network.
- Make your VM preemptible, if required.
-
Under Network settings:
-
In the Subnet field, enter the ID of a subnet in the new VM’s availability zone. Alternatively, you can select a cloud network from the list.
-
Each network must have at least one subnet. If your network has no subnets, create one by selecting Create subnet.
-
If there are no networks in the list, click Create network to create one:
- In the window that opens, specify the network name and select the folder where you want to create it.
- Optionally, select Create subnets to automatically create subnets in all availability zones.
- Click Create network.
-
-
In the Public IP address field, select a method for assigning an IP address:
Auto
: To assign a random IP address from the Yandex Cloud IP address pool. In this case, you can enable DDoS protection using the option below.List
: To select a public IP address from the list of previously reserved static addresses. For more information, see Converting a dynamic public IP address to static.No address
: Not to assign a public IP address.
-
Select the relevant security groups. If you leave this field empty, the default security group will be assigned to the VM.
-
Expand Additional and select a method for assigning internal addresses in the Internal IPv4 address field:
Auto
: To assign a random IP address from the pool of IP addresses available in the selected subnet.Manual
: To manually assign a private IP address to the VM.- Enable DDoS protection, if required. The option is available if you previously selected the automatic IP assignment method in the public address settings.
-
Optionally, create records for your VM in the DNS zone:
- Expand DNS settings for internal addresses and click Add record.
- Specify a zone, FQDN, and TTL for the record. When setting the FQDN, you can enable
Detect automatically
for the zone.
You can add multiple records to internal DNS zones. For more information, see Cloud DNS integration with Compute Cloud. - To create another record, click Add record.
If you want to add another network interface to your VM, click Add network interface and repeat the settings from this step for the new interface. You can add up to eight network interfaces to a single VM.
-
-
Under Access:
-
Select Access by OS Login to connect and manage access to the new VM using OS Login in Yandex Cloud Organization.
With OS Login, you can connect to VMs using SSH keys and SSH certificates via a standard SSH client or the Yandex Cloud CLI. OS Login enables rotating the SSH keys used to access VMs, providing the most secure access option.
-
If you prefer not to use OS Login, select SSH key and specify the following VM access data:
-
Under Login, enter a username.
Alert
Do not use
root
or other usernames reserved by the OS. To perform operations requiring superuser privileges, use thesudo
command. -
In the SSH key field, select the SSH key saved in your organization user profile.
If there are no saved SSH keys in your profile, or you want to add a new key:
- Click Add key.
- Enter a name for the SSH key.
- Upload or paste the contents of the public key file. You need to create a key pair for the SSH connection to a VM yourself.
- Click Add.
The SSH key will be added to your organization user profile.
If users cannot add SSH keys to their profiles in the organization, the added public SSH key will only be saved to the user profile of the VM being created.
-
If you want to add multiple users with SSH keys to the VM at the same time, specify these users' data under Metadata. You can also use metadata to install additional software on a VM when creating it.
In public Linux images provided by Yandex Cloud, the functionality of connecting over SSH using login and password is disabled by default.
-
-
Under General information, enter a name for the VM:
- It must be 2 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
Note
The VM name is used to generate an internal FQDN, which is set only once, when you create the VM. If the internal FQDN is important to you, make sure to choose an appropriate name for your VM.
-
Under Additional:
-
Optionally, select or create a service account. With a service account, you can flexibly configure access permissions for your resources.
-
Optionally, enable access to the serial console.
-
Optionally, under Backup, enable Connect and select or create a backup policy to make automatic backups of your VMs using Cloud Backup.
For more information, see Connecting Compute Cloud VMs and Yandex BareMetal servers to Cloud Backup.
-
Optionally, under Monitoring, enable Agent for delivering metrics to configure delivering metrics to Yandex Monitoring.
-
Optionally, under Placement, select a VM placement group.
-
-
Click Create VM.
The VM will appear in the list. Every new VM gets an IP address and host name (FQDN).
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command for creating a VM:
yc compute instance create --help
-
Prepare snapshots of the disks you need by following the steps in Creating a disk snapshot.
-
Get a list of snapshots in the default folder:
yc compute snapshot list
Result:
+----------------------+-----------------+----------------------+--------+----------------------------+ | ID | NAME | PRODUCT IDS | STATUS | DESCRIPTION | +----------------------+-----------------+----------------------+--------+----------------------------+ | fd8rlt1u2rf0l******* | first-snapshot | f2ecl5vhsftd******** | READY | my first snapshot via CLI | | fhmolt1u2rf0******** | second-snapshot | f2eclmol5lps******** | READY | my second snapshot via CLI | +----------------------+-----------------+----------------------+--------+----------------------------+
-
Select
ID
orNAME
of the snapshots you need. -
Create a VM in the default folder:
yc compute instance create \ --name first-instance \ --zone ru-central1-a \ --public-ip \ --create-boot-disk snapshot-name=first-snapshot \ --create-disk snapshot-name=second-snapshot \ --ssh-key ~/.ssh/id_ed25519.pub
This command will create a VM named
first-instance
in theru-central1-a
availability zone, with a public IP address and disks from the snapshots.Note
The VM name is used to generate an internal FQDN, which is set only once, when you create the VM. If the internal FQDN is important to you, make sure to choose an appropriate name for your VM.
To create a VM without a public IP address, remove the
--public-ip
flag.If you want to add multiple network interfaces to your VM, specify the
--network-interface
parameter as many times as you need. You can add up to eight network interfaces to a single VM.
Use the create REST API method for the Instance resource or the InstanceService/Create gRPC API call.
Delete the source VM
To delete a source VM:
-
In the management console
, select the folder the VM belongs to. -
Select Compute Cloud.
-
In the line with the VM, click
and select Delete.To delete multiple VMs, select them from the list and click Delete at the bottom of the screen.
-
In the window that opens, click Delete.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command for deleting a VM:
yc compute instance delete --help
-
Get a list of VMs in the default folder:
yc compute instance list
Result:
+----------------------+-----------------+---------------+---------+----------------------+ | ID | NAME | ZONE ID | STATUS | DESCRIPTION | +----------------------+-----------------+---------------+---------+----------------------+ | fhm0b28lgfp4******** | first-instance | ru-central1-a | RUNNING | my first vm via CLI | | fhm9gk85nj7g******** | second-instance | ru-central1-a | RUNNING | my second vm via CLI | +----------------------+-----------------+---------------+---------+----------------------+
-
Select
ID
orNAME
of the VM, e.g.,first-instance
. -
Delete the VM:
yc compute instance delete first-instance
If you don't have Terraform, install it and configure the Yandex Cloud provider.
To delete a VM created using Terraform, follow these steps:
-
In the command line, go to the directory with the Terraform configuration file.
-
Delete the resources using this command:
terraform destroy
Alert
Terraform will delete all the resources you created in the current configuration, such as clusters, networks, subnets, and VMs.
-
Type
yes
and press Enter.
Use the delete REST API method for the Instance resource or the InstanceService/Delete gRPC API call.
Moving a VM using a special command
When you move a VM to a different availability zone using the management console or the CLI, its metadata and ID will remain unchanged. All disks attached to the VM will also be transferred to the new availability zone.
Note
The time it takes to move a VM to a different availability zone depends on the size of its disks. As an example, a 100 GB disk typically migrates within 10 minutes.
In some cases, the migration may take longer if you are moving it to the ru-central1-d
availability zone.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command for moving a VM to a different availability zone:
yc compute instance relocate --help
-
Get a list of all subnets in the default folder:
yc vpc subnet list
Result:
+----------------------+-----------------------+----------------------+----------------+---------------+-------------------+ | ID | NAME | NETWORK ID | ROUTE TABLE ID | ZONE | RANGE | +----------------------+-----------------------+----------------------+----------------+---------------+-------------------+ | bucqps2lt75g******** | subnet-ru-central1-a1 | c64pv6m0aqq6******** | | ru-central1-a | [192.168.11.0/24] | | e2lrucutusnd******** | subnet-ru-central1-a2 | c64pv6m0aqq6******** | | ru-central1-a | [192.168.12.0/24] | | e2lv9c6aek1d******** | subnet-ru-central1-a3 | c64pv6m0aqq6******** | | ru-central1-a | [192.168.13.0/24] | | bltign9kcffv******** | default-ru-central1-b | c64pv6m0aqq6******** | | ru-central1-b | [192.168.1.0/24] | +----------------------+-----------------------+----------------------+----------------+---------------+-------------------+
-
Get a list of all security groups in the default folder:
yc vpc sg list
Result:
+----------------------+---------------------------------+--------------------------------+----------------------+ | ID | NAME | DESCRIPTION | NETWORK-ID | +----------------------+---------------------------------+--------------------------------+----------------------+ | c646ev94tb6k******** | my-sg-group | | c64pv6m0aqq6******** | | c64r84tbt32j******** | default-sg-c64pv6m0aqq6******** | Default security group for | c64pv6m0aqq6******** | | | | network | | +----------------------+---------------------------------+--------------------------------+----------------------+
-
Get a list of all VMs in the default folder:
yc compute instance list
Result:
+----------------------+---------+---------------+---------+---------------+-------------+ | ID | NAME | ZONE ID | STATUS | EXTERNAL IP | INTERNAL IP | +----------------------+---------+---------------+---------+---------------+-------------+ | a7lh48f5jvlk******** | my-vm-1 | ru-central1-a | RUNNING | | 192.168.0.7 | | epdsj30mndgj******** | my-vm-2 | ru-central1-b | RUNNING | | 192.168.1.7 | +----------------------+---------+---------------+---------+---------------+-------------+
-
Get a list of network interfaces of the VM in question by specifying the VM ID:
yc compute instance get <VM_ID>
Result:
... network_interfaces: - index: "0" mac_address: d0:0d:24:**:**:** subnet_id: bucqps2lt75g******** primary_v4_address: address: 192.168.11.23 one_to_one_nat: address: 158.160.**.*** ip_version: IPV4 - index: "1" mac_address: d0:1d:24:**:**:** subnet_id: e2lrucutusnd******** primary_v4_address: address: 192.168.12.32 - index: "2" mac_address: d0:2d:24:**:**:** subnet_id: e2lv9c6aek1d******** primary_v4_address: address: 192.168.13.26 ...
-
Move the VM to a different availability zone:
yc compute instance relocate <VM_ID> \ --destination-zone-id <availability_zone_ID> \ --network-interface subnet-id=<subnet_ID>,security-group-ids=<security_group_ID>
Where:
-
<VM_ID>
: ID of the VM to move to a different availability zone. -
--destination-zone-id
: ID of the availability zone to move the VM to. -
--network-interface
: VM network interface settings:subnet-id
: ID of the subnet in the availability zone to move the VM to.security-group-ids
: ID of the security group to link to the VM you are moving. You can link multiple security groups to a single VM by providing a comma-separated list of security group IDs in[id1,id2]
format.
If a VM has multiple network interfaces, specify the
--network-interface
parameter as many times as needed (for each network interface).
For more information about the
yc compute instance relocate
command, see the CLI reference.Example:
yc compute instance relocate a7lh48f5jvlk******** \ --destination-zone-id ru-central1-b \ --network-interface \ subnet-id=bltign9kcffv********,security-group-ids=c646ev94tb6k********
In this example, we are moving a VM named
my-vm-1
from theru-central1-a
availability zone toru-central1-b
.Result:
done (3m15s) id: a7lh48f5jvlk******** folder_id: aoeg2e07onia******** created_at: "2023-10-13T19:47:40Z" name: my-vm-1 zone_id: ru-central1-b platform_id: standard-v3 resources: memory: "2147483648" cores: "2" core_fraction: "100" status: RUNNING metadata_options: gce_http_endpoint: ENABLED aws_v1_http_endpoint: ENABLED gce_http_token: ENABLED aws_v1_http_token: DISABLED boot_disk: mode: READ_WRITE device_name: a7lp7jpslu59******** auto_delete: true disk_id: a7lp7jpslu59******** network_interfaces: - index: "0" mac_address: d0:0d:11:**:**:** subnet_id: bltign9kcffv******** primary_v4_address: address: 192.168.1.17 security_group_ids: - c646ev94tb6k******** gpu_settings: {} fqdn: my-vm-1.ru-central1.internal scheduling_policy: {} network_settings: type: STANDARD placement_policy: {}
If you are moving a VM with a disk in a placement group, use this command:
yc compute instance relocate <VM_ID> \ --destination-zone-id <availability_zone_ID> \ --network-interface subnet-id=<subnet_ID>,security-group-ids=<security_group_ID> \ --boot-disk-placement-group-id <disk_placement_group_ID> \ --boot-disk-placement-group-partition <partition_number> \ --secondary-disk-placement disk-name=<disk_name>,disk-placement-group-id=<disk_placement_group_ID>,disk-placement-group-partition=<partition_number>
Where:
-
--boot-disk-placement-group-id
: Disk placement group ID. -
--boot-disk-placement-group-partition
: Partition number in the disk placement group with the partition placement strategy. -
--secondary-disk-placement
: Placement policy for secondary disks. Parameters:disk-name
: Disk name.disk-placement-group-id
: ID of the disk placement group to place the disk in.disk-placement-group-partition
: Partition number in the disk placement group.
For more information about the
yc compute instance relocate
command, see the CLI reference. -
Please note that connecting VM network interfaces to new subnets changes their IP addressing. If you need to specify internal IP addresses for the VM network interfaces, use the ipv4-address=<internal_IP_address>
property of the network-interface
parameter; for public IP addresses, use the nat-address=<public_IP_address>
property. Other than that, setting up network interface parameters when moving a VM to a different availability zone is similar to setting up the same parameters when creating a VM.
Note
Active writes to the VM disks being moved may cause the migration to fail. In this case, stop writing to the disks or shut down the VM and restart the migration.
Examples
Moving a VM to a different zone
In this example, we are moving a VM named my-vm-1
from the ru-central1-a
availability zone to ru-central1-d
.
yc compute instance relocate a7lh48f5jvlk******** \
--destination-zone-id ru-central1-d \
--network-interface \
subnet-id=bltign9kcffv********,security-group-ids=c646ev94tb6k********
Result:
done (3m15s)
id: a7lh48f5jvlk********
folder_id: aoeg2e07onia********
created_at: "2023-10-13T19:47:40Z"
name: my-vm-1
zone_id: ru-central1-d
platform_id: standard-v3
resources:
memory: "2147483648"
cores: "2"
core_fraction: "100"
status: RUNNING
metadata_options:
gce_http_endpoint: ENABLED
aws_v1_http_endpoint: ENABLED
gce_http_token: ENABLED
aws_v1_http_token: DISABLED
boot_disk:
mode: READ_WRITE
device_name: a7lp7jpslu59********
auto_delete: true
disk_id: a7lp7jpslu59********
network_interfaces:
- index: "0"
mac_address: d0:0d:11:**:**:**
subnet_id: bltign9kcffv********
primary_v4_address:
address: 192.168.1.17
security_group_ids:
- c646ev94tb6k********
gpu_settings: {}
fqdn: my-vm-1.ru-central1.internal
scheduling_policy: {}
network_settings:
type: STANDARD
placement_policy: {}
Moving a VM with disks in a placement group
In this example, we are moving a VM named my-vm-1
with two disks in the placement group from the ru-central1-b
availability zone to ru-central1-d
.
yc compute instance relocate epd6qtn128k1******** \
--destination-zone-id ru-central1-d \
--network-interface \
subnet-id=fl8glc5v0lqj********,security-group-ids=enp1gjh3q042******** \
--boot-disk-placement-group-id fv4pfmor782v******** \
--boot-disk-placement-group-partition 1 \
--secondary-disk-placement \
disk-name=disk-two,fv4pfmor782v********,disk-placement-group-partition=2
Result:
done (9m0s)
id: epd6qtn128k1********
folder_id: b1gmit33ngp3********
created_at: "2023-12-07T19:30:20Z"
name: my-vm-1
zone_id: ru-central1-d
platform_id: standard-v3
resources:
memory: "2147483648"
cores: "2"
core_fraction: "100"
status: RUNNING
metadata_options:
gce_http_endpoint: ENABLED
aws_v1_http_endpoint: ENABLED
gce_http_token: ENABLED
aws_v1_http_token: DISABLED
boot_disk:
mode: READ_WRITE
device_name: epdeqrm6g38j********
auto_delete: true
disk_id: epdeqrm6g38j********
secondary_disks:
- mode: READ_WRITE
device_name: epdi54snn7t6********
disk_id: epdi54snn7t6********
network_interfaces:
- index: "0"
mac_address: d0:0d:6d:76:e1:12
subnet_id: fl8glc5v0lqj********
primary_v4_address:
address: 10.130.0.12
security_group_ids:
- enp1gjh3q042********
gpu_settings: {}
fqdn: my-vm-1.ru-central1.internal
scheduling_policy: {}
network_settings:
type: STANDARD
placement_policy: {}