KVM虚拟机迁移(冷迁移、共享存储热迁移、本地存储热迁移)
1.本地存储冷迁移
首先查看 虚拟机状态,以及虚拟机文件位置,并关闭虚拟机
[root@kvm-test41 data]# virsh list --all
Id Name State
----------------------------------------------------
2 centos7 running
[root@kvm-test41 data]# virsh domblklist centos7
Target Source
------------------------------------------------
vda /data/centos7.qcow2
hda -
[root@kvm-test41 data]# virsh shutdown centos7
Domain centos7 is being shutdown
[root@kvm-test41 data]# virsh list --all
Id Name State
----------------------------------------------------
- centos7 shut off
将虚拟机文件以及注册信息传输到kvm-test42 宿主机 上
[root@kvm-test41 qemu]# scp /data/centos7.qcow2 root@192.168.232.42:/data/
centos7.qcow2 100% 1627MB 143.0MB/s 00:11
[root@kvm-test41 qemu]# scp /etc/libvirt/qemu/centos7.xml root@192.168.232.42:/etc/libvirt/qemu/
centos7.xml 100% 3645 2.4MB/s 00:00
在kvm-test42 IP为:192.168.232.42的宿主机上恢复机器
[root@kvm-test42 qemu]# ls /data/
centos7.qcow2
### 在kvm-test4恢复该虚拟机
[root@kvm-test42 qemu]# virsh define /etc/libvirt/qemu/centos7.xml
Domain centos7 defined from /etc/libvirt/qemu/centos7.xml
[root@kvm-test42 qemu]# virsh list --all
Id Name State
----------------------------------------------------
- centos7 shut off
- test03 shut off
- test03ovf shut off
[root@kvm-test42 qemu]# virsh start centos7
Domain centos7 started
[root@kvm-test42 yum.repos.d]# virsh list --all
Id Name State
----------------------------------------------------
2 centos7 running
- test03 shut off
- test03ovf shut off
进入虚拟机 测试迁移是否正常
[root@kvm-test42 ~]# virsh console centos7
Connected to domain centos7
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
localhost login: root
密码:
Last login: Wed Mar 9 13:13:41 on ttyS0
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data.
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=128 time=14.7 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=2 ttl=128 time=13.3 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=3 ttl=128 time=12.7 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=4 ttl=128 time=13.5 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=5 ttl=128 time=13.2 ms
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4008ms
rtt min/avg/max/mdev = 12.767/13.523/14.737/0.667 ms
2.共享存储热迁移
准备一台ip:192.168.232.200 主机名为nfs200 的机器,在所有机器上安装nfs 服务
yum install nfs-utils rpcbind
配置 nfs server
[root@nfs200 ~]# mkdir /data_nfs
[root@nfs200 ~]# cat /etc/exports
/data_nfs 192.168.232.0/16(rw,sync,no_root_squash,no_all_squash)
[root@nfs200 ~]# systemctl enable --now nfs-server rpcbind
在kvm-test41和kvm-test42 上挂载 nfs200 /data_nfs
#创建新的挂在目录
[root@kvm-test42 ~]# mkdir /data_nfs
[root@kvm-test41 ~]# mkdir /data_nfs
#在kvm-test42和kvm-test42上挂载nfs共享目录
mount -t nfs 192.168.232.200:/data_nfs /data_nfs
#设置nfd 客户端开机自动挂载
cat /etc/fstab
....
192.168.232.200:/data_nfs /data_nfs nfs _netdev 0 0
#将kvm-test42 上的虚拟机centos7 删除掉
[root@kvm-test42 /]# virsh undefine centos7
Domain centos7 has been undefined
[root@kvm-test42 /]# virsh list --all
Id Name State
----------------------------------------------------
- test03 shut off
#将kvm-test41 上的虚拟机centos7 重新注册到 共享文件夹里
[root@kvm-test41 ~]# cp /data/centos7.qcow2 /data_nfs/
[root@kvm-test41 ~]# cp /etc/libvirt/qemu/centos7.xml /data_nfs/
#将kvm-test41上的虚拟机centos7 删除掉
[root@kvm-test41 data]# virsh undefine centos7
Domain centos7 has been undefined
[root@kvm-test41 data]# virsh list --all
Id Name State
----------------------------------------------------
#修改centos7.xml 文件中存储 centos7.qcow2文件位置
<source file='/data_nfs/centos7.qcow2'/>
#重新注册虚拟机
[root@kvm-test41 data_nfs]# virsh define centos7.xml
Domain centos7 defined from centos7.xml
[root@kvm-test41 data_nfs]# virsh start centos7
Domain centos7 started
#可以看到名字为centos7的虚拟机位置
[root@kvm-test41 data_nfs]# virsh domblklist centos7
Target Source
------------------------------------------------
vda /data_nfs/centos7.qcow2
hda -
开始进行热迁移,将kvm-test41宿主机 下的centos7虚拟机迁移到 kvm-test42宿主机 下,
centos7虚拟机的ip 地址为:192.168.232.12
[root@kvm-test41 qemu]# virsh migrate --live --verbose --unsafe --persistent centos7 qemu+ssh://192.168.232.42/system
Migration: [100 %]
#在192.168.232.42上查看 centos7 已经迁移过来,并且状态时running
[root@kvm-test42 qemu]# virsh list --all
Id Name State
----------------------------------------------------
3 centos7 running
- test03 shut off
执行热迁移过程中ping centos7 虚拟机192.168.232.12 可以热迁移的影响大概在0.2毫秒
迁移完成后需要删除kvm-test41主机 centos7虚拟机
[root@kvm-test41 qemu]# virsh list --all
Id Name State
----------------------------------------------------
- centos7 shut off
[root@kvm-test41 data]# virsh undefine centos7
Domain centos7 has been undefined
[root@kvm-test41 data]# virsh list --all
Id Name State
----------------------------------------------------
3.本地存储热迁移基于kvm-ev
在kvm-test41 kvm-test42宿主机 安装kvm-ev
yum -y install qemu-kvm-ev
安装完qemu-kvm-ev 会替换掉原有的qemu-kvm,qemu-img也会被替换程qemu-img-ev
[root@kvm-test41 data]# virsh list --all
Id Name State
----------------------------------------------------
- test-ev running
[root@kvm-test41 data]# virsh domblklist test-ev
Target Source
------------------------------------------------
vda /data/test-ev.qcow2
hda -
将test-ev 虚拟机迁移到 kvm-test42 上
#在迁移前需要在kvm-test42宿主机 相同目录下创建一个相同大小名称的qcow2磁盘
[root@kvm-test42 data]# qemu-img create -f qcow2 /data/test-ev.qcow2 10G
Formatting '/data/test-ev.qcow2', fmt=qcow2 size=10737418240 cluster_size=65536 lazy_refcounts=off refcount_bits=16
[root@kvm-test42 data]# ls
test-ev.qcow2
#在源机器上执行迁移命令 迁移test-ev 虚拟机
[root@kvm-test41 data]# virsh migrate --live --copy-storage-all --persistent --verbose --unsafe test-ev qemu+ssh://192.168.232.42/system
Migration: [100 %]
#热迁移完成后,可以在看到kvm-test42 宿主机 上的 test-ev 虚拟机
[root@kvm-test42 data]# virsh list --all
Id Name State
----------------------------------------------------
12 test-ev running
- test03 shut off
[root@kvm-test42 data]# ls
test-ev.qcow2
#进入虚拟查看可以正常上网
[root@kvm-test42 data]# virsh console test-ev
Connected to domain test-ev
Escape character is ^]
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (112.80.248.76) 56(84) bytes of data.
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=1 ttl=128 time=10.3 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=2 ttl=128 time=10.0 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=3 ttl=128 time=9.64 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=4 ttl=128 time=9.94 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=5 ttl=128 time=10.0 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=6 ttl=128 time=9.17 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=7 ttl=128 time=10.3 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=8 ttl=128 time=10.2 ms
--- www.a.shifen.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7014ms
rtt min/avg/max/mdev = 9.170/9.982/10.398/0.400 ms
#在kvm-test41宿主机 删除test-ev虚拟机
root@kvm-test41 data]# virsh undefine test-ev
Domain test-ev has been undefined
[root@kvm-test41 data]# virsh list --all
Id Name State
----------------------------------------------------
[root@kvm-test41 data]# rm -rf /data/test-ev.*
test-ev 虚拟ip 为:192.168.232.14 在执行热迁移过程中ping 192.168.232.14可以热迁移的影响极小
当然我测试虚拟机文件过小也有一定原因
[root@kvm-test41 data]# qemu-img info test-ev.qcow2
image: test-ev.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
Comments | NOTHING