KVM虚拟机迁移(冷迁移、热迁移)

KVM虚拟机迁移

准备环境

主机名 IP 内存 网络 软件需求 虚拟化
kvm-server-01 10.0.0.11 2G 创建br0桥接网卡 kvm、nfs 开启虚拟化
kvm-server-02 10.0.0.12 2G 创建br0桥接网卡 kvm、nfs 开启虚拟化
nfs-01 10.0.0.31 1G nfs

1.冷迁移(虚拟机关机迁移)

kvm-server-01上的虚拟主机配置和镜像推送到kvm-server-02上,然后进行导入配置、并启动虚拟主机,就实现了迁移操作。

  • 传输镜像到新的服务器kvm-server-02
[root@kvm-server-01 ~]# scp -rp /opt/centos7.6.qcow2 root@172.16.1.12:/opt
[root@kvm-server-01 ~]# scp -rp /etc/libvirt/qemu/centos7.6.xml root@172.16.1.12:/opt
  • kvm-server-02上恢复kvm主机
[root@kvm-server-02 ~]# cd /opt/
[root@kvm-server-02 opt]# virsh define centos7.6.xml 
Domain web01 defined from centos7.6.xml

[root@kvm-server-02 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7.6                      shut off

[root@kvm-server-02 opt]# virsh start centos7.6
Domain centos7.6 started

[root@kvm-server-02 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     centos7.6                      running
  • 连接centos7.6,测试是否可用
[root@kvm-server-02 opt]# virsh console centos7.6
Connected to domain web01
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last failed login: Fri Jul 31 00:13:23 EDT 2020 on ttyS0
There was 1 failed login attempt since the last successful login.
Last login: Thu Jul 30 05:43:36 on ttyS0
[root@localhost ~]# ping baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79: icmp_seq=1 ttl=128 time=30.4 ms

--- baidu.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 30.445/30.445/30.445/0.000 ms

2.热迁移(虚拟机在线迁移)

  • 所有服务上面都安装并配置nfs服务
#安装nfs-utils软件包
yum install -y nfs-utils

#在nfs-01服务端配置nfs配置文件
[root@nfs-01 ~]# vim /etc/exports
/data   172.16.1.0/24(rw,sync,no_root_squash,no_all_squash)

#启动nfs服务
[root@nfs-01 ~]# systemctl enable nfs-server rpcbind
[root@nfs-01 ~]# systemctl start nfs-server rpcbind
  • 挂载共享目录
#创建挂载目录
[root@kvm-server-01 ~]# mkdir /data
[root@kvm-server-02 ~]# mkdir /data

#在kvm-server-01和kvm-server-02上挂载nfs共享目录
[root@kvm-server-01 ~]# mount -t nfs 172.16.1.31:/data /data
[root@kvm-server-02 ~]# mount -t nfs 172.16.1.31:/data /data

#复制镜像到共享目录
[root@kvm-server-01 ~]# cp -rp /opt/centos7.6.qcow2 /data
  • 进行热迁移

注意:

1.需要对kvm服务器做hosts解析

2.做热迁移的虚拟主机不能有快照

3.手动热迁移后关闭主机,虚拟主机会消失(使用WebVirtMgr管理界面不会出现此问题)

#把kvm-server-01上的centos7.6虚拟主机热迁移到kvm-server-02上

# 查看当前状态
[root@kvm-server-01 ~]# virsh list 
 Id    Name                           State
----------------------------------------------------
 6     centos7.6                      running

# 做hosts解析
[root@kvm-server-01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.11   kvm-server-01
10.0.0.12   kvm-server-02

# 删除快照
[root@kvm-server-01 ~]# virsh snapshot-list centos7.6 
 Name                 Creation Time             State
------------------------------------------------------------
 1596113410           2020-07-30 20:50:10 +0800 shutoff
[root@kvm-server-01 ~]# virsh snapshot-delete centos7.6 1596113410

# 进行热迁移
[root@kvm-server-01 ~]# virsh migrate --live --verbose centos7.6 qemu+ssh://10.0.0.12/system --unsafe
root@10.0.0.12's password: 
Migration: [100 %]

# 查看kvm-server-02上主机列表
[root@kvm-server-02 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 5     centos7.6                      running
#关闭centos7.6
[root@kvm-server-02 ~]# virsh shutdown centos7.6   
Domain centos7.6 is being shutdown
#查看主机列表
[root@kvm-server-02 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
点赞

发表回复