KVM热迁移实现需要使用共享的存储池,最简便的方法是使用NFS共享。更为复杂且高可用、高性能的是使用共享的分布式文件系统,如Ceph。
本实践采用CentOS 7系统构建的NFS服务作为共享存储
构建NFS共享存储
以下操作在作为NFS服务的主机上执行
mkdir -p /var/lib/nfs-data/images
yum install nfs-utils nfs-utils-lib
systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap
systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap
服务器端设置NFS卷输出,即编辑 /etc/exports
添加:
/var/lib/nfs-data/images 192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check)
NFS客户端挂载
编辑需要挂载共享NFS磁盘的设置/etc/fstab
配置将共享NFS卷挂载到/var/lib/libvirt/nfs-images
192.168.1.1:/var/lib/nfs-data/images /var/lib/libvirt/nfs-images nfs auto,rw,vers=3,hard,intr,tcp,rsize=32768,wsize=32768 0 0
mkdir -p /var/lib/libvirt/nfs-images
mount /var/lib/libvirt/nfs-images
所有使用共享存储的KVM主机都挂载好这个NFS共享
配置libvirt的VM存储池
将默认的/var/lib/libvirt/images
目录对应的default
存储池修改到新的共享存储
virsh shutdown centos7
virsh shutdown win2012
显示输出
Name State Autostart
-------------------------------------------
huatai active yes
images active yes
root active yes
virsh pool-info images
virsh pool-dumpxml images | grep -i path
输出显示
<path>/var/lib/libvirt/images</path>
virsh vol-list images
输出显示
Name Path
------------------------------------------------------------------------------
CentOS-7-x86_64-DVD-1511.iso /var/lib/libvirt/images/CentOS-7-x86_64-DVD-1511.iso
centos7.img /var/lib/libvirt/images/centos7.img
win2012.img /var/lib/libvirt/images/win2012.img
win2012.iso /var/lib/libvirt/images/win2012.iso
virsh pool-destroy images
virsh pool-edit images
编辑内容,将其中的存储路径修改成新的NFS共享存储路径
<pool type='dir'>
<name>images</name>
<uuid>82b69f47-1c20-4e99-9dad-5665181c0d77</uuid>
...
<target>
<path>/var/lib/libvirt/nfs-images</path>
...
</target>
</pool>
virsh pool-start images
virsh pool-dumpxml images | grep -i path
确认修改路径正确
<path>/var/lib/libvirt/nfs-images</path>
mv /var/lib/libvirt/images/* /var/lib/libvirt/nfs-images/
sudo virsh edit centos7
将存储路径修改到共享存储目录
<source file='/var/lib/libvirt/nfs-images/centos7.img'/>
KVM热迁移
测试环境:将虚拟机 centos7
从KVM物理服务器 test-1-3
迁移到 test-1-4
上,我们要先打通test-1-3
登录到test-1-4
的免密码ssh,并验证ssh登录无误
ssh test-1-4 "virsh list"
显示输出
Id Name State
----------------------------------------------------
virsh migrate centos7 qemu+ssh://root@testtfs-1-4/system
提示信息
error: Unsafe migration: Migration may lead to data corruption if disks use cache != none
要避免这个报错,需要关闭每个VM的disk I/O cache功能,即编辑vm
virsh shutdown centos7
virsh edit centos7
在 driver
行添加`cache='none'
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/var/lib/libvirt/nfs-images/centos7.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
然后再次启动vm
然后再次迁移。
另外一种方法是使用 --unsafe
,这样对于非关键应用可以适合。
virsh migrate centos7 qemu+ssh://root@testtfs-1-4/system --unsafe
然后在test-1-4
物理服务器上可以看到迁移后正常运行的虚拟机
[root@test-1-4 /root]
#virsh list
Id Name State
----------------------------------------------------
1 centos7 running
参考