kvm hot and cold state transition

KVM migration

Static Migration (cold migration)
to migrate static, you can save the client a mirror snapshot done on the host, and then close or suspend the client in the host machine, and then copy the image file to another client places host, use the command to start the client source host to start the copied image.

Dynamic migration (live migration)
If the host and the destination host shared memory system, only needs to send vCPU execution state of the client via the network, the contents of memory, the state of the virtual machine host to the destination device. Shared memory image file system refers to the source and destination directory is on a virtual machine a shared storage.

When you share a storage system based on the specific process for the KVM live migration:

  • 1, when the migration starts, the client is still running on the host, at the same time, the client's memory page is transmitted to the destination host.
  • 2, QEMU / KVM monitors and records any changes have all been transferred memory page, under the migration process, and begin transmitting in the previous procedure to change the contents of memory pages in memory after all pages are transferred.
  • 3, QEMU / KVM estimates the transmission rate of the migration process, when the remaining amount of memory data can be set in a time period (default 30 ms) the transfer is complete, QEMU / KVM client closes on the source host machine, and then the remaining amount of data transmitted to the destination host, and finally restore the memory contents transmitted by the operating state of the client on the destination host.
  • 4. At this point, KVM live migration operation is complete. Migrated as far as possible consistent with the client prior to migration, missing some configuration unless the destination host, such as bridges and so on.

    Note that when the client memory usage is very large and is modified often, in-memory data constantly being modified faster than memory speed KVM can be transmitted when the live migration process is not complete, and this time only static migration.

Migration considerations:
1, the best brands, like cpu server migration
, 64-bit only between 64 host migration, you can migrate 32-bit 32-bit and 64-bit host
3, the virtual machine host name can not be in conflict
4, the same purposes as the configuration and the source host host software, if the same bridge card, resource pool.
5, two migrated host cat / proc / cpuinfo | grep nx settings are the same.

A static migration

Environment Description:

CPU name IP addresses operating system
kvm01 172.16.1.30 centos7
kvm02 172.16.1.40 centos7

1. Preparation
1) install kvm on kvm01 and kvm02 host

Environmental requirements:
1, if a physical server, you need to turn on virtualization capabilities (Virtualization Technology) in the BIOS, the general server is enabled by default this feature.
2. If you are using VMware Workstation to experiment, to establish the virtual machine to check the CPU virtualization capabilities, this virtual machine will support the KVM virtualization

# Verify cpu virtualization:
kvm hot and cold state transition

#安装虚拟化软件:
[root@kvm01 ~]# yum -y install qemu-kvm qemu-kvm-tools qemu-img libvirt virt-install virt-manager  bridge-utils

Each kit role:

qemu-kvm  //KVM核心模块
qemu-kvm-tools //KVM调试工具,可选择性安装
qemu-img  //创建,转换和修改镜像,能处理被qemu支持的所有镜像格式
libvirt //管理虚拟机的工具包
virt-install  //libvirt库构建新虚拟机的命令行工具
virt-manager //图形化界面管理虚拟机
 bridge-utils //配置linux以太网桥

# If the installation is minimized, you also need to install the GNOME desktop environment yum -y groupinstall "GNOME Desktop."

 #启动libvirtd工具:
 [root@kvm01 ~]# systemctl start libvirtd
[root@kvm01 ~]# systemctl status libvirtd

kvm hot and cold state transition
kvm02 host operating with the same upper side.

2) Create a virtual machine (it is created on kvm01 host)

#创建存放磁盘文件和centos镜像的目录:
[root@kvm01 ~]# mkdir /kvm-vm
[root@kvm01 ~]# mkdir /iso
[root@kvm01 ~]# ls /iso/
CentOS-7-x86_64-DVD-1708.iso    #上传centos镜像

#kvm command-line installation:

[root@kvm01 ~]# virt-install --os-type=linux --os-variant centos7.0 --name web01 --ram 1024 --vcpus 1 --disk /kvm-vm/web01.qcow2,format=qcow2,size=10 --location /iso/CentOS-7-x86_64-DVD-1708.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

Starting install...
Retrieving file .treeinfo...                                                       |  354 B  00:00:00     
Retrieving file vmlinuz...                                                         | 5.6 MB  00:00:00     
Retrieving file initrd.img...                                                      |  46 MB  00:00:00     
Allocating 'web01.qcow2'                                                           |  10 GB  00:00:00     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

Parameters explanation:

通过virt-install命令创建新虚拟机并进行安装
 --os-type= linux  #系统类型为linux
 --os-variant=  #指定操作系统版本   
 --name  #指定虚拟机名称
 --ram   #以MB为单位为客户端事件分配的内存
 --vcpus  #配置虚拟机的虚拟CPU(vcpu)数量
 --disk   #指定虚拟机的存储位置,磁盘文件的格式及大小(单位GB)
 --location  #选择安装源
 --network   #配置客户网络接口
 --graphics #配置虚拟机显示设置(vnc)
 listen=0.0.0.0  #表示所有网段都可以连接该虚拟机
 --noautoconsole #不管是否部署成功,执行完都会退出,如果不加的话,将会一直卡在终端

After successful installation, at the command line open kvm virt-manager graphical management interface, as follows:
kvm hot and cold state transition
Click centos into the installation system. After the installation is complete, view the status of a virtual machine:

[root@kvm01 ~]# virsh list --all
 Id    Name                           State
---------------------------------------------------- 
 -     web01                          shut off     #安装完成后,默认保持关闭状态

Kvm virtual machine is configured bridged networks (so that it can access the Internet):

#关闭宿主机的网络控制台:
[root@kvm01 ~]# systemctl stop NetworkManager
#在宿主机上创建br0(桥接网卡):
[root@kvm01 ~]# virsh iface-bridge ens33 br0
Created bridge br0 with attached device ens33
Bridge interface br0 started
#修改实例的配置文件:
[root@kvm01 ~]# virsh edit web01 
     69     <interface type='bridge'>    #修改网卡类型为桥接
     70       <mac address='52:54:00:dc:0f:d0'/>
     71       <source bridge='br0'/>   #指定桥接网卡

#启动实例:
[root@kvm01 ~]# virsh start web01 
Domain web01 started

[root@kvm01 ~]# virsh list 
 Id    Name                           State
----------------------------------------------------
 2     web01                          running

Host Configuration web01 network configuration information, and test ping the external network:
kvm hot and cold state transition

2, static migration
static migration is divided into two steps:
a) copying the image file and the virtual machine configuration file
2) redefine the virtual machine

1)关闭或挂起虚拟机:
[root@kvm01 ~]# virsh shutdown web01 
Domain web01 is being shutdown
[root@kvm01 ~]# virsh list 
 Id    Name                           State
----------------------------------------------------

2) copies of image files and configuration files to kvm02 host:
# ready before the first copy of kvm02 environment on the host, would be to go to save modified parameters (trouble) configuration file corresponding.

[root@kvm02 ~]# mkdir /kvm-vm   //创建对应的镜像文件目录
[root@kvm02 ~]# systemctl stop NetworkManager  
[root@kvm02 ~]# virsh iface-bridge ens33 br0   //创建桥接网卡
Created bridge br0 with attached device ens33
Bridge interface br0 started
#拷贝:
[root@kvm01 ~]# scp /kvm-vm/web01.qcow2 172.16.1.40:/kvm-vm/
[root@kvm01 ~]# scp /etc/libvirt/qemu/web01.xml  172.16.1.40:/etc/libvirt/qemu/

3) the definition of the virtual machine on the host kvm02:

[root@kvm02 ~]# virsh define /etc/libvirt/qemu/web01.xml 
Domain web01 defined from /etc/libvirt/qemu/web01.xml

#启动虚拟机:
[root@kvm02 ~]# virsh start web01 
Domain web01 started

[root@kvm02 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     web01                          running

[root@kvm02 ~]# 

The successful completion of static migration. . .

Second, the dynamic migration (live migration)

Live migration is divided into five steps:
1, to create a shared memory
2, the two machines to mount the shared storage (manually mount; using a resource pool)
3, start the migration dynamic
4, creating a virtual machine configuration file after the migration
5, again define a virtual machine

Environment Description:

CPU name IP addresses operating system
nfs 172.16.1. 10 centos7
kvm01 172.16.1.30 centos7
kvm02 172.16.1.40 centos7
1)第一步,关闭所有主机的selinux(一定要关)及防火墙;
[root@kvm01 ~]# vim /etc/selinux/config
SELINUX=disabled
[root@kvm01 ~]# reboot  #重启生效

[root@kvm01 ~]# systemctl stop firewalld     

Note: Do not use setenforce 0 (temporarily closed), otherwise an error will be migration.

2) deployment nfs on (in the nfs server)

[root@nfs ~]# yum -y install nfs-utils
[root@nfs ~]# vim /etc/exports
/nfs-share 172.16.1.0/24(rw,sync,no_root_squash)
[root@nfs ~]# mkdir /nfs-share
[root@nfs ~]# systemctl start rpcbind
[root@nfs ~]# systemctl start nfs-server
[root@nfs ~]# systemctl enable nfs-server

# Kvm01 and whether the test can be remotely mounted kvm02

[root@kvm01 ~]# showmount -e 172.16.1.10
Export list for 172.16.1.10:
/nfs-share 172.16.1.0/24
[root@kvm02 ~]# showmount -e 172.16.1.10
Export list for 172.16.1.10:
/nfs-share 172.16.1.0/24

3) set the host name, / etc / hosts parsing each other, and two secret Free kvm host configuration log

[root@kvm01 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.10 nfs
172.16.1.30 kvm01
172.16.1.40 kvm02
#拷贝给其他主机:
[root@kvm01 ~]# for i in 10 40; do scp /etc/hosts 172.16.1.$i:/etc/; done
#配置免密登录:
[root@kvm01 ~]# ssh-keygen -t rsa
[root@kvm01 ~]# ssh-copy-id kvm02
[root@kvm01 ~]# ssh kvm02 hostname
kvm02

4) two shared storage mount kvm host:

#需要将kvm01上的镜像文件,拷贝至nfs的共享目录中:
[root@kvm01 ~]# scp /kvm-vm/web01.qcow2  nfs:/nfs-share
[root@nfs ~]# ls /nfs-share/
web01.qcow2
kvm01:  
[root@kvm01 ~]# vim /etc/fstab    
//添加以下内容:
172.16.1.10:/nfs-share          /kvm-vm                 nfs     defaults        0 0
[root@kvm01 ~]# mount -a     //重新加载
[root@kvm01 ~]# ls /kvm-vm/
web01.qcow2

kvm02:
[root@kvm02 ~]# mkdir /kvm-vm
[root@kvm02 ~]# vim /etc/fstab
172.16.1.10:/nfs-share          /kvm-vm                 nfs     defaults        0 0
[root@kvm02 ~]# mount -a
[root@kvm02 ~]# ls /kvm-vm/
web01.qcow2
#创建桥接网络:
[root@kvm02 ~]# systemctl stop NetworkManager
[root@kvm02 ~]# virsh iface-bridge ens33 br0 
Created bridge br0 with attached device ens33
Bridge interface br0 started

4) dynamic migration:
# before a live migration, virtual machine simulation web01 normal operation:
kvm hot and cold state transition

kvm提供了迁移工具migrate:
[root@kvm01 ~]# virsh migrate --live --unsafe --verbose web01 qemu+ssh://172.16.1.40/system
Migration: [100 %]     #迁移成功

Parameters explanation:

--live # live migration
--unsafe # insecurity have forced even if migrated
--verbose # displays the migration process
web01 said the current domain name (virtual machine), behind the address of the target host is pointing.

# After the migration, you can check the state of the virtual machine on the host kvm02:

[root@kvm02 ~]# virsh list 
 Id    Name                           State
----------------------------------------------------
 2     web01                          running

You can see the state of the virtual machine is running.

# Log virtual machine to see if the status of ongoing work:
kvm hot and cold state transition

Can be seen through live migration, virtual machine has to keep working, no data is lost, and QEMU / KVM client closes on the source host, and then the remaining amount of data transferred to the destination host. So far, the migration target has been achieved.

In a production environment, then we can choose an appropriate time to re-define the virtual machine:

#将虚拟机的配置文件拷贝至新机上(或自己创建配置文件):
[root@kvm01 ~]# scp /etc/libvirt/qemu/web01.xml kvm02:/etc/libvirt/qemu/
web01.xml                                                               100% 4047     4.0KB/s   00:00   
[root@kvm02 ~]# ls /etc/libvirt/qemu  #在web02上查看
networks  web01.xml
#重新定义虚拟机:
[root@kvm02 ~]# virsh shutdown web01 
Domain web01 is being shutdown

[root@kvm02 ~]# virsh define /etc/libvirt/qemu/web01.xml 
Domain web01 defined from /etc/libvirt/qemu/web01.xml

[root@kvm02 ~]# virsh start web01   #启动虚拟机
Domain web01 started

[root@kvm02 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     web01                          running    

[root@kvm02 ~]# 

At this point, the hot end of the migration. .

Common mistakes:

1、迁移时遇到的错误描述:
# virsh migrate centos --live qemu+ssh://192.168.30.132/system
error: unable to connect to server at 'KVM-2:49152': No route to host    
原因:你的免密登录没有成功
解决方法:重新做免密登录即可
2、迁移时的存储错误:
# virsh migrate centos --live qemu+ssh://192.168.30.132/system
 error: Failed to open file '/mnt/CentOS6.8.qcow2': Input/output error
  原因:存储没有挂载成功
  解决方法:mount -o remount /dev/sdb /mnt
3、迁移时FQDN错误:
# virsh migrate centos --live qemu+ssh://192.168.30.132/system
 error: internal error hostname on destination resolved to localhost, but migration requires an FQDN
  原因:两台宿主机无法解析主机名
  解决方法:重新配置主机名和ip的解析
4、迁移时语法错误:
# virsh migrate centos --live qemu+ssh://192.168.30.132:/system
error: internal error Unable to parse URI qemu+ssh://192.168.30.132:/system
  原因:qemu+ssh语法写错了
  解决方法:正确的应该是:virsh migrate centos --live qemu+ssh://192.168.30.132/system

Guess you like

Origin blog.51cto.com/13972012/2481525