KubeVirt虚拟机导出磁盘镜像

SuKai October 24, 2024

  1. 开启KubeVirt特性门控
  2. 创建磁盘镜像导出服务
  3. 下载磁盘镜像

开启KubeVirt特性门控VMExport

root@rack1-master34:~$ kubectl -n kubevirt edit kubevirt
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: kubevirt.io/v1
kind: KubeVirt
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"kubevirt.io/v1","kind":"KubeVirt","metadata":{"annotations":{},"name":"kubevirt","namespace":"kubevirt"},"spec":{"certificateRotateStrategy":{},"configuration":{"developerConfiguration":{"featureGates":[]}},"customizeComponents":{},"imagePullPolicy":"IfNotPresent","workloadUpdateStrategy":{}}}
    kubevirt.io/latest-observed-api-version: v1
    kubevirt.io/storage-observed-api-version: v1
  creationTimestamp: "2024-09-19T07:11:27Z"
  finalizers:
  - foregroundDeleteKubeVirt
  generation: 3
  name: kubevirt
  namespace: kubevirt
  resourceVersion: "31602825"
  uid: 86857887-d8c3-4c17-81de-c43a45457d5a
spec:
  certificateRotateStrategy: {}
  configuration:
    developerConfiguration:
      featureGates:
      - DataVolumes
      - LiveMigration
      - VMExport
  customizeComponents: {}
  imagePullPolicy: IfNotPresent
  workloadUpdateStrategy: {}

创建磁盘镜像导出服务,挂载rbd块设备到virt-export-bj-test01-os-export Pod。

root@rack1-master34:~/VMexport$ more token.yaml
apiVersion: v1
kind: Secret
metadata:
  name: export-vm-token
stringData:
  token: 1234567890ab



root@rack1-master34:~/VMexport$ more pvcexport.yaml
apiVersion: export.kubevirt.io/v1alpha1
kind: VirtualMachineExport
metadata:
  name: bj-test01-os-export
spec:
  tokenSecretRef: export-vm-token
  source:
    apiGroup: ""
    kind: PersistentVolumeClaim
    name: bj-test01-os


root@master:~/VMexport$ kubectl -n beijing describe virtualmachineexport bj-test01-os-export
Name:         bj-test01-os-export
Namespace:    beijing
Labels:       <none>
Annotations:  <none>
API Version:  export.kubevirt.io/v1beta1
Kind:         VirtualMachineExport
Metadata:
  Creation Timestamp:  2024-10-23T01:48:17Z
  Generation:          5
  Resource Version:    23124997
  UID:                 b8d894c7-fd92-4e67-ba72-5d616c62196c
Spec:
  Source:
    API Group:
    Kind:            PersistentVolumeClaim
    Name:            bj-test01-os
  Token Secret Ref:  export-vm-token
Status:
  Conditions:
    Last Probe Time:       <nil>
    Last Transition Time:  2024-10-23T02:04:09Z
    Reason:                PodReady
    Status:                True
    Type:                  Ready
    Last Probe Time:       <nil>
    Last Transition Time:  2024-10-23T01:48:17Z
    Reason:                PVCBound
    Status:                True
    Type:                  PVCReady
  Links:
    Internal:
      Cert:  -----BEGIN CERTIFICATE-----
MIIBhzCCAS6gAwIBAgIIMzmGYCiQ2TAwCgYIKoZIzj0EAwIwKDEmMCQGA1UEAwwd
ZXhwb3J0Lmt1YmV2aXJ0LmlvQDE3Mjk2NDgwODcwHhcNMjQxMDIzMDE0ODA3WhcN
MjQxMDMwMDE0ODA3WjAoMSYwJAYDVQQDDB1leHBvcnQua3ViZXZpcnQuaW9AMTcy
OTY0ODA4NzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABFlEZnphR8vzs6YkOIUj
zt6y1oDohFJ8CFCGcjLxjBM2kNPGETWjnl7uIvE71XkNvlRsRqSXBuv0jDDwgmm/
jTGjQjBAMA4GA1UdDwEB/wQEAwICpDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW
BBRV9+wvxFVU+uv5zZc94DixkSnljjAKBggqhkjOPQQDAgNHADBEAiBKZH3KixS+
hv+mD0YbIwSjk2vURcuvjCGOA2pHfKXS9AIgJyC4NsgD5hR4QHLeZHe4xGt3OzAT
/XA4+gX9yuoaTOQ=
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIIBiDCCAS6gAwIBAgIIRnTxuqZG3ucwCgYIKoZIzj0EAwIwKDEmMCQGA1UEAwwd
ZXhwb3J0Lmt1YmV2aXJ0LmlvQDE3Mjk1MTkxMzYwHhcNMjQxMDIxMTM1ODU2WhcN
MjQxMDI4MTM1ODU2WjAoMSYwJAYDVQQDDB1leHBvcnQua3ViZXZpcnQuaW9AMTcy
OTUxOTEzNjBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABE0b6a3347TZoOzQJeob
hcS9jVwSCkeTTBt2UMUFndjCw+c6RV+WYpX4KkCyo99QnZzlgSfaoaF8iB5QI+sd
ZnmjQjBAMA4GA1UdDwEB/wQEAwICpDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW
BBT8+xEz67NGXIxVpXQ0SIin5CpjyTAKBggqhkjOPQQDAgNIADBFAiBDFOdgxYM7
FowpC7pBkLMak25Gar/1t6NT7vX3bEmoDgIhANw2f6zUlhRSiUvdviIcV4pmDy+3
3I8fKTyhgmwBhmv9
-----END CERTIFICATE-----
      Manifests:
        Type:  all
        URL:   https://virt-export-bj-test01-os-export.beijing.svc/internal/manifests/all
        Type:  auth-header-secret
        URL:   https://virt-export-bj-test01-os-export.beijing.svc/internal/manifests/secret
      Volumes:
        Formats:
          Format:       raw
          URL:          https://virt-export-bj-test01-os-export.beijing.svc/volumes/bj-test01-os/disk.img
          Format:       gzip
          URL:          https://virt-export-bj-test01-os-export.beijing.svc/volumes/bj-test01-os/disk.img.gz
        Name:           bj-test01-os
  Phase:                Ready
  Service Name:         virt-export-bj-test01-os-export
  Token Secret Ref:     export-vm-token
  Ttl Expiration Time:  2024-10-23T03:48:17Z
Events:
  Type    Reason              Age                From               Message
  ----    ------              ----               ----               -------
  Normal  ServiceCreated      35m                export-controller  Created service beijing/virt-export-bj-test01-os-export
  Normal  ExporterPodCreated  30m                export-controller  Created exporter pod beijing/virt-export-bj-test01-os-export
  Normal  SecretCreated       20m (x6 over 30m)  export-controller  Created exporter pod secret




root@master:~/VMexport$ kubectl -n beijing get pods
NAME                              READY   STATUS    RESTARTS   AGE
static-ip                         1/1     Running   0          53d
virt-export-bj-test01-os-export   1/1     Running   0          34m
virt-launcher-bj-tyht01-zblnk     2/2     Running   0          53d
virt-launcher-bj-tyht02-g2whn     2/2     Running   0          53d
virt-launcher-bj-tyht03-4g98h     2/2     Running   0          53d
virt-launcher-bj-tyht04-7qq78     2/2     Running   0          53d
root@master:~/VMexport$ kubectl -n beijing exec -it virt-export-bj-test01-os-export -- /bin/bash
bash-5.1$ ls
afs  bin  boot  cert  dev  etc  export-volumes  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  token  usr  var
bash-5.1$ cd export-volumes/
bash-5.1$ ls
bj-test01-os
bash-5.1$ cd bj-test01-os/
bash-5.1$ ls
disk.img  lost+found
bash-5.1$ df -Th
Filesystem                        Type     Size  Used Avail Use% Mounted on
overlay                           overlay   98G   55G   39G  59% /
tmpfs                             tmpfs     64M     0   64M   0% /dev
tmpfs                             tmpfs    1.0G  8.0K  1.0G   1% /cert
tmpfs                             tmpfs    1.0G  4.0K  1.0G   1% /token
/dev/rbd1                         ext4      38G  1.3G   36G   4% /export-volumes/bj-test01-os
/dev/mapper/ubuntu--vg-ubuntu--lv ext4      98G   55G   39G  59% /etc/hosts
shm                               tmpfs     64M     0   64M   0% /dev/shm
tmpfs                             tmpfs    1.0G   12K  1.0G   1% /var/run/secrets/kubernetes.io/serviceaccount
tmpfs                             tmpfs     40G     0   40G   0% /proc/acpi
tmpfs                             tmpfs     40G     0   40G   0% /proc/scsi
tmpfs                             tmpfs     40G     0   40G   0% /sys/firmware
tmpfs                             tmpfs     40G     0   40G   0% /sys/devices/virtual/powercap
bash-5.1$

下载磁盘镜像

root@master:~$ kubectl -n beijing get virtualmachineexport bj-test01-os-export -o jsonpath={.status.links.internal.cert} > cacert.crt
root@master:~$ cat cacert.crt
-----BEGIN CERTIFICATE-----
MIIBhzCCAS6gAwIBAgIIMzmGYCiQ2TAwCgYIKoZIzj0EAwIwKDEmMCQGA1UEAwwd
ZXhwb3J0Lmt1YmV2aXJ0LmlvQDE3Mjk2NDgwODcwHhcNMjQxMDIzMDE0ODA3WhcN
MjQxMDMwMDE0ODA3WjAoMSYwJAYDVQQDDB1leHBvcnQua3ViZXZpcnQuaW9AMTcy
OTY0ODA4NzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABFlEZnphR8vzs6YkOIUj
zt6y1oDohFJ8CFCGcjLxjBM2kNPGETWjnl7uIvE71XkNvlRsRqSXBuv0jDDwgmm/
jTGjQjBAMA4GA1UdDwEB/wQEAwICpDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW
BBRV9+wvxFVU+uv5zZc94DixkSnljjAKBggqhkjOPQQDAgNHADBEAiBKZH3KixS+
hv+mD0YbIwSjk2vURcuvjCGOA2pHfKXS9AIgJyC4NsgD5hR4QHLeZHe4xGt3OzAT
/XA4+gX9yuoaTOQ=
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIIBiDCCAS6gAwIBAgIIRnTxuqZG3ucwCgYIKoZIzj0EAwIwKDEmMCQGA1UEAwwd
ZXhwb3J0Lmt1YmV2aXJ0LmlvQDE3Mjk1MTkxMzYwHhcNMjQxMDIxMTM1ODU2WhcN
MjQxMDI4MTM1ODU2WjAoMSYwJAYDVQQDDB1leHBvcnQua3ViZXZpcnQuaW9AMTcy
OTUxOTEzNjBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABE0b6a3347TZoOzQJeob
hcS9jVwSCkeTTBt2UMUFndjCw+c6RV+WYpX4KkCyo99QnZzlgSfaoaF8iB5QI+sd
ZnmjQjBAMA4GA1UdDwEB/wQEAwICpDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW
BBT8+xEz67NGXIxVpXQ0SIin5CpjyTAKBggqhkjOPQQDAgNIADBFAiBDFOdgxYM7
FowpC7pBkLMak25Gar/1t6NT7vX3bEmoDgIhANw2f6zUlhRSiUvdviIcV4pmDy+3
3I8fKTyhgmwBhmv9
-----END CERTIFICATE-----

root@master:~/VMexport$ curl -O --cacert cacert.crt https://virt-export-bj-test01-os-export.beijing.svc/volumes/bj-test01-os/disk.img -H "x-kubevirt-export-token:1234567890ab"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 35.0G  100 35.0G    0     0   343M      0  0:01:44  0:01:44 --:--:--  397M

root@master:~/VMexport$ ll
total 36753436
drwxrwxr-x  2 root root        4096 Oct 23 02:18 ./
drwxr-x--- 18 root root        4096 Oct 23 02:18 ../
-rw-rw-r--  1 root root        1181 Oct 23 02:10 cacert.crt
-rw-rw-r--  1 root root 37635489792 Oct 23 02:19 disk.img
-rw-rw-r--  1 root root         227 Oct 23 01:43 pvcexport.yaml
-rw-rw-r--  1 root root          96 Oct 23 01:43 token.yaml
root@master:~/VMexport$ du -sh disk.img
36G     disk.img