在KVM中,您可以将运行中的虚拟机从一个KVM主机迁移到另一个KVM主机,而不需要任何停机时间。如果两个KVM主机都能够访问相同的存储池,那么实时迁移工作就很好了。要使存储池在KVM主机上都可访问,您需要使用NFS或GFS2文件系统(集群文件系统)。在本例中,我使用NFS文件系统来存储VM映像。在迁移过程中,VM的“内存”内容将被复制到目标KVM主机,而在某个时间点上,将发生迁移VM。注意,当您在KVM主机上共享文件系统时,将不会在网络上复制VM的磁盘映像,因为两个KVM主机都可以访问相同的存储池。
虚拟机热迁移演示
- 为kvm-node2安装kvm软件及手动配置网桥br0
- 关闭kvm-node2的防火墙
[root@kvm-node2 ~]# iptables -F
[root@kvm-node2 ~]# systemctl stop firewalld.service
- 登录到kvm-node1上检查vm运行情况
[root@kvm-node1 ~]# virsh list
Id Name State
----------------------------------------------------
3 test running
- 打通kvm-node1到kvm-node2的ssh key通道
[root@kvm-node1 ~]# ssh-copy-id 192.168.57.202
The authenticity of host '192.168.57.202 (192.168.57.202)' can't be established.
ECDSA key fingerprint is cf:b5:cd:88:0c:05:fb:9b:b9:46:b0:c0:c4:38:cf:c1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.57.202's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.57.202'"
and check to make sure that only the key(s) you wanted were added.
[root@kvm-node1 ~]# ssh 192.168.57.202
Last login: Wed Jun 14 16:28:06 2017 from 192.168.57.1
[root@kvm-node2 ~]# exit
logout
- 登录到vm实例“test”上,长ping“kvm-node1”,注意随时观察网络是否有中断。
[root@kvm-node1 ~]# virsh console test
Connected to domain test
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-514.el7.x86_64 on an x86_64
localhost login: root
Password:
Last login: Wed Jun 14 17:06:02 on ttyS0
[root@localhost ~]# uptime
17:07:12 up 2 min, 1 user, load average: 0.15, 0.13, 0.05
[root@localhost ~]# ping 192.168.57.201
PING 192.168.57.201 (192.168.57.201) 56(84) bytes of data.
64 bytes from 192.168.57.201: icmp_seq=1 ttl=64 time=0.269 ms
64 bytes from 192.168.57.201: icmp_seq=2 ttl=64 time=0.308 ms
64 bytes from 192.168.57.201: icmp_seq=3 ttl=64 time=0.788 ms
- 开始从KVM-node1上迁移vm机到KVM-node2上:
[root@kvm-node1 ~]# virsh migrate test qemu+ssh://root@192.168.57.202/system
error: Unsafe migration: Migration may lead to data corruption if disks use cache != none
- (⊙o⊙)哦,看起来我们没发迁移vm,从报错中我们很轻易的得知,热迁移必须配置虚拟机的磁盘使用缓存,现在我们来配置这个选项:
[root@kvm-node1 ~]# virsh edit test
Domain test XML configuration edited.
############################################添加cache='none'
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/var/lib/libvirt/images/test.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
############################################
- 启动虚拟机后,重复步骤5,注意观察vm是否有业务中断
- 我们再来尝试一下迁移操作,没有输出,O(∩_∩)O~ 通常没有结果就是最好的结果。
[root@kvm-node1 ~]# virsh migrate test qemu+ssh://root@192.168.57.202/system
- 下面我们登录到kvm-node2上检查一下虚拟机迁移是否成功
[root@kvm-node2 ~]# virsh list
Id Name State
----------------------------------------------------
3 test running
- 在整个迁移过程中,我们可以观察到对虚拟机(vm)上跑的业务基本没哟影响,我们长ping的进程仅仅只有轻微额网络抖动。当然KVM热迁移只适合迁移业务不繁忙的虚拟机,如果业务过于繁忙,热迁移可能由于数据同步无法达到读写一致而超时失败。
[root@localhost ~]# ping 192.168.57.201
PING 192.168.57.201 (192.168.57.201) 56(84) bytes of data.
64 bytes from 192.168.57.201: icmp_seq=1 ttl=64 time=0.457 ms
64 bytes from 192.168.57.201: icmp_seq=2 ttl=64 time=0.928 ms
64 bytes from 192.168.57.201: icmp_seq=3 ttl=64 time=0.795 ms
64 bytes from 192.168.57.201: icmp_seq=4 ttl=64 time=0.683 ms
64 bytes from 192.168.57.201: icmp_seq=5 ttl=64 time=0.374 ms
64 bytes from 192.168.57.201: icmp_seq=6 ttl=64 time=0.584 ms
64 bytes from 192.168.57.201: icmp_seq=7 ttl=64 time=0.253 ms
64 bytes from 192.168.57.201: icmp_seq=8 ttl=64 time=0.667 ms
64 bytes from 192.168.57.201: icmp_seq=9 ttl=64 time=0.297 ms
64 bytes from 192.168.57.201: icmp_seq=10 ttl=64 time=0.268 ms
64 bytes from 192.168.57.201: icmp_seq=11 ttl=64 time=0.757 ms
64 bytes from 192.168.57.201: icmp_seq=12 ttl=64 time=0.193 ms
64 bytes from 192.168.57.201: icmp_seq=13 ttl=64 time=0.234 ms
64 bytes from 192.168.57.201: icmp_seq=14 ttl=64 time=0.298 ms
64 bytes from 192.168.57.201: icmp_seq=15 ttl=64 time=0.289 ms
64 bytes from 192.168.57.201: icmp_seq=16 ttl=64 time=0.171 ms
64 bytes from 192.168.57.201: icmp_seq=17 ttl=64 time=0.754 ms
64 bytes from 192.168.57.201: icmp_seq=18 ttl=64 time=0.731 ms
64 bytes from 192.168.57.201: icmp_seq=19 ttl=64 time=0.371 ms
64 bytes from 192.168.57.201: icmp_seq=20 ttl=64 time=2.05 ms # 轻微的网络抖动
64 bytes from 192.168.57.201: icmp_seq=21 ttl=64 time=0.359 ms
64 bytes from 192.168.57.201: icmp_seq=22 ttl=64 time=0.662 ms
64 bytes from 192.168.57.201: icmp_seq=23 ttl=64 time=0.555 ms