雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

将 Proxmox VM 迁移到另一个 Proxmox 服务器

2024-07-16 58

需要将虚拟机从一台 Proxmox 主机迁移到另一台主机的原因有很多。它包括需要平衡多个计算主机之间的虚拟机工作负载,并防止出现影响应用程序性能的资源瓶颈。结果是更好的资源利用率和应用程序效率。虚拟机迁移也可以在灾难恢复策略中完成。可以使用迁移策略在不同的主机上复制虚拟机,以确保冗余并最大限度地减少发生硬件故障时数据丢失或完全停机的风险。

Proxmox VE 上提供的 Vzdump 实用程序用于创建正在运行的 KVM 虚拟机的一致快照。它主要创建虚拟机的 tar 存档,其中包括 VM 配置文件。我们将使用 vzdump 实用程序来备份虚拟机并将其迁移到 Proxmox 虚拟化环境中的不同虚拟化主机。

对于操作系统模板创建,请参阅:

  • 在 Proxmox VE 上创建 Rocky/AlmaLinux/CentOS 操作系统模板
  • 在 Proxmox VE 上创建 Ubuntu 和 Debian 操作系统模板

如何在 Proxmox VE 中将虚拟机备份为 tar

vzdump 实用程序的基本用法是在没有快照的情况下简单转储来宾操作系统。这只是将来宾专用区域和配置文件存档到默认转储目录(通常/var/lib/vz/dump/)。

可以使用 man 检查命令使用文档

man vzdump

命令使用语法为:

vzdump <VMID> <OPTIONS>

可与该实用程序一起使用的可用选项:

  • <vmid> 您要备份的访客系统的 ID。
  • <代码>--压缩<0 | 1 |压缩包 |左左| zstd> 压缩转储文件。
  • --dumpdir 将结果文件存储到指定目录。

列出主机 1 上正在运行的虚拟机:

# qm list
      VMID NAME                 STATUS     MEM(MB)    BOOTDISK(GB) PID
       101 AD-Server-2022       stopped    8192             100.00 0
       102 pfsense              stopped    4096              32.00 0
       104 Ubuntu-22-Desktop    running    16384             50.00 1231

对于容器:

pct list

执行以下命令备份虚拟机:

vzdump --compress <VMID>
vzdump --compress <ContainerID>

在备份过程中,可以使用以下算法之一来压缩文件:lzogzip、或zstd。如果您未指定备份算法,则默认为lzo

vzdump --compress gzip <VMID>

压缩产生的文件扩展名

  • .zst:用于 Zstandard (zstd) 压缩
  • .gz 或 .tgz:用于 gzip 压缩
  • .lzo:用于 lzo 压缩

您还可以指定要保存备份的目录。

vzdump --compress gzip <VMID> --dumpdir /home/backups

对于容器:

vzdump --compress gzip <ContainerID> --dumpdir /home/backups

命令执行示例输出:

INFO: starting new backup job: vzdump 104 --compress 1
INFO: Starting Backup of VM 104 (qemu)
INFO: Backup started at 2023-12-06 21:28:43
INFO: status = running
INFO: VM Name: Ubuntu-22-Desktop
INFO: include disk 'scsi0' 'local:104/vm-104-disk-0.qcow2' 50G
INFO: backup mode: snapshot
INFO: ionice priority: 7
INFO: creating vzdump archive '/var/lib/vz/dump/vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo'
INFO: skipping guest-agent 'fs-freeze', agent configured but not running?
INFO: started backup task '179beeea-4d05-4c7b-bab6-0896f2a4a35c'
INFO: resuming VM again
INFO:   7% (3.9 GiB of 50.0 GiB) in 3s, read: 1.3 GiB/s, write: 437.1 MiB/s
INFO:  10% (5.4 GiB of 50.0 GiB) in 6s, read: 508.7 MiB/s, write: 491.1 MiB/s
INFO:  15% (7.8 GiB of 50.0 GiB) in 9s, read: 806.0 MiB/s, write: 786.2 MiB/s
INFO:  19% (9.9 GiB of 50.0 GiB) in 12s, read: 728.6 MiB/s, write: 687.3 MiB/s
INFO:  23% (11.6 GiB of 50.0 GiB) in 15s, read: 593.0 MiB/s, write: 577.3 MiB/s
INFO:  27% (14.0 GiB of 50.0 GiB) in 18s, read: 802.6 MiB/s, write: 769.8 MiB/s
INFO:  32% (16.4 GiB of 50.0 GiB) in 21s, read: 837.4 MiB/s, write: 820.7 MiB/s
INFO:  36% (18.4 GiB of 50.0 GiB) in 24s, read: 681.5 MiB/s, write: 632.9 MiB/s
INFO:  40% (20.3 GiB of 50.0 GiB) in 27s, read: 625.9 MiB/s, write: 597.3 MiB/s
INFO:  45% (22.6 GiB of 50.0 GiB) in 30s, read: 801.1 MiB/s, write: 765.0 MiB/s
INFO:  49% (25.0 GiB of 50.0 GiB) in 33s, read: 802.3 MiB/s, write: 776.4 MiB/s
INFO:  54% (27.0 GiB of 50.0 GiB) in 36s, read: 700.5 MiB/s, write: 682.9 MiB/s
INFO:  56% (28.0 GiB of 50.0 GiB) in 39s, read: 348.4 MiB/s, write: 337.0 MiB/s
INFO:  60% (30.4 GiB of 50.0 GiB) in 42s, read: 814.4 MiB/s, write: 765.6 MiB/s
INFO:  64% (32.2 GiB of 50.0 GiB) in 45s, read: 611.4 MiB/s, write: 579.4 MiB/s
INFO:  68% (34.3 GiB of 50.0 GiB) in 48s, read: 725.0 MiB/s, write: 683.0 MiB/s
INFO:  71% (35.8 GiB of 50.0 GiB) in 51s, read: 495.4 MiB/s, write: 475.7 MiB/s
INFO:  74% (37.3 GiB of 50.0 GiB) in 54s, read: 529.2 MiB/s, write: 502.4 MiB/s
INFO:  79% (39.7 GiB of 50.0 GiB) in 57s, read: 799.9 MiB/s, write: 766.0 MiB/s
INFO:  82% (41.2 GiB of 50.0 GiB) in 1m, read: 519.6 MiB/s, write: 498.8 MiB/s
INFO:  85% (42.8 GiB of 50.0 GiB) in 1m 3s, read: 536.5 MiB/s, write: 495.1 MiB/s
INFO:  89% (44.5 GiB of 50.0 GiB) in 1m 6s, read: 594.5 MiB/s, write: 590.0 MiB/s
INFO:  93% (46.7 GiB of 50.0 GiB) in 1m 9s, read: 729.8 MiB/s, write: 669.5 MiB/s
INFO:  96% (48.1 GiB of 50.0 GiB) in 1m 12s, read: 480.9 MiB/s, write: 472.1 MiB/s
INFO: 100% (50.0 GiB of 50.0 GiB) in 1m 15s, read: 662.3 MiB/s, write: 579.6 MiB/s
INFO: backup is sparse: 4.77 GiB (9%) total zero data
INFO: transferred 50.00 GiB in 75 seconds (682.7 MiB/s)
INFO: archive file size: 22.76GB
INFO: Finished Backup of VM 104 (00:01:18)
INFO: Backup finished at 2023-12-06 21:30:01
INFO: Backup job finished successfully

备份文件将位于默认备份目录中

# ls /var/lib/vz/dump/
vzdump-qemu-104-2023_12_06-21_28_43.log  vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo

将生成的备份复制到新节点

可以使用 du 命令检查备份的大小。

# du -sh /var/lib/vz/dump/vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo
23G	/var/lib/vz/dump/vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo

集装箱用

# du -sh /var/lib/vz/dump/vzdump-lxc-101-2024_01_17-00_29_42.tar
6.7G	/var/lib/vz/dump/vzdump-lxc-101-2024_01_17-00_29_42.tar

备份完成后,我们可以将存档复制到目标计算主机进行恢复。

scp /var/lib/vz/dump/vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo root@PVE2IP:/var/lib/vz/dump/

在哪里;

  • /var/lib/vz/dump/vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo 是虚拟机备份的路径
  • PVE2IP 将替换为目标 Proxmox 服务器的实际 IP 地址
  • 我们正在将备份复制到目标服务器中的 /var/lib/vz/dump/

从 Proxmox 服务器上的备份存档恢复虚拟机

qmrestore 命令用于恢复 QemuServer vzdump 备份。以下是使用帮助页面。

qmrestore <archive> <vmid> [OPTIONS]

       Restore QemuServer vzdump backups.

       <archive>: <string>
           The backup file. You can pass - to read from standard input.

       <vmid>: <integer> (100 - 999999999)
           The (unique) ID of the VM.

       --bwlimit <number> (0 - N)
           Override I/O bandwidth limit (in KiB/s).

       --force <boolean>
           Allow to overwrite existing VM.

       --live-restore <boolean>
           Start the VM immediately from the backup and restore in background. PBS only.

       --pool <string>
           Add the VM to the specified pool.

       --storage <string>
           Default storage.

       --unique <boolean>
           Assign a unique random ethernet address.

停止第一台 Proxmox 服务器上的虚拟机。

qm stop <VMID>

停止容器用户

pct stop <ContainerID>

例如,要从文件和 VM ID 601 恢复备份,我们将运行:

qmrestore /var/lib/vz/dump/vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo 601

您可以使用--storage指定存储池。使用以下命令列出可用的存储池。

root@pve02:~# pvesm status
Name              Type     Status           Total            Used       Available        %
local              dir     active       100597760        11813608        88784152   11.74%
local-lvm      lvmthin     active       365760512         7790698       357969813    2.13%

恢复到 local-zfs 而不是 local-lvm 的示例

qmrestore --storage local-zfs  /var/lib/vz/dump/vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo 601

容器恢复。

pct restore <NEWID> zdump-lxc-104-2024_01_17-00_38_15.tar.gz

# Specifying storage pool
 pct restore  --storage local-zfs<NEWID> zdump-lxc-104-2024_01_17-00_38_15.tar.gz  

恢复虚拟机的命令输出。

restore vma archive: lzop -d -c /var/lib/vz/dump/vzdump-qemu-104-2023_12_06-21_28_43.vma.lzo | vma extract -v -r /var/tmp/vzdumptmp1055630.fifo - /var/tmp/vzdumptmp1055630
CFG: size: 450 name: qemu-server.conf
DEV: dev_id=1 size: 53687091200 devname: drive-scsi0
CTIME: Wed Dec  6 21:28:46 2023
Formatting '/var/lib/vz/images/601/vm-601-disk-0.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off preallocation=metadata compression_type=zlib size=53687091200 lazy_refcounts=off refcount_bits=16
new volume ID is 'local:601/vm-601-disk-0.qcow2'
map 'drive-scsi0' to '/var/lib/vz/images/601/vm-601-disk-0.qcow2' (write zeros = 0)
progress 1% (read 536870912 bytes, duration 0 sec)
progress 2% (read 1073741824 bytes, duration 0 sec)
progress 3% (read 1610612736 bytes, duration 0 sec)
progress 4% (read 2147483648 bytes, duration 0 sec)
progress 5% (read 2684354560 bytes, duration 1 sec)
progress 6% (read 3221225472 bytes, duration 1 sec)
progress 7% (read 3758096384 bytes, duration 3 sec)
progress 8% (read 4294967296 bytes, duration 4 sec)
progress 9% (read 4831838208 bytes, duration 5 sec)
progress 10% (read 5368709120 bytes, duration 7 sec)
progress 11% (read 5905580032 bytes, duration 8 sec)
progress 12% (read 6442450944 bytes, duration 9 sec)
progress 13% (read 6979321856 bytes, duration 10 sec)
progress 14% (read 7516192768 bytes, duration 11 sec)
progress 15% (read 8053063680 bytes, duration 12 sec)
progress 16% (read 8589934592 bytes, duration 13 sec)
progress 17% (read 9126805504 bytes, duration 14 sec)
progress 18% (read 9663676416 bytes, duration 15 sec)
progress 19% (read 10200547328 bytes, duration 16 sec)
progress 20% (read 10737418240 bytes, duration 17 sec)
progress 21% (read 11274289152 bytes, duration 19 sec)
progress 22% (read 11811160064 bytes, duration 20 sec)
progress 23% (read 12348030976 bytes, duration 21 sec)
progress 24% (read 12884901888 bytes, duration 22 sec)
progress 25% (read 13421772800 bytes, duration 23 sec)
progress 26% (read 13958643712 bytes, duration 24 sec)
progress 27% (read 14495514624 bytes, duration 25 sec)
progress 28% (read 15032385536 bytes, duration 26 sec)
progress 29% (read 15569256448 bytes, duration 27 sec)
progress 30% (read 16106127360 bytes, duration 28 sec)
progress 31% (read 16642998272 bytes, duration 29 sec)
progress 32% (read 17179869184 bytes, duration 30 sec)
progress 33% (read 17716740096 bytes, duration 31 sec)
progress 34% (read 18253611008 bytes, duration 32 sec)
progress 35% (read 18790481920 bytes, duration 33 sec)
progress 36% (read 19327352832 bytes, duration 34 sec)
progress 37% (read 19864223744 bytes, duration 36 sec)
progress 38% (read 20401094656 bytes, duration 37 sec)
progress 39% (read 20937965568 bytes, duration 38 sec)
progress 40% (read 21474836480 bytes, duration 39 sec)
progress 41% (read 22011707392 bytes, duration 40 sec)
progress 42% (read 22548578304 bytes, duration 41 sec)
progress 43% (read 23085449216 bytes, duration 42 sec)
progress 44% (read 23622320128 bytes, duration 43 sec)
progress 45% (read 24159191040 bytes, duration 45 sec)
progress 46% (read 24696061952 bytes, duration 46 sec)
progress 47% (read 25232932864 bytes, duration 47 sec)
progress 48% (read 25769803776 bytes, duration 48 sec)
progress 49% (read 26306674688 bytes, duration 49 sec)
progress 50% (read 26843545600 bytes, duration 50 sec)
progress 51% (read 27380416512 bytes, duration 51 sec)
progress 52% (read 27917287424 bytes, duration 52 sec)
progress 53% (read 28454158336 bytes, duration 53 sec)
progress 54% (read 28991029248 bytes, duration 54 sec)
progress 55% (read 29527900160 bytes, duration 56 sec)
progress 56% (read 30064771072 bytes, duration 57 sec)
progress 57% (read 30601641984 bytes, duration 58 sec)
progress 58% (read 31138512896 bytes, duration 59 sec)
progress 59% (read 31675383808 bytes, duration 60 sec)
progress 60% (read 32212254720 bytes, duration 61 sec)
progress 61% (read 32749125632 bytes, duration 62 sec)
progress 62% (read 33285996544 bytes, duration 63 sec)
progress 63% (read 33822867456 bytes, duration 65 sec)
progress 64% (read 34359738368 bytes, duration 66 sec)
progress 65% (read 34896609280 bytes, duration 67 sec)
progress 66% (read 35433480192 bytes, duration 68 sec)
progress 67% (read 35970351104 bytes, duration 70 sec)
progress 68% (read 36507222016 bytes, duration 71 sec)
progress 69% (read 37044092928 bytes, duration 72 sec)
progress 70% (read 37580963840 bytes, duration 73 sec)
progress 71% (read 38117834752 bytes, duration 75 sec)
progress 72% (read 38654705664 bytes, duration 76 sec)
progress 73% (read 39191576576 bytes, duration 77 sec)
progress 74% (read 39728447488 bytes, duration 78 sec)
progress 75% (read 40265318400 bytes, duration 79 sec)
progress 76% (read 40802189312 bytes, duration 81 sec)
progress 77% (read 41339060224 bytes, duration 82 sec)
progress 78% (read 41875931136 bytes, duration 82 sec)
progress 79% (read 42412802048 bytes, duration 84 sec)
progress 80% (read 42949672960 bytes, duration 85 sec)
progress 81% (read 43486543872 bytes, duration 86 sec)
progress 82% (read 44023414784 bytes, duration 88 sec)
progress 83% (read 44560285696 bytes, duration 89 sec)
progress 84% (read 45097156608 bytes, duration 90 sec)
progress 85% (read 45634027520 bytes, duration 91 sec)
progress 86% (read 46170898432 bytes, duration 92 sec)
progress 87% (read 46707769344 bytes, duration 94 sec)
progress 88% (read 47244640256 bytes, duration 95 sec)
progress 89% (read 47781511168 bytes, duration 96 sec)
progress 90% (read 48318382080 bytes, duration 97 sec)
progress 91% (read 48855252992 bytes, duration 98 sec)
progress 92% (read 49392123904 bytes, duration 99 sec)
progress 93% (read 49928994816 bytes, duration 100 sec)
progress 94% (read 50465865728 bytes, duration 101 sec)
progress 95% (read 51002736640 bytes, duration 103 sec)
progress 96% (read 51539607552 bytes, duration 104 sec)
progress 97% (read 52076478464 bytes, duration 106 sec)
progress 98% (read 52613349376 bytes, duration 106 sec)
progress 99% (read 53150220288 bytes, duration 107 sec)
progress 100% (read 53687091200 bytes, duration 108 sec)
total bytes read 53687091200, sparse bytes 5123014656 (9.54%)
space reduction due to 4K zero blocks 0.96%
rescan volumes...

列出 Proxmox VE 服务器中的可用虚拟机。

# qm list
      VMID NAME                 STATUS     MEM(MB)    BOOTDISK(GB) PID
       100 pfsense              running    16384             32.00 3638583
       102 Fedora-39            running    2048              32.00 460944
       103 WinServer-2019       running    8192              50.00 3226348
       104 WinServer-2022       running    8192              50.00 3229915
       198 Ubuntu-Bionic        running    8192              30.00 1248387
       199 workstation          running    8192              30.00 3671877
       201 k8smaster1           running    8192              50.00 20651
       211 k8sworker1           running    16384             30.00 21307
       212 k8sworker2           running    16384             30.00 21357
       213 k8sworker3           running    16384             30.00 21428
       601 Ubuntu-22-Desktop    stopped    16384             50.00 0

现在可以启动导入的虚拟机。

qm start <VMID>

如果您在虚拟机中拥有静态 IP 地址,并且新主机上的网络不同,您可能需要登录实例并使用操作系统特定工具设置 IP 地址。


更新于:2个月前
赞一波!3

文章评论

全部评论