12.5. 将虚拟机磁盘镜像与其他主机共享

要在 支持的 KVM 主机 间执行虚拟机实时迁移,需要共享虚拟机存储。以下流程提供了使用 NFS 协议将本地存储的虚拟机镜像与源主机和目标主机进行共享的说明。

先决条件

  • 旨在迁移的虚拟机被关闭。
  • 可选: 一个主机系统可用于托管不是源或目标主机的存储,但源和目标主机都可以通过网络访问它。这是共享存储的最佳解决方案,并被红帽推荐。
  • 请确定 NFS 文件锁定没有被使用,因为在 KVM 中不支持它。
  • 在源主机和目标主机上安装并启用 NFS。如果没有:

    • 安装 NFS 软件包:

      # {PackageManagerCommand} install nfs-utils
    • 确保在防火墙中打开了 NFS 的端口,如 2049。

      # firewall-cmd --permanent --add-service=nfs
      # firewall-cmd --permanent --add-service=mountd
      # firewall-cmd --permanent --add-service=rpc-bind
      # firewall-cmd --permanent --add-port=2049/tcp
      # firewall-cmd --permanent --add-port=2049/udp
      # firewall-cmd --reload
    • 启动 NFS 服务。

      # systemctl start nfs-server

流程

  1. 连接到提供共享存储的主机。在本例中,它是 example-shared-storage 主机:

    # ssh root@example-shared-storage
    root@example-shared-storage's password:
    Last login: Mon Sep 24 12:05:36 2019
    root~#
  2. 在保存磁盘镜像的源主机上创建一个目录,并与迁移主机共享:

    # mkdir /var/lib/libvirt/shared-images
  3. 将虚拟机的磁盘镜像从源主机复制到新创建的目录。以下示例将虚拟机的磁盘镜像 example-disk-1 复制到 example-shared-storage 主机的 /var/lib/libvirt/shared-images/ 目录中:

    # scp /var/lib/libvirt/images/example-disk-1.qcow2 root@example-shared-storage:/var/lib/libvirt/shared-images/example-disk-1.qcow2
  4. 在您要用于共享存储的主机上,将共享目录添加到 /etc/exports 文件中。以下示例将 /var/lib/libvirt/shared-images 目录与 example-source-machineexample-destination-machine 主机共享:

    # /var/lib/libvirt/shared-images example-source-machine(rw,no_root_squash) example-destination-machine(rw,no\_root_squash)
  5. 在源和目标主机上,将共享目录挂载到 /var/lib/libvirt/images 目录中:

    # mount example-shared-storage:/var/lib/libvirt/shared-images /var/lib/libvirt/images

验证

  • 在源主机上启动虚拟机,并观察它是否已成功启动。