Cold and hot migration of kvm virtual machine
Cold migration
principle
For static migration, you can save a complete client image snapshot on the host, then shut down or pause the client in the host, then copy the client image file to another host, and use the command when starting the client in the source host to start the copied image.
1. Shut down the virtual machine first
2. Backup and migrate xml files
virsh dumpxml test1.xml > /path
3. Create the same pool directory as the source host
4. Copy the disk to the target host pool directory
5. Copy the xml file to / etc/libvirt/qemu of the target host/
6. Redefine the virtual machine
virsh define test1.xml
Thermal migration
principle
If the source host and destination host share the storage system, you only need to send the vCPU execution status of the client, the content in the memory and the status of the virtual machine device to the destination host through the network. Otherwise, you need to send the disk storage of the client to the destination host. Shared storage system refers to that the image file directory of the source and destination virtual machines is on a shared storage.
Specific process of dynamic migration:
1. At the beginning of the migration, the client is still running on the host, while the client's memory pages are transferred to the destination host. 2. QEMU/KVM will monitor and record any modification of all transferred memory pages during the migration process, and start transmitting the changes of memory pages in the previous process after all memory pages are transferred. 3. QEMU/KVM will estimate the transmission speed during the migration process. When the remaining memory data can be transmitted within a settable time period (30 milliseconds by default), QEMU/KVM will shut down the client on the source host and then transmit the remaining data to the destination host, Finally, the transmitted memory content restores the running state of the client on the destination host. 4. At this point, the dynamic migration operation of KVM is completed. The migrated client should be consistent with that before migration as much as possible, unless some configurations, such as bridges, are missing on the destination host.
Specific implementation steps
Environmental preparation:
Source host 192.168.1.100
nfs server 192.168.1.101
Target host 192.168.1.102
Approximate process
The two virtual machine environments are the basic kvm environment. Build a bridge of the same network segment, resolve the host name, pair the secret key pairs with each other, close the firewall seliunx, mount the nfs directory at the same time, and close the firewall seliunx before migration.
1. Turn off the firewall and selinux uniformly, and set the host names to test1, NFS and test2 respectively
2. The ssh secret key pairs of the source host and the target host are paired with each other
ssh-key -t rsa ssh-copy-id root@192.168.1.102/100
3. Host name resolution
vi /etc/hosts 192.168.1.100 test1 192.168.1.102 test2 scp /etc/hosts root@192.168.1.102:/etc/hosts
4. Build nfs server
yum -y install rpcbind nfs-utils mkdir /nfs vi /etc/exports /nfs *(rw,sync,no_root_squash) systemctl start rpcbind systemctl start nfs
5. Create pool and disk for source host
Try whether you can mount it first virsh pool-define-as nfspool --type netfs --source-host 192.168.1.101 --source-path /nfs --target /mykvm/nfspool/ virsh pool-build nfspool virsh pool-start nfspool virsh pool-autostart nfspool virsh vol-create-as nfspool test01 5G --format qcow2
6. Create a virtual machine and build a service
virt-install -n test01 -r 512 --vcpus 1 -l /mykvm/iso/centos7.iso --disk /mykvm/nfspool/test01 --nographics -x 'console=ttyS0'
7. Build a bridge
cp ifcfg-ens33 ifcfg-virbr1 vi ifcfg-virbr1 TYPE=Bridge BOOTPROTO=static IPADDR=192.168.1.100 GATEWAY=192.168.1.254 DNS1=8.8.8.8 NAME=virbr5 DEVICE=virbr5 ONBOOT=yes vi ifcfg-ens33 TYPE=Ethernet NAME=ens33 DEVICE=ens33 ONBOOT=yes BRIDGE=virbr5 Restart the network card
8. Build a service. Take httpd service as an example
Configuring bridges for virtual machines virsh attach-interface test1 --type bridge --sourve virbr1 --current yum -y install httpd echo "hello" > /var/www/html/index.html systemctl start httpd
So far, the environment has been built, and then the thermal migration begins
9. Configure the bridge for the target host
scp /etc/sysconfig/network-scrips/ifcfg-virbr1 root@test2:/etc/sysconfig/network-scrips/ifcfg-virbr1 Change the address. It can't conflict. Change it again ifcfg-ens33
10. The target host creates the same pool path and mounts nfs
mkdir -p /mykvm/nfspool virsh pool-define-as nfspool --type netfs --source-host 192.168.1.101 --source-path /nfs --target /mykvm/nfspool/ virsh pool-build nfspool virsh pool-start nfspool virsh pool-autostart nfspool
11. Start migration
virsh migrate test1 qemu+ssh://192.168.1.102/system --live --unsafe --verbose
12. Before migration, you can always ping the virtual machine to see if it is interrupted