10.5. 导出虚拟机

您可以导出虚拟机 (VM) 及其关联的磁盘,以将虚拟机导入到另一个集群或分析卷以备备目的。

您可以使用命令行界面创建一个 VirtualMachineExport 自定义资源 (CR)。

另外,您可以使用 virtctl vmexport 命令创建一个 VirtualMachineExport CR 并下载导出的卷。

10.5.1. 创建 VirtualMachineExport 自定义资源

您可以创建一个 VirtualMachineExport 自定义资源 (CR) 来导出以下对象:

  • 虚拟机 (VM):导出指定虚拟机的持久性卷声明 (PVC)。
  • VM 快照:导出 VirtualMachineSnapshot CR 中包含的 PVC。
  • PVC :导出 PVC。如果 PVC 被另一个 pod (如 virt-launcher pod)使用,则导出会一直处于 Pending 状态,直到 PVC 不再使用为止。

VirtualMachineExport CR 为导出的卷创建内部和外部链接。内部链接在集群中有效。可以使用 IngressRoute 访问外部链接。

导出服务器支持以下文件格式:

  • raw: 原始磁盘镜像文件。
  • gzip :压缩的磁盘镜像文件.
  • dir :PVC 目录和文件。
  • tar.gz :压缩的 PVC 文件。

先决条件

  • 必须为虚拟机导出关闭虚拟机。

流程

  1. 创建一个 VirtualMachineExport 清单,根据以下示例从 VirtualMachineVirtualMachineSnapshotPersistentVolumeClaim CR 导出卷,并将其保存为 example-export.yaml

    VirtualMachineExport 示例

    apiVersion: export.kubevirt.io/v1alpha1
    kind: VirtualMachineExport
    metadata:
      name: example-export
    spec:
      source:
        apiGroup: "kubevirt.io" 1
        kind: VirtualMachine 2
        name: example-vm
      ttlDuration: 1h 3

    1
    指定适当的 API 组:
    • "kubevirt.io" 用于 VirtualMachine
    • "snapshot.kubevirt.io" 用于 VirtualMachineSnapshot
    • "" 用于 PersistentVolumeClaim
    2
    指定 VirtualMachine, VirtualMachineSnapshot, 或 PersistentVolumeClaim
    3
    可选。默认持续时间为 2 小时。
  2. 创建 VirtualMachineExport CR:

    $ oc create -f example-export.yaml
  3. 获取 VirtualMachineExport CR:

    $ oc get vmexport example-export -o yaml

    导出的卷的内部和外部链接显示在 status 小节中:

    输出示例

    apiVersion: export.kubevirt.io/v1alpha1
    kind: VirtualMachineExport
    metadata:
      name: example-export
      namespace: example
    spec:
      source:
        apiGroup: ""
        kind: PersistentVolumeClaim
        name: example-pvc
      tokenSecretRef: example-token
    status:
      conditions:
      - lastProbeTime: null
        lastTransitionTime: "2022-06-21T14:10:09Z"
        reason: podReady
        status: "True"
        type: Ready
      - lastProbeTime: null
        lastTransitionTime: "2022-06-21T14:09:02Z"
        reason: pvcBound
        status: "True"
        type: PVCReady
      links:
        external: 1
          cert: |-
            -----BEGIN CERTIFICATE-----
            ...
            -----END CERTIFICATE-----
          volumes:
          - formats:
            - format: raw
              url: https://vmexport-proxy.test.net/api/export.kubevirt.io/v1alpha1/namespaces/example/virtualmachineexports/example-export/volumes/example-disk/disk.img
            - format: gzip
              url: https://vmexport-proxy.test.net/api/export.kubevirt.io/v1alpha1/namespaces/example/virtualmachineexports/example-export/volumes/example-disk/disk.img.gz
            name: example-disk
        internal:  2
          cert: |-
            -----BEGIN CERTIFICATE-----
            ...
            -----END CERTIFICATE-----
          volumes:
          - formats:
            - format: raw
              url: https://virt-export-example-export.example.svc/volumes/example-disk/disk.img
            - format: gzip
              url: https://virt-export-example-export.example.svc/volumes/example-disk/disk.img.gz
            name: example-disk
      phase: Ready
      serviceName: virt-export-example-export

    1
    可以使用 IngressRoute 从集群外部访问外部链接。
    2
    内部链接只在集群内有效。