搜索

6.8. 导出虚拟机

download PDF

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

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

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

注意

您可以使用 Migration Toolkit for Virtualization 在 OpenShift Virtualization 集群间迁移虚拟机。

6.8.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
    内部链接只在集群内有效。

6.8.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
      使用 VirtualMachineExport 对象中的 metadata.name 值替换 <export_name>
    3. cacert.crt 文件复制到目标集群。
  2. 运行以下命令,解码源集群中的令牌并将其保存到 token_decode 文件中:

    $ oc get secret export-token-<export_name> -o jsonpath={.data.token} | base64 --decode > token_decode 1
    1
    使用 VirtualMachineExport 对象中的 metadata.name 值替换 <export_name>
  3. token_decode 文件复制到目标集群。
  4. 运行以下命令来获取 VirtualMachineExport 自定义资源:

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

    输出示例

    apiVersion: export.kubevirt.io/v1alpha1
    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/v1alpha1/namespaces/example/virtualmachineexports/example-export/external/manifests/all 1
          - type: auth-header-secret
            url: https://vmexport-proxy.test.net/api/export.kubevirt.io/v1alpha1/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 ingress 或路由的公共证书的 ConfigMap 清单。
    2
    包含与 Containerized Data Importer (CDI) 兼容的标头的 secret。标头包含导出令牌的文本版本。
    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/v1alpha1/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/v1alpha1/namespaces/example/virtualmachineexports/example-export/external/manifests/all -H "x-kubevirt-export-token:token_decode" -H "Accept:application/yaml"

后续步骤

  • 现在,您可以使用导出的清单在目标集群中创建 ConfigMapVirtualMachine 对象。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.