×

您可以导出虚拟机 (VM) 及其关联的磁盘,以便将虚拟机导入另一个集群或分析卷以进行取证。

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

或者,您可以使用virtctl vmexport命令创建VirtualMachineExport CR 并下载导出的卷。

您可以使用虚拟化迁移工具包在 OpenShift Virtualization 集群之间迁移虚拟机。

创建 VirtualMachineExport 自定义资源

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

  • 虚拟机 (VM):导出指定虚拟机的持久卷声明 (PVC)。

  • 虚拟机快照:导出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/v1beta1
    kind: VirtualMachineExport
    metadata:
      name: example-export
    spec:
      source:
        apiGroup: "kubevirt.io" (1)
        kind: VirtualMachine (2)
        name: example-vm
      ttlDuration: 1h (3)
    1 指定相应的 API 组
    • VirtualMachine"kubevirt.io"

    • VirtualMachineSnapshot"snapshot.kubevirt.io"

    • PersistentVolumeClaim""

    2 指定VirtualMachineVirtualMachineSnapshotPersistentVolumeClaim
    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/v1beta1
    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/v1beta1/namespaces/example/virtualmachineexports/example-export/volumes/example-disk/disk.img
            - format: gzip
              url: https://vmexport-proxy.test.net/api/export.kubevirt.io/v1beta1/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 内部链接仅在集群内有效。

访问导出的虚拟机清单

导出虚拟机 (VM) 或快照后,您可以从导出服务器获取VirtualMachine清单和相关信息。

先决条件
  • 您通过创建VirtualMachineExport自定义资源 (CR) 来导出虚拟机或虚拟机快照。

    具有spec.source.kind: PersistentVolumeClaim参数的VirtualMachineExport对象不会生成虚拟机清单。

步骤
  1. 要访问清单,您必须首先将证书从源集群复制到目标集群。

    1. 登录到源集群。

    2. 通过运行以下命令将证书保存到cacert.crt文件:

      $ oc get vmexport <export_name> -o jsonpath={.status.links.external.cert} > cacert.crt (1)
      1 <export_name>替换为VirtualMachineExport对象的metadata.name值。
    3. cacert.crt文件复制到目标集群。

  2. 在源集群中解码令牌,并将其保存到token_decode文件,方法是运行以下命令:

    $ oc get secret export-token-<export_name> -o jsonpath={.data.token} | base64 --decode > token_decode (1)
    1 <export_name>替换为VirtualMachineExport对象的metadata.name值。
  3. token_decode文件复制到目标集群。

  4. 运行以下命令获取VirtualMachineExport自定义资源

    $ oc get vmexport <export_name> -o yaml
  5. 查看status.links部分,该部分分为externalinternal两个部分。注意每个部分中的manifests.url字段。

    示例输出
    apiVersion: export.kubevirt.io/v1beta1
    kind: VirtualMachineExport
    metadata:
      name: example-export
    spec:
      source:
        apiGroup: "kubevirt.io"
        kind: VirtualMachine
        name: example-vm
      tokenSecretRef: example-token
    status:
    #...
      links:
        external:
    #...
          manifests:
          - type: all
            url: https://vmexport-proxy.test.net/api/export.kubevirt.io/v1beta1/namespaces/example/virtualmachineexports/example-export/external/manifests/all (1)
          - type: auth-header-secret
            url: https://vmexport-proxy.test.net/api/export.kubevirt.io/v1beta1/namespaces/example/virtualmachineexports/example-export/external/manifests/secret (2)
        internal:
    #...
          manifests:
          - type: all
            url: https://virt-export-export-pvc.default.svc/internal/manifests/all (3)
          - type: auth-header-secret
            url: https://virt-export-export-pvc.default.svc/internal/manifests/secret
      phase: Ready
      serviceName: virt-export-example-export
    1 包含VirtualMachine清单、DataVolume清单(如果存在)以及包含外部URL入口或路由的公共证书的ConfigMap清单。
    2 包含一个包含与容器化数据导入器 (CDI) 兼容的头的密钥。该头包含导出令牌的文本版本。
    3 包含VirtualMachine清单、DataVolume清单(如果存在)以及包含内部URL导出服务器证书的ConfigMap清单。
  6. 登录目标集群。

  7. 运行以下命令获取Secret清单

    $ curl --cacert cacert.crt <secret_manifest_url> -H \ (1)
    "x-kubevirt-export-token:token_decode" -H \ (2)
    "Accept:application/yaml"
    1 <secret_manifest_url>替换为VirtualMachineExport YAML 输出中的auth-header-secret URL。
    2 参考之前创建的token_decode文件。

    例如

    $ curl --cacert cacert.crt https://vmexport-proxy.test.net/api/export.kubevirt.io/v1beta1/namespaces/example/virtualmachineexports/example-export/external/manifests/secret -H "x-kubevirt-export-token:token_decode" -H "Accept:application/yaml"
  8. 运行以下命令获取type: all类型的清单,例如ConfigMapVirtualMachine清单

    $ curl --cacert cacert.crt <all_manifest_url> -H \ (1)
    "x-kubevirt-export-token:token_decode" -H \ (2)
    "Accept:application/yaml"
    1 <all_manifest_url>替换为VirtualMachineExport YAML 输出中的URL。
    2 参考之前创建的token_decode文件。

    例如

    $ curl --cacert cacert.crt https://vmexport-proxy.test.net/api/export.kubevirt.io/v1beta1/namespaces/example/virtualmachineexports/example-export/external/manifests/all -H "x-kubevirt-export-token:token_decode" -H "Accept:application/yaml"
后续步骤
  • 现在可以使用导出的清单在目标集群上创建ConfigMapVirtualMachine对象。