1. ubuntu-18.04.5-desktop

构建包含 iso 的 docker镜像

从官网下载 iso。
Dockerfile内容:

FROM scratch
ADD --chown=107:107 ubuntu-18.04.5-desktop-amd64.iso /disk/

命令:

docker build -t udesk:18.04.5 .

try ubuntu

仅试用,不安装。
udesk-c.yaml

metadata:
  name: udesk-c
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
spec:
  domain:
    resources:
      requests:
        memory: 4096M
        cpu: 4
    devices:
      blockMultiQueue: true
      inputs:
        - type: tablet
          bus: usb
          name: tablet1
      disks:
      - name: containerdisk
        bootOrder: 1        
        disk:
          bus: virtio
      interfaces:
        - name: default
          masquerade: {}
  networks:
  - name: default
    pod: {}
  volumes:
    - name: containerdisk
      containerDisk:
        image: udesk:18.04.5

结果:
kubevirt try ubuntu 18.04 desktop

install ubuntu

从容器镜像启动,安装到 hostDisk。
udesk-c-install.yaml

metadata:
  name: udesk-c-2
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
spec:
  domain:
    resources:
      requests:
        memory: 4096M
        cpu: 4
    devices:
      blockMultiQueue: true
      #networkInterfaceMultiqueue: true
      inputs:
        - type: tablet
          bus: usb
          name: tablet1
      disks:
      - name: rootdisk
        bootOrder: 1
        disk:
          bus: virtio
      - name: containerdisk
        bootOrder: 2        
        cdrom:
          bus: sata
      interfaces:
        - name: default
          masquerade: {}
  networks:
  - name: default
    pod: {}
  volumes:
    - name: containerdisk
      containerDisk:
        image: udesk:18.04.5
    - name: rootdisk
      hostDisk:
        capacity: 30Gi
        path: /media/sf_bigshare/vm/udesk01/disk.img
        type: DiskOrCreate

2.云镜像 ubuntu-20.04-cloud-kvm

如果使用嵌套虚拟化,需要使用 kvm版本。否则 kubectl describe vmi ucloud-c 看到如下错误:

server error. command SyncVMI failed: "LibvirtError(Code=1, Domain=10, Message='internal error: unable to execute QEMU command 'cont': Resetting the Virtual Machine is required')"

构建包含 iso 的 docker镜像

从官网下载 iso: https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-disk-kvm.img
Dockerfile内容:

FROM scratch
ADD --chown=107:107 focal-server-cloudimg-amd64-disk-kvm.img /disk/

命令:

docker build -t ucloud-kvm:20.04 .

使用 containerDisk 形式1

ucloud-c.yaml, 其中初始化脚本,采用base64编码。

apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
metadata:
  name: ucloud-c
  labels:
    kubevirt.io/domain: ucloud-c
spec:
  domain:
    resources:
      requests:
        memory: 4096M
        cpu: 4
    devices:
      blockMultiQueue: true
      disks:
      - name: containerdisk
        bootOrder: 1        
        disk:
          bus: virtio
      - name: cloudinit
        bootOrder: 2
        disk:
          bus: virtio
      interfaces:
        - name: default
          masquerade: {}
  networks:
  - name: default
    pod: {}
  volumes:
    - name: containerdisk
      containerDisk:
        image: ucloud-kvm:20.04
    - name: cloudinit
      cloudInitNoCloud:
        userDataBase64: IyEvYmluL2Jhc2gKc3VkbyBlY2hvIHJvb3Q6MTIzNDU2fGNocGFzc3dkCmV4cG9ydCBORVdfVVNFUj0iZm9vIgpleHBvcnQgU1NIX1BVQl9LRVk9InNzaC1yc2EgQUFBQUIzTnphQzF5YzJFQUFBQURBUUFCQUFBQkFRQzZ6ZGdGaUxyMXVBSzdQZGNjaERkK0xzZUE1ZkVPY3hDQ3Q3VExscjdNeDZoOGpVZytHKzhMOUpCTlp1RHpUWlNGMGRSN3F3emRCQlFqb3JBblpUbVkzQmhzS2NGcjhHdDRLTUdyUzZyM0RObUdydVA4R09SdmVnZFdadVhnQVNLVnBYZUk3bkNJalJKd0FhSzF4K2VHSHdBV085WjhvaGNib0hiTHlmZk9vU1pEU0l1azJrUkljNDcrRU5SamcwVDZ4MlZSc3FYMjdnNmo0RGZQS1FaR2swenZYa1phWXRyMWUydFpncVRCV3FaVWxvTUpLOG1pUXE2TWt0Q0tBUzRWdFBrMGs3dGVRWDU3T0d3RDZEN3VvNGIrQ2w4YVlBQXdobjBoYzBDMlVTZmJ1VkhncTg4RVNvMi8rTndWNFNRY2wzc3hDVzIxeUdJakFHdDRIeTdKIEBsb2NhbGhvc3QubG9jYWxkb21haW4iCgpzdWRvIGFkZHVzZXIgLVUgLW0gCmVjaG8gIjphdG9taWMiIHwgY2hwYXNzd2QKc3VkbyBta2RpciAvaG9tZS8vLnNzaApzdWRvIGVjaG8gIiIgPiAvaG9tZS8vLnNzaC9hdXRob3JpemVkX2tleXMKc3VkbyBjaG93biAtUiA6IC9ob21lLy8uc3NoCgo=

使用 containerDisk 形式2

ucloud-c-2.yaml, 其中初始化脚本,直接使用 shell。

apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
metadata:
  name: ucloud-c
  labels:
    kubevirt.io/domain: ucloud-c
spec:
  domain:
    resources:
      requests:
        memory: 4096M
        cpu: 4
    devices:
      blockMultiQueue: true
      disks:
      - name: containerdisk
        bootOrder: 1        
        disk:
          bus: virtio
      - name: cloudinit
        bootOrder: 2
        disk:
          bus: virtio
      interfaces:
        - name: default
          masquerade: {}
  networks:
  - name: default
    pod: {}
  volumes:
    - name: containerdisk
      containerDisk:
        image: ucloud-kvm:20.04
    - name: cloudinit
      cloudInitNoCloud:
        userData: |
          #!/bin/bash
          sudo echo root:123456|chpasswd
          export NEW_USER="foo"
          export SSH_PUB_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6zdgFiLr1uAK7PdcchDd+LseA5fEOcxCCt7TLlr7Mx6h8jUg+G+8L9JBNZuDzTZSF0dR7qwzdBBQjorAnZTmY3BhsKcFr8Gt4KMGrS6r3DNmGruP8GORvegdWZuXgASKVpXeI7nCIjRJwAaK1x+eGHwAWO9Z8ohcboHbLyffOoSZDSIuk2kRIc47+ENRjg0T6x2VRsqX27g6j4DfPKQZGk0zvXkZaYtr1e2tZgqTBWqZUloMJK8miQq6MktCKAS4VtPk0k7teQX57OGwD6D7uo4b+Cl8aYAAwhn0hc0C2USfbuVHgq88ESo2/+NwV4SQcl3sxCW21yGIjAGt4Hy7J @localhost.localdomain"

          sudo adduser -U -m
          echo ":atomic" | chpasswd
          sudo mkdir /home//.ssh
          sudo echo "" > /home//.ssh/authorized_keys
          sudo chown -R : /home//.ssh

使用 pvc

先创建上传镜像 nodePort 服务
cdi-proxy-service.yml

apiVersion: v1
kind: Service
metadata:
  namespace: cdi
  name: cdi-uploadproxy-node
spec:
  type: NodePort
  selector:
    cdi.kubevirt.io: cdi-uploadproxy
  ports:
    - protocol: TCP
      port: 443
      targetPort: 8443
      nodePort: 30000

上传命令:

在这里插入代码片

pvc.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: u2004cloudkvm
  namespace: default
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: u2004cloudkvm
  labels:
    type: local
    app: ubuntu
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /media/sf_bigshare/vm/u2004cloudkvm01
  persistentVolumeReclaimPolicy: Recycle

pv-scratch.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: u2004cloudkvm-scratch
  labels:
    type: local
    app: ubuntu
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /media/sf_bigshare/vm/u2004cloudkvm02
  persistentVolumeReclaimPolicy: Delete

ucloud-pvc.yaml

metadata:
  name: ucloud-pvc
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
spec:
  domain:
    resources:
      requests:
        memory: 4096M
        cpu: 4
    devices:
      blockMultiQueue: true
      disks:
      - name: pvcdisk
        bootOrder: 1        
        disk:
          bus: virtio
      - name: cloudinit
        bootOrder: 2
        disk:
          bus: virtio
      interfaces:
        - name: default
          masquerade: {}
  networks:
  - name: default
    pod: {}
  volumes:
    - name: pvcdisk
      persistentVolumeClaim:
        claimName: u2004cloudkvm
    - name: cloudinit
      cloudInitNoCloud:
        userDataBase64: IyEvYmluL2Jhc2gKc3VkbyBlY2hvIHJvb3Q6MTIzNDU2fGNocGFzc3dkCmV4cG9ydCBORVdfVVNFUj0iZm9vIgpleHBvcnQgU1NIX1BVQl9LRVk9InNzaC1yc2EgQUFBQUIzTnphQzF5YzJFQUFBQURBUUFCQUFBQkFRQzZ6ZGdGaUxyMXVBSzdQZGNjaERkK0xzZUE1ZkVPY3hDQ3Q3VExscjdNeDZoOGpVZytHKzhMOUpCTlp1RHpUWlNGMGRSN3F3emRCQlFqb3JBblpUbVkzQmhzS2NGcjhHdDRLTUdyUzZyM0RObUdydVA4R09SdmVnZFdadVhnQVNLVnBYZUk3bkNJalJKd0FhSzF4K2VHSHdBV085WjhvaGNib0hiTHlmZk9vU1pEU0l1azJrUkljNDcrRU5SamcwVDZ4MlZSc3FYMjdnNmo0RGZQS1FaR2swenZYa1phWXRyMWUydFpncVRCV3FaVWxvTUpLOG1pUXE2TWt0Q0tBUzRWdFBrMGs3dGVRWDU3T0d3RDZEN3VvNGIrQ2w4YVlBQXdobjBoYzBDMlVTZmJ1VkhncTg4RVNvMi8rTndWNFNRY2wzc3hDVzIxeUdJakFHdDRIeTdKIEBsb2NhbGhvc3QubG9jYWxkb21haW4iCgpzdWRvIGFkZHVzZXIgLVUgLW0gCmVjaG8gIjphdG9taWMiIHwgY2hwYXNzd2QKc3VkbyBta2RpciAvaG9tZS8vLnNzaApzdWRvIGVjaG8gIiIgPiAvaG9tZS8vLnNzaC9hdXRob3JpemVkX2tleXMKc3VkbyBjaG93biAtUiA6IC9ob21lLy8uc3NoCgo=

2. ubuntu-20.04.5-desktop

构建包含 iso 的 docker镜像

从官网下载 iso。
Dockerfile内容:

FROM scratch
ADD --chown=107:107 ubuntu-20.04.5-desktop-amd64.iso /disk/

命令:

docker build -t ubuntu-desktop:20.04.5 .

install ubuntu 20.04.5

从容器镜像启动,安装到 hostDisk。
u-desc-c.yaml

apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
metadata:
  name: udesk-c
  labels:
    kubevirt.io/domain: ucloud-c
spec:
  domain:
    resources:
      requests:
        memory: 4096M
        cpu: 4
    devices:
      blockMultiQueue: true
      #networkInterfaceMultiqueue: true
      inputs:
      - type: tablet
        bus: usb
        name: tablet1
      disks:
      - name: rootdisk
        bootOrder: 1
        disk:
          bus: virtio
      - name: containerdisk
        bootOrder: 2
        cdrom:
          bus: sata
      interfaces:
      - name: default
        masquerade: {}
  networks:
  - name: default
    pod: {}
  volumes:
  - name: rootdisk
    hostDisk:
      capacity: 30Gi
      path: /data/vm/instance/ubuntu-desktop-20045.img
      type: DiskOrCreate
  - name: containerdisk
    containerDisk:
      image: ubuntu-desktop:20.04.5

启动后,virtctl vnc udesk-c 安装系统。

从硬盘启动 ubuntu-20.04.5

安装好系统后,直接从硬盘启动。
u-desc-h.yaml

apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
metadata:
  name: udesk-c
  labels:
    kubevirt.io/domain: ucloud-c
spec:
  domain:
    resources:
      requests:
        memory: 4096M
        cpu: 4
    devices:
      blockMultiQueue: true
      #networkInterfaceMultiqueue: true
      inputs:
      - type: tablet
        bus: usb
        name: tablet1
      disks:
      - name: rootdisk
        bootOrder: 1
        disk:
          bus: virtio
      interfaces:
      - name: default
        masquerade: {}
  networks:
  - name: default
    pod: {}
  volumes:
  - name: rootdisk
    hostDisk:
      capacity: 30Gi
      path: /data/vm/instance/ubuntu-desktop-20045.img
      type: DiskOrCreate
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐