⬆️ ⬇️

Transfer of the KVM virtual machine installed on the LVM partition to another server using lvmsync

Greetings Habr!



In this small how-to, I would like to share with you my experience of using the utility lvmsync.



This utility allows you to solve the problem of transferring a virtual machine from one KVM server to another, with minimal downtime of the virtual machine, without using shared storage (non-shared storadge).

We will transfer the entire LVM partition on which the virtual machine is installed. Well, the magic of LVM snapshot, which you can easily find on the Internet, can help reduce downtime.

')

Here's what a virtual machine transfer looks like in brief:



  1. Take a snapshot of the LVM section.
  2. We transfer the main LVM partition over the network without stopping our VM.
  3. When the transfer of the main section is finished, we stop the VM.
  4. Run lvmsync to transfer the snapshot over the network. Not the whole picture is transferred, but only the changed blocks.
  5. Prepare and run the VM on the new server.


As a result, when using lvmsync, the idle time of the virtual machine will be equal to the transfer time of the changed blocks in the snapshot of the main section.



More information about the work of lvmsync, and additional buns you can read on the project page .







Further, it is assumed that we have sudo rights in the system, ssh access is configured by keys, and the input is denied under root.



Let's start VM migration:


Installation:


For lvmsync, we need Ruby 1.8 (or later), ssh, and dmsetup.

Download lvmsync to the local computer:



wget https://github.com/mpalmer/lvmsync.git 


Copy lvmsync to root PATH, for example in / usr / bin /



Preparing a remote server (server2):


1) Download and install lvmsync.

2) Create an LVM partition for the copied VM.



 server2# lvcreate vg -n new-virtual -L 16G 


The size of the partition must be equal to the original partition (in principle, it may be larger than the original, but this option has not been tested by me).



Prepare local server and migrate VM.


Further, all commands must be executed on the server from which we want to move the virtual machine (server1).



1) Create a definition xml:



 server1# virsh dumpxml virtual > virtual.xml 


2) Take a snapshot of the section:



 server1# lvcreate --snapshot -L10G -n virtual-snap /dev/vg/virtual 


Warning!
Please note that the size of the snapshot should be selected depending on the intensity of use of the VM. Since All data, while we transfer the main section, will be “saved” to the snapshot.

And when the image is full, it is automatically deactivated .



3) Without stopping the VM, transfer the primary partition using dd:



 server1# dd if=/dev/vg/virtual bs=1M | gzip -c | pv -ptrb | ssh me@server2 "gunzip -c | sudo dd of=/dev/vg/new-virtual" 


Here is added compression of the transmitted data in zip, and the display of the progress of data transmission using pv.



4) When the transfer is complete, stop the virtual machine:



 server1# virsh shutdown virtual 


5) And after a full stop of the machine, run lvmsync to transfer the snapshot:



 server1# lvmsync --stdout /dev/vg/virtual-snap | ssh me@server2 sudo lvmsync --apply - /dev/vg/new-virtual 


This operation will not only transfer the snapshot to a new server, but also merge it immediately with the main LVM partition.



6) Copy the definition xml to a remote server:



 server1# scp virtual.xml me@server2:/home/me/new-virtual.xml 




Preparing and running a virtual machine on a new server:





1) Change the definition xml if necessary.



2) Create a virtual machine based on xml:



 server2# virsh define new-virtual.xml 


3) We start our virtual machine, and register it in autostart:



 server2# virsh start new-virtual server2# virsh autostart new-virtual 




That's all, the virtual machine transfer is over!



Notes


The utility was tested on Centos 6.4. It’s difficult to say anything about the transfer time, because It all depends on the intensity of working with the virtual machine during its transfer, and, accordingly, the size of the snapshot.

Source: https://habr.com/ru/post/185240/



All Articles